Add the current language information to the response data.
[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 /**
39 * @implements \hoplite\http\RootControllerDelegate
40 */
41 class FrontController
42 {
43 /** @var \hoplite\http\RootController */
44 private $controller = NULL;
45
46 public function __construct(http\RootController $controller)
47 {
48 $this->controller = $controller;
49 }
50
51 public function OnInitialRequest(http\Request $request, http\Response $response)
52 {
53 $response->data['language'] = fetch_user_language();
54 }
55
56 public function WillRouteRequest(http\Request $request, http\Response $response)
57 {
58 // TODO(port): Write a new cookie function.
59 global $funct;
60
61 $cookie = COOKIE_PREFIX . 'adminsession';
62
63 if (can_perform('canadminpanel')) {
64 $stmt = bugdar::$db->Prepare("SELECT * FROM ". TABLE_PREFIX . "adminsession WHERE sessionid = ?");
65 $stmt->Execute(array(bugdar::$input->InputClean('c', $cookie, http\Input::TYPE_STR)));
66 $session = $stmt->FetchObject();
67
68 if ($session && $session->userid == bugdar::$user['userid'] && $session->dateline >= TIMENOW - 3600) {
69 // Renew the cookie.
70 $funct->cookie($cookie, $session->sessionid, false);
71 return;
72 }
73 }
74
75 $funct->cookie($cookie, NULL);
76 $this->LoginPage();
77 }
78
79 public function LoginPage()
80 {
81 $this->controller->StopWithCode(http\ResponseCode::FORBIDDEN);
82 }
83
84 public function WillInvokeAction(http\Action $action, http\Request $request, http\Response $response)
85 {
86 $templates = array('admin_header', 'admin_footer');
87 if ($action instanceof TemplatePreCaching)
88 $templates = array_merge($templates, $action->TemplateSet());
89 views\TemplateLoader::GetInstance()->PreCache($templates);
90 }
91 }
92
93 // Actions can implement this interface to have their required templates loaded
94 // with the common templates.
95 interface TemplatePreCaching
96 {
97 // Return an array of template names to cache.
98 public function TemplateSet();
99 }
100
101 $controller = new http\RootController($GLOBALS);
102 $controller->set_delegate(new FrontController($controller));
103
104 $url_map = new http\UrlMap($controller);
105 $url_map->set_file_loader(function($name, $value) {
106 require_once BUGDAR_ROOT . '/admin/' . $value;
107 return 'bugdar\\admin\\' . $name . 'Action';
108 });
109 $controller->set_url_map($url_map);
110
111 $url_map->set_map(array(
112 '' => 'home',
113 'fields' => 'fields',
114 ));
115
116 $output_filter = new http\OutputFilter($controller);
117 $controller->set_output_filter($output_filter);
118
119 $tpl_loader = views\TemplateLoader::GetInstance();
120 $tpl_loader->set_template_path(BUGDAR_ROOT . '/admin/templates/%s.tpl');
121 $tpl_loader->set_cache_backend(new views\PDOCacheBackend(
122 Bugdar::$db, // PDO object.
123 TABLE_PREFIX . 'template', // Database table.
124 'filename', // Name column.
125 'template', // Data column.
126 'timestamp' // Modified time column.
127 ));
128
129 $controller->Run();