PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/helpers/router.php

https://github.com/MilkZoft/zan
PHP | 546 lines | 535 code | 11 blank | 0 comment | 21 complexity | 48502503e270fdb403006eab1153e6ec MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. if (!function_exists("execute")) {
  6. function execute()
  7. {
  8. global $Load, $ZP;
  9. $applicationController = false;
  10. $match = false;
  11. $special = false;
  12. $params = array();
  13. if (file_exists("www/config/routes.php")) {
  14. include "www/config/routes.php";
  15. if (is_array($routes)) {
  16. $application = segment(0, isLang());
  17. foreach ($routes as $route) {
  18. $pattern = $route["pattern"];
  19. $match = preg_match($pattern, $application);
  20. if ($match) {
  21. $application = $route["application"];
  22. $applicationController = $route["controller"];
  23. $method = $route["method"];
  24. $params = $route["params"];
  25. break;
  26. }
  27. }
  28. }
  29. }
  30. if (!$match) {
  31. if (!segment(0)) {
  32. $application = _get("defaultApplication");
  33. } elseif (segment(0) and !segment(1)) {
  34. $application = isLang() ? _get("defaultApplication") : segment(0);
  35. } else {
  36. $application = segment(0, isLang());
  37. $applicationController = segment(1, isLang());
  38. if (isController($applicationController, $application)) {
  39. $Controller = getController($applicationController, $application);
  40. $controllerFile = getController($applicationController, $application, true);
  41. $method = segment(2, isLang());
  42. if (!isMethod($method, $Controller)) {
  43. if (isMethod("index", $Controller)) {
  44. $method = "index";
  45. $special = true;
  46. } else {
  47. getException("Method \"$method\" doesn't exists");
  48. }
  49. }
  50. } else {
  51. $applicationController = false;
  52. $Controller = getController(null, $application);
  53. $controllerFile = getController(null, $application, true);
  54. $method = segment(1, isLang());
  55. if (!isMethod($method, $Controller)) {
  56. if (isMethod("index", $Controller)) {
  57. $method = "index";
  58. $special = true;
  59. } else {
  60. getException("Method \"$method\" doesn't exists");
  61. }
  62. }
  63. }
  64. if ($applicationController) {
  65. if (segments() >= 3) {
  66. $j = isLang() ? 4 : 3;
  67. $j = ($special) ? $j - 1 : $j;
  68. for ($i = 0; $i < segments(); $i++) {
  69. if (segment($j) or segment($j) === 0) {
  70. $params[$i] = segment($j);
  71. $j++;
  72. }
  73. }
  74. }
  75. } else {
  76. $count = ($special) ? 1 : 2;
  77. if (segments() > $count) {
  78. $j = isLang() ? 3 : 2;
  79. $j = ($special) ? $j - 1 : $j;
  80. for ($i = 0; $i < segments(); $i++) {
  81. if (segment($j) or segment($j) === 0) {
  82. $params[$i] = segment($j);
  83. $j++;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. if (_get("webSituation") !== "Active" and !SESSION("ZanUserID") and $application !== "cpanel") {
  91. die(_get("webMessage"));
  92. }
  93. $Load->app($application);
  94. $controllerFile = ($applicationController) ? getController($applicationController, $application, true) : getController(null, $application, true);
  95. if (!$controllerFile) {
  96. getException("The application \"$application\" doesn't exists");
  97. }
  98. $Controller = isset($Controller) ? $Controller : getController(null, $application);
  99. if (isset($method) and count($params) > 0) {
  100. if (isMethod($method, $Controller)) {
  101. try {
  102. $Reflection = new ReflectionMethod($Controller, $method);
  103. if (!$Reflection->isPublic()) {
  104. throw new RuntimeException("The called method is not public.", 100);
  105. }
  106. call_user_func_array(array($Controller, $method), $params);
  107. } catch(RuntimeException $e) {
  108. getException($e);
  109. }
  110. } else {
  111. if (isController($controllerFile, true)) {
  112. if (isset($method) and count($params) > 0) {
  113. if (isMethod($method, $Controller)) {
  114. try {
  115. $Reflection = new ReflectionMethod($Controller, $method);
  116. if (!$Reflection->isPublic()) {
  117. throw new RuntimeException("The called method is not public.", 100);
  118. }
  119. call_user_func_array(array($Controller, $method), $params);
  120. } catch(RuntimeException $e) {
  121. getException($e);
  122. }
  123. }
  124. }
  125. } else {
  126. if (method_exists($Controller, "index")) {
  127. try {
  128. $reflection = new ReflectionMethod($Controller, "index");
  129. if (!$reflection->isPublic()) {
  130. throw new RuntimeException("The called method is not public.", 100);
  131. } elseif ($Reflection->getNumberOfRequiredParameters() > 0 and count($params) === 0) {
  132. throw new RuntimeException("The called method need required parameters (". getParameters($Reflection->getParameters()) .").", 200);
  133. }
  134. call_user_func_array(array($Controller, "index"), $params);
  135. } catch(RuntimeException $e) {
  136. getException($e);
  137. }
  138. } else {
  139. getException("Method index doesn't exists");
  140. }
  141. }
  142. }
  143. } elseif (isset($method)) {
  144. if (isMethod($method, $Controller)) {
  145. try {
  146. $Reflection = new ReflectionMethod($Controller, $method);
  147. if (!$Reflection->isPublic()) {
  148. throw new RuntimeException("The called method is not public.", 100);
  149. } elseif ($Reflection->getNumberOfRequiredParameters() > 0 and count($params) === 0) {
  150. throw new RuntimeException("The called method need required parameters (". getParameters($Reflection->getParameters()) .").", 200);
  151. }
  152. $Controller->$method();
  153. } catch(RuntimeException $e) {
  154. getException($e);
  155. }
  156. } else {
  157. if (isMethod("index", $Controller)) {
  158. call_user_func_array(array($Controller, "index"), $params);
  159. } else {
  160. getException("Method \"index\" doesn't exists");
  161. }
  162. }
  163. } else {
  164. if (isMethod("index", $Controller)) {
  165. try {
  166. $Reflection = new ReflectionMethod($Controller, "index");
  167. if (!$Reflection->isPublic()) {
  168. throw new RuntimeException("The called method is not public.", 100);
  169. } elseif ($Reflection->getNumberOfRequiredParameters() > 0 and count($params) === 0) {
  170. throw new RuntimeException("The called method need required parameters (". getParameters($Reflection->getParameters()) .").", 200);
  171. }
  172. call_user_func_array(array($Controller, "index"), $params);
  173. } catch(RuntimeException $e) {
  174. getException($e);
  175. }
  176. } else {
  177. getException("Method \"index\" doesn't exists");
  178. }
  179. }
  180. }
  181. }
  182. if (!function_exists("getParameters")) {
  183. function getParameters($params)
  184. {
  185. $parameters = null;
  186. if (count($params) > 0) {
  187. $i = 0;
  188. foreach ($params as $param) {
  189. $parameters .= ($i === count($params) - 1) ? '$'. $param->name : '$'. $param->name .", ";
  190. $i++;
  191. }
  192. }
  193. return $parameters;
  194. }
  195. }
  196. if (!function_exists("currentPath")) {
  197. function currentPath($path = null)
  198. {
  199. if ($path) {
  200. if (getURL() === path($path)) {
  201. return ' class="current"';
  202. }
  203. } else {
  204. if (getURL() === path()) {
  205. return ' class="current"';
  206. }
  207. }
  208. }
  209. }
  210. if (!function_exists("getURL")) {
  211. function getURL()
  212. {
  213. $URL = null;
  214. for ($i = 0; $i <= segments() - 1; $i++) {
  215. $URL .= ($i === (segments() - 1)) ? segment($i) : segment($i) ."/";
  216. }
  217. $a = path($URL, false, false);
  218. return $a;
  219. }
  220. }
  221. if (!function_exists("setURL")) {
  222. function setURL($URL = false)
  223. {
  224. return ($URL) ? SESSION("lastURL", $URL) : SESSION("lastURL", getURL());
  225. }
  226. }
  227. if (!function_exists("getController")) {
  228. function getController($applicationController = null, $application, $file = false)
  229. {
  230. global $Load;
  231. if (isController($applicationController, $application)) {
  232. $controller = ucfirst($applicationController) ."_Controller";
  233. $controllerFile = "www/applications/". strtolower($application) ."/controllers/". strtolower($applicationController). ".php";
  234. $$controller = (!$file) ? $Load->controller($controller, $application) : false;
  235. } else {
  236. $controller = ucfirst($application) ."_Controller";
  237. $controllerFile = "www/applications/". strtolower($application) ."/controllers/". strtolower($application) .".php";
  238. $$controller = (!$file) ? $Load->controller($controller) : false;
  239. }
  240. if ($file) {
  241. return file_exists($controllerFile) ? $controllerFile : false;
  242. }
  243. return $$controller;
  244. }
  245. }
  246. if (!function_exists("whichApplication")) {
  247. function whichApplication()
  248. {
  249. if (file_exists("www/applications/". segment(0) ."/controllers/". segment(0) .".php")) {
  250. return segment(0);
  251. } elseif (file_exists("www/applications/". segment(1) ."/controllers/". segment(1) .".php")) {
  252. return segment(1);
  253. } elseif (file_exists("www/applications/". segment(0) ."/models/". segment(0) .".php")) {
  254. return segment(0);
  255. } elseif (file_exists("www/applications/". segment(1) ."/models/". segment(1) .".php")) {
  256. return segment(1);
  257. } elseif (file_exists("www/applications/". _get("defaultApplication") ."/controllers/". _get("defaultApplication") .".php")) {
  258. return _get("defaultApplication");
  259. }
  260. return false;
  261. }
  262. }
  263. if (!function_exists("addCDN")) {
  264. function addCDN($content)
  265. {
  266. $server = _get("webServer");
  267. $cdnServers = _get("cdnServers");
  268. if (is_array($cdnServers) and count($cdnServers) > 0) {
  269. $servers = count($cdnServers) - 1;
  270. for ($i = 0; $i <= $servers; $i++) {
  271. $content = str_replace($cdnServers[$i] ."/www", "{{CDN_SERVER}}/www", $content);
  272. }
  273. }
  274. $content = str_replace($server ."/www", "{{CDN_SERVER}}/www", $content);
  275. return $content;
  276. }
  277. }
  278. if (!function_exists("getCDN")) {
  279. function getCDN()
  280. {
  281. if (_get("environment") > 1 and _get("cdnStatus") and count(_get("cdnServers")) > 0) {
  282. $cdnServers = _get("cdnServers");
  283. $count = count($cdnServers) - 1;
  284. $server = ($count > 0) ? rand(0, $count) : 0;
  285. return $cdnServers[$server];
  286. } else {
  287. return _get("webURL");
  288. }
  289. }
  290. }
  291. if (!function_exists("path")) {
  292. function path($path = false, $URL = false, $lang = true, $anchor = false)
  293. {
  294. if (_get("cdnStatus") and count(_get("cdnServers")) > 0 and strstr($path, "www/applications") === false) {
  295. $cdnServers = _get("cdnServers");
  296. $count = count($cdnServers) - 1;
  297. $server = ($count > 0) ? rand(0, $count) : 0;
  298. $webURL = $cdnServers[$server];
  299. } else {
  300. $webURL = _get("webURL");
  301. }
  302. $anchor = ($anchor and defined("_anchor")) ? SH . ANCHOR : null;
  303. if ($path === false) {
  304. return isLang() ? _get("webBase") ."/". _get("webLang") : _get("webBase");
  305. } elseif ($path === true) {
  306. return $lang ? _get("webBase") ."/". _get("webLang") : _get("webBase");
  307. }
  308. if ($URL === "zan") {
  309. return getDomain(CORE_PATH) ."/zan/". $path;
  310. } elseif (isLang($path)) {
  311. return _get("webBase") ."/". $path . $anchor;
  312. }
  313. if ($lang) {
  314. if ($lang !== true) {
  315. $lang = getLang($lang);
  316. return ($URL) ? $webURL ."/". $path : _get("webBase") ."/". $lang ."/". $path . $anchor;
  317. }
  318. return ($URL) ? $webURL ."/". $path : _get("webBase") ."/". _get("webLang") ."/". $path . $anchor;
  319. } else {
  320. return ($URL) ? $webURL ."/". $path : _get("webBase") ."/". $path . $anchor;
  321. }
  322. }
  323. }
  324. if (!function_exists("getDomain")) {
  325. function getDomain($path = false)
  326. {
  327. if ($path) {
  328. $URL = str_replace("http://", "", _get("webURL"));
  329. $parts = explode("/", $URL);
  330. if ($path === "../../zan" and isset($parts[0]) and isset($parts[2])) {
  331. return "http://". $parts[0] . "/". $parts[1];
  332. } elseif ($path === "../zan" and isset($parts[2])) {
  333. return "http://". $parts[0] . "/". $parts[1];
  334. }
  335. return ($path === "zan") ? _get("webURL") : "http://". $parts[0];
  336. }
  337. return _get("webURL");
  338. }
  339. }
  340. if (!function_exists("ping")) {
  341. function ping($domain)
  342. {
  343. return (!@file_get_contents("http://" . str_replace("http://", "", $domain))) ? false : true;
  344. }
  345. }
  346. if (!function_exists("redirect")) {
  347. function redirect($URL = false, $time = false)
  348. {
  349. global $ZP;
  350. if (!$time) {
  351. if (!$URL) {
  352. header("location: ". path());
  353. } elseif (substr($URL, 0, 7) !== "http://" and substr($URL, 0, 8) !== "https://") {
  354. header("location: ". path($URL));
  355. exit;
  356. } else {
  357. header("location: $URL");
  358. exit;
  359. }
  360. } elseif (!is_bool($time) and $time > 0) {
  361. $time = $time * 1000;
  362. echo '<script type="text/javascript">function delayedRedirect() { window.location.replace("'. $URL .'"); } setTimeout("delayedRedirect()", '. $time .'); </script>';
  363. }
  364. }
  365. }
  366. if (!function_exists("returnTo")) {
  367. function returnTo($path, $var = "return_to")
  368. {
  369. return "?$var=". encode($path, true);
  370. }
  371. }
  372. if (!function_exists("route")) {
  373. function route()
  374. {
  375. $URL = explode("/", substr($_SERVER["REQUEST_URI"], 1));
  376. $paths = explode("/", dirname($_SERVER["SCRIPT_FILENAME"]));
  377. $path = $paths[count($paths) - 1];
  378. if (is_array($URL)) {
  379. $URL = array_diff($URL, array(""));
  380. if (!_get("domain")) {
  381. $vars[] = array_shift($URL);
  382. }
  383. if (isset($URL[0]) and $URL[0] === $path) {
  384. $vars[] = array_shift($URL);
  385. }
  386. if (!_get("modRewrite") and isset($URL[0])) {
  387. if ($URL[0] === basename($_SERVER["SCRIPT_FILENAME"])) {
  388. $vars[] = array_shift($URL);
  389. }
  390. }
  391. }
  392. return $URL;
  393. }
  394. }
  395. if (!function_exists("routePath")) {
  396. function routePath()
  397. {
  398. $flag = false;
  399. $rsaquo = " &rsaquo;&rsaquo; ";
  400. $path = path(whichApplication()) ."/";
  401. if (segments() > 0) {
  402. for ($i = 0; $i <= segments() - 1; $i++) {
  403. if (!$flag) {
  404. if (segments() === 6) {
  405. $flag = true;
  406. $HTML = a(__("Home"), PATH("cpanel")) . $rsaquo;
  407. $HTML .= a(__(ucfirst(segment(2))), $path . segment(2)) . $rsaquo;
  408. $HTML .= a(__(ucfirst(segment(3))), $path . segment(2) . SH . segment(3)) . $rsaquo;
  409. $HTML .= a(__(ucfirst(segment(4))), $path . segment(2) . SH . segment(3) . SH . segment(4)) . $rsaquo;
  410. $HTML .= a(__(ucfirst(segment(5))), $path . segment(2) . SH . segment(3) . SH . segment(4) . SH . segment(5));
  411. } elseif (segments() === 5) {
  412. $flag = true;
  413. $HTML = a(__("Home"), path("cpanel")) . $rsaquo;
  414. $HTML .= a(__(ucfirst(segment(2))), $path . segment(2)) . $rsaquo;
  415. $HTML .= a(__(ucfirst(segment(3))), $path . segment(2) . SH . segment(3)) . $rsaquo;
  416. $HTML .= a(__(ucfirst(segment(4))), $path . segment(2) . SH . segment(3) . SH . segment(4));
  417. } elseif (segments() === 4) {
  418. $flag = true;
  419. $HTML = a(__("Home"), path("cpanel")) . $rsaquo;
  420. $HTML .= a(__(ucfirst(segment(1))), $path . "cpanel") . $rsaquo;
  421. $HTML .= a(__(ucfirst(segment(3))), $path . segment(2) . SH . segment(3));
  422. } elseif (segments() === 3) {
  423. $flag = true;
  424. $HTML = a(__("Home"), path("cpanel")) . $rsaquo;
  425. $HTML .= a(__(ucfirst(segment(1))), $path . segment(3));
  426. } elseif (segments() === 2) {
  427. $flag = true;
  428. $HTML = a(__("Home"), path("cpanel"));
  429. } else {
  430. $HTML = a(__("Home"), path("cpanel"));
  431. }
  432. }
  433. }
  434. }
  435. return $HTML;
  436. }
  437. }
  438. if (!function_exists("segment")) {
  439. function segment($segment = 0, $isLang = false)
  440. {
  441. $route = route();
  442. $segment = ($isLang) ? $segment + 1 : $segment;
  443. if (count($route) > 0) {
  444. if (isset($route[$segment]) and strlen($route[$segment]) > 0) {
  445. if ($route[$segment] === "0") {
  446. return (int) 0;
  447. }
  448. return filter($route[$segment], "remove");
  449. } else {
  450. return false;
  451. }
  452. } else {
  453. return false;
  454. }
  455. }
  456. }
  457. if (!function_exists("segments")) {
  458. function segments()
  459. {
  460. return count(route());
  461. }
  462. }