Get templates and database connection running

This commit is contained in:
Kumi 2020-08-05 07:51:29 +02:00
parent e7a062027b
commit d5ddc0bb9a
11 changed files with 191 additions and 35 deletions

24
Connection.class.php Normal file
View file

@ -0,0 +1,24 @@
<?php
class Connection {
private $mysqli = null;
public function __construct()
{
$config = require("config.php");
$this->mysqli = new mysqli($config["db_host"], $config["db_user"], $config["db_pass"], $config["db_name"], $config["db_port"]);
if ($this->mysqli->connect_errno) {
die("Failed to connect to database: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error);
};
}
public function query($query="")
{
return $this->mysqli->query($query);
}
public function escape($string="")
{
return $this->mysqli->real_escape_string($string);
}
}

106
Router.class.php Normal file
View file

@ -0,0 +1,106 @@
<?php
# Schamlos geklaut von https://steampixel.de/einfaches-und-elegantes-url-routing-mit-php/
class Route{
private static $routes = Array();
private static $pathNotFound = null;
private static $methodNotAllowed = null;
public static function add($expression, $function, $method = 'get'){
array_push(self::$routes,Array(
'expression' => $expression,
'function' => $function,
'method' => $method
));
}
public static function pathNotFound($function){
self::$pathNotFound = $function;
}
public static function methodNotAllowed($function){
self::$methodNotAllowed = $function;
}
public static function run($basepath = '/'){
// Parse current url
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri
if(isset($parsed_url['path'])){
$path = $parsed_url['path'];
}else{
$path = '/';
}
// Get current request method
$method = $_SERVER['REQUEST_METHOD'];
$path_match_found = false;
$route_match_found = false;
foreach(self::$routes as $route){
// If the method matches check the path
// Add basepath to matching string
if($basepath!=''&&$basepath!='/'){
$route['expression'] = '('.$basepath.')'.$route['expression'];
}
// Add 'find string start' automatically
$route['expression'] = '^'.$route['expression'];
// Add 'find string end' automatically
$route['expression'] = $route['expression'].'$';
// echo $route['expression'].'<br/>';
// Check path match
if(preg_match('#'.$route['expression'].'#',$path,$matches)){
$path_match_found = true;
// Check method match
if(strtolower($method) == strtolower($route['method'])){
array_shift($matches);// Always remove first element. This contains the whole string
if($basepath!=''&&$basepath!='/'){
array_shift($matches);// Remove basepath
}
call_user_func_array($route['function'], $matches);
$route_match_found = true;
// Do not check other routes
break;
}
}
}
// No matching route was found
if(!$route_match_found){
// But a matching path exists
if($path_match_found){
header("HTTP/1.0 405 Method Not Allowed");
if(self::$methodNotAllowed){
call_user_func_array(self::$methodNotAllowed, Array($path,$method));
}
}else{
header("HTTP/1.0 404 Not Found");
if(self::$pathNotFound){
call_user_func_array(self::$pathNotFound, Array($path));
}
}
}
}
}

View file

@ -1,19 +1,21 @@
<?php
include_once("./connection.php");
require_once("Connection.class.php");
class Setting {
private $connection = null;
private $settingKey = "";
public function __construct($setting_key)
{
$this->settingKey = $mysqli->real_escape_string($setting_key);
$this->connection = new Connection();
$this->settingKey = $this->connection->escape($setting_key);
}
public function get($default="")
{
$query = "SELECT `setting_value` FROM `settings` WHERE `setting_key` = '" . $this->settingKey . "';";
$result = $mysqli->query($query);
$result = $this->connection->query($query);
if ($result->num_rows == 1) {
return $result->fetch_assoc()["setting_key"];
} else {
@ -26,7 +28,7 @@ class Setting {
public function set($value)
{
$escaped_value = $mysqli->real_escape_string($value);
$escaped_value = $this->connection->escape($value);
if ($this->get()) {
$query = "UPDATE `settings` SET `setting_value` = '" . $escaped_value . "' WHERE `setting_key` = '" . $this->settingKey . "';";
@ -34,8 +36,10 @@ class Setting {
$query = "INSERT INTO `settings` (`setting_key`, `setting_value`) VALUES ('". $this->settingKey . "', '" . $escaped_value . "');";
}
if (!$mysqli->query($query)) {
return False;
if (!$this->connection->query($query)) {
return false;
}
return true;
}
}

View file

@ -243,7 +243,7 @@ class Template
$this->template = preg_replace_callback( "/" .$this->leftDelimiterF ."include file=\"(.*)\.(.*)\""
.$this->rightDelimiterF."/isU",
function($matches) {
return file_get_contents($this->templateDir.$matches[0].'.'.$matches[1]);
return file_get_contents($this->templateDir.$matches[1].'.'.$matches[2]);
},
$this->template );
}

View file

@ -1,9 +0,0 @@
<?php
$config = require("./config.php");
$mysqli = new mysqli($config["db_host"], $config["db_user"], $config["db_pass"], $config["db_name"], $config["db_port"]);
if ($mysqli->connect_errno) {
die("Failed to connect to database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
};

View file

@ -1,22 +1,13 @@
<?php
include("Template.class.php");
include("Setting.class.php");
// Das Template laden
$tpl = new Template();
$tpl->load("index.tpl");
define("GastroData", true);
// Die Sprachdatei laden
$langs[] = "de/lang_main.php";
$lang = $tpl->loadLanguage($langs);
include("Router.class.php");
// Platzhalter ersetzen
$tpl->assign( "website_title", "MyHomepage" );
$tpl->assign( "time", date("H:i") );
Route::add('/',function(){
include("views/index.php");
});
// Zugriff auf eine Sprachvariable
$tpl->assign( "test", $lang['test'] );
Route::run('/');
// Und die Seite anzeigen
$tpl->display();
?>

View file

@ -65,11 +65,11 @@ CREATE TABLE IF NOT EXISTS `visitors` (
FOREIGN KEY (`visit_id`) REFERENCES `visits`(`visit_id`)
);
CREATE TABLE IF NOT EXISTS `visit_sessions` (
CREATE TABLE IF NOT EXISTS `visitor_sessions` (
`session_id` VARCHAR(255) NOT NULL,
`salt` VARCHAR(255) NOT NULL,
`visit_id` VARCHAR(255) NOT NULL,
`visitor_id` VARCHAR(255) NOT NULL,
`expiry` DATETIME,
PRIMARY KEY (`session_id`),
FOREIGN KEY (`visit_id`) REFERENCES `visits`(`visit_id`)
FOREIGN KEY (`visitor_id`) REFERENCES `visitors`(`visitor_id`)
);

View file

@ -0,0 +1,2 @@
</body>
</html>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<title>{$website_title}</title>
<meta http-equiv="Content-Type" content="text/xhtml; charset=ISO-8859-1" />
</head>
<body>

View file

@ -1 +1,3 @@
Test
{include file="header.tpl"}
Test
{include file="footer.tpl"}

27
views/index.php Normal file
View file

@ -0,0 +1,27 @@
<?php
if (!defined("GastroData")) {
die("This file cannot be accessed directly. Sorry.");
};
require_once("Template.class.php");
require_once("Setting.class.php");
// Das Template laden
$tpl = new Template();
$tpl->load("index.tpl");
// Die Sprachdatei laden
$langs[] = "de/lang_main.php";
$lang = $tpl->loadLanguage($langs);
// Platzhalter ersetzen
$title = new Setting("title");
$tpl->assign( "website_title", $title->get("GastroData") );
$tpl->assign( "time", date("H:i") );
// Zugriff auf eine Sprachvariable
$tpl->assign( "test", $lang['test'] );
// Und die Seite anzeigen
$tpl->display();