api.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2013-2020 Johannes 'Banana' Keßler
  5. *
  6. * https://www.bananas-playground.net
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  10. *
  11. * You should have received a copy of the
  12. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  13. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  14. */
  15. /**
  16. * This is a very simple api to the dataset stored in the DB
  17. * Use this as a base to extend
  18. */
  19. mb_http_output('UTF-8');
  20. mb_internal_encoding('UTF-8');
  21. ini_set('error_reporting',-1); // E_ALL & E_STRICT
  22. date_default_timezone_set('Europe/Berlin');
  23. ## check request
  24. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
  25. if(!empty($_urlToParse)) {
  26. # see http://de2.php.net/manual/en/regexp.reference.unicode.php
  27. if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
  28. die('Malformed request. Make sure you know what you are doing.');
  29. }
  30. }
  31. ## set the error reporting
  32. ini_set('log_errors',true);
  33. ini_set('error_log','./error.log');
  34. require 'lib/helper.class.php';
  35. ## database settings
  36. define('DB_HOST','localhost');
  37. define('DB_USER','user');
  38. define('DB_PASSWORD','test');
  39. define('DB_NAME','imdb');
  40. ## DB connection
  41. $DB = new mysqli(DB_HOST, DB_USER,DB_PASSWORD, DB_NAME);
  42. if ($DB->connect_errno) exit("Can not connect to MySQL Server\n");
  43. $DB->set_charset("utf8mb4");
  44. $DB->query("SET collation_connection = 'utf8mb4_bin'");
  45. $driver = new mysqli_driver();
  46. $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
  47. ## defaults
  48. $returnData = array();
  49. $returnStatusCode = 200;
  50. $_s = '';
  51. if(isset($_GET['s']) && !empty($_GET['s'])) {
  52. $_s = Helper::validate($_GET['s']) ? trim($_GET['s']) : '';
  53. $_s = strtolower($_s);
  54. }
  55. if(!empty($_s)) {
  56. $queryStr = "SELECT `tconst`, `primaryTitle`, `originalTitle`, `startYear`, `runtimeMinutes`, `genres`,
  57. MATCH (`primaryTitle`)
  58. AGAINST ('".$DB->real_escape_string($_s)."' IN NATURAL LANGUAGE MODE) AS score
  59. FROM `title_basics`
  60. WHERE MATCH (`primaryTitle`)
  61. AGAINST ('".$DB->real_escape_string($_s)."' IN NATURAL LANGUAGE MODE)
  62. LIMIT 10";
  63. try {
  64. $query = $DB->query($queryStr);
  65. if ($query !== false && $query->num_rows > 0) {
  66. while (($result = $query->fetch_assoc()) != false) {
  67. $returnData[$result['tconst']] = $result;
  68. }
  69. }
  70. } catch (Exception $e) {
  71. error_log("ERROR search query failed: ".$e->getMessage());
  72. error_log("ERROR search query: ".$queryStr);
  73. }
  74. }
  75. header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  76. header("Cache-Control: post-check=0, pre-check=0", false);
  77. header("Pragma: no-cache");
  78. header('Content-Type: application/json');
  79. if($returnStatusCode !== 200) {
  80. http_response_code($returnStatusCode);
  81. }
  82. echo json_encode($returnData);