Browse Source

user creation

master
Robin Thoni 9 years ago
parent
commit
5b680da53f
3 changed files with 35 additions and 5 deletions
  1. 2
    2
      index.php
  2. 25
    1
      user.php
  3. 8
    2
      utils.php

+ 2
- 2
index.php View File

@@ -17,7 +17,7 @@ function check_api_key_()
17 17
 function check_api_key()
18 18
 {
19 19
   if (!check_api_key_())
20
-    error(401, "Bad API Key");
20
+    error(401, "Bad API key");
21 21
 }
22 22
 
23 23
 function main()
@@ -41,7 +41,7 @@ function main()
41 41
   else if ($path === "/user/login" && $m_post)
42 42
     user_login();
43 43
   else
44
-    error(501, "Not Implemented");
44
+    error(501, "Not implemented");
45 45
 }
46 46
 
47 47
 check_api_key();

+ 25
- 1
user.php View File

@@ -1,9 +1,33 @@
1 1
 <?php
2
+require_once "sql.php";
3
+require_once "utils.php";
4
+
5
+function hash_password($password)
6
+{
7
+  return md5($password);
8
+}
9
+
2 10
 function user_create()
3 11
 {
12
+  $username = get_post("username");
13
+  $password = get_post("password");
14
+  if (strlen($username) < 3)
15
+    error(422, "Username too short");
16
+  if (database_exec("SELECT id FROM users WHERE `username` = :username",
17
+    array(":username" => $username))->fetch() !== false)
18
+    error(409, "Username already exists");
19
+  database_exec("INSERT INTO users (`username`, `password`) ".
20
+    "VALUES(:username, :password)", array(":username" => $username,
21
+    ":password" => hash_password($password)));
22
+  user_login($username);
4 23
 }
5 24
 
6
-function user_login()
25
+function user_login($username = false )
7 26
 {
27
+  if ($username === false)
28
+  {
29
+    $username = get_post("username");
30
+    $password = get_post("password");
31
+  }
8 32
 }
9 33
 ?>

+ 8
- 2
utils.php View File

@@ -18,8 +18,14 @@ function error($code, $message)
18 18
   die(json_encode(array("code" => intval($code), "message" => $message)));
19 19
 }
20 20
 
21
-function error_missing()
21
+function get_post($key, $is_error = true)
22 22
 {
23
-  error(400, "Missing Parameter");
23
+  if (isset($_POST[$key]))
24
+    return $_POST[$key];
25
+  else if ($is_error)
26
+    error(400, "Missing parameters");
27
+  else
28
+    return false;
24 29
 }
30
+
25 31
 ?>

Loading…
Cancel
Save