RootControllerDelegate is no longer a WeakInterface.
[bugdar.git] / admin / index.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2002-2013 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 namespace bugdar\admin;
23
24 use \bugdar;
25 use \hoplite\http;
26 use \hoplite\views;
27
28 chdir('../');
29 require_once './includes/init.php';
30
31 require_once HOPLITE_ROOT . '/http/output_filter.php';
32 require_once HOPLITE_ROOT . '/http/response_code.php';
33 require_once HOPLITE_ROOT . '/http/root_controller.php';
34 require_once HOPLITE_ROOT . '/http/url_map.php';
35 require_once HOPLITE_ROOT . '/views/pdo_cache_backend.php';
36 require_once HOPLITE_ROOT . '/views/template_loader.php';
37
38 require_once BUGDAR_ROOT . '/admin/login.php';
39
40 class FrontController implements http\RootControllerDelegate
41 {
42 /** @var \hoplite\http\RootController */
43 private $controller = NULL;
44
45 public function __construct(http\RootController $controller)
46 {
47 $this->controller = $controller;
48 }
49
50 public function LoginPage()
51 {
52 $this->controller->InvokeAction(new LoginAction($this->controller));
53 }
54
55 // RootControllerDelegate:
56
57 public function OnInitialRequest(http\Request $request, http\Response $response)
58 {
59 $response->data['language'] = fetch_user_language();
60 }
61
62 public function WillRouteRequest(http\Request $request, http\Response $response)
63 {
64 // TODO(port): Write a new cookie function.
65 global $funct;
66
67 $cookie = COOKIE_PREFIX . 'adminsession';
68
69 if (can_perform('canadminpanel')) {
70 $stmt = bugdar::$db->Prepare("SELECT * FROM ". TABLE_PREFIX . "adminsession WHERE sessionid = ?");
71 $stmt->Execute(array(bugdar::$input->InputClean('c', $cookie, http\Input::TYPE_STR)));
72 $session = $stmt->FetchObject();
73
74 if ($session && $session->userid == bugdar::$user['userid'] && $session->dateline >= TIMENOW - 3600) {
75 // Renew the cookie.
76 $funct->cookie($cookie, $session->sessionid, false);
77 return;
78 }
79 }
80
81 $funct->cookie($cookie, NULL);
82 $this->LoginPage();
83 }
84
85 public function WillInvokeAction(http\Action $action, http\Request $request, http\Response $response)
86 {
87 $templates = array('admin_header', 'admin_footer');
88 if ($action instanceof TemplatePreCaching)
89 $templates = array_merge($templates, $action->TemplateSet());
90 views\TemplateLoader::GetInstance()->PreCache($templates);
91 }
92
93 public function DidInvokeAction(http\Action $action, http\Request $request, http\Response $response) {}
94
95 public function WillStop(http\Request $request, http\Response $response) {}
96 }
97
98 // Actions can implement this interface to have their required templates loaded
99 // with the common templates.
100 interface TemplatePreCaching
101 {
102 // Return an array of template names to cache.
103 public function TemplateSet();
104 }
105
106 $controller = new http\RootController($GLOBALS);
107 $controller->set_delegate(new FrontController($controller));
108
109 $url_map = new http\UrlMap($controller);
110 $url_map->set_file_loader(function($name, $value) {
111 require_once BUGDAR_ROOT . '/admin/' . $value;
112 return 'bugdar\\admin\\' . $name . 'Action';
113 });
114 $controller->set_url_map($url_map);
115
116 $url_map->set_map(array(
117 '' => 'home',
118 'fields/{action}' => 'fields',
119 'settings' => 'settings',
120 ));
121
122 $output_filter = new http\OutputFilter($controller);
123 $controller->set_output_filter($output_filter);
124
125 $tpl_loader = views\TemplateLoader::GetInstance();
126 $tpl_loader->set_template_path(BUGDAR_ROOT . '/admin/templates/%s.tpl');
127 $tpl_loader->set_cache_backend(new views\PDOCacheBackend(
128 Bugdar::$db, // PDO object.
129 TABLE_PREFIX . 'template', // Database table.
130 'filename', // Name column.
131 'template', // Data column.
132 'timestamp' // Modified time column.
133 ));
134
135 error_reporting(E_ALL);
136
137 $controller->Run();