summaryrefslogtreecommitdiffstats
path: root/application/libraries/Duser/drivers/Duser_db.php
blob: a58b5a29819d8097d49a992b3de5ffa5cd8f4f50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
 * Copyright 2013 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

class Duser_db extends Duser_Driver {

	/* FIXME: If you use this driver as a template, remove can_reset_password
	 * and can_register_new_users. These features require the DB driver and
	 * will NOT work with other drivers.
	 */
	public $optional_functions = array(
		'can_reset_password',
		'can_register_new_users',
	);

	public function login($username, $password)
	{
		$CI =& get_instance();

		$query = $CI->db->query('
			SELECT username, id, password
			FROM `users`
			WHERE `username` = ?
			', array($username))->row_array();

		if (empty($query)) {
			return false;
		}

		if (crypt($password, $query["password"]) === $query["password"]) {
			return array(
				"username" => $username,
				"userid" => $query["id"]
			);
		} else {
			return false;
		}
	}

	public function username_exists($username)
	{
		$CI =& get_instance();

		$query = $CI->db->query("
			SELECT id
			FROM users
			WHERE username = ?
			", array($username));

		if ($query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public function get_email($userid)
	{
		$CI =& get_instance();

		$query = $CI->db->query("
			SELECT email
			FROM users
			WHERE id = ?
			", array($userid))->row_array();

		if (empty($query)) {
			show_error("Failed to get email address from db");
		}

		return $query["email"];
	}

}