public
Description: PyroCMS is a CMS built using the CodeIgniter PHP framework with modularity in mind. Lightweight, themeable and dynamic.
Homepage: http://pyrocms.com
Clone URL: git://github.com/philsturgeon/pyrocms.git
Click here to lend your support to: pyrocms and make a donation at www.pledgie.com !
pyrocms / application / libraries / MY_Controller.php
100644 158 lines (116 sloc) 5.77 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
// Code here is run before ALL controllers
class MY_Controller extends Controller {
 
var $module;
var $controller;
var $method;
 
function MY_Controller() {
 
parent::Controller();
 
        // Make sure we have the user module
        if( ! is_module('users') )
        {
         show_error('The user module is missing.');
        }
        
        else
        {
// Load the user model and get user data
$this->load->module_model('users', 'users_m');
$this->load->module_library('users', 'user_lib');
 
$this->data->user =& $this->user_lib->user_data;
        }
        
        
        // Work out module, controller and method and make them accessable throught the CI instance
        $this->module = str_replace(array('modules/', '/'), '', $this->matchbox->fetch_module());
        $this->controller = strtolower(get_class($this));
        $s = $this->uri->rsegment_array();
        $n = array_search($this->controller, $s);
        $this->method = $this->uri->rsegment($n+1);
 
// Get meta data for the module
        $this->module_data = $this->modules_m->getModule($this->module);
        
        // Make them available to all layout files
        $this->data->module_data =& $this->module_data;
        
        $this->data->module =& $this->module;
        $this->data->controller =& $this->controller;
        $this->data->method =& $this->method;
}
}
 
// Code here is run before frontend controllers
class Public_Controller extends MY_Controller {
    
function Public_Controller() {
        
parent::MY_Controller();
        
        // Check the frontend hasnt been disabled by an admin
        if(!$this->settings->item('frontend_enabled'))
        {
         $error = $this->settings->item('unavailable_message') ? $this->settings->item('unavailable_message') : 'Fatal error, is CMS installed?';
         show_error($this->settings->item('unavailable_message'));
        }
        
        // -- Navigation menu -----------------------------------
        $this->load->module_model('pages', 'pages_m');
        $this->load->module_model('navigation', 'navigation_m');
        
        $this->data->navigation = $this->cache->call('navigation_m', 'frontendNavigation', array(), $this->settings->item('navigation_cache'));
 
        // Set the theme view folder
        $this->data->theme_view_folder = '../themes/'.$this->settings->item('default_theme').'/views/';
        $this->layout->layout_file = $this->data->theme_view_folder.'layout.php';
        
        // Make sure whatever page the user loads it by, its telling Google the correct formatted URL
        $this->layout->set_metadata('canonical', site_url($this->uri->uri_string()), 'link');
        
        // If there is a news module, link to its RSS feed in the head
        if(is_module('news'))
        {
$this->layout->extra_head('<link rel="alternate" type="application/rss+xml" title="'.$this->settings->item('site_name').'" href="'.site_url('news/rss/all|rss').'" />');
        }
 
     //$this->output->enable_profiler(TRUE);
    }
 
}
 
 
// Code here is run before admin controllers
class Admin_Controller extends MY_Controller {
    
function Admin_Controller() {
        
parent::MY_Controller();
        
        $allow_access = FALSE;
        
        // These pages get past permission checks
        $ignored_pages = array('admin/login', 'admin/logout');
 
        // Check if the current page is to be ignored
        $current_page = $this->uri->segment(1, '') . '/' . $this->uri->segment(2, '');
        $is_ignored_page = in_array($current_page, $ignored_pages);
        
        // Check the user is an admin
        $is_admin = $this->user_lib->check_role('admin');
        
        // Login: If not logged in and its not an ignored page, force login
        if( ! $this->data->user && ! $is_ignored_page)
        {
         redirect('admin/login');
        }
        
        // Logged in or ignored page
        else
        {
         $allow_access = TRUE;
        }
        
        // We are looking at the index page. Show it if they have ANY admin access at all
        if( in_array($current_page, array('admin/', 'admin/index')) && $this->permissions_m->hasAdminAccess($this->data->user->role) )
        {
         $allow_access = TRUE;
        }
        
        // Check Perms: Not an admin and this is not a page to ignore
        elseif( ! $is_admin && ! $is_ignored_page )
        {
// Check if the current user can view that page
$location = array( 'module'=>$this->module, 'controller'=>$this->controller, 'method'=>$this->method );
$allow_access = $this->permissions_m->checkRuleByRole( $this->data->user->role, $location );
        }
        
        // Show error and exit if the user does not have sufficient permissions
        if( ! $allow_access )
        {
show_error('You do not have sufficient permissions to view this page.');
exit;
        }
 
        // TODO: PJS I do not think this cache is working. Got bored and went to the pub...
        // Get a list of all modules available to this role
        if($current_page != 'admin/login')
        {
$this->data->admin_modules = $this->cache->call('modules_m', 'getModules', array(
array('is_backend'=>true, 'role' => @$this->data->user->role) // This function does NOT need role, that is to keep caching seperate
));
}
        
        $this->data->toolbar = $this->modules_m->getModuleToolbar($this->module);
        
        $this->layout->layout_file = 'admin/layout.php';
    
        //$this->output->enable_profiler(TRUE);
    }
    
}
 
?>