detect baseurl automatically, load config file from .phar
[phorkie.git] / www / www-header.php
1 <?php
2 namespace phorkie;
3 session_start();
4
5 require_once __DIR__ . '/../src/phorkie/autoload.php';
6 set_exception_handler(
7     function ($e) {
8         if ($e instanceof Exception) {
9             header('HTTP/1.0 ' . $e->httpStatusCode);
10         } else {
11             header('HTTP/1.0 500 Internal server error');
12         }
13
14         if (!isset($GLOBALS['twig'])) {
15             echo '<h1>Exception</h1>';
16             echo '<p>' . $e->getMessage() . '</p>';
17             echo "\n";
18             exit();
19         }
20
21         render(
22             'exception',
23             array(
24                 'exception' => $e,
25                 'debug'     => $GLOBALS['phorkie']['cfg']['debug']
26             )
27         );
28         exit();
29     }
30 );
31
32 require_once __DIR__ . '/../data/config.default.php';
33 $pharFile = \Phar::running();
34 if ($pharFile == '') {
35     $cfgFilePath = __DIR__ . '/../data/config.php';
36 } else {
37     //remove phar:// from the path
38     $cfgFilePath = substr($pharFile, 7) . '.config.php';
39 }
40 $GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = false;
41 if (file_exists($cfgFilePath)) {
42     $GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = true;
43     require_once $cfgFilePath;
44 }
45
46 if ($GLOBALS['phorkie']['cfg']['baseurl'] === null) {
47     $GLOBALS['phorkie']['cfg']['baseurl'] = Tools::detectBaseUrl();
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 ?>