mirror of
https://codeberg.org/vlw/victorwesterlund.com.git
synced 2025-09-14 03:23:41 +02:00
dev21w34e
This commit is contained in:
parent
180bc0b0d9
commit
49222eb0f9
3 changed files with 44 additions and 19 deletions
|
@ -14,6 +14,7 @@ header {
|
|||
align-items: center;
|
||||
height: 100px;
|
||||
min-height: 80px;
|
||||
flex: none;
|
||||
}
|
||||
|
||||
header h1 {
|
||||
|
|
|
@ -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);
|
||||
|
||||
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() {
|
||||
foreach($this->config->servers as $server) {
|
||||
yield $server;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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" => []
|
||||
|
|
Loading…
Add table
Reference in a new issue