/
index.php
132 lines (118 loc) · 3.22 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Front controller
*/
session_start();
require_once __DIR__ . '/app.php';
/**
* Einfacher Renderer.
* @param $template wird in "templates/layout.php" eingebettet
* @param $vars stehen als lokale Variable zur Verfügung
*/
function render($template, $vars = array(), $layout = FALSE) {
global $logged_user;
extract($vars);
ob_start();
require __DIR__ . '/templates/' . $template . '.php';
$contents = ob_get_clean();
if ($layout) {
ob_start();
require __DIR__ . '/templates/layout.php';
return ob_get_clean();
}
else {
return $contents;
}
}
/**
* @param Neue URL
*/
function redirect($location) {
header('Location: ' . $location);
exit;
}
/*
* --------------------------------------------------------
*/
/*
* Maximales Debugging zur Entwicklung
* @todo für Produktivbetrieb deaktivieren
*/
error_reporting(-1);
ini_set('display_errors', TRUE);
/*
* Einfacher Router auf der Basis von request_uri in nginx
* Apache/mod_rewrite may require path_info instead
*/
if (preg_match('/^([^\?]*)\?/', $_SERVER['REQUEST_URI'], $matches)) {
$path_info = $matches[1];
}
else {
$path_info = $_SERVER['REQUEST_URI'];
}
$path_info = explode('/', empty($path_info)
? '/index'
: $path_info);
// Default-Controller ist 'index'
$path = empty($path_info[1]) ? 'index' : $path_info[1];
// Nur erlaubte Zeichen für Controller-Namen
$controller = preg_replace('/[^_0-9a-z]+/i', '_', $path);
/*
* Verbindung mit der Datenbank.
* Der Einfachheit halber wird der DB-Link implizit gespeichert.
*/
$config = parse_ini_file(__DIR__ . '/database.ini');
if (!empty($config['db_driver']) && $config['db_driver'] == 'sqlite') {
// SQlite
class DB extends SQLite3 {
const type = 'sqlite';
public function __construct($fname) {
parent::__construct($fname);
}
}
$GLOBALS['db'] = new DB($config['db_host']);
}
else {
// MySQL
class DB {
const type = 'mysql';
function __construct($config) {
$this->db = new mysqli(
$config['db_host'],
$config['db_user'],
$config['db_pass'],
$config['db_name']
);
$this->query('SET NAMES UTF8');
}
function escapeString($txt) { return $this->db->real_escape_string($txt); }
function exec($sql) { return $this->db->query($sql); }
function query($sql) { $this->result = $this->db->query($sql); }
function querySingle($sql) { return $this->db->query($sql)->fetch_object(); }
function fetchArray() { return $this->result->fetch_row(); }
}
$GLOBALS['db'] = new DB($config);
}
/*
* Aufruf des Controllers und rendern eines gleichnamigen Templates,
* falls das Ergebnis ein Array ist
*/
try {
if (function_exists($controller)) {
$result = call_user_func_array($controller, $path_info);
}
else {
header('Content-type: text/html', /*replace*/TRUE, /*Service Unavailable*/503);
echo render('error', array(
'message' => "Kein Controller {$controller}"), /*layout*/TRUE);
exit();
}
if (is_array($result)) {
echo render($controller, $result, /*layout*/TRUE);
}
}
catch (Exception $e) {
header('Content-type: text/html', /*replace*/TRUE, /*Service Unavailable*/503);
echo render('error', array(
'message' => $e->getMessage()), /*layout*/TRUE);
}