SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/PageRouting.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use karmabunny\pdb\Exceptions\QueryException;
  17.  
  18.  
  19. /**
  20. * Logic for selecting the page if no controller matches
  21. **/
  22. class PageRouting
  23. {
  24.  
  25. /**
  26.   * Called before the main Kohana routing code
  27.   **/
  28. public static function prerouting()
  29. {
  30. if (strpos(Router::$current_uri, 'admin/') === 0) return;
  31. if (strpos(Router::$current_uri, 'dbtools/') === 0) return;
  32.  
  33. // Redirect
  34. try {
  35. $url_std = trim(Router::$current_uri, '/ ');
  36. $params = [
  37. 'url_std' => $url_std,
  38. 'url_like' => Pdb::likeEscape($url_std),
  39. 'subsite_id' => SubsiteSelector::$subsite_id,
  40. 'domain_std' => $_SERVER['HTTP_HOST'],
  41. ];
  42.  
  43. $q = "SELECT type, destination
  44. FROM ~redirects
  45. WHERE
  46. active = 1
  47. AND
  48. (path_exact = '' OR path_exact LIKE :url_like)
  49. AND
  50. (path_contains = '' OR :url_std LIKE CONCAT('%', path_contains, '%'))
  51. AND
  52. (subsite_id = 0 OR subsite_id = :subsite_id)
  53. AND
  54. (domain_contains = '' OR :domain_std LIKE CONCAT('%', domain_contains, '%'))
  55. ORDER BY id
  56. LIMIT 1";
  57. $row = Pdb::q($q, $params, 'row');
  58.  
  59. $url = Lnk::url($row['destination']);
  60. $method = ($row['type'] == 'Permanent' ? 301 : 302);
  61.  
  62. if ($url) {
  63. Url::redirect($url, $method);
  64. }
  65. } catch (QueryException $ex) {}
  66. }
  67.  
  68.  
  69. /**
  70.   * Called after the main Kohana routing code
  71.   **/
  72. public static function postrouting()
  73. {
  74. if (Router::$current_uri === '') {
  75. Router::$controller = 'SproutModules\Karmabunny\HomePage\Controllers\HomePageController';
  76. Router::$method = 'index';
  77. Router::$arguments = array();
  78. return;
  79. }
  80.  
  81. // If we've already got a controller, there isn't anything to do here
  82. if (Router::$controller !== NULL) {
  83. return;
  84. }
  85.  
  86. Router::$controller = 'Sprout\\Controllers\\PageController';
  87.  
  88. // Look for a valid page
  89. $root = Navigation::getRootNode();
  90. $matcher = new TreenodePathMatcher(Router::$current_uri);
  91. $node = $root->findNode($matcher);
  92. if ($node) {
  93. Router::$method = 'viewById';
  94. Router::$arguments = array($node['id']);
  95. return;
  96. }
  97.  
  98. // 404 error
  99. Router::$method = 'fourOhFour';
  100. Router::$arguments = array(Router::$current_uri);
  101. }
  102.  
  103. }
  104.