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