From d5ddc0bb9a18138d2440cc9215e390df1d5d3acf Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Wed, 5 Aug 2020 07:51:29 +0200 Subject: [PATCH] Get templates and database connection running --- Connection.class.php | 24 ++++++++ Router.class.php | 106 +++++++++++++++++++++++++++++++++++ Setting.class.php | 16 ++++-- Template.class.php | 2 +- connection.php | 9 --- index.php | 21 ++----- sql/schema.sql | 6 +- templates/default/footer.tpl | 2 + templates/default/header.tpl | 9 +++ templates/default/index.tpl | 4 +- views/index.php | 27 +++++++++ 11 files changed, 191 insertions(+), 35 deletions(-) create mode 100644 Connection.class.php create mode 100644 Router.class.php delete mode 100644 connection.php create mode 100644 templates/default/footer.tpl create mode 100644 views/index.php diff --git a/Connection.class.php b/Connection.class.php new file mode 100644 index 0000000..47d2b02 --- /dev/null +++ b/Connection.class.php @@ -0,0 +1,24 @@ +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); + } +} \ No newline at end of file diff --git a/Router.class.php b/Router.class.php new file mode 100644 index 0000000..a3789c0 --- /dev/null +++ b/Router.class.php @@ -0,0 +1,106 @@ + $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'].'
'; + + // 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)); + } + } + + } + +} + +} \ No newline at end of file diff --git a/Setting.class.php b/Setting.class.php index 67c3fdb..c9ac022 100644 --- a/Setting.class.php +++ b/Setting.class.php @@ -1,19 +1,21 @@ 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; } } \ No newline at end of file diff --git a/Template.class.php b/Template.class.php index 4ae07c2..91899e6 100644 --- a/Template.class.php +++ b/Template.class.php @@ -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 ); } diff --git a/connection.php b/connection.php deleted file mode 100644 index b545588..0000000 --- a/connection.php +++ /dev/null @@ -1,9 +0,0 @@ -connect_errno) { - die("Failed to connect to database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error); -}; \ No newline at end of file diff --git a/index.php b/index.php index 1d12da2..b2f7f31 100644 --- a/index.php +++ b/index.php @@ -1,22 +1,13 @@ 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(); ?> \ No newline at end of file diff --git a/sql/schema.sql b/sql/schema.sql index 0ec7627..d720e01 100644 --- a/sql/schema.sql +++ b/sql/schema.sql @@ -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`) ); \ No newline at end of file diff --git a/templates/default/footer.tpl b/templates/default/footer.tpl new file mode 100644 index 0000000..691287b --- /dev/null +++ b/templates/default/footer.tpl @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/templates/default/header.tpl b/templates/default/header.tpl index e69de29..f3e900b 100644 --- a/templates/default/header.tpl +++ b/templates/default/header.tpl @@ -0,0 +1,9 @@ + + + + +{$website_title} + + + diff --git a/templates/default/index.tpl b/templates/default/index.tpl index 8318c86..5205a91 100644 --- a/templates/default/index.tpl +++ b/templates/default/index.tpl @@ -1 +1,3 @@ -Test \ No newline at end of file +{include file="header.tpl"} +Test +{include file="footer.tpl"} \ No newline at end of file diff --git a/views/index.php b/views/index.php new file mode 100644 index 0000000..2c0beb6 --- /dev/null +++ b/views/index.php @@ -0,0 +1,27 @@ +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(); \ No newline at end of file