PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/h-source/Library/Call.php

https://gitlab.com/7slayer/h-node
PHP | 503 lines | 347 code | 83 blank | 73 comment | 56 complexity | 19fb312afdefcf4b0b5152abeb246e56 MD5 | raw file
  1. <?php
  2. // EasyGiant is a PHP framework for creating and managing dynamic content
  3. //
  4. // Copyright (C) 2009 - 2014 Antonio Gallo (info@laboratoriolibero.com)
  5. // See COPYRIGHT.txt and LICENSE.txt.
  6. //
  7. // This file is part of EasyGiant
  8. //
  9. // EasyGiant is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // EasyGiant is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with EasyGiant. If not, see <http://www.gnu.org/licenses/>.
  21. if (!defined('EG')) die('Direct access not allowed!');
  22. /* SANITIZE SUPERGLOBAL ARRAYS */
  23. function sanitizeSuperGlobal()
  24. {
  25. $_GET = stripslashesDeep($_GET);
  26. $_POST = stripslashesDeep($_POST);
  27. $_COOKIE = stripslashesDeep($_COOKIE);
  28. $_SERVER = stripslashesDeep($_SERVER);
  29. }
  30. function checkPostLength($checkArray = null)
  31. {
  32. $a = isset($checkArray) ? $checkArray : $_POST;
  33. if (MAX_POST_LENGTH !== 0)
  34. {
  35. foreach ($a as $key => $value)
  36. {
  37. if (is_array($value))
  38. {
  39. checkPostLength($value);
  40. }
  41. else
  42. {
  43. if (strlen($value) > MAX_POST_LENGTH) die('the length of some of the $_POST values is too large');
  44. }
  45. }
  46. }
  47. }
  48. //remove elements that are arrays
  49. //applied to $_POST and $_GET
  50. function fixArray($array)
  51. {
  52. $temp = array();
  53. foreach ($array as $key => $value)
  54. {
  55. $temp[$key] = is_array($value) ? "" : $value;
  56. }
  57. return $temp;
  58. }
  59. function checkRequestUriLength()
  60. {
  61. if (MAX_REQUEST_URI_LENGTH !== 0)
  62. {
  63. if (strlen($_SERVER['REQUEST_URI']) > MAX_REQUEST_URI_LENGTH) die('the length of the REQUEST_URI is too large');
  64. }
  65. }
  66. function checkRegisterGlobals()
  67. {
  68. if (ini_get('register_globals')) die('register globals is on: easyGiant works only with register globals off');
  69. }
  70. //geth the name of the current application used
  71. function getApplicationName()
  72. {
  73. if (isset(Params::$currentApplication))
  74. {
  75. return Params::$currentApplication;
  76. }
  77. return null;
  78. }
  79. //geth the path of the current application used
  80. //add the trailing slash to the application name
  81. function getApplicationPath()
  82. {
  83. if (isset(Params::$currentApplication))
  84. {
  85. return "Apps".DS.ucfirst(Params::$currentApplication).DS;
  86. }
  87. return null;
  88. }
  89. function languageInUrl($url)
  90. {
  91. $url = trim($url,"/");
  92. if (in_array($url,Params::$frontEndLanguages))
  93. {
  94. return $url."/";
  95. }
  96. return false;
  97. }
  98. function callHook()
  99. {
  100. $currentUrl = null;
  101. if (MOD_REWRITE_MODULE === true)
  102. {
  103. if (isset($_GET['url']))
  104. {
  105. if (!languageInUrl($_GET['url']))
  106. {
  107. $url = $_GET['url'];
  108. }
  109. else
  110. {
  111. $url = languageInUrl($_GET['url']) . DEFAULT_CONTROLLER . '/' . DEFAULT_ACTION;
  112. }
  113. }
  114. else
  115. {
  116. $url = DEFAULT_CONTROLLER . '/' . DEFAULT_ACTION;
  117. }
  118. }
  119. else
  120. {
  121. if (strcmp(getQueryString(),"") !== 0)
  122. {
  123. if (!languageInUrl(getQueryString()))
  124. {
  125. $url = getQueryString();
  126. }
  127. else
  128. {
  129. $url = languageInUrl(getQueryString()) . DEFAULT_CONTROLLER . '/' . DEFAULT_ACTION;
  130. }
  131. }
  132. else
  133. {
  134. $url = DEFAULT_CONTROLLER . '/' . DEFAULT_ACTION;
  135. }
  136. }
  137. $arriveUrl = $url;
  138. $urlArray = array();
  139. $urlArray = explode("/",$url);
  140. //get the language
  141. if (count(Params::$frontEndLanguages) > 0)
  142. {
  143. if (in_array($urlArray[0],Params::$frontEndLanguages))
  144. {
  145. Params::$lang = sanitizeAll($urlArray[0]);
  146. array_shift($urlArray);
  147. }
  148. else
  149. {
  150. Params::$lang = Params::$defaultFrontEndLanguage;
  151. /*
  152. if (isset($_GET['url']) and Params::$redirectToDefaultLanguage)
  153. {
  154. $h = new HeaderObj(DOMAIN_NAME);
  155. $h->redirect($arriveUrl);
  156. }*/
  157. }
  158. }
  159. $url = implode("/",$urlArray);
  160. // rewrite the URL
  161. if (Route::$rewrite === 'yes')
  162. {
  163. $res = rewrite($url);
  164. $url = $res[0];
  165. $currentUrl = $res[1];
  166. }
  167. // echo $url;
  168. $urlArray = explode("/",$url);
  169. $controller = DEFAULT_CONTROLLER;
  170. $action = DEFAULT_ACTION;
  171. //check if an application name is found in the URL
  172. if (isset(Params::$installed) and isset($urlArray[0]) and strcmp($urlArray[0],'') !== 0 and in_array($urlArray[0],Params::$installed))
  173. {
  174. Params::$currentApplication = strtolower(trim($urlArray[0]));
  175. array_shift($urlArray);
  176. }
  177. if (isset($urlArray[0]))
  178. {
  179. $controller = (strcmp($urlArray[0],'') !== 0) ? strtolower(trim($urlArray[0])) : DEFAULT_CONTROLLER;
  180. }
  181. array_shift($urlArray);
  182. if (isset($urlArray[0]))
  183. {
  184. $action = (strcmp($urlArray[0],'') !== 0) ? strtolower(trim($urlArray[0])) : DEFAULT_ACTION;
  185. }
  186. //set ERROR_CONTROLLER and ERROR_ACTION
  187. $errorController = ERROR_CONTROLLER !== false ? ERROR_CONTROLLER : DEFAULT_CONTROLLER;
  188. $errorAction = ERROR_ACTION !== false ? ERROR_ACTION : DEFAULT_ACTION;
  189. /*
  190. CHECK COUPLES CONTROLLER,ACTION
  191. */
  192. if (!in_array('all',Route::$allowed))
  193. {
  194. $couple = "$controller,$action";
  195. if (getApplicationName() !== null)
  196. {
  197. $couple = getApplicationName().",".$couple;
  198. }
  199. if (!in_array($couple,Route::$allowed))
  200. {
  201. Params::$currentApplication = null;
  202. $controller = $errorController;
  203. $action = $errorAction;
  204. $urlArray = array();
  205. }
  206. }
  207. /*
  208. VERIFY THE ACTION NAME
  209. */
  210. if (method_exists('Controller', $action) or !ctype_alnum($action) or (strcmp($action,'') === 0))
  211. {
  212. Params::$currentApplication = null;
  213. $controller = $errorController;
  214. $action = $errorAction;
  215. $urlArray = array();
  216. }
  217. /*
  218. VERIFY THE CONTROLLER NAME
  219. */
  220. if (!ctype_alnum($controller) or (strcmp($controller,'') === 0))
  221. {
  222. Params::$currentApplication = null;
  223. $controller = $errorController;
  224. $action = $errorAction;
  225. $urlArray = array();
  226. }
  227. //check that the controller class belongs to the application/controllers folder
  228. //otherwise set the controller to the default controller
  229. // if (!file_exists(ROOT.DS.APPLICATION_PATH.DS.'Controllers'.DS.ucwords($controller).'Controller.php') and !file_exists(ROOT.DS.APPLICATION_PATH.DS.getApplicationPath().'Controllers'.DS.ucwords($controller).'Controller.php'))
  230. if (!file_exists(ROOT.DS.APPLICATION_PATH.DS.getApplicationPath().'Controllers'.DS.ucwords($controller).'Controller.php'))
  231. {
  232. Params::$currentApplication = null;
  233. $controller = $errorController;
  234. $action = $errorAction;
  235. $urlArray = array();
  236. }
  237. //set the controller class to DEFAULT_CONTROLLER if it doesn't exists
  238. if (!class_exists(ucwords($controller).'Controller'))
  239. {
  240. Params::$currentApplication = null;
  241. $controller = $errorController;
  242. $action = $errorAction;
  243. $urlArray = array();
  244. }
  245. //set the action to DEFAULT_ACTION if it doesn't exists
  246. if (!method_exists(ucwords($controller).'Controller', $action))
  247. {
  248. Params::$currentApplication = null;
  249. $controller = $errorController;
  250. $action = $errorAction;
  251. $urlArray = array();
  252. }
  253. array_shift($urlArray);
  254. $queryString = $urlArray;
  255. //set the name of the application
  256. $controllerName = $controller;
  257. $controller = ucwords($controller);
  258. $model = $controller;
  259. $controller .= 'Controller';
  260. $model .= 'Model';
  261. // echo $controller."-".$action;
  262. //include the file containing the set of actions to carry out before the initialization of the controller class
  263. Hooks::load(ROOT . DS . APPLICATION_PATH . DS . 'Hooks' . DS . 'BeforeInitialization.php');
  264. if (class_exists($controller))
  265. {
  266. $dispatch = new $controller($model,$controllerName,$queryString, getApplicationName());
  267. //pass the action to the controller object
  268. $dispatch->action = $action;
  269. $dispatch->currPage = $dispatch->baseUrl.'/'.$dispatch->controller.'/'.$dispatch->action;
  270. if (isset($currentUrl))
  271. {
  272. $dispatch->currPage = $dispatch->baseUrl.'/'.$currentUrl;
  273. }
  274. //require the file containing the set of actions to carry out after the initialization of the controller class
  275. Hooks::load(ROOT . DS . APPLICATION_PATH . DS . 'Hooks' . DS . 'AfterInitialization.php');
  276. $templateFlag= true;
  277. if (method_exists($dispatch, $action))
  278. {
  279. //pass the action to the theme object
  280. $dispatch->theme->action = $action;
  281. $dispatch->theme->currPage = $dispatch->baseUrl.'/'.$dispatch->controller.'/'.$dispatch->action;
  282. if (isset($currentUrl))
  283. {
  284. $dispatch->theme->currPage = $dispatch->baseUrl.'/'.$currentUrl;
  285. }
  286. call_user_func_array(array($dispatch,$action),$queryString);
  287. }
  288. else
  289. {
  290. $templateFlag= false;
  291. }
  292. if ($templateFlag)
  293. {
  294. $dispatch->theme->render();
  295. }
  296. }
  297. else
  298. {
  299. echo "<h2>the '$controller' controller is not present!</h2>";
  300. }
  301. }
  302. //rewrite the URL
  303. function rewrite($url)
  304. {
  305. foreach (Route::$map as $key => $address)
  306. {
  307. $oldKey = $key;
  308. $key = str_replace('\/','/',$key);
  309. $key = str_replace('/','\/',$key);
  310. $regExpr = Params::$exactUrlMatchRewrite ? '/^'.$key.'$/' : '/^'.$key.'/';
  311. if (preg_match($regExpr,$url))
  312. {
  313. $nurl = preg_replace('/^'.$key.'/',$address,$url);
  314. return array($nurl,$oldKey);
  315. // return preg_replace('/^'.$key.'/',$address,$url);
  316. }
  317. }
  318. // return $url;
  319. return array($url,null);
  320. }
  321. function getQueryString()
  322. {
  323. if (strstr($_SERVER['REQUEST_URI'],'index.php/'))
  324. {
  325. return Params::$mbStringLoaded === true ? mb_substr(mb_strstr($_SERVER['REQUEST_URI'],'index.php/'),10) : substr(strstr($_SERVER['REQUEST_URI'],'index.php/'),10);
  326. }
  327. return '';
  328. }
  329. function __autoload($className)
  330. {
  331. $backupName = $className;
  332. if (strstr($className,'_'))
  333. {
  334. $parts = explode('_',$className);
  335. $className = implode(DS,$parts);
  336. }
  337. if (file_exists(ROOT . DS . 'Library' . DS . $className . '.php'))
  338. {
  339. require_once(ROOT . DS . 'Library' . DS . $className . '.php');
  340. }
  341. else if (getApplicationName() and file_exists(ROOT . DS . APPLICATION_PATH . DS . getApplicationPath() . 'Controllers' . DS . $backupName . '.php'))
  342. {
  343. require_once(ROOT . DS . APPLICATION_PATH . DS . getApplicationPath() . 'Controllers' . DS . $backupName . '.php');
  344. }
  345. else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Controllers' . DS . $backupName . '.php'))
  346. {
  347. require_once(ROOT . DS . APPLICATION_PATH . DS . 'Controllers' . DS . $backupName . '.php');
  348. }
  349. else if (getApplicationName() and file_exists(ROOT . DS . APPLICATION_PATH . DS . getApplicationPath() . 'Models' . DS . $backupName . '.php'))
  350. {
  351. require_once(ROOT . DS . APPLICATION_PATH . DS . getApplicationPath() . 'Models' . DS . $backupName . '.php');
  352. }
  353. else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Models' . DS . $backupName . '.php'))
  354. {
  355. require_once(ROOT . DS . APPLICATION_PATH . DS . 'Models' . DS . $backupName . '.php');
  356. }
  357. else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Modules' . DS . $backupName . '.php'))
  358. {
  359. require_once(ROOT . DS . APPLICATION_PATH . DS . 'Modules' . DS . $backupName . '.php');
  360. }
  361. else if (getApplicationName() and file_exists(ROOT . DS . APPLICATION_PATH . DS . getApplicationPath() . 'Strings' . DS . $backupName . '.php'))
  362. {
  363. require_once(ROOT . DS . APPLICATION_PATH . DS . getApplicationPath() . 'Strings' . DS . $backupName . '.php');
  364. }
  365. else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Strings' . DS . $className . '.php'))
  366. {
  367. require_once(ROOT . DS . APPLICATION_PATH . DS . 'Strings' . DS . $className . '.php');
  368. }
  369. }
  370. try {
  371. $_POST = fixArray($_POST);
  372. $_GET = fixArray($_GET);
  373. //check the length of the $_POST values
  374. checkPostLength();
  375. //check the length of the REQUEST_URI
  376. checkRequestUriLength();
  377. //connect to the database
  378. Factory_Db::getInstance(DATABASE_TYPE,array(HOST,USER,PWD,DB));
  379. //set htmlentities charset
  380. switch (DEFAULT_CHARSET)
  381. {
  382. case 'SJIS':
  383. Params::$htmlentititiesCharset = 'Shift_JIS';
  384. break;
  385. }
  386. $allowedCharsets = array('UTF-8','ISO-8859-1','EUC-JP','SJIS');
  387. if (!in_array(DEFAULT_CHARSET,$allowedCharsets)) die('charset not-allowed');
  388. //check if the mbstring extension is loaded
  389. if (extension_loaded('mbstring'))
  390. {
  391. //set the internal encoding
  392. mb_internal_encoding(DEFAULT_CHARSET);
  393. Params::$mbStringLoaded = true;
  394. }
  395. //load the files defined inside Config/Autoload.php
  396. foreach (Autoload::$files as $file)
  397. {
  398. $ext = strtolower(end(explode('.', $file)));
  399. $path = ROOT . DS . APPLICATION_PATH . DS . 'Include' . DS . $file;
  400. if (file_exists($path) and $ext === 'php')
  401. {
  402. require_once($path);
  403. }
  404. }
  405. //include the file containing the set of actions to carry out before the check of the super global array
  406. Hooks::load(ROOT . DS . APPLICATION_PATH . DS . 'Hooks' . DS . 'BeforeChecks.php');
  407. //sanitize super global arrays
  408. sanitizeSuperGlobal();
  409. //report errors
  410. ErrorReporting();
  411. //verify that register globals is not active
  412. checkRegisterGlobals();
  413. //call the main hook
  414. callHook();
  415. //disconnect to the database
  416. Factory_Db::disconnect(DATABASE_TYPE);
  417. } catch (Exception $e) {
  418. echo '<div class="alert">Message: '.$e->getMessage().'</div>';
  419. }