allow people to logout and login in secure mode
[phorkie.git] / www / www-header.php
1 <?php
2 namespace phorkie;
3 if (session_id() == "") {
4     session_start();
5 }
6
7 set_include_path(
8     __DIR__ . '/../src/'
9     . PATH_SEPARATOR . get_include_path()
10 );
11 spl_autoload_register(
12     function ($class) {
13         $file = str_replace(array('\\', '_'), '/', $class) . '.php';
14         $hdl = @fopen($file, 'r', true);
15         if ($hdl !== false) {
16             fclose($hdl);
17             require $file;
18         }
19     }
20 );
21 set_exception_handler(
22     function ($e) {
23         if ($e instanceof Exception) {
24             header('HTTP/1.0 ' . $e->httpStatusCode);
25         } else {
26             header('HTTP/1.0 500 Internal server error');
27         }
28
29         if (!isset($GLOBALS['twig'])) {
30             echo '<h1>Exception</h1>';
31             echo '<p>' . $e->getMessage() . '</p>';
32             exit();
33         }
34
35         render(
36             'exception',
37             array(
38                 'exception' => $e,
39                 'debug'     => $GLOBALS['phorkie']['cfg']['debug']
40             )
41         );
42         exit();
43     }
44 );
45
46 require_once __DIR__ . '/../data/config.default.php';
47 if (file_exists(__DIR__ . '/../data/config.php')) {
48     require_once __DIR__ . '/../data/config.php';
49 }
50 if ($GLOBALS['phorkie']['cfg']['setupcheck']) {
51     SetupCheck::run();
52 }
53
54 // Set/Get git commit session variables
55 $_SESSION['ipaddr'] = $_SERVER['REMOTE_ADDR'];
56 if (!isset($_SESSION['name'])) {
57     $_SESSION['name'] = $GLOBALS['phorkie']['auth']['anonymousName'];
58 }
59 if (!isset($_SESSION['email'])) {
60     $_SESSION['email'] = $GLOBALS['phorkie']['auth']['anonymousEmail'];
61 }
62
63 \Twig_Autoloader::register();
64
65 $loader = new \Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
66 $twig = new \Twig_Environment(
67     $loader,
68     array(
69         //'cache' => '/path/to/compilation_cache',
70         'debug' => true
71     )
72 );
73 //$twig->addExtension(new \Twig_Extension_Debug());
74
75 if (!isset($noSecurityCheck) || $noSecurityCheck !== true) {
76     require __DIR__ . '/www-security.php';
77 }
78
79 function render($tplname, $vars = array())
80 {
81     $vars['css'] = $GLOBALS['phorkie']['cfg']['css'];
82     $vars['title'] = $GLOBALS['phorkie']['cfg']['title'];
83     $vars['topbar'] = $GLOBALS['phorkie']['cfg']['topbar'];
84     if (isset($_SESSION['identity'])) {
85         $vars['identity'] = $_SESSION['identity'];
86         $vars['name'] = $_SESSION['name'];
87         $vars['email'] = $_SESSION['email'];
88     }
89     $vars['db'] = new Database();
90
91     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
92     echo $template->render($vars);
93 }
94 function redirect($target)
95 {
96     header('Location: ' . $target);
97     exit();
98 }
99 ?>