PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/core/Admin_Controller.php

https://gitlab.com/sheldonels/pyrocms
PHP | 233 lines | 113 code | 50 blank | 70 comment | 20 complexity | d22a9570b990208deeb89543a10a0fe9 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. use Pyro\Module\Addons\ThemeOptionModel;
  3. /**
  4. * This is the basis for the Admin class that is used throughout PyroCMS.
  5. *
  6. * Code here is run before admin controllers
  7. *
  8. * @copyright Copyright (c) 2012, PyroCMS LLC
  9. * @package PyroCMS\Core\Controllers
  10. */
  11. class Admin_Controller extends MY_Controller
  12. {
  13. /**
  14. * Admin controllers can have sections, normally an arbitrary string
  15. *
  16. * @var string
  17. */
  18. protected $section = null;
  19. /**
  20. * Load language, check flashdata, define https, load and setup the data
  21. * for the admin theme
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. // Load the Language files ready for output
  27. $this->lang->load('admin');
  28. $this->lang->load('buttons');
  29. // Show error and exit if the user does not have sufficient permissions
  30. if ( ! self::checkAccess()) {
  31. $this->session->set_flashdata('error', lang('cp:access_denied'));
  32. redirect();
  33. }
  34. // If the setting is enabled redirect request to HTTPS
  35. if (Settings::get('admin_force_https') and strtolower(substr(current_url(), 4, 1)) != 's') {
  36. redirect(str_replace('http:', 'https:', current_url()).'?session='.session_id());
  37. }
  38. $this->load->helper('admin_theme');
  39. $theme = $this->themeManager->locate(Settings::get('admin_theme'));
  40. // Using a bad slug? Weak
  41. if (is_null($theme)) {
  42. show_error('This site has been set to use an admin theme that does not exist.');
  43. }
  44. $this->theme = ci()->theme = $theme;
  45. // make a constant as this is used in a lot of places
  46. defined('ADMIN_THEME') or define('ADMIN_THEME', $this->theme->model->slug);
  47. // Set the location of assets
  48. Asset::add_path('theme', $this->theme->web_path.'/');
  49. Asset::set_path('theme');
  50. $this->registerWidgetLocations();
  51. // Active Admin Section (might be null, but who cares)
  52. $this->template->active_section = $this->section;
  53. Events::trigger('admin_controller');
  54. // -------------------------------------
  55. // Build Admin Navigation
  56. // -------------------------------------
  57. // We'll get all of the backend modules
  58. // from the DB and run their module items.
  59. // -------------------------------------
  60. if (is_logged_in())
  61. {
  62. // Here's our menu array.
  63. $menu_items = array();
  64. // This array controls the order of the admin items.
  65. $this->template->menu_order = array('lang:cp:nav_content', 'lang:cp:nav_structure', 'lang:cp:nav_data', 'lang:cp:nav_users', 'lang:cp:nav_settings', 'lang:global:profile');
  66. $modules = $this->moduleManager->getAllEnabled(array(
  67. 'is_backend' => true,
  68. ));
  69. foreach ($modules as $module) {
  70. // Only enabled ones
  71. if (! module_enabled($module['slug'])) continue;
  72. // If we do not have an admin_menu function, we use the
  73. // regular way of checking out the details.php data.
  74. if ($module['menu'] and ($this->current_user->hasAccess($module['slug']))) {
  75. // Legacy module routing. This is just a rough
  76. // re-route and modules should change using their
  77. // upgrade() details.php functions.
  78. if ($module['menu'] == 'utilities') $module['menu'] = 'data';
  79. if ($module['menu'] == 'design') $module['menu'] = 'structure';
  80. $menu_items['lang:cp:nav_'.$module['menu']][$module['name']] = 'admin/'.$module['slug'];
  81. }
  82. // If a module has an admin_menu function, then
  83. // we simply run that and allow it to manipulate the
  84. // menu array.
  85. if (method_exists($module['module'], 'admin_menu')) {
  86. $module['module']->admin_menu($menu_items);
  87. }
  88. }
  89. // We always have our
  90. // edit profile links and such.
  91. $menu_items['lang:global:profile'] = array(
  92. 'lang:cp:edit_profile_label' => 'edit-profile',
  93. 'lang:cp:logout_label' => 'admin/logout'
  94. );
  95. // Trigger an event so modules can mess with the
  96. // menu items array via the events structure.
  97. $event_output = Events::trigger('admin_menu', $menu_items);
  98. // If we get an array, we assume they have altered the menu items
  99. // and are returning them to us to use.
  100. if (is_array($event_output)) {
  101. $menu_items = $event_output;
  102. }
  103. // Order the menu items. We go by our menu_order array.
  104. $ordered_menu = array();
  105. foreach ($this->template->menu_order as $order) {
  106. if (isset($menu_items[$order])) {
  107. $ordered_menu[lang_label($order)] = $menu_items[$order];
  108. unset($menu_items[$order]);
  109. }
  110. }
  111. // Any stragglers?
  112. if ($menu_items) {
  113. $translated_menu_items = array();
  114. // translate any additional top level menu keys so the array_merge works
  115. foreach ($menu_items as $key => $menu_item)
  116. {
  117. $translated_menu_items[lang_label($key)] = $menu_item;
  118. }
  119. $ordered_menu = array_merge_recursive($ordered_menu, $translated_menu_items);
  120. }
  121. ksort($ordered_menu);
  122. // And there we go! These are the admin menu items.
  123. $this->template->menu_items = $ordered_menu;
  124. }
  125. // ------------------------------
  126. // Template configuration
  127. $this->template
  128. ->enable_parser(false)
  129. ->set('theme_options', (object) $this->theme->model->getOptionValues())
  130. ->set_theme(ADMIN_THEME)
  131. ->set_layout('default', 'admin');
  132. // trigger the run() method in the selected admin theme
  133. $class = 'Theme_'.ucfirst($this->theme->model->slug);
  134. call_user_func(array(new $class, 'run'));
  135. }
  136. /**
  137. * Checks to see if a user object has access rights to the admin area.
  138. *
  139. * @return boolean
  140. */
  141. private function checkAccess()
  142. {
  143. // These pages get past permission checks
  144. $ignored_pages = array('admin/login', 'admin/logout', 'admin/help');
  145. // Check if the current page is to be ignored
  146. $current_page = $this->uri->segment(1, '') . '/' . $this->uri->segment(2, 'index');
  147. // Dont need to log in, this is an open page
  148. if (in_array($current_page, $ignored_pages)) {
  149. return true;
  150. }
  151. if ( ! $this->current_user) {
  152. // save the location they were trying to get to
  153. $this->session->set_userdata('admin_redirect', $this->uri->uri_string());
  154. redirect('admin/login');
  155. // Well they at least better have permissions!
  156. } if ($this->current_user) {
  157. if ($this->current_user->isSuperUser()) {
  158. return true;
  159. // We are looking at the index page. Show it if they have ANY admin access at all
  160. } elseif ($current_page === 'admin/index' && $this->current_user->hasAccess('admin.general')){
  161. return true;
  162. }
  163. // Check if the current user can view that page
  164. return $this->current_user->hasAccess("{$this->module}.*");
  165. }
  166. // god knows what this is... erm...
  167. return false;
  168. }
  169. /**
  170. * Let the Frontend know where Widgets are hiding
  171. */
  172. protected function registerWidgetLocations()
  173. {
  174. $this->widgetManager->setLocations(array(
  175. SHARED_ADDONPATH.'themes/'.ADMIN_THEME.'/widgets/',
  176. APPPATH.'themes/'.ADMIN_THEME.'/widgets/',
  177. ADDONPATH.'themes/'.ADMIN_THEME.'/widgets/',
  178. APPPATH.'widgets/',
  179. ADDONPATH.'widgets/',
  180. SHARED_ADDONPATH.'widgets/',
  181. ));
  182. }
  183. }