A bunch of new files... :D

This commit is contained in:
Kumi 2020-08-02 14:07:03 +02:00
parent 2446f66e27
commit 6e1c3a98f1
13 changed files with 522 additions and 65 deletions

3
README.md Normal file
View file

@ -0,0 +1,3 @@
### Required PHP modules
* mysqli

41
Setting.class.php Normal file
View file

@ -0,0 +1,41 @@
<?php
include_once("./connection.php");
class Setting {
private $settingKey = "";
public function __construct($setting_key)
{
$this->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;
}
}
}

264
Template.class.php Normal file
View file

@ -0,0 +1,264 @@
<?php
class Template
{
/**
* Der Ordner in dem sich die Templates befinden.
*
* @access private
* @var string
*/
private $templateDir = "templates/default/";
/**
* Der Ordner in dem sich die Sprach-Dateien befinden.
*
* @access private
* @var string
*/
private $languageDir = "language/";
/**
* Der linke Delimter für einen Standard-Platzhalter.
*
* @access private
* @var string
*/
private $leftDelimiter = '{$';
/**
* Der rechte Delimter für einen Standard-Platzhalter.
*
* @access private
* @var string
*/
private $rightDelimiter = '}';
/**
* Der linke Delimter für eine Funktion.
*
* @access private
* @var string
*/
private $leftDelimiterF = '{';
/**
* Der rechte Delimter für eine Funktion.
*
* @access private
* @var string
*/
private $rightDelimiterF = '}';
/**
* Der linke Delimter für ein Kommentar.
* Sonderzeichen müssen escapt werden, weil der Delimter in einem regulärem
* Ausdruck verwendet wird.
*
* @access private
* @var string
*/
private $leftDelimiterC = '\{\*';
/**
* Der rechte Delimter für ein Kommentar.
* Sonderzeichen müssen escapt werden, weil der Delimter in einem regulärem
* Ausdruck verwendet wird.
*
* @access private
* @var string
*/
private $rightDelimiterC = '\*\}';
/**
* Der linke Delimter für eine Sprachvariable
* Sonderzeichen müssen escapt werden, weil der Delimter in einem regulärem
* Ausdruck verwendet wird.
*
* @access private
* @var string
*/
private $leftDelimiterL = '\{L_';
/**
* Der rechte Delimter für eine Sprachvariable
* Sonderzeichen müssen escapt werden, weil der Delimter in einem regulärem
* Ausdruck verwendet wird.
*
* @access private
* @var string
*/
private $rightDelimiterL = '\}';
/**
* Der komplette Pfad der Templatedatei.
*
* @access private
* @var string
*/
private $templateFile = "";
/**
* Der komplette Pfad der Sprachdatei.
*
* @access private
* @var string
*/
private $languageFile = "";
/**
* Der Dateiname der Templatedatei.
*
* @access private
* @var string
*/
private $templateName = "";
/**
* Der Inhalt des Templates.
*
* @access private
* @var string
*/
private $template = "";
/**
* Die Pfade festlegen.
*
* @access public
*/
public function __construct($tpl_id = "") {
// Template Ordner
if ( !empty($tpl_id) ) {
$this->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;
}
}
?>

9
connection.php Normal file
View file

@ -0,0 +1,9 @@
<?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);
};

1
cron.php Normal file
View file

@ -0,0 +1 @@
<?php

22
index.php Normal file
View file

@ -0,0 +1,22 @@
<?php
include("Template.class.php");
include("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
$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();
?>

31
install.php Normal file
View file

@ -0,0 +1,31 @@
<?php
include("./connection.php");
echo("Connected to " . $mysqli->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.");

View file

@ -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)
);

75
sql/schema.sql Normal file
View file

@ -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`)
);

6
sql/settings.sql Normal file
View file

@ -0,0 +1,6 @@
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`)
VALUES
('timezone', 'Europe/Berlin'),
('language', 'de'),
('registration', '1')
;

View file

View file

@ -0,0 +1 @@
Test

View file

@ -0,0 +1,69 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<form>
<div class="form-group row">
<label for="firstname" class="col-4 col-form-label">First Name</label>
<div class="col-8">
<input id="firstname" name="firstname" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="lastname" class="col-4 col-form-label">Last Name</label>
<div class="col-8">
<input id="lastname" name="lastname" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="address1" class="col-4 col-form-label">Address</label>
<div class="col-8">
<input id="address1" name="address1" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="address2" class="col-4 col-form-label"></label>
<div class="col-8">
<input id="address2" name="address2" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="zip" class="col-4 col-form-label">ZIP</label>
<div class="col-8">
<input id="zip" name="zip" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="city" class="col-4 col-form-label">City</label>
<div class="col-8">
<input id="city" name="city" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="state" class="col-4 col-form-label">State</label>
<div class="col-8">
<input id="state" name="state" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="country" class="col-4 col-form-label">Country</label>
<div class="col-8">
<input id="country" name="country" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-4 col-form-label">Email</label>
<div class="col-8">
<input id="email" name="email" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-4 col-form-label">Phone</label>
<div class="col-8">
<input id="phone" name="phone" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>