d76374212b3c5fb879e32dece99430a1919be4ef
[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             echo "\n";
31             exit();
32         }
33
34         render(
35             'exception',
36             array(
37                 'exception' => $e,
38                 'debug'     => $GLOBALS['phorkie']['cfg']['debug']
39             )
40         );
41         exit();
42     }
43 );
44
45 require_once __DIR__ . '/../data/config.default.php';
46 if (file_exists(__DIR__ . '/../data/config.php')) {
47     require_once __DIR__ . '/../data/config.php';
48 }
49 if ($GLOBALS['phorkie']['cfg']['setupcheck']) {
50     SetupCheck::run();
51 }
52
53 // Set/Get git commit session variables
54 $_SESSION['ipaddr'] = $_SERVER['REMOTE_ADDR'];
55 if (!isset($_SESSION['name'])) {
56     $_SESSION['name'] = $GLOBALS['phorkie']['auth']['anonymousName'];
57 }
58 if (!isset($_SESSION['email'])) {
59     $_SESSION['email'] = $GLOBALS['phorkie']['auth']['anonymousEmail'];
60 }
61
62 \Twig_Autoloader::register();
63
64 $loader = new \Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
65 $twig = new \Twig_Environment(
66     $loader,
67     array(
68         //'cache' => '/path/to/compilation_cache',
69         'debug' => true
70     )
71 );
72 $twig->addFunction('ntext', new \Twig_Function_Function('\phorkie\ntext'));
73 function ntext($value, $singular, $plural)
74 {
75     if (abs($value) == 1) {
76         return sprintf($singular, $value);
77     }
78     return sprintf($plural, $value);
79 }
80 //$twig->addExtension(new \Twig_Extension_Debug());
81
82 if (!isset($noSecurityCheck) || $noSecurityCheck !== true) {
83     require __DIR__ . '/www-security.php';
84 }
85
86 function render($tplname, $vars = array())
87 {
88     $vars['baseurl'] = '/';
89     if (!empty($GLOBALS['phorkie']['cfg']['baseurl'])) {
90         $vars['baseurl'] = $GLOBALS['phorkie']['cfg']['baseurl'];
91     }
92     $vars['css'] = $GLOBALS['phorkie']['cfg']['css'];
93     $vars['iconpng'] = $GLOBALS['phorkie']['cfg']['iconpng'];
94     $vars['title'] = $GLOBALS['phorkie']['cfg']['title'];
95     $vars['topbar'] = $GLOBALS['phorkie']['cfg']['topbar'];
96     if (isset($_SESSION['identity'])) {
97         $vars['identity'] = $_SESSION['identity'];
98         $vars['name'] = $_SESSION['name'];
99         $vars['email'] = $_SESSION['email'];
100     }
101     $vars['db'] = new Database();
102     if (!isset($vars['htmlhelper'])) {
103         $vars['htmlhelper'] = new HtmlHelper();
104     }
105
106     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
107     echo $template->render($vars);
108 }
109 function redirect($target)
110 {
111     header('Location: ' . $target);
112     exit();
113 }
114 ?>