PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/core/Public_Controller.php

https://github.com/asalem/pyrocms
PHP | 127 lines | 70 code | 26 blank | 31 comment | 16 complexity | 73b35b6a09f73086ad91e399d46ad703 MD5 | raw file
Possible License(s): CC-BY-3.0, BSD-3-Clause, CC0-1.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT
  1. <?php
  2. use Pyro\Module\Redirects\Model\RedirectEntryModel;
  3. use Pyro\Module\Variables\VariableData;
  4. /**
  5. * Code here is run before frontend controllers
  6. *
  7. * @author PyroCMS Dev Team
  8. * @copyright Copyright (c) 2012, PyroCMS LLC
  9. * @package PyroCMS\Core\Controllers
  10. */
  11. class Public_Controller extends MY_Controller
  12. {
  13. /**
  14. * Loads the gazillion of stuff, in Flash Gordon speed.
  15. * @todo Document properly please.
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->benchmark->mark('public_controller_start');
  21. // Check redirects if GET and Not AJAX
  22. if ( ! $this->input->is_ajax_request() and $_SERVER['REQUEST_METHOD'] == 'GET') {
  23. $uri = trim(uri_string(), '/');
  24. if ($uri and $redirect = RedirectEntryModel::findByUri($uri)) {
  25. // Check if it was direct match
  26. if ($redirect->from == $uri) {
  27. redirect($redirect->to, 'location', $redirect->type);
  28. }
  29. // If it has back reference
  30. if (strpos($redirect->to, '$') !== false) {
  31. $from = str_replace('%', '(.*?)', $redirect->from);
  32. $redirect->to = preg_replace('#^'.$from.'$#', $redirect->to, $uri);
  33. }
  34. // Redirect with wanted redirect header type
  35. redirect($redirect->to, 'location', $redirect->type);
  36. }
  37. }
  38. Events::trigger('public_controller');
  39. // Check the frontend hasnt been disabled by an admin
  40. if ( ! Settings::get('frontend_enabled') && (empty($this->current_user) or ! $this->current_user->isSuperUser()) && ! $this->input->is_ajax_request()) {
  41. header('Retry-After: 600');
  42. $error = Settings::get('unavailable_message') ?: lang('cms:fatal_error');
  43. show_error($error, 503);
  44. }
  45. // Load the current theme so we can set the assets right away
  46. ci()->theme = $this->themeManager->locate(Settings::get('default_theme'));
  47. if (empty($this->theme->model->slug)) {
  48. show_error('This site has been set to use a theme that does not exist. If you are an administrator please '.anchor('admin/themes', 'change the theme').'.');
  49. }
  50. // Set the theme as a path for Asset library
  51. Asset::add_path('theme', $this->theme->path.'/');
  52. Asset::set_path('theme');
  53. $this->registerWidgetLocations();
  54. // Support CDN URL's like Amazon CloudFront
  55. if (Settings::get('cdn_domain')) {
  56. $protocol = ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ? 'https' : 'http';
  57. // Make cdn.pyrocms.com into https://cdn.pyrocms.com/
  58. Asset::set_url($protocol.'://'.rtrim(Settings::get('cdn_domain'), '/').'/');
  59. }
  60. // Set the theme view folder
  61. $this->template->set_theme($this->theme->model->slug);
  62. // Is there a layout file for this module?
  63. if ($this->template->layout_exists($this->module.'.html')) {
  64. $this->template->set_layout($this->module.'.html');
  65. }
  66. // Nope, just use the default layout
  67. elseif ($this->template->layout_exists('default.html')) {
  68. $this->template->set_layout('default.html');
  69. }
  70. // Make sure whatever page the user loads it by, its telling search robots the correct formatted URL
  71. $this->template->set_metadata('canonical', site_url($this->uri->uri_string()), 'link');
  72. // If there is a blog module, link to its RSS feed in the head
  73. if (module_enabled('blog')) {
  74. $this->template->append_metadata('<link rel="alternate" type="application/rss+xml" title="'.Settings::get('site_name').'" href="'.site_url('blog/rss/all.rss').'" />');
  75. }
  76. //ci()->variables = new VariableData;
  77. // Assign segments to the template the new way
  78. $this->template->server = $_SERVER;
  79. // Set the theme option values
  80. foreach ($this->theme->model->options as $option) {
  81. foreach ($this->theme->options as &$themeOption) {
  82. if (isset($themeOption['slug']) and $themeOption['slug'] == $option->slug) {
  83. $themeOption = $option->value;
  84. }
  85. }
  86. }
  87. $this->template->theme = $this->theme;
  88. $this->benchmark->mark('public_controller_end');
  89. }
  90. /**
  91. * Let the Frontend know where Widgets are hiding
  92. */
  93. public function registerWidgetLocations()
  94. {
  95. $this->widgetManager->setLocations(array(
  96. APPPATH.'widgets/',
  97. ADDONPATH.'widgets/',
  98. SHARED_ADDONPATH.'widgets/',
  99. ));
  100. }
  101. }