/app/Roam/Roam.php

https://github.com/tjeastmond/Rome-CMS · PHP · 191 lines · 155 code · 33 blank · 3 comment · 12 complexity · 3bafb1d1e4e0825a4f50ef3e00cca7e1 MD5 · raw file

  1. <?php
  2. class Roam
  3. {
  4. static private $_route = array();
  5. static private $_controller = 'index';
  6. static private $_action = 'index';
  7. static private $_id = '';
  8. static public $viewsPath = 'Views/';
  9. static public $layoutPath = 'layout/';
  10. static public $_customRoutes = array();
  11. static public function getAction() {
  12. return self::$_action;
  13. }
  14. static public function getController() {
  15. return self::$_controller;
  16. }
  17. static public function clean($clean) {
  18. if (!$clean) { // nothing to clean
  19. return true;
  20. }
  21. if (is_array($clean)) {
  22. $cleaned = array();
  23. foreach ($clean as $key=>$value) {
  24. $cleaned[$key] = self::paranoid($value);
  25. }
  26. return $cleaned;
  27. }
  28. return self::paranoid($clean);
  29. }
  30. static public function paranoid($string) {
  31. return strip_tags($string, '<p><a><b><strong><ul><li><img>');
  32. }
  33. // sanitize
  34. static public function getParams() {
  35. return self::clean(array_merge($_GET, $_POST));
  36. }
  37. // sanatize
  38. static public function getParam($param) {
  39. return ($_REQUEST[$param])? self::clean($_REQUEST[$param]) : false;
  40. }
  41. // sanatize
  42. static public function getId() {
  43. return (self::$_id)? intval(self::$_id) : (
  44. (self::getParam('id'))? intval(self::getParam('id')) : false
  45. );
  46. }
  47. static public function classNameToFileName($className) {
  48. return str_replace('_', '/', $className) . '.php';
  49. }
  50. static public function doesFileExist($file) {
  51. $paths = explode(":", ini_get('include_path'));
  52. foreach ($paths as $path) {
  53. if (file_exists($path . '/' . $file)) {
  54. return true;
  55. }
  56. }
  57. if (file_exists($file)) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. static function doesControllerExist($controller) {
  63. $c = 'Controllers_' . ucfirst($controller);
  64. return (self::doesFileExist(self::classNameToFileName($c)))? $c : false;
  65. }
  66. static public function layoutPath() {
  67. return self::$viewsPath . self::$layoutPath;
  68. }
  69. static public function viewsPath() {
  70. return self::$viewsPath;
  71. }
  72. static public function render($file, array $elements = array(), $layout = 'layout', $noLayout = false) {
  73. $content = self::_output(self::viewsPath() . $file . '.php', $elements);
  74. print ($noLayout)? $content : self::_output(
  75. self::layoutPath() . $layout . '.php',
  76. array_merge(array('content' => $content), $elements)
  77. );
  78. }
  79. static public function partial($file, array $elements = array()) {
  80. return self::_output(self::viewsPath() . $file . '.php', $elements);
  81. }
  82. static protected function _output($file, array $elements = array()) {
  83. extract($elements);
  84. ob_start();
  85. require $file;
  86. $output = ob_get_contents();
  87. ob_end_clean();
  88. return $output;
  89. }
  90. static public function redirect($path) {
  91. header("location:" . $path);
  92. exit();
  93. }
  94. static protected function getRoute() {
  95. $ru = substr(trim($_SERVER['REQUEST_URI'], '/'), 0);
  96. $ca = array();
  97. list($ru, $ruq) = explode('?', $ru);
  98. list($ca['c'], $ca['a'], $ca['i'], $ca['e']) = explode('/', $ru, 4);
  99. if ($custom = self::isCustomRoute($ru)) {
  100. $ca['c'] = $custom['controller'];
  101. $ca['a'] = $custom['action'];
  102. }
  103. $e = array();
  104. if(!empty($ca['e'])) {
  105. $p = explode('/', $ca['e']);
  106. if ((count($p) % 2) == 0) { // name value pair
  107. foreach (array_chunk($p, 2) as $k) {
  108. $e[$k[0]] = $k[1];
  109. }
  110. } else { // junk?
  111. $e = $p;
  112. }
  113. }
  114. return array(
  115. 'action' => ($ca['a'])? $ca['a'] : 'index',
  116. 'controller' => $ca['c'] ? $ca['c'] : 'index',
  117. 'extra' => $e,
  118. 'id' => $ca['i'] ? $ca['i'] : '',
  119. 'url' => $ru
  120. );
  121. }
  122. static public function printRoute() {
  123. print "<pre>Route: ";
  124. print_r(self::$_route);
  125. print "</pre>";
  126. }
  127. static public function addRoute($path, $controller, $action) {
  128. self::$_customRoutes[$path] = array(
  129. 'controller' => $controller,
  130. 'action' => $action
  131. );
  132. }
  133. static public function isCustomRoute($path) {
  134. if (array_key_exists($path, self::$_customRoutes)) {
  135. return self::$_customRoutes[$path];
  136. }
  137. return false;
  138. }
  139. static public function route() {
  140. $route = self::getRoute();
  141. self::$_route = $route;
  142. self::$_controller = $route['controller'];
  143. self::$_action = $route['action'];
  144. self::$_id = $route['id'];
  145. }
  146. static public function error($path = '', $msg = '') {
  147. self::redirect(($path)? $path : '/errors/');
  148. }
  149. static public function deploy() {
  150. $controller = self::doesControllerExist(self::getController());
  151. if ($controller) {
  152. $c = new $controller();
  153. $c->deploy();
  154. } else {
  155. self::error('/notfound/');
  156. }
  157. }
  158. }