1 <?php
defined('IN_CMS') or die('No direct access allowed.');
4 This is the front controller that
5 will route all incoming requests
9 private static function setup() {
10 // Query metadata and store into our config
11 $sql = "select `key`, `value` from meta";
14 foreach(Db
::results($sql) as $row) {
15 $meta[$row->key
] = $row->value
;
18 Config
::set('metadata', $meta);
20 // look up which page has our posts
21 $page = Pages
::find(array('id' => Config
::get('metadata.posts_page')));
22 IoC
::instance('posts_page', $page, true);
25 public static function run() {
26 // run setup and prepare env
29 // handle the requested uri
30 $uri = static::parse();
34 $segments = explode('/', $uri);
37 // lets log our translated uri
38 Log
::info('Translated URI: ' . $uri);
40 // set our action or our default if none is set
41 $action = count($segments) ?
array_shift($segments) : 'page';
43 // default to our front end router
44 $controller = 'Routes';
46 // set the template path
47 $theme = Config
::get('metadata.theme');
48 Template
::path(PATH
. 'themes/' . $theme . '/');
50 // remove admin as an argument and set the default action if there isnt one
51 if($action == 'admin') {
52 // set default controller for the admin
53 $controller = (count($segments) ?
array_shift($segments) : 'posts') . '_controller';
56 $action = count($segments) ?
array_shift($segments) : 'index';
58 // public admin actions
59 $public = array('users/login', 'users/amnesia', 'users/reset');
62 if(Users
::authed() === false and in_array(trim($controller, '_controller') . '/' . $action, $public) === false) {
63 return Response
::redirect(Config
::get('application.admin_folder') . '/users/login');
66 // set template path for admin
67 Template
::path(PATH
. 'system/admin/theme/');
70 // log the controller we are going to use and the action
71 Log
::info('Controller action: ' . $controller . '/' . $action);
73 // check we can find a action
74 $reflector = new ReflectionClass($controller);
76 if($reflector->isInstantiable() === false or $reflector->hasMethod($action) === false) {
77 // default back to front end template for 404 page
78 Template
::path(PATH
. 'themes/' . $theme . '/');
81 Log
::warning(($reflector->isInstantiable() === false ?
'Controller was not Instantiable' : 'Action does not exist'));
83 // method not found in controller
84 return Response
::error(404);
87 $reflector->getMethod($action)->invokeArgs(new $controller, $segments);
90 private static function parse() {
92 $uri = Request
::uri();
94 // lets log our initial uri
95 Log
::info('Requested URI: ' . $uri);
97 // if htaccess is not enabled and the file exists ignore the request
98 if(file_exists($uri)) {
106 if($page = IoC
::resolve('posts_page')) {
107 $routes[$page->slug
. '/(:any)'] = 'article/$1';
110 // fallback to 'admin'
111 $admin_folder = Config
::get('application.admin_folder', 'admin');
114 $routes = array_merge($routes, array(
115 $admin_folder . '/(:any)/(:any)/(:any)' => 'admin/$1/$2/$3',
116 $admin_folder . '/(:any)/(:any)' => 'admin/$1/$2',
117 $admin_folder . '/(:any)' => 'admin/$1',
118 $admin_folder => 'admin',
120 'search/(:any)' => 'search/$1',
121 'search' => 'search',
125 '(:any)' => 'page/$1'
129 $search = array(':any', ':num');
130 $replace = array('[0-9a-zA-Z~%\.:_\\-]+', '[0-9]+');
133 foreach($routes as $route => $translated) {
135 $route = str_replace($search, $replace, $route);
138 if(preg_match('#^' . $route . '#', $uri, $matches)) {
139 // replace matched values
140 foreach($matches as $k => $match) {
141 $translated = str_replace('$' . $k, $match, $translated);
144 // return on first match