authentication works
[anoweco.git] / www / www-header.php
index a221ca50566ce321206bae7c63c7e63a88ed5d90..1b4a6858bd6683d6ef6528b4cd80301c05e71521 100644 (file)
@@ -13,6 +13,55 @@ $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');