annotate urlredirect.php.txt @ 698:6ba9f961e463 default tip

quotedb: Bump version and copyright.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 18 Sep 2023 11:38:41 +0300
parents 6c999a7ac6d9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 <?
88
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
2 /* ==========================================================
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
3 * URLLog PHP-script for redirecting ShortURLs
307
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
4 * (C) Copyright 2006-2015 Tecnic Software productions (TNSP)
88
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
5 * ==========================================================
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
6 * How to set up short URL redirection:
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
7 *
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
8 * 1) Set up URLLog database (see urllog.tcl for more information)
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
9 *
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
10 * 2) In this example we assume that your short URL prefix is:
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
11 * http://example.com/u/
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
12 * This setting is called urllog_shorturl_prefix in urllog.tcl
468
6c999a7ac6d9 Delete trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 307
diff changeset
13 *
88
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
14 * 3) Place this PHP script under your www-server root. In this
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
15 * example we will use "/var/www/urlredirect.php" that is
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
16 * visible as http://example.com/urlredirect.php
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
17 *
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
18 * Edit script dbFilename setting below to point to correct
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
19 * URLLog SQLite database file location.
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
20 *
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
21 * 4) Create or edit /var/www/.htaccess and add following:
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
22 * RewriteEngine on
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
23 * RewriteRule ^u/([^\?\&]+)$ /urlredirect.php?u=$1
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
24 *
468
6c999a7ac6d9 Delete trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 307
diff changeset
25 * 5) You may need to restart/reload your www-server.
88
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
26 *
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
27 * 6) Profit.
7ed449f9b848 Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
28 */
307
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
29 try {
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
30 // You need to uncomment and configure one of the PDO object
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
31 // initialization lines below, depending on what kind of SQL
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
32 // database you are using.
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
33
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
34 // If using SQLite3, set the filename / path there:
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
35 //$db = new PDO("sqlite:/path/to/urllog.sqlite");
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
36 }
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
37 catch (PDOException $e) {
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
38 myerr("Could not connect to URL database: ".$e->getMessage().".");
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
39 }
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
40
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
51
eb17a6bfd443 urlredirect: Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
42 // ==========================================================
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 // Helper functions
180
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
44 function urlToID($url)
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
45 {
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
46 $idCodes = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
47 $idNumCodes = strlen($idCodes);
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
48
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
49 $len = strlen($url);
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
50 if ($len < 1)
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
51 return -1;
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
180
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
53 for ($id = 0, $i = 0; $i < $len; $i++)
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
54 {
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
55 $id *= $idNumCodes;
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
56 $n = strpos($idCodes, $url[$i]);
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
57 if ($n !== FALSE)
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
58 $id += $n;
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
59 else
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
60 return -2;
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
61 }
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
62 return $id;
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
63 }
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
64
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
65 function myerr($str = "")
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
66 {
30
3cf438db908a urlredirect: Indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
67 header("Status: 404 Not Found");
3cf438db908a urlredirect: Indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
68 echo "404 Not Found - ".$str;
3cf438db908a urlredirect: Indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
69 exit;
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 // Check arguments
17
b9c27bad2d9a Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
73 if (isset($_GET["u"]))
30
3cf438db908a urlredirect: Indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
74 $urlStr = $_GET["u"];
17
b9c27bad2d9a Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
75 else
30
3cf438db908a urlredirect: Indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
76 myerr("No ID given.");
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 // Calculate urlID
180
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
79 $urlID = urlToID($urlStr);
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
80 if ($urlID < 0)
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
81 myerr("Invalid ID.");
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
307
30bb894cfb58 Updated a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 180
diff changeset
84 // Find the matching URL, if any
180
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
85 foreach ($db->query("SELECT url FROM urls WHERE id=".$urlID) as $row)
68c4b25c982c Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
86 {
17
b9c27bad2d9a Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
87 header("Location: ".$row["url"]);
b9c27bad2d9a Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
88 exit;
b9c27bad2d9a Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
89 }
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
17
b9c27bad2d9a Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
91 $db = null;
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 // Output result
17
b9c27bad2d9a Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
93 myerr("No such ID in database.");
0
1c4e2814cd41 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 ?>