use stream_resolve_include_path
[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         if (stream_resolve_include_path($file)) {
13             require $file;
14         }
15     }
16 );
17 set_exception_handler(
18     function ($e) {
19         if ($e instanceof Exception) {
20             header('HTTP/1.0 ' . $e->httpStatusCode);
21         } else {
22             header('HTTP/1.0 500 Internal server error');
23         }
24
25         if (!isset($GLOBALS['twig'])) {
26             echo '<h1>Exception</h1>';
27             echo '<p>' . $e->getMessage() . '</p>';
28             echo "\n";
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
48 // Set/Get git commit session variables
49 $_SESSION['ipaddr'] = $_SERVER['REMOTE_ADDR'];
50 if (!isset($_SESSION['name'])) {
51     $_SESSION['name'] = $GLOBALS['phorkie']['auth']['anonymousName'];
52 }
53 if (!isset($_SESSION['email'])) {
54     $_SESSION['email'] = $GLOBALS['phorkie']['auth']['anonymousEmail'];
55 }
56
57 \Twig_Autoloader::register();
58
59 $loader = new \Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
60 $twig = new \Twig_Environment(
61     $loader,
62     array(
63         //'cache' => '/path/to/compilation_cache',
64         'debug' => true
65     )
66 );
67 $twig->addFunction('ntext', new \Twig_Function_Function('\phorkie\ntext'));
68 function ntext($value, $singular, $plural)
69 {
70     if (abs($value) == 1) {
71         return sprintf($singular, $value);
72     }
73     return sprintf($plural, $value);
74 }
75 //$twig->addExtension(new \Twig_Extension_Debug());
76
77 if (!isset($noSecurityCheck) || $noSecurityCheck !== true) {
78     require __DIR__ . '/www-security.php';
79 }
80
81 function render($tplname, $vars = array())
82 {
83     $vars['baseurl'] = '/';
84     if (!empty($GLOBALS['phorkie']['cfg']['baseurl'])) {
85         $vars['baseurl'] = $GLOBALS['phorkie']['cfg']['baseurl'];
86     }
87     $vars['css'] = $GLOBALS['phorkie']['cfg']['css'];
88     $vars['iconpng'] = $GLOBALS['phorkie']['cfg']['iconpng'];
89     $vars['title'] = $GLOBALS['phorkie']['cfg']['title'];
90     $vars['topbar'] = $GLOBALS['phorkie']['cfg']['topbar'];
91     if (isset($_SESSION['identity'])) {
92         $vars['identity'] = $_SESSION['identity'];
93         $vars['name'] = $_SESSION['name'];
94         $vars['email'] = $_SESSION['email'];
95     }
96     $vars['db'] = new Database();
97     if (!isset($vars['htmlhelper'])) {
98         $vars['htmlhelper'] = new HtmlHelper();
99     }
100
101     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
102     echo $template->render($vars);
103 }
104 function redirect($target)
105 {
106     header('Location: ' . $target);
107     exit();
108 }
109 ?>