123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- /**
- * dolphin. Collection of useful PHP skeletons.
- * Copyright (C) 2013-2020 Johannes 'Banana' Keßler
- *
- * https://www.bananas-playground.net
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
- *
- * You should have received a copy of the
- * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
- * along with this program. If not, see http://www.sun.com/cddl/cddl.html
- */
- /**
- * This is a very simple api to the dataset stored in the DB
- * Use this as a base to extend
- */
- mb_http_output('UTF-8');
- mb_internal_encoding('UTF-8');
- ini_set('error_reporting',-1); // E_ALL & E_STRICT
- date_default_timezone_set('Europe/Berlin');
- ## check request
- $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
- if(!empty($_urlToParse)) {
- # see http://de2.php.net/manual/en/regexp.reference.unicode.php
- if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
- die('Malformed request. Make sure you know what you are doing.');
- }
- }
- ## set the error reporting
- ini_set('log_errors',true);
- ini_set('error_log','./error.log');
- require 'lib/helper.class.php';
- ## database settings
- define('DB_HOST','localhost');
- define('DB_USER','user');
- define('DB_PASSWORD','test');
- define('DB_NAME','imdb');
- ## DB connection
- $DB = new mysqli(DB_HOST, DB_USER,DB_PASSWORD, DB_NAME);
- if ($DB->connect_errno) exit("Can not connect to MySQL Server\n");
- $DB->set_charset("utf8mb4");
- $DB->query("SET collation_connection = 'utf8mb4_bin'");
- $driver = new mysqli_driver();
- $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
- ## defaults
- $returnData = array();
- $returnStatusCode = 200;
- $_s = '';
- if(isset($_GET['s']) && !empty($_GET['s'])) {
- $_s = Helper::validate($_GET['s']) ? trim($_GET['s']) : '';
- $_s = strtolower($_s);
- }
- if(!empty($_s)) {
- $queryStr = "SELECT `tconst`, `primaryTitle`, `originalTitle`, `startYear`, `runtimeMinutes`, `genres`,
- MATCH (`primaryTitle`)
- AGAINST ('".$DB->real_escape_string($_s)."' IN NATURAL LANGUAGE MODE) AS score
- FROM `title_basics`
- WHERE MATCH (`primaryTitle`)
- AGAINST ('".$DB->real_escape_string($_s)."' IN NATURAL LANGUAGE MODE)
- LIMIT 10";
- try {
- $query = $DB->query($queryStr);
- if ($query !== false && $query->num_rows > 0) {
- while (($result = $query->fetch_assoc()) != false) {
- $returnData[$result['tconst']] = $result;
- }
- }
- } catch (Exception $e) {
- error_log("ERROR search query failed: ".$e->getMessage());
- error_log("ERROR search query: ".$queryStr);
- }
- }
- header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
- header("Cache-Control: post-check=0, pre-check=0", false);
- header("Pragma: no-cache");
- header('Content-Type: application/json');
- if($returnStatusCode !== 200) {
- http_response_code($returnStatusCode);
- }
- echo json_encode($returnData);
|