X-Git-Url: https://git.cweiske.de/anoweco.git/blobdiff_plain/ccb7bb3c75555c01e7dbc78e0b971abc86f3a59d..HEAD:/www/www-header.php diff --git a/www/www-header.php b/www/www-header.php index a221ca5..ec7e8b3 100644 --- a/www/www-header.php +++ b/www/www-header.php @@ -1,10 +1,11 @@ '/path/to/compilation_cache', @@ -13,9 +14,58 @@ $twig = new \Twig_Environment( ); //$twig->addExtension(new Twig_Extension_Debug()); +function verifyParameter($givenParams, $paramName) +{ + if (!isset($givenParams[$paramName])) { + error('"' . $paramName . '" parameter missing'); + } + return $givenParams[$paramName]; +} + +function verifyUrlParameter($givenParams, $paramName) +{ + verifyParameter($givenParams, $paramName); + $url = parse_url($givenParams[$paramName]); + if (!isset($url['scheme'])) { + error('Invalid URL in "' . $paramName . '" parameter: scheme missing'); + } + if (!isset($url['host'])) { + error('Invalid URL in "' . $paramName . '" parameter: host missing'); + } + + return $givenParams[$paramName]; +} + +function getOptionalParameter($givenParams, $paramName, $default) +{ + if (!isset($givenParams[$paramName])) { + return $default; + } + return $givenParams[$paramName]; +} + +/** + * Send out an error + * + * @param string $status HTTP status code line + * @param string $code One of the allowed status types: + * - forbidden + * - insufficient_scope + * - invalid_request + * - not_found + * @param string $description + */ +function error($description, $status = 'HTTP/1.0 400 Bad Request') +{ + header($status); + header('Content-Type: text/plain'); + echo $description . "\n"; + exit(1); +} + function render($tplname, $vars = array(), $return = false) { - $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm'); + $template = $GLOBALS['twig']->load($tplname . '.htm'); if ($return) { return $template->render($vars); @@ -23,4 +73,13 @@ function render($tplname, $vars = array(), $return = false) echo $template->render($vars); } } -?> \ No newline at end of file + +/** + * No trailing slash + */ +function getBaseUrl() +{ + return $_SERVER['REQUEST_SCHEME'] + . '://' . $_SERVER['HTTP_HOST']; +} +?>