forked from prdatur/phpminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
174 lines (146 loc) · 5.11 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
ob_start();
$phpminer_error_handler_messages = array();
$phpminer_error_handler_suppressed_messages = array();
if (!defined('SITEPATH')) {
define('SITEPATH', dirname(__FILE__));
}
$phpminer_request_is_ajax = false;
require 'includes/ErrorHandler.class.php';
set_error_handler(array('ErrorHandler', 'cc_error_handler'), E_ALL);
function fatal_handler() {
global $system_conf;
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
$stack = array();
if (function_exists('xdebug_get_function_stack')) {
foreach (array_slice(xdebug_get_function_stack(), 1, -1) as $row) {
if (isset($row['class'])) {
$row['type'] = isset($row['type']) && $row['type'] === 'dynamic' ? '->' : '::';
}
if (isset($row['params'])) {
$row['args'] = $row['params'];
}
$stack[] = $row;
}
}
if ($error !== NULL) {
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
}
global $phpminer_request_is_ajax, $phpminer_error_handler_messages;
ErrorHandler::cc_error_handler($errno, $errstr, $errfile, $errline, "", true, $stack);
$error = $phpminer_error_handler_messages;
if (empty($error)) {
return;
}
if ($phpminer_request_is_ajax) {
$code = 560;
$data = sys_get_temp_dir() . '/phpminer_' . uniqid() . '.bugreport';
$return = array("code" => $code, "desc" => null, "data" => $data);
if (is_array($error)) {
$error = implode("\n", $error);
}
file_put_contents($data, "PHPMiner version: " . implode('.', $system_conf['version']) . "\n" . $error);
echo json_encode($return);
die();
}
echo implode("<br>", $error);
die();
}
register_shutdown_function("fatal_handler");
require 'includes/common.php';
if (isset($_GET['controller'])) {
$url = '/' . $_GET['controller'];
if (isset($_GET['action'])) {
$url .= '/' . $_GET['action'];
}
if (isset($_GET['data'])) {
$url .= '/' . $_GET['data'];
}
if (isset($_GET['type']) && $_GET['type'] === 'json') {
$url .= '.json';
}
$_SERVER['REQUEST_URI'] = urlencode($url);
}
$request_uri = urldecode($_SERVER['REQUEST_URI']);
if ($system_conf['directory'] !== '/') {
$request_uri = str_replace($system_conf['directory'], '', $request_uri);
}
// Get the request current path.
$url_decoded_request_array = parse_url($request_uri);
// Get request type. .json is handled as json, anything else as html.
if (preg_match("/\.json$/", $url_decoded_request_array['path'])) {
$phpminer_request_is_ajax = true;
// We only need ajax within ajax requests.
include './includes/AjaxModul.php';
// Remove the .json ending.
$url_decoded_request_array['path'] = preg_replace("/\.json$/", "", $url_decoded_request_array['path']);
// Set type to json / ajax.
$set_request_type = 'json';
} else {
// Set type to normal html.
$set_request_type = 'html';
}
// Get all params as an array.
$params = explode("/", $url_decoded_request_array['path']);
// Remove special chars.
foreach ($params AS &$param) {
$param = preg_replace("/\W/", "", $param);
}
// Get the controller.
array_shift($params);
if (empty($params[0])) {
$params = array('main');
}
// Check if controller exist
if (!file_exists('controllers/' . $params[0] . '.php')) {
$controller = new Controller();
$controller->no_such_controller();
unset($controller);
exit();
}
// Include the controller.
include './controllers/' . $params[0] . '.php';
// Create controller instance.
$controller_name = strtolower($params[0]);
$controller = new $controller_name();
try {
// Set request type.
$controller->set_request_type($set_request_type);
// Get the controller action.
array_shift($params);
if (!isset($params[0])) {
$params[0] = 'init';
}
$method = strtolower($params[0]);
array_shift($params);
// Check if controller action exists.
if (!method_exists($controller, $method)) {
$controller->no_such_method();
} else {
// Provide the view the controller and action name.
$controller->set_controller_name($controller_name);
$controller->set_action_name($method);
// Try to init the controller. This will also create the api class and check for needec config files.
$controller->setup_controller();
// Call the controller action with optional additional params.
call_user_func_array(array($controller, $method), $params);
}
} catch (Exception $e) {
if ($set_request_type === 'html') {
if ($e instanceof AccessException) {
$controller->fatal_error($e->getMessage(), true);
}
$controller->add_message($e->getMessage(), ($e instanceof InfoException) ? Controller::MESSAGE_TYPE_INFO : Controller::MESSAGE_TYPE_ERROR);
} else {
AjaxModul::return_code(AjaxModul::ERROR_DEFAULT, null, true, $e->getMessage());
}
}
// Display the view.
unset($controller);