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