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