Set up templating in the admin/ section using hoplite\views.
[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
27 chdir('../');
28 require_once './includes/init.php';
29
30 require_once HOPLITE_ROOT . '/http/output_filter.php';
31 require_once HOPLITE_ROOT . '/http/response_code.php';
32 require_once HOPLITE_ROOT . '/http/root_controller.php';
33 require_once HOPLITE_ROOT . '/http/url_map.php';
34 require_once HOPLITE_ROOT . '/views/pdo_cache_backend.php';
35 require_once HOPLITE_ROOT . '/views/template_loader.php';
36
37 /**
38 * @implements \hoplite\http\RootControllerDelegate
39 */
40 class FrontController
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 WillRouteRequest(http\Request $request, http\Response $response)
51 {
52 // TODO(port): Write a new cookie function.
53 global $funct;
54
55 $cookie = COOKIE_PREFIX . 'adminsession';
56
57 if (can_perform('canadminpanel')) {
58 $stmt = bugdar::$db->Prepare("SELECT * FROM ". TABLE_PREFIX . "adminsession WHERE sessionid = ?");
59 $stmt->Execute(array(bugdar::$input->InputClean('c', $cookie, http\Input::TYPE_STR)));
60 $session = $stmt->FetchObject();
61
62 if ($session && $session->userid == bugdar::$user['userid'] && $session->dateline >= TIMENOW - 3600) {
63 // Renew the cookie.
64 $funct->cookie($cookie, $session->sessionid, false);
65 return;
66 }
67 }
68
69 $funct->cookie($cookie, NULL);
70 $this->LoginPage();
71 }
72
73 public function LoginPage()
74 {
75 $this->controller->StopWithCode(http\ResponseCode::FORBIDDEN);
76 }
77 }
78
79 $controller = new http\RootController($GLOBALS);
80 $controller->set_delegate(new FrontController($controller));
81
82 $url_map = new http\UrlMap($controller);
83 $url_map->set_file_loader(function($name, $value) {
84 require_once BUGDAR_ROOT . '/admin/' . $value;
85 return 'bugdar\\admin\\' . $name . 'Action';
86 });
87 $controller->set_url_map($url_map);
88
89 $url_map->set_map(array(
90 '' => 'home',
91 'fields' => 'fields',
92 ));
93
94 $output_filter = new http\OutputFilter($controller);
95 $controller->set_output_filter($output_filter);
96
97 $tpl_loader = \hoplite\views\TemplateLoader::GetInstance();
98 $tpl_loader->set_template_path(BUGDAR_ROOT . '/admin/templates/%s.tpl');
99 $tpl_loader->set_cache_backend(new \hoplite\views\PDOCacheBackend(
100 Bugdar::$db, // PDO object.
101 TABLE_PREFIX . 'template', // Database table.
102 'filename', // Name column.
103 'template', // Data column.
104 'timestamp' // Modified time column.
105 ));
106
107 $controller->Run();