1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework;
10:
11:
12: use GLFramework\Middleware\ControllerMiddleware;
13: use GLFramework\Module\Module;
14: use GLFramework\Module\ModuleManager;
15: use GLFramework\Upload\Uploads;
16:
17: abstract class Controller
18: {
19:
20: var $title = false;
21: var $name = null;
22: var $admin = false;
23:
24: private $template = null;
25: private $db = null;
26: private $view;
27: var $messages = array();
28: var $config = array();
29: var $description = "";
30: var $directory;
31:
32: 33: 34:
35: private $middleware = array();
36: 37: 38:
39: var $module;
40: var $params = array();
41:
42: var $redirect = false;
43: 44: 45:
46: var $response;
47:
48: 49: 50: 51: 52:
53: public function __construct($base = "", $module = null)
54: {
55: if($module == null)
56: $module = ModuleManager::getInstance()->getMainModule();
57:
58: $this->module = $module;
59: $this->config = $this->module->getConfig();
60: $this->restoreMessages();
61: if(empty($this->name))
62: $this->name = get_class($this);
63: $this->directory = dirname($base);
64: $base = substr($base, 0, strrpos($base, "."));
65: $this->template = $base . ".twig";
66:
67: $this->view = new View($this);
68: $this->response = new Response();
69: $this->addMiddleware(new ControllerMiddleware($this));
70: Events::fire('afterControllerConstruct', array($this));
71: }
72:
73: 74: 75: 76:
77: abstract public function run();
78:
79: 80: 81: 82: 83: 84:
85: public function display($data, $params)
86: {
87: return $this->view->render($data, $params);
88: }
89:
90: 91: 92: 93: 94:
95: public function call($request)
96: {
97:
98: $this->params = $request->getParams();
99: $this->middleware = array_reverse($this->middleware);
100: reset($this->middleware);
101: $this->middleware($request, $this->response);
102: return $this->response;
103: }
104:
105: public function middleware($request, &$response)
106: {
107: $that = $this;
108: if($middleware = current($this->middleware))
109: {
110: next($this->middleware);
111: $middleware->next($request, $response, function($request, $response) use($that)
112: {
113: $that->middleware($request, $response);
114: });
115:
116: }
117: }
118:
119: 120: 121: 122:
123: public function getResponse()
124: {
125: return $this->response;
126: }
127:
128: 129: 130: 131:
132: public function getTemplate()
133: {
134: return $this->template;
135: }
136:
137: 138: 139: 140:
141: public function setTemplate($template)
142: {
143: $this->template = $template;
144: }
145:
146: 147: 148: 149:
150: public function redirection($url)
151: {
152: $this->response->setRedirection($url);
153: }
154:
155: 156: 157:
158: public function restoreMessages()
159: {
160: if(isset($_SESSION['messages']))
161: {
162: $this->messages = $_SESSION['messages'];
163: unset($_SESSION['messages']);
164: }
165: }
166:
167: 168: 169:
170: public function shareMessages()
171: {
172: $_SESSION['messages'] = $this->messages;
173: }
174:
175: 176: 177: 178:
179: public function quit($redirection = null)
180: {
181: if(!$this->response->getAjax())
182: {
183:
184: if($redirection) $this->redirection($redirection);
185: $this->shareMessages();
186: if(!GL_TESTING)
187: {
188: Bootstrap::dispatch($this->response);
189: exit;
190: }
191: }
192: }
193:
194: 195: 196: 197:
198: public function getDb()
199: {
200: if(!$this->db)
201: $this->db = new DatabaseManager();
202: return $this->db;
203: }
204:
205: 206: 207: 208: 209:
210: public function addMessage($message, $type = "success")
211: {
212: Events::fire('onMessageDisplay', array('message' => $message, 'type' => $type));
213: $this->messages[] = array('message' => $message, 'type' => $type);
214: }
215:
216: 217: 218:
219: public function getMessages()
220: {
221: return $this->messages;
222: }
223:
224: 225: 226:
227: public function getView()
228: {
229: return $this->view;
230: }
231:
232: 233: 234: 235: 236: 237: 238: 239:
240: public function getLink($controller, $params = array(), $fullPath = false)
241: {
242: if($controller instanceof Controller) $controller = get_class($controller);
243: $controller = (string) $controller;
244: $url = Bootstrap::getSingleton()->getManager()->getRouter()->generate($controller, $params);
245: if($fullPath)
246: {
247: $protocol = "http";
248: if(strpos($_SERVER['SCRIPT_URI'], "https") !== FALSE) $protocol = "https";
249: return $protocol . "://" . $_SERVER['HTTP_HOST'] . $url;
250: }
251: return $url;
252: }
253:
254: 255: 256: 257: 258: 259:
260: public function getResource($name, $module = null)
261: {
262: if($module == null) $module = $this->module;
263: if(is_string($module)) $module = ModuleManager::getModuleInstanceByName($module);
264: $config = $module->getConfig();
265: $folders = $config['app']['resources'];
266: if(!is_array($folders)) $folders = array($folders);
267: foreach($folders as $folder)
268: {
269: $path = $module->getDirectory() . "/" . $folder . "/" . $name;
270: if(file_exists($path))
271: {
272: $path = realpath($path);
273: $base = dirname($_SERVER['SCRIPT_FILENAME']);
274: $index = strpos($path, $base);
275: $url = substr($path, $index + strlen($base));
276: $protocol = "http";
277: if(strpos($_SERVER['SCRIPT_URI'], "https") !== FALSE) $protocol = "https";
278:
279: return $protocol . "://" . $_SERVER['SERVER_NAME'] . $url;
280: }
281: }
282: }
283:
284: 285: 286: 287:
288: public function getUploads()
289: {
290: return new Uploads($this->module->getDirectory(), $this->config);
291: }
292:
293: 294: 295: 296:
297: public function setContentType($type)
298: {
299: $this->response->setContentType($type);
300: }
301:
302: public function log($message, $level)
303: {
304: Events::fire('onLog', array('message' => $message, 'level' => $level));
305: }
306:
307: 308: 309: 310:
311: public function csrf()
312: {
313: return Events::fire('validateCSRF');
314: }
315:
316: 317: 318: 319:
320: public function generate_csrf()
321: {
322: if(is_module_enabled("csrf"))
323: {
324: return \CSRF::generate()->token;
325: }
326: }
327:
328: 329: 330: 331:
332: public function addMiddleware(Middleware $middleware)
333: {
334: $this->middleware[] = $middleware;
335: }
336:
337: }