diff --git a/public/assets/css/search.css b/public/assets/css/search.css index 83cb33b..738f287 100644 --- a/public/assets/css/search.css +++ b/public/assets/css/search.css @@ -14,6 +14,7 @@ header { align-items: center; height: 100px; min-height: 80px; + flex: none; } header h1 { diff --git a/src/database/Database.php b/src/database/Database.php index 93ed58a..c9161e7 100644 --- a/src/database/Database.php +++ b/src/database/Database.php @@ -3,13 +3,34 @@ include_once dirname(__DIR__,1)."/Globals.php"; class Database extends mysqli { - public function __construct() { - $this->config = Import::json("config.json"); - } + public function __construct($table) { + // Load config file from this directory + $config_path = dirname(__FILE__,1)."/config.json"; + $config = Import::json($config_path); - private function get_server() { - foreach($this->config->servers as $server) { - yield $server; + parent::__construct(); + $this->ssl_set(); + + // Attempt to connect to MySQL servers in order (moving to the next on failure) + foreach($config->servers as $server) { + $db = $this->real_connect($server->host,$server->user,$server->pass,$server->db); + if($db) { + return true; + } } } + + protected function get_rows() { + if(!$this->ping()) { + return ["error" => "NO_DB"]; + } + + $query = $this->query($sql); + + $rows = []; + foreach($query->fetch_row() as $row) { + $rows[] = $row; + } + return $rows; + } } \ No newline at end of file diff --git a/src/search/Search.php b/src/search/Search.php index a2f39cf..0aec9ac 100644 --- a/src/search/Search.php +++ b/src/search/Search.php @@ -7,8 +7,9 @@ public function __construct() { parent::__construct("search"); - $this->query = $this->real_escape_string($query); + $this->query = $this->real_escape_string($query); // Escape the user-provided query + // Determine response type from request header or search param $mime_type = $_SERVER["HTTP_CONTENT_TYPE"] ? $_SERVER["HTTP_CONTENT_TYPE"] : $_GET["f"]; switch($mime_type) { case "html": @@ -24,15 +25,13 @@ } } + // Perform a seach on the given query and return the results as an array private function get_results() { - $sql = "SELECT id,template,content FROM `search` WHERE `content` LIKE '%{$this->query}%'"; - $query = $this->query($sql); - if(!$query->num_rows()) { - return false; - } - return $query->fetch_assoc(); + $sql = "SELECT template,title,content,href FROM `search` WHERE `content` LIKE '%{$this->query}%'"; + return $this->get_rows($sql); } + // Load HTML template from disk private function get_html_template($name) { $path = dirname(__FILE__,1)."/templates/${name}.html"; if(!is_file($path)) { @@ -42,18 +41,22 @@ return $html; } + // Return query as HTML from templates private function get_html() { - $results = [ - $this->get_html_template("card_default"), - $this->get_html_template("defaults"), - $this->get_html_template("card_default"), - $this->get_html_template("result_about") - ]; + $results = $this->get_results(); + $results = array_map(function($result) { + // Use first row as template name + $template = $this->get_html_template($result[0]); + // Use remaining rows as format arguments + $format = array_shift($result); + return sprintf($template,...$format); + },$results); header("Content-Type: text/html"); echo implode("",$results); } + // Return query as JSON private function get_json() { $data = [ "results" => []