view urllog.php.txt @ 0:1c4e2814cd41

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 21 Sep 2010 13:12:49 +0300
parents
children
line wrap: on
line source

<?
// =====================================================
// URLLog PHP-script for redirecting ShortURLs
// (C) Copyright 2006 Tecnic Software productions (TNSP)
// =====================================================

// URLLog datafile
$urlFilename = "data.urllog";

// =====================================================
// Helper functions
$idStr = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

function myerr() {
	header("Status: 404 Not Found");
	echo "404 Not Found";
	exit;
}

// Check arguments
if (!isset($argv[0])) myerr();

// Calculate urlID
$urlStr = $argv[0];
$urlLen = strlen($urlStr);
if ($urlLen < 1) myerr();

for ($urlID = 0, $i = 0; $i < $urlLen; $i++) {
	$urlID *= strlen($idStr);

	$n = strpos($idStr, $urlStr[$i]);
	if ($n !== FALSE) {
		$urlID += $n;
	} else
		myerr();
}

// Find the URL
$urlFile = fopen($urlFilename, "r");
if (!$urlFile) myerr();

$urlFound = 0;
while (!feof($urlFile) && !$urlFound) {
	$urlItems = split(" ", fgets($urlFile, 4096), 5);
	if ($urlItems[4] == $urlID) $urlFound = 1;
}

fclose($urlFile);

// Output result
if ($urlFound) {
	header("Location: ".$urlItems[0]);
} else
	myerr();

exit;
?>