urlshortener/expand.php

90 lines
2.6 KiB
PHP

<?php
include_once(dirname(__FILE__).'/./lib/functions.inc.php');
include_once(dirname(__FILE__).'/./lib/UrlShortener.class.php');
include_once(dirname(__FILE__).'/./lib/Image/QRCode.php');
$urlShortener = new UrlShortener();
$shortid = getRequestParameter("id");
$shortext = getRequestParameter("ext");
if (str_startswith($shortext, ".") == true) {
$shortext = strtolower(substr($shortext, 1));
}
$calledUrl = SERVICE_BASE_URL . $shortid;
$expandedEntry = $urlShortener->expandUrl($shortid, true);
$warnCookie = false;
if (isset($_COOKIE['warn']) == true) {
$warnCookie = ($_COOKIE['warn'] == "true");
}
$resultCode = -1;
$resultMessage = NULL;
$resultUrl = NULL;
if ($expandedEntry == NULL) {
$resultCode = 404;
$resultMessage = "NOT_FOUND";
$resultUrl = NULL;
} else {
$resultCode = 200;
$resultMessage = "OK";
$resultUrl = $expandedEntry['url'];
}
if (($shortext == "text") || ($shortext == "txt") || ($shortext == "plain")) {
header("Content-type: text/plain");
echo $resultUrl;
exit;
}
if ($shortext == "json") {
// prevent caching
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo '{ "status_code": ' . $resultCode . ', "data": { "expand": [ { "short_url": "' . $calledUrl . '", "global_hash": "' . $shortid . '", "long_url": "' . $resultUrl . '", "user_hash": "' . $shortid . '" } ] }, "status_text": "' . $resultMessage . '" }';
exit;
}
if ($shortext == "xml") {
header("Content-type: application/xml");
echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>' . "\n";
echo '<response>' . "\n";
echo '<status_code>' . $resultCode . '</status_code>' . "\n";
echo '<status_txt>' . $resultMessage . '</status_txt>' . "\n";
echo '<data>' . "\n";
if ($resultCode == 200) {
echo '<entry>' . "\n";
echo ' <short_url>' . $calledUrl . '</short_url>' . "\n";
echo ' <long_url>' . $resultUrl . '</long_url>' . "\n";
echo ' <user_hash>' . $shortid . '</user_hash>' . "\n";
echo ' <global_hash>' . $shortid . '</global_hash>' . "\n";
echo '</entry>' . "\n";
}
echo '</data>' . "\n";
echo '</response>';
exit;
}
if (($shortext == "qrcode") || ($shortext == "qr")) {
header("Content-type: image/png");
$qr = new Image_QRCode();
$codeData = $calledUrl;
$qr->makeCode($codeData);
exit;
}
if ($resultCode == 200) {
if ($warnCookie == true) {
include('expand_warnpage.php');
} else {
header("Location: " . $resultUrl);
}
} else {
include('expand_notfound.php');
}
?>