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