dev21w34e

This commit is contained in:
Victor Westerlund 2021-10-27 17:41:14 +02:00
parent 180bc0b0d9
commit 49222eb0f9
3 changed files with 44 additions and 19 deletions

View file

@ -14,6 +14,7 @@ header {
align-items: center; align-items: center;
height: 100px; height: 100px;
min-height: 80px; min-height: 80px;
flex: none;
} }
header h1 { header h1 {

View file

@ -3,13 +3,34 @@
include_once dirname(__DIR__,1)."/Globals.php"; include_once dirname(__DIR__,1)."/Globals.php";
class Database extends mysqli { class Database extends mysqli {
public function __construct() { public function __construct($table) {
$this->config = Import::json("config.json"); // Load config file from this directory
$config_path = dirname(__FILE__,1)."/config.json";
$config = Import::json($config_path);
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;
}
}
} }
private function get_server() { protected function get_rows() {
foreach($this->config->servers as $server) { if(!$this->ping()) {
yield $server; return ["error" => "NO_DB"];
} }
$query = $this->query($sql);
$rows = [];
foreach($query->fetch_row() as $row) {
$rows[] = $row;
}
return $rows;
} }
} }

View file

@ -7,8 +7,9 @@
public function __construct() { public function __construct() {
parent::__construct("search"); 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"]; $mime_type = $_SERVER["HTTP_CONTENT_TYPE"] ? $_SERVER["HTTP_CONTENT_TYPE"] : $_GET["f"];
switch($mime_type) { switch($mime_type) {
case "html": case "html":
@ -24,15 +25,13 @@
} }
} }
// Perform a seach on the given query and return the results as an array
private function get_results() { private function get_results() {
$sql = "SELECT id,template,content FROM `search` WHERE `content` LIKE '%{$this->query}%'"; $sql = "SELECT template,title,content,href FROM `search` WHERE `content` LIKE '%{$this->query}%'";
$query = $this->query($sql); return $this->get_rows($sql);
if(!$query->num_rows()) {
return false;
}
return $query->fetch_assoc();
} }
// Load HTML template from disk
private function get_html_template($name) { private function get_html_template($name) {
$path = dirname(__FILE__,1)."/templates/${name}.html"; $path = dirname(__FILE__,1)."/templates/${name}.html";
if(!is_file($path)) { if(!is_file($path)) {
@ -42,18 +41,22 @@
return $html; return $html;
} }
// Return query as HTML from templates
private function get_html() { private function get_html() {
$results = [ $results = $this->get_results();
$this->get_html_template("card_default"), $results = array_map(function($result) {
$this->get_html_template("defaults"), // Use first row as template name
$this->get_html_template("card_default"), $template = $this->get_html_template($result[0]);
$this->get_html_template("result_about") // Use remaining rows as format arguments
]; $format = array_shift($result);
return sprintf($template,...$format);
},$results);
header("Content-Type: text/html"); header("Content-Type: text/html");
echo implode("",$results); echo implode("",$results);
} }
// Return query as JSON
private function get_json() { private function get_json() {
$data = [ $data = [
"results" => [] "results" => []