// Quick and dirty shortlinks server // v0.2: Tweetdeck compatibility // The end point for this is http://muk.fm/api/123456/?url=%@ // ...where your API key is 123456 // v0.1: first public release // http://james.cridland.net/code/ // james@cridland.net // Installation instructions at the bottom of this file // Configuration block (you'll need to change this) $config['database_location']='localhost'; $config['database_username']='username'; $config['database_password']='password'; $config['database_name']='databasename'; $config['default_website']='http://www.mediauk.com/'; // Where we go if we have no link given $config['api_key']='123456'; // This -must- be six characters long. // The rest of the code follows. You don't need to change this. // // Have fun // // Config check in case you can't read if (strlen($config['api_key'])<>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 "
This page wasn't found. Sorry and all.