Overview

Classes

  • Ws_logged_inModel
  • Ws_permissionsModel
  • Ws_role_permModel
  • Ws_rolesModel
  • Ws_user_roleModel
  • Ws_userModel
  • WsAuth
  • WsauthController
  • WsChart
  • WsConfig
  • WsController
  • WsDatabase
  • WsForm
  • WsImage
  • WsLocalize
  • WsModel
  • WsModelForm
  • WsModelGridView
  • WsUrl

Functions

  • __autoload
  • callHook
  • WsErrorHandler
  • Overview
  • Class
  1: <?php
  2: /* start or resume session */
  3: session_start();
  4: 
  5: /* define some framework constants */
  6: /**
  7:  * WS_AUTH_USER_EXISTS => user for WsAuth module allready exists
  8:  */
  9: define('WS_AUTH_USER_EXISTS', 101);
 10: /**
 11:  * WS_AUTH_NOT_VERIFIED => user account not verified
 12:  */
 13: define('WS_AUTH_NOT_VERIFIED', 102);
 14: /**
 15:  * WS_AUTH_NO_MATCH => user name and paswword did not match
 16:  */
 17: define('WS_AUTH_NO_MATCH', 103);
 18: /**
 19:  * WS_AUTH_NOT_ACTIVE => user account not active
 20:  */
 21: define('WS_AUTH_NOT_ACTIVE', 104);
 22: /**
 23:  * WS_AUTH_LOGIN_OK => user succesfuly loged in
 24:  */
 25: define('WS_AUTH_LOGIN_OK', 105);
 26: 
 27: 
 28: /**
 29:  * autoload all neded classes from framework and web application
 30:  */
 31: function __autoload($className)
 32: {
 33:     // load all framework classes
 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:     // autoload all internal Controllers and Models
 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:     // load all application Controlers
 53:     if (file_exists(
 54:         WsROOT.'/application/controllers/'.$className.'.php')) {
 55:         require_once WsROOT.'/application/controllers/'.$className.'.php';
 56:         return;
 57:     }
 58: 
 59:     // load all application Models
 60:     if (file_exists(WsROOT.'/application/models/'.$className.'.php')) {
 61:         require_once WsROOT.'/application/models/'.$className.'.php';
 62:         return;
 63:     }
 64: }
 65: 
 66: // load config
 67: require_once WsROOT.'/protected/config/config.php';
 68: 
 69: // set default timezone
 70: date_default_timezone_set(WsConfig::get('app_tz'));
 71: 
 72: // track memory usage and script execution time if 'development'
 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: // disable standard error reporting in production
 81: if (WsConfig::get('app_stage') == 'development') {
 82:     error_reporting(-1);
 83: } else {
 84:     error_reporting(0);
 85: }
 86: 
 87: // user defined error handling function
 88: function WsErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
 89: {
 90:     /* timestamp for the error entry */
 91:     $dt = date('Y-m-d H:i:s (T)');
 92: 
 93:     // write error log
 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:     /*if (in_array($errno, $user_errors)) {
100:      *   $err .= "\tvariables: ".$vars."\n";
101:      *}
102:      */
103:     $err .= "*******************\n";
104:     // save to the error log
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:     // display error message
112:     // layout file
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:             // e-mail the administrator if there is a critical user error
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:     // construc error message depending of WsAPP_STAGE
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:     // display error message
156:     if (is_file($layoutFile)) {
157:         include($layoutFile);
158:     } else {
159:         echo $WsContent;
160:     }
161: 
162:     // if we have critical error then stop execution of script
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:  * main function that calls controller and action and also forward parameters to
184:  * call.
185:  */
186: function callHook()
187: {
188:     gc_enable();
189: 
190:     // search for controler name in request
191:     if (!isset($_REQUEST['request'])) { // no parameters
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:             // we have one parameter, it's controller
200:             $controller = $request[0];
201:             $action = 'index';
202:         } else if (count($request) >= 2) {
203:             /* first parameter is controller, second is action and all others
204:              * are parameters for action
205:              */
206: 
207:             $controller = $request[0];
208:             $action = $request[1];
209:             // remove controler from array
210:             unset($request[0]);
211:             // remove action from array
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:     // check if controller class exists
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:         // check if action method exist
238:         if (method_exists($dispatch, $action)) {
239:             // call action
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: // check if runtime directory is writable
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: // remove image files older then 1 hour from runtime directory
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) {// 1 hour
270:             unlink($file);
271:         }
272:     }
273: }
274: unset($now, $files);
275: 
276: // create database tables if they are not exists
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: // call controller/action
294: callHook();
295: 
API documentation generated by ApiGen