Browse Source

api key check

master
Robin Thoni 9 years ago
parent
commit
e5c63261cb
2 changed files with 31 additions and 8 deletions
  1. 7
    3
      index.php
  2. 24
    5
      sql.php

+ 7
- 3
index.php View File

@@ -7,15 +7,19 @@ require_once "sql.php";
7 7
 function check_api_key_()
8 8
 {
9 9
   $headers = apache_request_headers();
10
-  if (!isset($header["Authorization"]))
10
+  if (!isset($headers["Api-Key"]))
11 11
     return false;
12
-  return true;
12
+  $q = database_exec("SELECT id FROM api_keys WHERE `key` = :key",
13
+                     array(":key" => $headers["Api-Key"]));
14
+  if ($q->fetch() !== false)
15
+    return true;
16
+  return false;
13 17
 }
14 18
 
15 19
 function check_api_key()
16 20
 {
17 21
   if (!check_api_key_())
18
-    error(500, "Database Fail");
22
+    error(401, "Bad API Key");
19 23
 }
20 24
 
21 25
 function main()

+ 24
- 5
sql.php View File

@@ -1,27 +1,46 @@
1 1
 <?php
2
-require_once "misc/config.php";
3 2
 require_once "utils.php";
3
+require_once "misc/config.php";
4 4
 
5
-function get_database()
5
+function database_get()
6 6
 {
7 7
   static $init = 0;
8 8
   static $database = NULL;
9 9
   if ($init == 0)
10 10
   {
11 11
     $init = 1;
12
+    global $database_host;
13
+    global $database_port;
14
+    global $database_name;
15
+    global $database_user;
16
+    global $database_pass;
12 17
     try
13 18
     {
14
-      $database = new PDO("mysql:host:$database_host;$database_port;".
19
+      $database = new PDO("mysql:host=$database_host;port=$database_port;".
15 20
                           "dbname=$database_name", $database_user,
16 21
                           $database_pass);
17 22
     }
18 23
     catch (Exception $e)
19 24
     {
20
-      var_dump($e);
21
-      error(500, "Database Fail");
25
+      error(500, "Database Fail Connect");
22 26
     }
23 27
   }
24 28
   return $database;
25 29
 }
26 30
 
31
+function database_exec($query, $args)
32
+{
33
+  $q = database_get()->prepare($query);
34
+  if (!$q->execute($args))
35
+    error(500, "Database Fail Query");
36
+  return $q;
37
+}
38
+
39
+function database_query($query)
40
+{
41
+  $q = database_get()->query($query);
42
+  if (!$q)
43
+    error(500, "Database Fail Query");
44
+  return $q;
45
+}
27 46
 ?>

Loading…
Cancel
Save