6) { die("Webmaster: The API key must be six characters long in your config."); } // Set the headers. These links are perma-links and thus will never expire. // For the world, they all expire next Monday (and were built last Monday). header("Expires: " . gmdate('D, d M Y H:i:s', strtotime('next Monday')) . ' GMT'); header("Last-Modified: " . gmdate('D, d M Y H:i:s', strtotime('last Monday') ) . ' GMT'); if (trim($_SERVER['REQUEST_URI'])=="/") { // We have been asked for nothing. So let's just pop off to the default website. header('Location: '.$config['default_website'], TRUE, 301); exit; } if (substr($_SERVER['REQUEST_URI'],0,4)=="/api") { // This would be the API, then. // This is the key if (substr($_SERVER['REQUEST_URI'],5,6)!=$config['api_key']) { die("No valid API key given. Format is http://".$_SERVER['HTTP_HOST']."/api/:key/:url"); } $url=urldecode(substr($_SERVER['REQUEST_URI'],12)); if (substr($url,0,5)=="?url=") {$url=substr($url,5);} // so you can use Tweetdeck etc if (substr($url,0,4)!="http") {$url="http://".$url;} // in case you forget the http db_connect(); $query="INSERT INTO shortlinks (shortlink_url) VALUES ('".mysql_real_escape_string($url)."') ON DUPLICATE KEY UPDATE shortlink_id=LAST_INSERT_ID(shortlink_id),shortlink_dummy=NOT shortlink_dummy;"; mysql_query($query); $thing=mysql_query("SELECT LAST_INSERT_ID()"); $row=mysql_fetch_row($thing); header('Content-type: text/plain;', TRUE, 200); echo 'http://'.$_SERVER['HTTP_HOST'].'/'.base_convert($row[0],10,36); exit; } // Bespoke kludgy bit for muk.fm if (strpos(substr($_SERVER['REQUEST_URI'],1),'/')>0 AND $_SERVER['HTTP_HOST']=='muk.fm') { // We have been asked for something with more than one slash. // This is a kludgy Media UK shortlink. Apologies for this bit. You can edit this out. // It'll only work on my own server anyway. header('Location: http://www.mediauk.com/'.substr($_SERVER['REQUEST_URI'],1), TRUE, 301); exit; } // End of bespoke kludgy bit for muk.fm //We've been fed a shortlink. db_connect(); $query="select shortlink_url from shortlinks where shortlink_id=".mysql_real_escape_string(base_convert(substr($_SERVER['REQUEST_URI'],1),36,10)).";"; $page=mysql_query($query); if (!mysql_numrows($page)) { // No page found at all, oh dear oh dear header("HTTP/1.0 404 Not Found", TRUE, 404); echo "404 page not found

Page not found

This page wasn't found. Sorry and all.


muk.fm"; exit; } else { //We have a page, so pop off and get it then $page_info=mysql_fetch_object($page); $url=$page_info->shortlink_url; $url=str_replace("%26","&",$url); header('Location: '.$url, TRUE, 301); exit; } function db_connect() { GLOBAL $config; $database = mysql_connect($config['database_location'],$config['database_username'],$config['database_password']); if (!$database) { die('Erk. We cannot see the database, which is bad. Try refreshing this page and see if it works then.'); } $select_db = mysql_select_db($config['database_name'],$database); } /* Setup instructions: 1. Create a shortlinks table in your database. Use this SQL code to achieve this. CREATE TABLE IF NOT EXISTS `shortlinks` ( `shortlink_id` int(11) NOT NULL auto_increment, `shortlink_url` varchar(255) collate utf8_unicode_ci NOT NULL default '', `shortlink_dummy` smallint(6) NOT NULL default '0', PRIMARY KEY (`shortlink_id`), UNIQUE KEY `shortlink_url` (`shortlink_url`), KEY `shortlink_id` (`shortlink_id`,`shortlink_url`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5945 ; 2. Write an .htaccess file in the root of your shortlinks webserver. It should read like this: Options +FollowSymLinks RewriteEngine on RewriteRule ^(.*)$ index.php?i=%1 ... this redirects absolutely every single thing called to this script. 3. Edit the configuration variables at the top of this file to suit. 4. Save this file as 'index.php' in the root of your shortlinks webserver. 5. Profit. */ ?>