-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrontController.php
executable file
·154 lines (136 loc) · 5.14 KB
/
FrontController.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
require_once dirname(__FILE__) . '/ActionController.php';
class FrontController {
protected function getParams($params = []) {
$headers = [];
foreach ($_SERVER as $sI => $sItem) {
if (preg_match('/^HTTP_.*$/', $sI)) {
$sI = strtolower(preg_replace('/^HTTP_(.*)$/', '$1', $sI));
$headers[$sI] = $sItem;
}
}
if (VERBOSE_LOG) {
if ($headers) {
error_log('HEADER: ' . json_encode($headers));
}
if ($_POST) {
error_log('POST_FORM: ' . json_encode($_POST));
} else if (($input = file_get_contents('php://input'))) {
error_log('POST_BODY: ' . $input);
}
}
return $headers + $_GET + ($params ?: []);
}
public static function createInstance() {
if (!defined('PAGE_DIR')) {
header('HTTP/1.1 500 Internal Server Error');
return;
}
$instance = new self();
return $instance;
}
public function rockWeb($controllerName, $arrPath = [], $route = '') {
$controller = "{$controllerName}Actions";
$ctlFile = CONTROLLER_DIR . '/' . $controller . '.php';
if (!is_file($ctlFile)) {
header('location: /404');
return;
}
require_once $ctlFile;
$controller = new $controller();
$controller->setName($controllerName);
$controller->route = $route;
$action = 'index';
if ($arrPath) {
$action = $arrPath[0];
}
$params = $this->getParams($params);
$controller->dispatchAction($action, $params, $arrPath);
}
public function rockApi($controllerName, $arrPath = [], $version = 'v2', $route = '') {
header('Content-Type: application/json; charset=UTF-8');
if ($_GET['ssid']) {
session_id($_GET['ssid']);
}
$controller = "{$controllerName}Actions";
$ctlFile = API_CONTROLLER_DIR . '/' . $version . '/' . $controller . '.php';
if (!is_file($ctlFile)) {
header('location: /404');
return;
}
require_once $ctlFile;
$controller = new $controller();
$controller->setName($controllerName);
$controller->route = $route;
$params = [];
$action = 'index';
//objects/id
if (preg_match('/^\d+$/', $arrPath[0])) {
$params['id'] = array_shift($arrPath);
//objects/id/action
$action = $arrPath ? array_shift($arrPath) : $action;
//objects/action
} else {
$action = $arrPath ? array_shift($arrPath) : $action;
//objects/action/id
if ($arrPath) {
$params['id'] = array_shift($arrPath);
}
}
$params = $this->getParams($params);
$controller->dispatchAction($action, $params, $arrPath);
}
public function dispatch() {
header('Access-Control-Allow-Origin: ' . SITE_URL);
header('Access-Control-Allow-Credentials: true');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('HTTP/1.1 204 No Content');
return;
}
if (($_SERVER['SERVER_NAME'] !== '0d0f.redirectme.net' // @todo wechat debug
&& $_SERVER['SERVER_NAME'] !== 'exfe.com' // @todo wechat debug
&& $_SERVER['SERVER_NAME'] !== '127.0.0.1')
&& !preg_match('/^.*' . preg_replace(
'/^([^\/]*\/\/)(.*)$/', '$2', SITE_URL
) . '$/i', $_SERVER['SERVER_NAME'])) {
header('location: ' . SITE_URL);
return;
}
$route = isset($_GET['_route']) ? $_GET['_route'] : $_SERVER['REQUEST_URI'];
$arrPath = explode(
'/', strtolower(current(explode('?', $route)))
);
array_shift($arrPath);
if (!BUS_REMOTE && (@$arrPath[1] === 'bus' && @$_SERVER['REMOTE_ADDR'] !== '127.0.0.1')) {
header('HTTP/1.1 403 Forbidden');
echo "Human beings are a disease, a cancer of this planet. You are a plague, and we are the cure. - Smith, The Matrix\n";
return;
}
$first = array_shift($arrPath);
$last = sizeof($arrPath) - 1;
if ($arrPath[$last] === '') {
unset($arrPath[$last]);
}
if (!$first) {
$this->rockWeb('home', $arrPath, $route);
} else if (preg_match('/^!\d+$/', $first)) { // @todo: ignore crosses urls
array_unshift($arrPath, $first);
array_unshift($arrPath, 'index');
$this->rockWeb('home', $arrPath, $route);
} else if (preg_match('/^v\d+$/', $first)) {
$controller = array_shift($arrPath);
$this->rockApi($controller, $arrPath, $first, $route);
} else if ($arrPath[0] === '500') {
header('location: /500');
return;
} else {
switch ($first) {
case 'toapp':
case 'wechat':
$first = 'home';
array_unshift($arrPath, 'index');
}
$this->rockWeb($first, $arrPath);
}
}
}