api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. include "config.php";
  3. function connectDatabase($hostname,$username,$password,$database) {
  4. global $config;
  5. $mysqli = new mysqli($hostname,$username,$password,$database);
  6. if ($mysqli->connect_errno) {
  7. die('Connect failed: '.$mysqli->connect_error);
  8. }
  9. return $mysqli;
  10. }
  11. function parseGetParameter($name,$characters,$default) {
  12. $value = isset($_GET[$name])?$_GET[$name]:$default;
  13. return $characters?preg_replace("/[^$characters]/",'',$value):$value;
  14. }
  15. function applyWhitelist($table,$action,$list) {
  16. if ($list===false) return $table;
  17. $list = array_filter($list, function($actions){
  18. return strpos($actions,$action[0])!==false;
  19. });
  20. return array_intersect($table, array_keys($list));
  21. }
  22. function applyBlacklist($table,$action,$list) {
  23. if ($list===false) return $table;
  24. $list = array_filter($list, function($actions) use ($action) {
  25. return strpos($actions,$action[0])!==false;
  26. });
  27. return array_diff($table, array_keys($list));
  28. }
  29. function applyWhitelistAndBlacklist($table, $action, $whitelist, $blacklist) {
  30. $table = applyWhitelist($table, $action, $whitelist);
  31. $table = applyBlacklist($table, $action, $blacklist);
  32. if (empty($table)) exitWith404();
  33. return $table;
  34. }
  35. function processTableParameter($table,$database,$mysqli) {
  36. global $config;
  37. $tablelist = explode(',',$table);
  38. $tables = array();
  39. foreach ($tablelist as $table) {
  40. $table = str_replace('*','%',$table);
  41. if ($result = $mysqli->query("SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE '$table' AND `TABLE_SCHEMA` = '$database'")) {
  42. while ($row = $result->fetch_row()) $tables[] = $row[0];
  43. $result->close();
  44. }
  45. }
  46. return $tables;
  47. }
  48. function findPrimaryKey($table,$database,$mysqli) {
  49. global $config;
  50. $keys = array();
  51. if ($result = $mysqli->query("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `COLUMN_KEY` = 'PRI' AND `TABLE_NAME` = '$table[0]' AND `TABLE_SCHEMA` = '$database'")) {
  52. while ($row = $result->fetch_row()) $keys[] = $row[0];
  53. $result->close();
  54. }
  55. return count($keys)?$keys[0]:false;
  56. }
  57. function exitWith404() {
  58. die(header("Content-Type:",true,404));
  59. }
  60. function startOutput($callback) {
  61. if ($callback) {
  62. header("Content-Type: application/javascript");
  63. echo $callback.'(';
  64. } else {
  65. header("Content-Type: application/json");
  66. }
  67. }
  68. function endOutput($callback) {
  69. if ($callback) {
  70. echo ');';
  71. }
  72. }
  73. function processKeyParameter($key,$table,$database,$mysqli) {
  74. if ($key) {
  75. $key = array($key,findPrimaryKey($table,$database,$mysqli));
  76. if ($key[1]===false) exitWith404();
  77. }
  78. return $key;
  79. }
  80. function processFilterParameter($filter) {
  81. if ($filter) {
  82. $filter = explode(':',$filter,2);
  83. if (count($filter)==2) {
  84. $filter[0] = preg_replace('/[^a-zA-Z0-9\-_]/','',$filter[0]);
  85. $filter[1] = $mysqli->real_escape_string($filter[1]);
  86. $filter[2] = 'LIKE';
  87. if ($match=='any'||$match=='start') $filter[1] .= '%';
  88. if ($match=='any'||$match=='end') $filter[1] = '%'.$filter[1];
  89. if ($match=='exact') $filter[2] = '=';
  90. if ($match=='lower') $filter[2] = '<';
  91. if ($match=='upto') $filter[2] = '<=';
  92. if ($match=='from') $filter[2] = '>=';
  93. if ($match=='higher') $filter[2] = '>';
  94. } else {
  95. $filter = false;
  96. }
  97. }
  98. return $filter;
  99. }
  100. function processPageParameter($page) {
  101. if ($page) {
  102. $page = explode(':',$page,2);
  103. if (count($page)<2) $page[1]=20;
  104. $page[0] = ($page[0]-1)*$page[1];
  105. }
  106. return $page;
  107. }
  108. $action = parseGetParameter('action', 'a-z', 'list');
  109. $table = parseGetParameter('table', 'a-zA-Z0-9\-_*,', '*');
  110. $key = parseGetParameter('key', 'a-zA-Z0-9\-,', false); // auto-increment or uuid
  111. $callback = parseGetParameter('callback', 'a-zA-Z0-9\-_', false);
  112. $page = parseGetParameter('page', '0-9', false);
  113. $filter = parseGetParameter('filter', false, 'start');
  114. $match = parseGetParameter('match', 'a-z', false);
  115. $mysqli = connectDatabase($config["hostname"], $config["username"], $config["password"], $config["database"]);
  116. $table = processTableParameter($table,$config["database"],$mysqli);
  117. $key = processKeyParameter($key,$table,$config["database"],$mysqli);
  118. $filter = processFilterParameter($filter);
  119. $page = processPageParameter($page);
  120. $table = applyWhitelistAndBlacklist($table,$action,$config['whitelist'],$config['blacklist']);
  121. startOutput($callback);
  122. switch($action){
  123. case 'list':
  124. echo '{';
  125. $tables = $table;
  126. $first_table = true;
  127. foreach ($tables as $table) {
  128. if ($first_table) $first_table = false;
  129. else echo ',';
  130. echo '"'.$table.'":{';
  131. if (is_array($page)) {
  132. $sql = "SELECT COUNT(*) FROM `$table`";
  133. if (is_array($filter)) $sql .= " WHERE `$filter[0]` $filter[2] '$filter[1]'";
  134. if ($result = $mysqli->query($sql)) {
  135. $pages = $result->fetch_row();
  136. $pages = floor($pages[0]/$page[1])+1;
  137. echo '"pages":"'.$pages.'",';
  138. }
  139. }
  140. echo '"columns":';
  141. $sql = "SELECT * FROM `$table`";
  142. if (is_array($filter)) $sql .= " WHERE `$filter[0]` $filter[2] '$filter[1]'";
  143. if (is_array($page)) $sql .= " LIMIT $page[1] OFFSET $page[0]";
  144. if ($result = $mysqli->query($sql)) {
  145. $fields = array();
  146. foreach ($result->fetch_fields() as $field) $fields[] = $field->name;
  147. echo json_encode($fields);
  148. echo ',"records":[';
  149. $first_row = true;
  150. while ($row = $result->fetch_row()) {
  151. if ($first_row) $first_row = false;
  152. else echo ',';
  153. echo json_encode($row);
  154. }
  155. $result->close();
  156. }
  157. echo ']}';
  158. }
  159. echo '}';
  160. break;
  161. case 'read':
  162. if ($result = $mysqli->query("SELECT * FROM `$table[0]` WHERE `$key[1]` = '$key[0]'")) {
  163. $value = $result->fetch_assoc();
  164. echo json_encode($value);
  165. $result->close();
  166. }
  167. break;
  168. case 'create': break;
  169. case 'update': break;
  170. case 'delete': break;
  171. default: exitWith404();
  172. }
  173. endOutput($callback);