-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
104 lines (97 loc) · 1.84 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
<?php
/**
* Stupid simple dispatcher
*/
$url = parseurl();
$config = null;
/**
* Takes a url (or the current one) and "parses" it. Everything after
* the first `/` is considered a param
*
* @param string $url
* @return array
*/
function parseurl($url = null) {
if (!$url) {
$url = $_GET['url'];
}
$host = $_SERVER['HTTP_HOST'];
$path = trim(str_replace($_GET['url'], '', $_SERVER['REQUEST_URI']), '/');
$url = explode('/', $_GET['url']);
$url = array_filter($url);
if (empty($url)) {
$url = array(
'poll'
);
}
return array(
'base' => "http://$host/$path",
'page' => $url[0],
'params' => array_slice($url, 1)
);
}
/**
* Quick redirect helper
*
* @param string $location
*/
function redirect($location) {
global $url;
$location = trim($location, '/');
if ($location === '404') {
header("Location: {$url['base']}/$location", true, 404);
exit();
}
header("Location: {$url['base']}/$location");
exit();
}
/**
* Fairly useless debug function
*
* @param mixed $obj
*/
function debug($obj) {
$out = null;
if (is_string($obj)) {
$out = $obj;
} else {
$out = var_export($obj, true);
}
echo "<pre>$out</pre>";
}
/**
* Create connection
*/
$db = config('database');
$connection = null;
try {
$connection = new PDO(
"sqlite:$db",
null,
null,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);
} catch (PDOException $e) {
debug($e);
$url['page'] = 'error';
$url['params'] = array('database');
}
/**
* Helper function for getting config keys
*
* @param string $key The config key. If null, the object is returned
*/
function config($key = null) {
global $config;
if (!$config) {
$config = json_decode(file_get_contents('config.json'));
}
if ($key) {
return $config->{$key};
}
return $config;
}
if (!file_exists($url['page'].'.php')) {
$url['page'] = '404';
}
require $url['page'].'.php';