1: <?php
2:
3: session_start();
4:
5:
6: 7: 8:
9: define('WS_AUTH_USER_EXISTS', 101);
10: 11: 12:
13: define('WS_AUTH_NOT_VERIFIED', 102);
14: 15: 16:
17: define('WS_AUTH_NO_MATCH', 103);
18: 19: 20:
21: define('WS_AUTH_NOT_ACTIVE', 104);
22: 23: 24:
25: define('WS_AUTH_LOGIN_OK', 105);
26:
27:
28: 29: 30:
31: function __autoload($className)
32: {
33:
34: if (file_exists(
35: WsROOT.'/protected/library/'.$className.'.class.php')) {
36: require_once WsROOT.'/protected/library/'.$className.'.class.php';
37: return;
38: }
39:
40:
41: if (file_exists(
42: WsROOT.'/protected/library/controllers/'.$className.'.php')) {
43: require_once WsROOT.'/protected/library/controllers/'.$className.'.php';
44: return;
45: }
46: if (file_exists(
47: WsROOT.'/protected/library/models/'.$className.'.php')) {
48: require_once WsROOT.'/protected/library/models/'.$className.'.php';
49: return;
50: }
51:
52:
53: if (file_exists(
54: WsROOT.'/application/controllers/'.$className.'.php')) {
55: require_once WsROOT.'/application/controllers/'.$className.'.php';
56: return;
57: }
58:
59:
60: if (file_exists(WsROOT.'/application/models/'.$className.'.php')) {
61: require_once WsROOT.'/application/models/'.$className.'.php';
62: return;
63: }
64: }
65:
66:
67: require_once WsROOT.'/protected/config/config.php';
68:
69:
70: date_default_timezone_set(WsConfig::get('app_tz'));
71:
72:
73: if (WsConfig::get('app_stage') == 'development') {
74: define('WsSTART_MEMORY_USAGE',
75: number_format(memory_get_usage() / 1024, 2)
76: );
77: define('WsSTART_TIME', microtime(true));
78: }
79:
80:
81: if (WsConfig::get('app_stage') == 'development') {
82: error_reporting(-1);
83: } else {
84: error_reporting(0);
85: }
86:
87:
88: function WsErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
89: {
90:
91: $dt = date('Y-m-d H:i:s (T)');
92:
93:
94: $err = "****** ".$errno." ******\n";
95: $err .= "\tdatetime: ".$dt."\n";
96: $err .= "\terrormsg: ".$errmsg."\n";
97: $err .= "\tscriptname: ".$filename."\n";
98: $err .= "\tscriptlinenum: ".$linenum."\n";
99: 100: 101: 102:
103: $err .= "*******************\n";
104:
105: try {
106: error_log($err, 3, WsROOT.'/runtime/error.log');
107: } catch (Exception $e) {
108: echo 'Caught exception: ', $e->getMessage(), "\n";
109: }
110:
111:
112:
113: $layoutFile = WsROOT.'/public/layouts/';
114: $layoutFile .= WsConfig::get('html_layout');
115:
116: $WsContent = '<div class="row"><div class="column column-12">';
117:
118: switch($errno) {
119: case E_NOTICE:
120: case E_USER_NOTICE:
121: $WsContent .= '<div class="callout">';
122: break;
123: case E_WARNING:
124: case E_USER_WARNING:
125: case E_CORE_WARNING:
126: case E_COMPILE_WARNING:
127: case E_DEPRECATED:
128: case E_USER_DEPRECATED:
129: $WsContent .= '<div class="callout warning">';
130: break;
131: case E_ERROR:
132: case E_PARSE:
133: case E_CORE_ERROR:
134: case E_COMPILE_ERROR:
135: case E_USER_ERROR:
136: case E_RECOVERABLE_ERROR:
137:
138: mail(WsConfig::get('auth_admin'),
139: WsConfig::get('app_name').' - Critical User Error',
140: $err
141: );
142: $WsContent .= '<div class="callout error">';
143: }
144:
145:
146: if (WsConfig::get('app_stage') == 'development') {
147: $WsContent .= $errmsg.'<br/>';
148: $WsContent .= '<pre>'.$filename.'</pre><pre>line: '.$linenum.'</pre>';
149:
150: } else {
151: $WsContent .= $errmsg;
152: }
153: $WsContent .= '</div></div></div>';
154:
155:
156: if (is_file($layoutFile)) {
157: include($layoutFile);
158: } else {
159: echo $WsContent;
160: }
161:
162:
163: switch($errno) {
164: case E_ERROR:
165: case E_PARSE:
166: case E_CORE_ERROR:
167: case E_COMPILE_ERROR:
168: case E_USER_ERROR:
169: case E_RECOVERABLE_ERROR:
170: if (gc_enabled()) {
171: gc_collect_cycles();
172: gc_disable();
173: }
174: die();
175: }
176:
177: return true;
178: }
179: set_error_handler('WsErrorHandler');
180:
181:
182: 183: 184: 185:
186: function callHook()
187: {
188: gc_enable();
189:
190:
191: if (!isset($_REQUEST['request'])) {
192: $controller = 'site';
193: $action = 'index';
194: $params = array();
195: } else {
196: $request = explode('/', $_REQUEST['request']);
197: $params = array();
198: if (count($request) == 1) {
199:
200: $controller = $request[0];
201: $action = 'index';
202: } else if (count($request) >= 2) {
203: 204: 205:
206:
207: $controller = $request[0];
208: $action = $request[1];
209:
210: unset($request[0]);
211:
212: unset($request[1]);
213:
214: if (WsConfig::get('pretty_urls') == 'yes') {
215: foreach ($request as $r) {
216: array_push($params, urldecode($r));
217: }
218: } else {
219: $params = array_map('urldecode', $request);
220: }
221: }
222: }
223:
224: $controller = ucwords($controller);
225: $controller .= 'Controller';
226:
227:
228: if (class_exists($controller)) {
229: $dispatch = new $controller();
230: } else {
231: header('HTTP/1.1 404 Not Found');
232: trigger_error('Invalid call to non-existent controller: <strong>'
233: .$controller.'</strong>', E_USER_ERROR);
234: }
235:
236: try {
237:
238: if (method_exists($dispatch, $action)) {
239:
240: call_user_func_array(array($dispatch, $action), $params);
241: } else {
242: header('HTTP/1.1 404 Not Found');
243: trigger_error('Invalid call to non-existent action: <strong>'
244: .$controller.'::'.$action.'</strong>', E_USER_ERROR);
245: }
246: } catch (Exception $e) {
247: ob_end_clean();
248: trigger_error($e->getMessage(), E_USER_ERROR);
249: }
250:
251: gc_collect_cycles();
252: gc_disable();
253: }
254:
255:
256:
257: if (!is_writable(WsROOT.'/runtime')) {
258: header('HTTP/1.1 500 Internal Server Error');
259: trigger_error('Directory <strong>/runtime</strong> must be writable!',
260: E_USER_ERROR);
261: }
262:
263:
264: $files = glob(WsROOT."/runtime/wsimg_*.png");
265: $now = time();
266:
267: foreach ($files as $file) {
268: if (is_file($file)) {
269: if ($now - filemtime($file) >= 3600) {
270: unlink($file);
271: }
272: }
273: }
274: unset($now, $files);
275:
276:
277: if (WsConfig::get('db_driver') == 'pgsql') {
278: $db_file = WsROOT.'/schema_pgsql.sql';
279: } else {
280: $db_file = WsROOT.'/schema_mysql.sql';
281: }
282: if (file_exists($db_file)) {
283: $auth = new WsAuth();
284: $sql = file_get_contents($db_file);
285: $db = new WsDatabase();
286: $db->execute_batch($sql);
287: $db->close();
288: unset ($db, $auth, $sql, $db_file);
289: } else {
290: unset ($db_file);
291: }
292:
293:
294: callHook();
295: