<?php
/*
 * Postcode to TinyURL
 * Copyright (C) 2004 Chris Applegate (chris AT qwghlm.co.uk)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */


// This script takes a UK postcode, checks it is valid and exists, constructs a Multimap URL for it, sends that to tinyurl.com, and
// gets back a tinyURL to use.

// The author is in no way affiliated either with Multimap or TinyURL, and this should not be considered 'official' in any way at all.

if (count($_POST) > && !empty($_POST[postcode])) {

    
// Regular expression to match a UK postcode: 1-2 letters, 1-2 digits, an optional extra letter;
    // then an optional space, a digit and two more letters. See http://www.upu.int/post_code/en/countries/GBR.pdf
    // for more info

    
$postcodeRegex "^[[:alpha:]]{1,2}[[:digit:]]{1,2}[[:alpha:]]?[[:space:]]*[[:digit:]][[:alpha:]]{2}$";

    if (!
ereg($postcodeRegex$_POST[postcode])) {
        echo 
"Error! You have not supplied a correctly formatted postcode!";
    }
    else {

        
// Build the Multimap URL (we ask for the print version, apart from that all other options are default);
        
$mapURL "http://uk.multimap.com/map/browse.cgi";

        
$mapURL .= "?client=print&scale=5000&";
        
$mapURL .= "pc=".rawurlencode($_POST[postcode]);

        
// Check to see if it produces a valid map by opening the multimap URL

        
$f fopen($mapURL"rb");
        
$mapHTML fread($f1024000);

        
// Multimap's error message if they can't find it is this:
        
$mapErrorMessage "we do not recognise that as a valid Royal Mail postcode";

        
// Check for the error
        
if (strpos($mapHTML$mapErrorMessage) !== false) {
            echo 
"Error! The postcode does not exist!";
        }
        else {
            
// If all good then...

            // Sort out the data to be POSTed to the tinyurl server, so encode the whole Map URL
            
$tinyData "url=".rawurlencode($mapURL);
            
$contentLength strlen($tinyData);

            
$tinyServer "tinyurl.com";
            
$tinyRequest "/create.php";

            
$ReqHeader "POST $tinyRequest HTTP/1.0\n".
                        
"Host: $tinyServer\n".
                        
"Content-Type: application/x-www-form-urlencoded\n".
                        
"Content-Length: $contentLength\n\n".
                        
"$tinyData";

            
// HTML we get back from the tinyURL server
            
$tinyHTML "";

            
// Open the connection to the host
            
$tinySocket fsockopen($tinyServer"80");

            if (
$tinySocket) {

                
// Send the POST data
                
fputs($tinySocket$ReqHeader);

                
// Read what comes back
                
while (!feof($tinySocket)) {
                    
$tinyHTML .= fgets($tinySocket,1024);
                }


                
fclose($tinySocket);

                
// Extract the tinyURL from the HTML - it's after a <b> tag and consists of http://tinyurl.com/ followed by lots
                // of letters and digits, and dump the result into an array

                
$regexArray = array();
                
ereg("(<b>)(http://tinyurl.com/[[:alnum:]]+)"$tinyHTML$regexArray);

                
// Third element of the array corresponds with the second bracketed expression

                
$tinyURL $regexArray[2];

                
// If they change the formatting of the page the above will break and we won't get the right result

                
if (empty($tinyURL)) {
                    echo 
"Error! No URL was sent back from $tinyServer";
                }
                else {

                    
// If it is all good, then our tinyURL is made!
                    
echo "<h2>TinyURL made!</h2>";
                    echo 
"A TinyURL: <a href=\"$tinyURL\">$tinyURL</a> has been generated for your postcode <b>$_POST[postcode]</b>.";
                }
            }
            else {
                echo 
"Error connecting to $tinyServer! Contact webmaster for more information.";
            }

        }
    }
}
?>