]> git.bts.cx Git - cx.git/blob - cx/lib/users.php
style update
[cx.git] / cx / lib / users.php
1 <?php
2
3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5
6 function cx_users_find_user($id = null, $name = null, $password_hash = null) {
7 if ($id != null) {
8 $sql = 'SELECT
9 user_id
10 FROM users
11 WHERE user_id == ?
12 LIMIT 1;';
13
14 foreach (cx_db_query($sql, $id) as $user) {
15 return $user['user_id'];
16 }
17 } else if ($name != null && $password_hash != null) {
18 $sql = 'SELECT
19 user_id
20 FROM users
21 WHERE user_name == ?
22 AND user_password_hash == ?
23 LIMIT 1;';
24
25 foreach (cx_db_query($sql, $name, $password_hash) as $user) {
26 return $user['user_id'];
27 }
28 }
29
30 return null;
31 }
32
33 function cx_users_hash_password_for_user($username, $password) {
34 $sql = 'SELECT
35 user_salt
36 FROM users
37 WHERE user_name == ?
38 LIMIT 1;';
39
40 $salt = '';
41 foreach (cx_db_query($sql, $username) as $user) {
42 $salt = $user['user_salt'];
43 }
44
45 $password_hash = sha1($salt . $password);
46 return $password_hash;
47 }
48
49 function cx_users_add_user($name, $password) {
50 $creation_time = time();//
51 $salt = sha1(random_bytes(100));
52 $password_hash = sha1($salt . $password);
53
54 $sql = 'INSERT INTO users (
55 user_creation_time,
56 user_name,
57 user_salt,
58 user_password_hash
59 )
60 VALUES (?, ?, ?, ?);';
61 $user_id = cx_db_exec($sql, $creation_time, $name, $salt, $password_hash);
62 return $user_id;
63 }
64
65 cx_setup_register(1, function() {
66 cx_db_exec('CREATE TABLE users (
67 user_id INTEGER PRIMARY KEY,
68 user_creation_time INTEGER,
69 user_name STRING,
70 user_salt STRING,
71 user_password_hash STRING
72 );');
73 });