pre-fill auth form with $me data
[anoweco.git] / www / www-header.php
1 <?php
2 require_once __DIR__ . '/../src/anoweco/autoload.php';
3
4 \Twig_Autoloader::register();
5
6 $loader = new \Twig_Loader_Filesystem(__DIR__ . '/../data/templates/');
7 $twig = new \Twig_Environment(
8     $loader,
9     array(
10         //'cache' => '/path/to/compilation_cache',
11         'debug' => true
12     )
13 );
14 //$twig->addExtension(new Twig_Extension_Debug());
15
16 function verifyParameter($givenParams, $paramName)
17 {
18     if (!isset($givenParams[$paramName])) {
19         error('"' . $paramName . '" parameter missing');
20     }
21     return $givenParams[$paramName];
22 }
23
24 function verifyUrlParameter($givenParams, $paramName)
25 {
26     verifyParameter($givenParams, $paramName);
27     $url = parse_url($givenParams[$paramName]);
28     if (!isset($url['scheme'])) {
29         error('Invalid URL in "' . $paramName . '" parameter: scheme missing');
30     }
31     if (!isset($url['host'])) {
32         error('Invalid URL in "' . $paramName . '" parameter: host missing');
33     }
34
35     return $givenParams[$paramName];
36 }
37
38 function getOptionalParameter($givenParams, $paramName, $default)
39 {
40     if (!isset($givenParams[$paramName])) {
41         return $default;
42     }
43     return $givenParams[$paramName];
44 }
45
46 /**
47  * Send out an error
48  *
49  * @param string $status      HTTP status code line
50  * @param string $code        One of the allowed status types:
51  *                            - forbidden
52  *                            - insufficient_scope
53  *                            - invalid_request
54  *                            - not_found
55  * @param string $description
56  */
57 function error($description, $status = 'HTTP/1.0 400 Bad Request')
58 {
59     header($status);
60     header('Content-Type: text/plain');
61     echo $description . "\n";
62     exit(1);
63 }
64
65 function render($tplname, $vars = array(), $return = false)
66 {
67     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
68
69     if ($return) {
70         return $template->render($vars);
71     } else {
72         echo $template->render($vars);
73     }
74 }
75 ?>