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