Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / classes / testcms.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 /*
4 This is the front controller that
5 will route all incoming requests
6 */
7 class TestCMS {
8
9 private static function setup() {
10 // Query metadata and store into our config
11 $sql = "select `key`, `value` from meta";
12 $meta = array();
13
14 foreach(Db::results($sql) as $row) {
15 $meta[$row->key] = $row->value;
16 }
17
18 Config::set('metadata', $meta);
19
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);
23 }
24
25 public static function run() {
26 // run setup and prepare env
27 static::setup();
28
29 // handle the requested uri
30 $uri = static::parse();
31 $segments = array();
32
33 if(strlen($uri)) {
34 $segments = explode('/', $uri);
35 }
36
37 // lets log our translated uri
38 Log::info('Translated URI: ' . $uri);
39
40 // set our action or our default if none is set
41 $action = count($segments) ? array_shift($segments) : 'page';
42
43 // default to our front end router
44 $controller = 'Routes';
45
46 // set the template path
47 $theme = Config::get('metadata.theme');
48 Template::path(PATH . 'themes/' . $theme . '/');
49
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';
54
55 // set default action
56 $action = count($segments) ? array_shift($segments) : 'index';
57
58 // public admin actions
59 $public = array('users/login', 'users/amnesia', 'users/reset');
60
61 // redirect to login
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');
64 }
65
66 // set template path for admin
67 Template::path(PATH . 'system/admin/theme/');
68 }
69
70 // log the controller we are going to use and the action
71 Log::info('Controller action: ' . $controller . '/' . $action);
72
73 // check we can find a action
74 $reflector = new ReflectionClass($controller);
75
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 . '/');
79
80 // report error
81 Log::warning(($reflector->isInstantiable() === false ? 'Controller was not Instantiable' : 'Action does not exist'));
82
83 // method not found in controller
84 return Response::error(404);
85 }
86
87 $reflector->getMethod($action)->invokeArgs(new $controller, $segments);
88 }
89
90 private static function parse() {
91 // get uri
92 $uri = Request::uri();
93
94 // lets log our initial uri
95 Log::info('Requested URI: ' . $uri);
96
97 // if htaccess is not enabled and the file exists ignore the request
98 if(file_exists($uri)) {
99 return '';
100 }
101
102 // route definitions
103 $routes = array();
104
105 // posts host page
106 if($page = IoC::resolve('posts_page')) {
107 $routes[$page->slug . '/(:any)'] = 'article/$1';
108 }
109
110 // fallback to 'admin'
111 $admin_folder = Config::get('application.admin_folder', 'admin');
112
113 // static routes
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',
119
120 'search/(:any)' => 'search/$1',
121 'search' => 'search',
122
123 'rss' => 'rss',
124
125 '(:any)' => 'page/$1'
126 ));
127
128 // define wild-cards
129 $search = array(':any', ':num');
130 $replace = array('[0-9a-zA-Z~%\.:_\\-]+', '[0-9]+');
131
132 // parse routes
133 foreach($routes as $route => $translated) {
134 // replace wildcards
135 $route = str_replace($search, $replace, $route);
136
137 // look for matches
138 if(preg_match('#^' . $route . '#', $uri, $matches)) {
139 // replace matched values
140 foreach($matches as $k => $match) {
141 $translated = str_replace('$' . $k, $match, $translated);
142 }
143
144 // return on first match
145 return $translated;
146 }
147 }
148
149 return $uri;
150 }
151
152 }