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