Browse Source

Initial commit

Maurits van der Schee 9 years ago
parent
commit
bc4e64f26e
4 changed files with 67 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 7
    0
      .htaccess
  3. 50
    0
      api.php
  4. 9
    0
      config.php.dist

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+config.php

+ 7
- 0
.htaccess View File

@@ -0,0 +1,7 @@
1
+RewriteEngine On
2
+RewriteBase /api/
3
+
4
+RewriteCond %{REQUEST_FILENAME} !-f
5
+RewriteCond %{REQUEST_FILENAME} !-d
6
+RewriteRule (.+) api.php?table=$1 [L,QSA]
7
+RewriteRule ^$ api.php [L,QSA]

+ 50
- 0
api.php View File

@@ -0,0 +1,50 @@
1
+<?php
2
+include "config.php";
3
+
4
+$table = str_replace('*','%',preg_replace('/[^a-zA-Z0-9\-_*]/','',isset($_GET["table"])?$_GET["table"]:'*'));
5
+$callback = preg_replace('/[^a-zA-Z0-9\-_]/','',isset($_GET["callback"])?$_GET["callback"]:false);
6
+
7
+$mysqli = new mysqli($config["hostname"], $config["username"], $config["password"], $config["database"]);
8
+
9
+if ($mysqli->connect_errno) die('Connect failed: '.$mysqli->connect_error);
10
+
11
+$tables = array();
12
+
13
+if ($result = $mysqli->query("SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE '$table' AND `TABLE_SCHEMA` = '$config[database]'")) {
14
+    while ($row = $result->fetch_row()) $tables[] = $row[0];
15
+    $result->close();
16
+}
17
+
18
+if ($config["whitelist"]) $tables = array_intersect($tables, $config["whitelist"]);
19
+if ($config["blacklist"]) $tables = array_diff($tables, $config["blacklist"]);
20
+
21
+if (empty($tables)) {
22
+    die(header("Content-Type:",true,404));
23
+} if ($callback) {
24
+    header("Content-Type: application/javascript");
25
+    echo $callback.'({';
26
+} else {
27
+    header("Content-Type: application/json");
28
+    echo '{';
29
+}
30
+
31
+$first = true;
32
+foreach ($tables as $table) {
33
+    if ($first) $first = false;
34
+    else echo ',';
35
+    echo '"'.$table.'":[';
36
+    if ($result = $mysqli->query("SELECT * FROM `$table`")) {
37
+        $fields = array();
38
+        foreach ($result->fetch_fields() as $field) $fields[] = $field->name;
39
+        echo json_encode($fields);
40
+        while ($row = $result->fetch_row()) echo ','.json_encode($row);
41
+        $result->close();
42
+    }
43
+    echo ']';
44
+}
45
+
46
+if ($callback) {
47
+    echo '});';
48
+} else {
49
+    echo '}';
50
+}

+ 9
- 0
config.php.dist View File

@@ -0,0 +1,9 @@
1
+<?php
2
+$config = array(
3
+    "hostname"=>"localhost",
4
+    "username"=>"username",
5
+    "password"=>"password",
6
+    "database"=>"database",
7
+    "whitelist"=>false,
8
+    "blacklist"=>array("users"),
9
+);

Loading…
Cancel
Save