Mercurial > hg > egg-tcls
comparison urllog.php.txt @ 0:1c4e2814cd41
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 21 Sep 2010 13:12:49 +0300 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1c4e2814cd41 |
---|---|
1 <? | |
2 // ===================================================== | |
3 // URLLog PHP-script for redirecting ShortURLs | |
4 // (C) Copyright 2006 Tecnic Software productions (TNSP) | |
5 // ===================================================== | |
6 | |
7 // URLLog datafile | |
8 $urlFilename = "data.urllog"; | |
9 | |
10 // ===================================================== | |
11 // Helper functions | |
12 $idStr = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
13 | |
14 function myerr() { | |
15 header("Status: 404 Not Found"); | |
16 echo "404 Not Found"; | |
17 exit; | |
18 } | |
19 | |
20 // Check arguments | |
21 if (!isset($argv[0])) myerr(); | |
22 | |
23 // Calculate urlID | |
24 $urlStr = $argv[0]; | |
25 $urlLen = strlen($urlStr); | |
26 if ($urlLen < 1) myerr(); | |
27 | |
28 for ($urlID = 0, $i = 0; $i < $urlLen; $i++) { | |
29 $urlID *= strlen($idStr); | |
30 | |
31 $n = strpos($idStr, $urlStr[$i]); | |
32 if ($n !== FALSE) { | |
33 $urlID += $n; | |
34 } else | |
35 myerr(); | |
36 } | |
37 | |
38 // Find the URL | |
39 $urlFile = fopen($urlFilename, "r"); | |
40 if (!$urlFile) myerr(); | |
41 | |
42 $urlFound = 0; | |
43 while (!feof($urlFile) && !$urlFound) { | |
44 $urlItems = split(" ", fgets($urlFile, 4096), 5); | |
45 if ($urlItems[4] == $urlID) $urlFound = 1; | |
46 } | |
47 | |
48 fclose($urlFile); | |
49 | |
50 // Output result | |
51 if ($urlFound) { | |
52 header("Location: ".$urlItems[0]); | |
53 } else | |
54 myerr(); | |
55 | |
56 exit; | |
57 ?> |