-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.php
129 lines (106 loc) · 3.28 KB
/
init.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
<?php
require __DIR__ . '/config.php';
/**
* Dynamic Content
* Get PATH part from REQUEST_URI.
* Basic hack protections.
* Remove slashes from the beginning and end of the string.
* Fix trailing slash problem: Keep it /'d.
* Split REQUEST_URI at the slashes.
* Look if anything in the application matches the request.
*/
$path = parse_url($_SERVER['REQUEST_URI']);
$uri = strip_tags($path['path']);
$uri = strtolower($uri);
$uri = trim($uri, '/');
$http['301'] = ( strcmp($path['path'], "/$uri/") != 0 );
$http['404'] = false;
/**
* Make pear/Pager custom links work.
*/
if (preg_match('/\/(?:page|pagina)?(?:\-)?([0-9]+)$/', $uri, $matches)) {
$_GET['pageID'] = $_REQUEST['pageID'] = $matches[1];
// $uri = substr($uri, 0, strrpos($uri, '/'));
}
unset($matches);
$level = explode('/', $uri);
$depth = count($level);
$resource = end($level);
foreach ($level as &$v) {
$v = trim($v);
if (empty($v)) {
$http['404'] = true;
break;
}
}
unset($v);
/**
* Sections.
* Sections refers to content that doesn't change data.
*
* Modules.
* Every resource that receives GET or POST data must be a module.
*/
if (!$http['404'] && $depth > 0) {
$modules = [
'.admin' => 'administrator',
'blog' => 'blog',
'captcha' => 'captcha',
'contact' => 'contact',
'buscar' => 'search',
'sign-in' => 'sign-in',
'sign-out' => 'sign-out',
'sign-up' => 'sign-up',
];
$section = APP_PATH . '/lib/default/' . str_replace('/', '-', $uri) . '.php';
if (is_file($section)) {
$rewrite = $section;
} elseif (isset($modules[$level[0]])) {
require APP_PATH . '/lib/modules/' . $modules[$level[0]] . '/.main.php';
} else {
require APP_PATH . '/lib/modules/rewrite/.main.php';
}
if (isset($rewrite)) {
if ($http['301']) {
header('Location: ' . $app['url'] . $uri . '/', true, 301);
exit;
}
/**
* Load Twig.
*/
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem([
APP_PATH . '/lib/views'
]);
$loader->addPath(APP_PATH . '/lib/views/2016', '2016');
$loader->addPath(APP_PATH . '/lib/views/administrator', 'administrator');
$twig = new Twig_Environment($loader);
if (!$app['production']) {
$twig->addExtension(new Twig_Extension_Debug());
}
require $rewrite;
$twig->addGlobal('app', $app);
$twig->addGlobal('act', $act);
$twig->addGlobal('pager', ( isset($pager) ? $pager : null ));
$twig->addGlobal('_GET', $_GET);
$twig->addGlobal('_POST', $_POST);
$twig->addGlobal('_REQUEST', $_REQUEST);
$twig->addGlobal('_ERROR', ( isset($error) ? $error : null ));
if (isset($template)) {
echo $template->render(
( isset($templateVars) ? $templateVars : [] )
);
}
return;
}
/**
* Fix broken links.
* Prevent parsing too long strings with the regular expressions engine.
*/
if (strlen($_SERVER['REQUEST_URI']) < 100) {
require APP_PATH . '/lib/modules/rewrite/link-rot.php';
}
}
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
include APP_PATH . '/www/404.html';
exit;