From 6e1c3a98f15ef3c21aba25fa01c510b9b3ecc670 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 2 Aug 2020 14:07:03 +0200 Subject: [PATCH] A bunch of new files... :D --- README.md | 3 + Setting.class.php | 41 +++++ Template.class.php | 264 +++++++++++++++++++++++++++++ connection.php | 9 + cron.php | 1 + index.php | 22 +++ install.php | 31 ++++ schema.sql | 65 ------- sql/schema.sql | 75 ++++++++ sql/settings.sql | 6 + templates/default/header.tpl | 0 templates/default/index.tpl | 1 + templates/default/registration.tpl | 69 ++++++++ 13 files changed, 522 insertions(+), 65 deletions(-) create mode 100644 README.md create mode 100644 Setting.class.php create mode 100644 Template.class.php create mode 100644 connection.php create mode 100644 cron.php create mode 100644 index.php create mode 100644 install.php delete mode 100644 schema.sql create mode 100644 sql/schema.sql create mode 100644 sql/settings.sql create mode 100644 templates/default/header.tpl create mode 100644 templates/default/index.tpl create mode 100644 templates/default/registration.tpl diff --git a/README.md b/README.md new file mode 100644 index 0000000..891303a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +### Required PHP modules + +* mysqli diff --git a/Setting.class.php b/Setting.class.php new file mode 100644 index 0000000..67c3fdb --- /dev/null +++ b/Setting.class.php @@ -0,0 +1,41 @@ +settingKey = $mysqli->real_escape_string($setting_key); + } + + public function get($default="") + { + $query = "SELECT `setting_value` FROM `settings` WHERE `setting_key` = '" . $this->settingKey . "';"; + $result = $mysqli->query($query); + if ($result->num_rows == 1) { + return $result->fetch_assoc()["setting_key"]; + } else { + if (!empty($default)) { + return $default; + } + return false; + } + } + + public function set($value) + { + $escaped_value = $mysqli->real_escape_string($value); + + if ($this->get()) { + $query = "UPDATE `settings` SET `setting_value` = '" . $escaped_value . "' WHERE `setting_key` = '" . $this->settingKey . "';"; + } else { + $query = "INSERT INTO `settings` (`setting_key`, `setting_value`) VALUES ('". $this->settingKey . "', '" . $escaped_value . "');"; + } + + if (!$mysqli->query($query)) { + return False; + } + } +} \ No newline at end of file diff --git a/Template.class.php b/Template.class.php new file mode 100644 index 0000000..c835f30 --- /dev/null +++ b/Template.class.php @@ -0,0 +1,264 @@ +templateDir = "templates/" . $tpl_id . "/"; + } + } + + /** + * Eine Templatedatei öffnen. + * + * @access public + * @param string $file Dateiname des Templates. + * @uses $templateName + * @uses $templateFile + * @uses $templateDir + * @uses $templateId + * @uses parseFunctions() + * @return boolean + */ + public function load($file) { + // Eigenschaften zuweisen + $this->templateName = $file; + $this->templateFile = $this->templateDir . $file; + + // Wenn ein Dateiname übergeben wurde, versuchen, die Datei zu öffnen + if( !empty($this->templateFile) ) { + if( file_exists($this->templateFile) ) { + $this->template = file_get_contents($this->templateFile); + } else { + return false; + } + } else { + return false; + } + + // Funktionen parsen + $this->parseFunctions(); + } + + /** + * Einen Standard-Platzhalter ersetzen. + * + * @access public + * @param string $replace Name des Platzhalters. + * @param string $replacement Der Text, mit dem der Platzhalter ersetzt + * werden soll. + * @uses $leftDelimiter + * @uses $rightDelimiter + * @uses $template + */ + public function assign($replace, $replacement) { + $this->template = str_replace( $this->leftDelimiter .$replace.$this->rightDelimiter, + $replacement, $this->template ); + } + + /** + * Die Sprachdateien öffnen und Sprachvariablem im Template ersetzen. + * + * @access public + * @param array $files Dateinamen der Sprachdateien. + * @uses $languageFiles + * @uses $languageDir + * @uses replaceLangVars() + * @return array + */ + public function loadLanguage($files) { + $this->languageFiles = $files; + + // Versuchen, alle Sprachdateien einzubinden + for( $i = 0; $i < count( $this->languageFiles ); $i++ ) { + if ( !file_exists( $this->languageDir .$this->languageFiles[$i] ) ) { + return false; + } else { + include_once( $this->languageDir .$this->languageFiles[$i] ); + // Jetzt steht das Array $lang zur Verfügung + } + } + + // Die Sprachvariablen mit dem Text ersetzen + $this->replaceLangVars($lang); + + // $lang zurückgeben, damit $lang auch im PHP-Code verwendet werden kann + return $lang; + } + + /** + * Sprachvariablen im Template ersetzen. + * + * @access private + * @param string $lang Die Sprachvariablen. + * @uses $template + */ + private function replaceLangVars($lang) { + $this->template = preg_replace("/\{L_(.*)\}/isUe", "\$lang[strtolower('\\1')]", $this->template); + } + + /** + * Includes parsen und Kommentare aus dem Template entfernen. + * + * @access private + * @uses $leftDelimiterF + * @uses $rightDelimiterF + * @uses $template + * @uses $leftDelimiterC + * @uses $rightDelimiterC + */ + private function parseFunctions() { + // Includes ersetzen ( {include file="..."} ) + while( preg_match( "/" .$this->leftDelimiterF ."include file=\"(.*)\.(.*)\"" + .$this->rightDelimiterF ."/isU", $this->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]); + }, + $this->template ); + } + + + // Kommentare löschen + $this->template = preg_replace( "/" .$this->leftDelimiterC ."(.*)" .$this->rightDelimiterC ."/isU", + "", $this->template ); + } + + /** + * Das "fertige Template" ausgeben. + * + * @access public + * @uses $template + */ + public function display() { + echo $this->template; + } +} +?> \ No newline at end of file diff --git a/connection.php b/connection.php new file mode 100644 index 0000000..b545588 --- /dev/null +++ b/connection.php @@ -0,0 +1,9 @@ +connect_errno) { + die("Failed to connect to database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error); +}; \ No newline at end of file diff --git a/cron.php b/cron.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/cron.php @@ -0,0 +1 @@ +load("index.tpl"); + +// Die Sprachdatei laden +$langs[] = "de/lang_main.php"; +$lang = $tpl->loadLanguage($langs); + +// Platzhalter ersetzen +$tpl->assign( "website_title", "MyHomepage" ); +$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 diff --git a/install.php b/install.php new file mode 100644 index 0000000..0980111 --- /dev/null +++ b/install.php @@ -0,0 +1,31 @@ +host_info . "\n"); + +echo("Installing database...\n"); + +$schema = file_get_contents("./sql/schema.sql"); + +if($mysqli->multi_query($schema)){ + do {} while ($mysqli->more_results() && $mysqli->next_result()); +}; + +if($error=$mysqli->error) { + die("An error has occured while setting up the database: $error"); +}; + +echo("Setting default values...\n"); + +$settings = file_get_contents("./sql/settings.sql"); + +if($mysqli->multi_query($settings)){ + do {} while ($mysqli->more_results() && $mysqli->next_result()); +}; + +if($error=$mysqli->error) { + die("An error has occured while setting up the default values: $error"); +}; + +echo("Done."); \ No newline at end of file diff --git a/schema.sql b/schema.sql deleted file mode 100644 index 29b0be6..0000000 --- a/schema.sql +++ /dev/null @@ -1,65 +0,0 @@ -CREATE TABLE IF NOT EXISTS settings ( - setting_key VARCHAR(255) NOT NULL, - setting_value VARCHAR(255), - PRIMARY KEY (setting_key) -); - -CREATE TABLE IF NOT EXISTS users ( - user_id INT NOT NULL AUTO_INCREMENT, - display_name VARCHAR(255), - email VARCHAR(255) NOT NULL UNIQUE, - password VARCHAR(255) NOT NULL, - salt VARCHAR(255) NOT NULL, - is_admin BOOLEAN, - PRIMARY KEY (user_id) -); - -CREATE TABLE IF NOT EXISTS user_sessions ( - session_id VARCHAR(255) NOT NULL, - salt VARCHAR(255) NOT NULL, - user_id INT NOT NULL, - expiry DATETIME NOT NULL, - PRIMARY KEY (session_id), - FOREIGN KEY (user_id) REFERENCES users(user_id) -); - -CREATE TABLE IF NOT EXISTS locations ( - location_id INT NOT NULL AUTO_INCREMENT, - name VARCHAR(255) NOT NULL, - logo BLOB, - PRIMARY KEY (location_id) -); - -CREATE TABLE IF NOT EXISTS permissions ( - user_id INT NOT NULL, - location_id INT NOT NULL, - is_owner BOOLEAN, - PRIMARY KEY (user_id, location_id), - FOREIGN KEY (user_id) REFERENCES users(user_id), - FOREIGN KEY (location_id) REFERENCES locations(location_id) -); - -CREATE TABLE IF NOT EXISTS visits ( - visit_id INT NOT NULL AUTO_INCREMENT, - location_id INT NOT NULL, - arrival DATETIME NOT NULL, - departure DATETIME NOT NULL, - PRIMARY KEY (visit_id), - FOREIGN KEY (location_id) REFERENCES locations(location_id) -); - -CREATE TABLE IF NOT EXISTS visitors ( - visitor_id INT NOT NULL AUTO_INCREMENT, - visit_id INT NOT NULL, - first_name VARCHAR(255) NOT NULL, - last_name VARCHAR(255) NOT NULL, - address1 VARCHAR(255), - address2 VARCHAR(255), - zip VARCHAR(255), - city VARCHAR(255), - country VARCHAR(255), - phone VARCHAR(255), - email VARCHAR(255), - PRIMARY KEY (visitor_id), - FOREIGN KEY (visit_id) REFERENCES visits(visit_id) -); \ No newline at end of file diff --git a/sql/schema.sql b/sql/schema.sql new file mode 100644 index 0000000..0ec7627 --- /dev/null +++ b/sql/schema.sql @@ -0,0 +1,75 @@ +CREATE TABLE IF NOT EXISTS `settings` ( + `setting_key` VARCHAR(255) NOT NULL, + `setting_value` VARCHAR(255), + PRIMARY KEY (`setting_key`) +); + +CREATE TABLE IF NOT EXISTS `users` ( + `user_id` INT NOT NULL AUTO_INCREMENT, + `display_name` VARCHAR(255), + `email` VARCHAR(255) NOT NULL UNIQUE, + `password` VARCHAR(255) NOT NULL, + `salt` VARCHAR(255) NOT NULL, + `is_admin` BOOLEAN, + PRIMARY KEY (`user_id`) +); + +CREATE TABLE IF NOT EXISTS `user_sessions` ( + `session_id` VARCHAR(255) NOT NULL, + `salt` VARCHAR(255) NOT NULL, + `user_id` INT NOT NULL, + `expiry` DATETIME NOT NULL, + PRIMARY KEY (`session_id`), + FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`) +); + +CREATE TABLE IF NOT EXISTS `locations` ( + `location_id` INT NOT NULL AUTO_INCREMENT, + `name` VARCHAR(255) NOT NULL, + `logo` BLOB, + PRIMARY KEY (`location_id`) +); + +CREATE TABLE IF NOT EXISTS `permissions` ( + `user_id` INT NOT NULL, + `location_id` INT NOT NULL, + `is_owner` BOOLEAN, + PRIMARY KEY (`user_id`, `location_id`), + FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`), + FOREIGN KEY (`location_id`) REFERENCES `locations`(`location_id`) +); + +CREATE TABLE IF NOT EXISTS `visits` ( + `visit_id` VARCHAR(255) NOT NULL, + `location_id` INT NOT NULL, + `arrival` DATETIME NOT NULL, + `departure` DATETIME NOT NULL, + PRIMARY KEY (`visit_id`), + FOREIGN KEY (`location_id`) REFERENCES `locations`(`location_id`) +); + +CREATE TABLE IF NOT EXISTS `visitors` ( + `visitor_id` INT NOT NULL AUTO_INCREMENT, + `visit_id` VARCHAR(255) NOT NULL, + `first_name` VARCHAR(255) NOT NULL, + `last_name` VARCHAR(255) NOT NULL, + `address1` VARCHAR(255), + `address2` VARCHAR(255), + `zip` VARCHAR(255), + `city` VARCHAR(255), + `state` VARCHAR(255), + `country` VARCHAR(255), + `phone` VARCHAR(255), + `email` VARCHAR(255), + PRIMARY KEY (`visitor_id`), + FOREIGN KEY (`visit_id`) REFERENCES `visits`(`visit_id`) +); + +CREATE TABLE IF NOT EXISTS `visit_sessions` ( + `session_id` VARCHAR(255) NOT NULL, + `salt` VARCHAR(255) NOT NULL, + `visit_id` VARCHAR(255) NOT NULL, + `expiry` DATETIME, + PRIMARY KEY (`session_id`), + FOREIGN KEY (`visit_id`) REFERENCES `visits`(`visit_id`) +); \ No newline at end of file diff --git a/sql/settings.sql b/sql/settings.sql new file mode 100644 index 0000000..a542c6c --- /dev/null +++ b/sql/settings.sql @@ -0,0 +1,6 @@ +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) +VALUES + ('timezone', 'Europe/Berlin'), + ('language', 'de'), + ('registration', '1') +; \ No newline at end of file diff --git a/templates/default/header.tpl b/templates/default/header.tpl new file mode 100644 index 0000000..e69de29 diff --git a/templates/default/index.tpl b/templates/default/index.tpl new file mode 100644 index 0000000..8318c86 --- /dev/null +++ b/templates/default/index.tpl @@ -0,0 +1 @@ +Test \ No newline at end of file diff --git a/templates/default/registration.tpl b/templates/default/registration.tpl new file mode 100644 index 0000000..214f52f --- /dev/null +++ b/templates/default/registration.tpl @@ -0,0 +1,69 @@ + + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
\ No newline at end of file