urlshortener/index.php

197 lines
7.4 KiB
PHP

<?php
session_start();
include_once(dirname(__FILE__).'/./lib/functions.inc.php');
include_once(dirname(__FILE__).'/./lib/config.inc.php');
include_once(dirname(__FILE__).'/./lib/UrlShortener.class.php');
include_once(dirname(__FILE__).'/./lib/Captcha.class.php');
$msgError = NULL;
$msgLong = NULL;
$msgShort = NULL;
$urlShortener = new UrlShortener();
$captcha = new Captcha();
$longurl = getRequestParameter("longurl");
$plain = getRequestParameter("plain");
if (strlen($longurl) > 0) {
$captchaInput = getRequestParameter($_SESSION['CAPTCHA_FIELD']);
$captchaValid = false;
if (empty($captchaInput) == false) {
$captchaValid = $captcha->validateCaptcha($captchaInput);
}
if ($captchaValid == true) {
$shortId = $urlShortener->shortenUrl($longurl, $plain);
if ($shortId == NULL) {
$msgError = "The given URL could not be shortened.";
} else {
$msgShort = SERVICE_BASE_URL . $shortId;
$msgLong = $longurl;
$longurl = "";
}
} else {
$msgError = "Captcha invalid. Are sure you're not a bot?";
}
}
$displayCaptcha = $captcha->generateAndUseCaptcha();
$statsOverview = $urlShortener->getStatisticsOverview();
echo "<" . "?xml version=\"1.0\"?".">\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>URL Shortener</title>
<link rel="stylesheet" href="website/reset.css" type="text/css" />
<link rel="stylesheet" href="website/style.1.css" type="text/css" />
<script src="website/jquery-1.4.3.min.js" type="text/javascript"></script>
<script src="website/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
function enableOptionalForm() {
$("#optionalFormEnabler").hide();
$("#optionalFormData").show();
}
function preventDoubleSubmit() {
$("shortenUrlButton").disable();
}
var warningCookieEnabled = null;
$(document).ready(function() {
$('#warningcookie').show();
var warnCookieValue = $.cookie('warn');
if (warnCookieValue == null) {
warnCookieValue = "false";
}
warningCookieEnabled = warnCookieValue;
if (warnCookieValue == "true") {
$('#cookie_image').attr('src', 'website/16_accept.png');
$('#cookie_message').html('Warning enabled.');
$('#cookie_link').html('Click to disable warning.');
} else {
$('#cookie_image').attr('src', 'website/16_delete.png');
$('#cookie_message').html('Warning disabled.');
$('#cookie_link').html('Click to enable warning.');
}
});
function toggleWarning() {
if (warningCookieEnabled == "true") {
warningCookieEnabled = "false";
$.cookie('warn', 'false', { expires: 365, path: '/' });
$('#cookie_image').attr('src', 'website/16_delete.png');
$('#cookie_message').html('Warning disabled.');
$('#cookie_link').html('Click to enable warning.');
} else {
warningCookieEnabled = "true";
$.cookie('warn', 'true', { expires: 365, path: '/' });
$('#cookie_image').attr('src', 'website/16_accept.png');
$('#cookie_message').html('Warning enabled.');
$('#cookie_link').html('Click to disable warning.');
}
}
function show(elem) {
$('#'+elem).show();
var x = $('#'+elem).offset().top - 100; // 100 provides buffer in viewport
$('html,body').animate({scrollTop: x}, 500);
}
//-->
</script>
</head>
<body>
<div id="header" class="pagearea">
<div class="contentblock">
&nbsp;
</div>
</div>
<div id="caption" class="pagearea">
<div class="contentblock">
<div style="padding-top:170px;padding-right:30px;"><a href="index.php">URL Shortener</a></div>
</div>
</div>
<div id="main" class="pagearea">
<div class="contentblock">
<div style="padding:20px;">
<?php
if ($msgError != NULL) {
?>
<div class="errormsg"><?php echo $msgError; ?></div>
<?php
}
if (($msgLong != NULL) || ($msgShort != NULL)) {
?>
<div class="result contentwidth">
<div class="shorturl"><strong>Short URL:</strong> <?php echo $msgShort; ?></div>
<div class="longurl"><strong>Original URL:</strong> <?php echo $msgLong; ?></div>
</div>
<?php
}
?>
<form action="index.php" method="post">
<div class="formrow">URL to shorten</div>
<div class="formrow"><input type="text" name="longurl" value="<?php echo empty($longurl) ? "" : $longurl; ?>" class="biginput contentwidth" /></div>
<div class="formrow">Please answer: <?php echo $displayCaptcha; ?> <input type="text" name="<?php echo $_SESSION['CAPTCHA_FIELD']; ?>" value="" class="mediuminput" /></div>
<div class="formrow" id="optionalFormEnabler"><a href="javascript:void(0);" onclick="enableOptionalForm();">show optional information</a></div>
<div id="optionalFormData" style="display:none;">
<div class="formrow italic">optional: custum short id</div>
<div class="formrow"><input type="text" name="plain" value="" class="mediuminput" style="width:435px;" /></div>
</div>
<div class="formrow">
<button class="bigbutton" id="shortenUrlButton" name="shortenUrlButton" onclick="preventDoubleSubmit();" type="submit">Shorten URL!</button>
</div>
</form>
</div>
</div>
</div>
<div id="features" class="pagearea">
<div class="contentblock">
<div style="padding: 20px;">
Key features:
<ul>
<li>API and data structure compatible to urlShort (by mavrev.org)</li>
<li>extended API with different output formats like XML, JSON, plain text and even QR-Code images</li>
</ul>
<a href="javascript:void(0);" onclick="show('featureinfo');">see more infos...</a>
</div>
</div>
</div>
<div id="options" class="pagearea">
<div class="contentblock">
<div id="warningcookie" style="display:none;padding:20px;">
<img src="16_blank.png" id="cookie_image" alt="" /> <span id="cookie_message"></span> <a href="javascript:void(0);" id="cookie_link" onclick="toggleWarning();"></a>
<?php
if ($statsOverview != NULL) {
echo "<span style=\"margin-left:240px;font-style:italic;\">" . $statsOverview['count_ids'] . " links shortened, " . $statsOverview['sum_visited'] . " hits</span>";
}
?>
</div>
</div>
</div>
<div id="featureinfo" class="pagearea" style="display:none;">
<div class="contentblock">
<div style="padding: 20px;">
This Url Shortener provides the basic functions like shortening and expanding links.<br />
It is works on the same data structure and provides the same API (see <sup><small>*)</small></sup> below) as urlShort (by mavrev.org)<br />
You can enable a preview: if you visit a short URL, then you are not automatically redirected to the target page
but you get to see the full link and you can go here manually (this preview is set using a browser cookie).<br />
&nbsp;<br />
<b>API</b><br />
<ul>
<li>To expand <sup><small>*)</small></sup> a link:<br /><code><?php echo SERVICE_BASE_URL; ?>/api.php?url=<i><?php echo SERVICE_BASE_URL; ?>SHORT_ID</i></code></li>
<li>To expand a link and go to the web page:<br /><code><?php echo SERVICE_BASE_URL; ?><i>SHORT_ID</i></code></li>
<li>To expand a link to text:<br /><code><?php echo SERVICE_BASE_URL; ?><i>SHORT_ID.txt</i></code></li>
<li>To expand a link to xml:<br /><code><?php echo SERVICE_BASE_URL; ?><i>SHORT_ID.xml</i></code></li>
<li>To expand a link to json:<br /><code><?php echo SERVICE_BASE_URL; ?><i>SHORT_ID.json</i></code></li>
<li>To display a link as QR-Code (links to short URL):<br /><code><?php echo SERVICE_BASE_URL; ?><i>SHORT_ID.qr</i></code></li>
</ul>
</div>
</div>
</div>
<div>
&nbsp;
</div>
</body>
</html>