search paging
[phorkie.git] / www / www-header.php
1 <?php
2 namespace phorkie;
3 set_include_path(
4     __DIR__ . '/../src/'
5     . PATH_SEPARATOR . get_include_path()
6 );
7 spl_autoload_register(
8     function ($class) {
9         $file = str_replace(array('\\', '_'), '/', $class) . '.php';
10         $hdl = @fopen($file, 'r', true);
11         if ($hdl !== false) {
12             fclose($hdl);
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             exit();
29         }
30
31         render(
32             'exception',
33             array(
34                 'exception' => $e,
35                 'debug'     => $GLOBALS['phorkie']['cfg']['debug']
36             )
37         );
38         exit();
39     }
40 );
41
42 require_once __DIR__ . '/../data/config.default.php';
43 if (file_exists(__DIR__ . '/../data/config.php')) {
44     require_once __DIR__ . '/../data/config.php';
45 }
46 if ($GLOBALS['phorkie']['cfg']['setupcheck']) {
47     SetupCheck::run();
48 }
49 \Twig_Autoloader::register();
50
51 $loader = new \Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
52 $twig = new \Twig_Environment(
53     $loader,
54     array(
55         //'cache' => '/path/to/compilation_cache',
56         'debug' => true
57     )
58 );
59
60 function render($tplname, $vars)
61 {
62     $vars['css'] = $GLOBALS['phorkie']['cfg']['css'];
63     $vars['title'] = $GLOBALS['phorkie']['cfg']['title'];
64     $vars['topbar'] = $GLOBALS['phorkie']['cfg']['topbar'];
65
66     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
67     echo $template->render($vars);
68 }
69 function redirect($target)
70 {
71     header('Location: ' . $target);
72     exit();
73 }
74 ?>