echo str_replace(array("%DEFAULT_CHARSET%", "%ENTRADA_URL%", "%TITLE%", "%BODY%"), array(DEFAULT_CHARSET, ENTRADA_URL, $TITLE, $BODY), $template_html);
         }
         exit;
     } else {
         if ($result["release_until"] && $result["release_until"] < time()) {
             $TITLE = "Not Available";
             $BODY = display_notice(array("The link that you are trying to access was only available until <strong>" . date(DEFAULT_DATE_FORMAT, $result["release_until"]) . "</strong>.<br /><br />For further information or to contact a teacher, please see the <a href=\"" . ENTRADA_URL . "/events?id=" . $result["event_id"] . "\" style=\"font-weight: bold\">event page</a>."));
             $template_html = fetch_template("global/external");
             if ($template_html) {
                 echo str_replace(array("%DEFAULT_CHARSET%", "%ENTRADA_URL%", "%TITLE%", "%BODY%"), array(DEFAULT_CHARSET, ENTRADA_URL, $TITLE, $BODY), $template_html);
             }
             exit;
         } else {
             $db->Execute("UPDATE `event_links` SET `accesses` = '" . ($accesses + 1) . "' WHERE `elink_id` = " . $db->qstr($ELINK_ID));
             add_statistic("events", "link_access", "link_id", $ELINK_ID);
             if ($result["proxify"] == "1" && check_proxy("default") && isset($PROXY_URLS["default"]["active"]) && $PROXY_URLS["default"]["active"] != "") {
                 header("Location: " . $PROXY_URLS["default"]["active"] . $result["link"]);
             } else {
                 header("Location: " . $result["link"]);
             }
             exit;
         }
     }
 } else {
     $TITLE = "Not Authorized";
     $BODY = display_notice(array("The link that you are trying to access is only accessible by authorized users."));
     $template_html = fetch_template("global/external");
     if ($template_html) {
         echo str_replace(array("%DEFAULT_CHARSET%", "%ENTRADA_URL%", "%TITLE%", "%BODY%"), array(DEFAULT_CHARSET, ENTRADA_URL, $TITLE, $BODY), $template_html);
     }
     exit;
    $response->setJsonContent(array("ready" => check_memcached($id)));
    return $response;
});
$app->get("/status/zebra/{id}", function ($id) {
    # Start building the response.
    $response = new Response();
    # Fill the response with the return value of the function that checks
    # the status of the component.
    $response->setStatusCode(200, "OK");
    $response->setJsonContent(array("ready" => check_zebra($id)));
    return $response;
});
$app->get("/status/koha/{id}", function ($id) {
    # Start building the response.
    $response = new Response();
    # Fill the response with the return value of the function that checks
    # the status of the component.
    $response->setStatusCode(200, "OK");
    $response->setJsonContent(array("ready" => check_koha($id)));
    return $response;
});
$app->get("/status/proxy/{id}", function ($id) {
    # Start building the response.
    $response = new Response();
    # Fill the response with the return value of the function that checks
    # the status of the component.
    $response->setStatusCode(200, "OK");
    $response->setJsonContent(array("ready" => check_proxy($id)));
    return $response;
});
$app->handle();
        if (count($headers)) {
            foreach ($headers as $header) {
                if (isset($_SERVER[$header])) {
                    $proxy = true;
                    break;
                }
            }
        }
        // Do a port scan
        if (!$proxy && count($ports)) {
            foreach ($ports as $port) {
                if ($test = @fsockopen($userip, $port, $errno, $errstr, 0.5)) {
                    fclose($test);
                    $proxy = true;
                    break;
                }
            }
        }
        // Delete older result and insert new
        $proxy = intval($proxy);
        $db_delete_ip = sprintf("DELETE FROM `proxyblock` WHERE `ip`='%s'", mysql_real_escape_string($userip));
        $db_insert_ip = sprintf("INSERT INTO `proxyblock` VALUES ('%s','{$proxy}',NOW())", mysql_real_escape_string($userip));
        mysql_query($db_delete_ip) or die(mysql_error());
        mysql_query($db_insert_ip) or die(mysql_error());
    }
    // Return result
    return $proxy;
}
if (check_proxy()) {
    die("<title>403: Forbidden</title>Oops... A proxy");
}
    exit;
} elseif (!$_SESSION["isAuthorized"]) {
    header("Location: " . ENTRADA_URL);
    exit;
} elseif (!$ENTRADA_ACL->amIAllowed('library', 'read')) {
    $ONLOAD[] = "setTimeout('window.location=\\'" . ENTRADA_URL . "/" . $MODULE . "\\'', 15000)";
    $ERROR++;
    $ERRORSTR[] = "Your account does not have the permissions required to use this module.<br /><br />If you believe you are receiving this message in error please contact <a href=\"mailto:" . html_encode($AGENT_CONTACTS["administrator"]["email"]) . "\">" . html_encode($AGENT_CONTACTS["administrator"]["name"]) . "</a> for assistance.";
    echo display_error();
    application_log("error", "Group [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["group"] . "] and role [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["role"] . "] do not have access to this module [" . $MODULE . "]");
} else {
    /**
     * SEE: https://developer.entrada-project.org/ticket/30
     *
     * @todo This needs to be rewritten to support multiple proxy servers. Basically
     * what I imagine is that when a user clicks the "Library" tab (or any other
     * proxified link within the system) they are directed to a /proxy page that
     * allows them to choose which of the available proxy servers they are going to
     * be directed through.
     *
     * It should give them the option of choosing "Don't ask me again." and could
     * also provide a list of proxied URLs they have visted through the system.
     */
    if (check_proxy("library") || isset($_GET["override"])) {
        header("Location: " . $PROXY_URLS["library"]["active"]);
        exit;
    } else {
        header("Location: " . $PROXY_URLS["library"]["inactive"]);
        exit;
    }
}