-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboot.php
221 lines (183 loc) · 5.86 KB
/
boot.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* @file
* Boot.
*/
define('OPENLIST_VERSION', '2.0.1');
require_once OPENLIST_ROOT . '/utils.php';
// Fetch library code
$GLOBALS['library_code'] = isset($_COOKIE['library_code']) ?
$_COOKIE['library_code'] : (isset($_GET['library_code']) ?
$_GET['library_code'] : FALSE);
if ($GLOBALS['library_code'] == FALSE) {
// Status output for index file with no library code sent.
$out = array('OpenList Version ' . OPENLIST_VERSION);
$out[] = 'Name:' . OPENLIST_INSTANCE_NAME;
require_once OPENLIST_CLASSES_PATH . '/DB.php';
$result = DB::q('SELECT VERSION();');
if ($result) {
$out[] = 'Status:OK';
}
$out[] = 'Servertime:' . gmdate('D, d M Y H:i:s \G\M\T', time());
$out[] = 'Authenticated accounts:' . count($authkeys);
send_cache_headers(1);
exit(implode("<br>\n", $out));
}
/**
* Authentication
*/
// Fetch authkey
$authkey = isset($_COOKIE['authkey']) ?
$_COOKIE['authkey'] : (isset($_GET['authkey']) ?
$_GET['authkey'] : FALSE);
if (OPENLIST_REQUIRE_AUTHKEY) {
if (!isset($authkeys[$authkey])) {
header("HTTP/1.1 401 Unauthorized");
exit('Invalid authkey');
}
// @TODO Implement IP check
//
//if (!check_ip($authkeys[$authkey])) {
// header("HTTP/1.1 401 Unauthorized");
// exit('IP not whitelisted');
//}
$GLOBALS['library_access'] = $authkeys[$authkey];
}
else {
$GLOBALS['library_access'] = array($GLOBALS['library_code']);
}
// Full boot
require_once OPENLIST_CLASSES_PATH . '/Dev.php';
require_once OPENLIST_CLASSES_PATH . '/DB.php';
require_once OPENLIST_CLASSES_PATH . '/EventHandler.php';
require_once OPENLIST_CLASSES_PATH . '/Admin.php';
require_once OPENLIST_CLASSES_PATH . '/OpenList.php';
require_once OPENLIST_CLASSES_PATH . '/OpenListDecorator.php';
require_once MODULES_LIST_FILE;
define('OPENLIST_RESPONSE_UNKNOWN_ERROR', 'Unknown error');
define('OPENLIST_RESPONSE_SUCCESS', 'Success');
define('OPENLIST_RESPONSE_UNKNOWN_FUNCTION', 'Unknown function');
if (!isset($_SERVER['PATH_INFO'])) {
$_SERVER['PATH_INFO'] = '';
}
$input = explode('/', $_SERVER['PATH_INFO']);
if (in_array($input[1], array('json', 'php', 'xml'))) {
EventHandler::trigger('boot');
$function = $input[2];
$args = array_diff_key($_GET, array_flip(array('admin', 'library_code')));
$args = array_merge($args, $_POST);
$response = OPENLIST_RESPONSE_UNKNOWN_ERROR;
$result = array('error' => OPENLIST_RESPONSE_UNKNOWN_ERROR);
if (method_exists('OpenList', $function)) {
$reflection_method = new \ReflectionMethod('OpenList', $function);
$arglist = array();
foreach ($reflection_method->getParameters() as $param) {
if (isset($args[$param->name])) {
$arglist[] = $args[$param->name];
}
}
$old = new OpenListDecorator();
$response = OPENLIST_RESPONSE_SUCCESS;
$result = call_user_func_array(array($old, $function), $arglist);
}
else {
$response = OPENLIST_RESPONSE_UNKNOWN_FUNCTION;
$result = array('error' => OPENLIST_RESPONSE_UNKNOWN_FUNCTION);
if (Module::getModule($input[2]) && method_exists($input[2], $input[3])) {
$reflection_method = new \ReflectionMethod($input[2], $input[3]);
$arglist = array();
foreach ($reflection_method->getParameters() as $param) {
if (isset($args[$param->name])) {
$arglist[] = $args[$param->name];
}
}
$old = new OpenListDecorator();
$response = OPENLIST_RESPONSE_SUCCESS;
$result = call_user_func_array(array(
$old,
'callModule',
), array(
$input[2],
$input[3],
$arglist,
));
}
}
switch ($response) {
case OPENLIST_RESPONSE_UNKNOWN_ERROR:
case OPENLIST_RESPONSE_UNKNOWN_FUNCTION:
http_response_code(400);
break;
}
switch ($input[1]) {
case 'json':
header('Content-Type: application/json;charset=utf-8');
echo json_encode($result);
break;
case 'php':
echo serialize($result);
break;
case 'xml':
header('Content-Type: text/xml;charset=utf-8');
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><root></root>");
array_to_xml($result, $xml);
echo $xml->asXML();
break;
}
EventHandler::trigger('end');
}
elseif (in_array($input[1], array('page'))) {
$calls = EventHandler::trigger('links');
$url = substr($_SERVER['SCRIPT_URL'], 6);
foreach ($calls as $links) {
if (isset($links[$url])) {
require_once $links[$url];
}
}
}
else {
if (count($_GET) === 0) {
try {
EventHandler::trigger('boot');
if (!IS_LOCAL) {
require_once 'Zend/Soap/Server.php';
ini_set("soap.wsdl_cache_enabled", WSDL_CACHE_NONE);
ini_set('soap.wsdl_cache_ttl', 0);
if (filemtime(OPENLIST_CLASSES_PATH . '/OpenList.php') < filemtime(WSDL_LOCAL_PATH)) {
$server = new Zend_Soap_Server(WSDL_LOCAL_PATH);
}
else {
$server = new Zend_Soap_Server($_SERVER['SCRIPT_URI'] . '?wsdl&local');
}
$server->setClass('OpenListDecorator');
$server->handle();
}
else {
$old = new OpenListDecorator();
var_dump(call_user_func_array(array($old, $GLOBALS['local'][0]), $GLOBALS['local'][1]));
}
EventHandler::trigger('end');
}
catch(Exception $e) {
}
}
else {
if (isset($_GET['cron'])) {
var_dump(EventHandler::trigger('cron', array(explode(',', $_GET['cron']))));
}
if (isset($_GET['admin']) && $_GET['admin'] == OPENLIST_ADMIN_GET_PASSWORD) {
$call_list = array();
if (isset($_GET['install'])) {
$call_list['install'] = explode(',', $_GET['install']);
}
if (isset($_GET['uninstall'])) {
$call_list['uninstall'] = explode(',', $_GET['uninstall']);
}
foreach ($call_list as $method => $modules) {
foreach ($modules as $module) {
echo $module . '.' . $method . ': ' . Module::admin($module, $method);
}
}
}
}
}