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.

list.php 2.4KB

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