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