fix rewrite map loading
[phorkie.git] / www / www-header.php
1 <?php
2 namespace phorkie;
3 error_reporting(error_reporting() & ~E_STRICT);
4 session_set_cookie_params(14 * 86400);//2 weeks session expiry time
5 session_start();
6
7 require_once __DIR__ . '/../src/phorkie/autoload.php';
8 set_exception_handler(
9     function ($e) {
10         if ($e instanceof Exception) {
11             header('HTTP/1.0 ' . $e->httpStatusCode);
12         } else {
13             header('HTTP/1.0 500 Internal server error');
14         }
15
16         if (!isset($GLOBALS['twig'])) {
17             echo '<h1>Exception</h1>';
18             echo '<p>' . $e->getMessage() . '</p>';
19             echo "\n";
20             exit();
21         }
22
23         render(
24             'exception',
25             array(
26                 'exception' => $e,
27                 'debug'     => $GLOBALS['phorkie']['cfg']['debug']
28             )
29         );
30         exit();
31     }
32 );
33
34 require_once __DIR__ . '/../data/config.default.php';
35 $pharFile = \Phar::running();
36 if ($pharFile == '') {
37     $cfgFilePath = __DIR__ . '/../data/config.php';
38 } else {
39     //remove phar:// from the path
40     $cfgFilePath = substr($pharFile, 7) . '.config.php';
41 }
42 $GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = false;
43 if (file_exists($cfgFilePath)) {
44     $GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = true;
45     require_once $cfgFilePath;
46 }
47
48 if ($GLOBALS['phorkie']['cfg']['baseurl'] === null) {
49     $GLOBALS['phorkie']['cfg']['baseurl'] = Tools::detectBaseUrl();
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->addFunction('ntext', new \Twig_Function_Function('\phorkie\ntext'));
72 function ntext($value, $singular, $plural)
73 {
74     if (abs($value) == 1) {
75         return sprintf($singular, $value);
76     }
77     return sprintf($plural, $value);
78 }
79 //$twig->addExtension(new \Twig_Extension_Debug());
80
81 if (!isset($noSecurityCheck) || $noSecurityCheck !== true) {
82     require __DIR__ . '/www-security.php';
83 }
84
85 function render($tplname, $vars = array())
86 {
87     $vars['baseurl'] = '/';
88     if (!empty($GLOBALS['phorkie']['cfg']['baseurl'])) {
89         $vars['baseurl'] = $GLOBALS['phorkie']['cfg']['baseurl'];
90     }
91     $vars['css'] = $GLOBALS['phorkie']['cfg']['css'];
92     $vars['iconpng'] = $GLOBALS['phorkie']['cfg']['iconpng'];
93     $vars['title'] = $GLOBALS['phorkie']['cfg']['title'];
94     $vars['topbar'] = $GLOBALS['phorkie']['cfg']['topbar'];
95     if (isset($_SESSION['identity'])) {
96         $vars['identity'] = $_SESSION['identity'];
97         $vars['name'] = $_SESSION['name'];
98         $vars['email'] = $_SESSION['email'];
99     }
100     $vars['db'] = new Database();
101     if (!isset($vars['htmlhelper'])) {
102         $vars['htmlhelper'] = new HtmlHelper();
103     }
104
105     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
106     echo $template->render($vars);
107 }
108 function redirect($target)
109 {
110     header('Location: ' . $target);
111     exit();
112 }
113 ?>