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

/_app/routes.php

https://bitbucket.org/sirestudios/fortis-wellness
PHP | 369 lines | 204 code | 81 blank | 84 comment | 61 complexity | 2222839315ac5a96076e968456f08d67 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Hook: Routes Before
  5. |--------------------------------------------------------------------------
  6. |
  7. | Useful for running your own route. Remember to use $app->pass() if
  8. | you're not doing anything with the current request.
  9. |
  10. */
  11. Hook::run('core', 'routes_before');
  12. /////////////////////////////////////////////////////////////////////////////////////////////////
  13. // ROUTING HOOKS
  14. /////////////////////////////////////////////////////////////////////////////////////////////////
  15. $app->map('/TRIGGER/:namespace/:hook', function ($namespace, $hook) use ($app) {
  16. Hook::run($namespace, $hook);
  17. })->via('GET', 'POST', 'HEAD');
  18. /////////////////////////////////////////////////////////////////////////////////////////////////
  19. // Static Asset Pipeline (for development only!)
  20. /////////////////////////////////////////////////////////////////////////////////////////////////
  21. $app->get('/assets/(:segments+)', function($segments = array()) use ($app) {
  22. $file_requested = implode($segments, '/');
  23. $file = Theme::getPath() . $file_requested;
  24. # Routes only if the file doesn't already exist (e.g. /assets/whatever.ext)
  25. if ( ! File::exists($file_requested) && File::exists($file)) {
  26. $mime = File::resolveMime($file);
  27. header("Content-type: {$mime}");
  28. readfile($file);
  29. exit();
  30. } else {
  31. // Moving on. Not a valid asset.
  32. $app->pass();
  33. }
  34. });
  35. /////////////////////////////////////////////////////////////////////////////////////////////////
  36. // GLOBAL STATAMIC CONTENT ROUTING
  37. /////////////////////////////////////////////////////////////////////////////////////////////////
  38. $app->map('/(:segments+)', function ($segments = array()) use ($app) {
  39. $requesting_xml = false;
  40. $content_found = false;
  41. // segments
  42. foreach ($segments as $key => $seg) {
  43. $count = $key + 1;
  44. $app->config['segment_' . $count] = $seg;
  45. }
  46. $app->config['last_segment'] = end($segments);
  47. /*
  48. |--------------------------------------------------------------------------
  49. | Routes: Ignore Segment
  50. |--------------------------------------------------------------------------
  51. |
  52. | Globally ignore a specific URL segment. For example, "success".
  53. |
  54. */
  55. if (isset($app->config['_routes']['ignore']) && is_array($app->config['_routes']['ignore']) && count($app->config['_routes']['ignore']) > 0) {
  56. $ignore = $app->config['_routes']['ignore'];
  57. $remove_segments = array_intersect($ignore, $segments);
  58. $segments = array_diff($segments, $remove_segments);
  59. }
  60. /*
  61. |--------------------------------------------------------------------------
  62. | Routes: Ignore AFTER a Segment
  63. |--------------------------------------------------------------------------
  64. |
  65. | Globally ignore all URL segments after a specified one. For example,
  66. | "search" could let you use additional segments as match conditions.
  67. |
  68. */
  69. if ($ignore_after = array_get($app->config, '_routes:ignore_after', false)) {
  70. if ( ! is_array($ignore_after)) {
  71. $ignore_after = array($ignore_after);
  72. }
  73. foreach ($ignore_after as $segment) {
  74. $position = array_search($segment, $segments);
  75. if ($position !== false) {
  76. array_splice($segments, $position + 1);
  77. }
  78. }
  79. }
  80. // determine paths
  81. $path = '/' . implode($segments, '/');
  82. // let XML files through
  83. if (substr($path, -4) == '.xml') {
  84. $path = substr($path, 0, -4);
  85. $requesting_xml = true;
  86. }
  87. $current_url = $path;
  88. $complete_current_url = Path::tidy(Config::getSiteRoot() . "/" . $current_url);
  89. // allow mod_rewrite for .html file extensions
  90. if (substr($path, -5) == '.html') {
  91. $path = str_replace('.html', '', $path);
  92. }
  93. $app->config['current_path'] = $path;
  94. // init some variables for below
  95. $content_root = Config::getContentRoot();
  96. $content_type = Config::getContentType();
  97. $response_code = 200;
  98. $visible = true;
  99. $add_prev_next = false;
  100. $template_list = array('default');
  101. // set up the app based on if a
  102. if (File::exists("{$content_root}/{$path}.{$content_type}") || Folder::exists("{$content_root}/{$path}")) {
  103. // endpoint or folder exists!
  104. } else {
  105. $path = Path::resolve($path);
  106. $app->config['current_url'] = $app->config['current_path'];
  107. $app->config['current_path'] = $path; # override global current_path
  108. }
  109. // routes via routes.yaml
  110. if (isset($app->config['_routes']['routes'][$current_url]) || isset($app->config['_routes'][$current_url])) {
  111. # allows the route file to run without "route:" as the top level array key (backwards compatibility)
  112. $current_route = isset($app->config['_routes']['routes'][$current_url]) ? $app->config['_routes']['routes'][$current_url] : $app->config['_routes'][$current_url];
  113. $route = $current_route;
  114. $template = $route;
  115. $data = array();
  116. if (is_array($route)) {
  117. $template = isset($route['template']) ? $route['template'] : 'default';
  118. if (isset($route['layout'])) {
  119. $data['_layout'] = $route['layout'];
  120. }
  121. }
  122. $template_list = array($template);
  123. $content_found = true;
  124. // actual file exists
  125. } elseif (File::exists("{$content_root}/{$path}.{$content_type}")) {
  126. $add_prev_next = true;
  127. $template_list[] = 'post';
  128. $page = basename($path);
  129. $data = Content::get($complete_current_url);
  130. $data['current_url'] = $current_url;
  131. $data['slug'] = basename($current_url);
  132. if ($path !== "404") {
  133. $content_found = true;
  134. }
  135. // url is taxonomy-based
  136. } elseif (Taxonomy::isTaxonomyURL($path)) {
  137. list($type, $slug) = Taxonomy::getCriteria($path);
  138. // create data array
  139. $data = array_merge(Config::getAll(), array(
  140. 'homepage' => Config::getSiteRoot(),
  141. 'raw_url' => Request::getResourceURI(),
  142. 'page_url' => Request::getResourceURI(),
  143. 'taxonomy_slug' => urldecode($slug),
  144. 'taxonomy_name' => Taxonomy::getTaxonomyName($type, $slug)
  145. ));
  146. $template_list[] = "taxonomies";
  147. $template_list[] = $type;
  148. $content_found = true;
  149. // this is a directory,so we look for page.md
  150. } elseif (is_dir("{$content_root}/{$path}")) {
  151. $data = Content::get($complete_current_url);
  152. $content_found = true;
  153. // URL found in the cache
  154. } elseif ($data = Content::get($complete_current_url)) {
  155. $add_prev_next = true;
  156. $page = basename($path);
  157. $data = Content::get($complete_current_url);
  158. $data['current_url'] = $current_url;
  159. $data['slug'] = basename($current_url);
  160. if ($path !== "404") {
  161. $content_found = true;
  162. }
  163. }
  164. // Nothing found. 404 O'Clock.
  165. if (!$content_found || ($requesting_xml && (!isset($data['_type']) || $data['_type'] != 'xml'))) {
  166. // determine where user came from for log message
  167. if (strstr($path, 'favicon.ico')) {
  168. // Favicons are annoying.
  169. Log::info("The site favicon could not be found.", "site", "favicon");
  170. } else {
  171. if (isset($_SERVER['HTTP_REFERER'])) {
  172. $url_parts = parse_url($_SERVER['HTTP_REFERER']);
  173. // get local referrer
  174. $local_referrer = $url_parts['path'];
  175. $local_referrer .= (isset($url_parts['query']) && $url_parts['query']) ? '?' . $url_parts['query'] : '';
  176. $local_referrer .= (isset($url_parts['fragment']) && $url_parts['fragment']) ? '#' . $url_parts['fragment'] : '';
  177. if (strstr($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== false) {
  178. // the call came from inside the house!
  179. $more = 'There is a bad link on <a href="' . $local_referrer . '">' . $local_referrer . '</a>.';
  180. $aspect = 'page';
  181. } else {
  182. // external site linked to here
  183. $more = 'User clicked an outside bad link at <a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a>.';
  184. $aspect = 'external';
  185. }
  186. } else {
  187. // user typing error
  188. $more = 'Visitor came directly to this page and may have typed the URL incorrectly.';
  189. $aspect = 'visitor';
  190. }
  191. Log::error("404 - Page not found. " . $more, $aspect, "content");
  192. }
  193. $data = Content::get(Path::tidy(Config::getSiteRoot() . "/404"));
  194. $template_list = array('404');
  195. $response_code = 404;
  196. }
  197. # We now have all the YAML content
  198. # Let's process action fields
  199. # Redirect
  200. if (isset($data['_redirect'])) {
  201. $response = 302;
  202. if (is_array($data['_redirect'])) {
  203. $url = isset($data['_redirect']['to']) ? $data['_redirect']['to'] : false;
  204. if (!$url) {
  205. $url = isset($data['_redirect']['url']) ? $data['_redirect']['url'] : false; #support url key as alt
  206. }
  207. $response = isset($data['_redirect']['response']) ? $data['_redirect']['response'] : $response;
  208. } else {
  209. $url = $data['_redirect'];
  210. }
  211. if ($url) {
  212. $app->redirect($url, $response);
  213. }
  214. }
  215. // status
  216. if (isset($data['_is_draft']) && $data['_is_draft'] && !$app->config['logged_in']) {
  217. $data = Content::get(Path::tidy(Config::getSiteRoot() . "/404"));
  218. $template_list = array('404');
  219. $visible = false;
  220. $response_code = 404;
  221. // legacy status
  222. } elseif (isset($data['status']) && $data['status'] != 'live' && $data['status'] != 'hidden' && !$app->config['logged_in']) {
  223. $data = Content::get(Path::tidy(Config::getSiteRoot() . "/404"));
  224. $template_list = array('404');
  225. $visible = false;
  226. $response_code = 404;
  227. }
  228. // find next/previous
  229. if ($add_prev_next && $visible) {
  230. $folder = substr(preg_replace(Pattern::ORDER_KEY, "", substr($path, 0, (-1*strlen($page))-1)), 1);
  231. $relative = Statamic::find_relative($current_url, $folder);
  232. $data['prev'] = $relative['prev'];
  233. $data['next'] = $relative['next'];
  234. }
  235. // grab data for this folder
  236. $folder_data = Content::get(dirname($current_url));
  237. $fields_data = YAML::parseFile(Path::tidy(BASE_PATH . "/" . Config::getContentRoot() . dirname($current_url) . '/fields.yaml'));
  238. // Check for fallback template
  239. if (empty($data['_template'])) {
  240. // check fields.yaml first
  241. if (array_get($fields_data, '_default_folder_template')) {
  242. $data['_template'] = $fields_data['_default_folder_template'];
  243. // fall back to the folder's page.md file
  244. } elseif (array_get($folder_data, '_default_folder_template')) {
  245. $data['_template'] = $folder_data['_default_folder_template'];
  246. }
  247. }
  248. // Check for fallback layout
  249. if (empty($data['_layout'])) {
  250. // check fields.yaml first
  251. if (array_get($fields_data, '_default_folder_layout')) {
  252. $data['_layout'] = $fields_data['_default_folder_layout'];
  253. // fall back to the folder's page.md file
  254. } elseif (array_get($folder_data, '_default_folder_layout')) {
  255. $data['_layout'] = $folder_data['_default_folder_layout'];
  256. }
  257. }
  258. // set template and layout
  259. if (isset($data['_template'])) {
  260. $template_list[] = $data['_template'];
  261. }
  262. if (isset($data['_layout'])) {
  263. Statamic_View::set_layout("layouts/{$data['_layout']}");
  264. }
  265. // set up the view
  266. Statamic_View::set_templates(array_reverse($template_list));
  267. // set type, allows for RSS feeds
  268. if (isset($data['_type'])) {
  269. if ($data['_type'] == 'rss' || $data['_type'] == 'xml') {
  270. $data['_xml_header'] = '<?xml version="1.0" encoding="utf-8" ?>';
  271. $response = $app->response();
  272. $response['Content-Type'] = 'application/xml';
  273. }
  274. }
  275. /*
  276. |--------------------------------------------------------------------------
  277. | Hook: Render Before
  278. |--------------------------------------------------------------------------
  279. |
  280. | Allows actions to occur before the template is rendered and parsed.
  281. | For example, pre-process a POST or set global variables dynamically.
  282. |
  283. */
  284. Hook::run('core', 'render_before');
  285. // and go!
  286. $app->render(null, $data, $response_code);
  287. $app->halt($response_code, ob_get_clean());
  288. })->via('GET', 'POST', 'HEAD');