/** * Handle POSTs on the index page * * This function is used by index.php to handle various submission * methods, to make the index file itself as simple and standardized * as possible. * * @param array $config Optional configuration array * @return array Array of results and/or errors */ function index_handle_post($config = array()) { $m29 = new M29($config); $post = $_POST; $ret = array(); if(isset($post['longUrlEncrypted']) && $post['longUrlEncrypted']) { $longUrlEncrypted_bin = $m29->base64_decode_url($post['longUrlEncrypted']); $firstKey_bin = $m29->base64_decode_url($post['firstKey']); if(isset($post['secondKey'])) { $secondKey_bin = $m29->base64_decode_url($post['secondKey']); } else { $secondKey_bin = ''; } try { $ret = $m29->insert_encrypted_url($longUrlEncrypted_bin, $firstKey_bin, $secondKey_bin); } catch(M29Exception $e) { $ret['error'] = $e->error; } } elseif(isset($post['longUrl']) && $post['longUrl']) { try { $ret = $m29->insert_long_url($post['longUrl']); } catch(M29Exception $e) { $ret['error'] = $e->error; } } if(isset($post['xhrRequest']) && ($post['xhrRequest'] == 'true')) { header("Content-Type: text/xml; charset=UTF-8"); echo '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; echo '<!--' . "\n"; echo 'This response is for the benefit of the front page XMLHttpRequest.' . "\n"; echo 'Please do not use this by third-party scripts. A fully-featured JSON' . "\n"; echo 'API is available; please see the main web site for details.' . "\n"; echo '-->' . "\n"; echo '<response>' . "\n"; if(isset($ret['short_url']) && $ret['short_url']) { echo '<shortUrl>' . $ret['short_url'] . '</shortUrl>' . "\n"; if(isset($ret['short_url_incomplete'])) { echo '<shortUrlIncomplete>' . ($ret['short_url_incomplete'] ? 'true' : 'false') . '</shortUrlIncomplete>' . "\n"; } } if(isset($ret['error']) && $ret['error']) { echo '<error>' . htmlspecialchars($ret['error']) . '</error>' . "\n"; } echo '</response>' . "\n"; exit(); } return($ret); }
if (isset($config['disable_api']) && $config['disable_api']) { http_error(array('reason' => 'notFound', 'message' => 'The API is disabled'), 'json'); } if ($contenttype == 'application/x-www-form-urlencoded') { http_error(array('reason' => 'parseError', 'message' => 'This API does not support parsing form-encoded input.'), 'json'); } if ($pathinfo == '/url/history') { $out = array('totalItems' => 0, 'items' => array()); header("Content-Type: application/json; charset=UTF-8"); print json_encode($out) . "\n"; exit; } if (!($pathinfo == '/url')) { http_error(array('reason' => 'notFound', 'message' => 'The requested URL was not found on this server.'), 'json'); } $m29 = new M29($config); if ($requestmethod == 'POST') { if (!($contenttype == 'application/json')) { http_error(array('reason' => 'parseError', 'message' => 'A Content-Type of application/json is required to POST to this method'), 'json'); } $json = json_decode($HTTP_RAW_POST_DATA, true); if (isset($json['longUrl'])) { $longUrl = $json['longUrl']; try { $ret = $m29->insert_long_url($longUrl); } catch (M29Exception $e) { http_error($e->error, 'json'); } $out = array('kind' => 'urlshortener#url', 'id' => $ret['short_url'], 'longUrl' => $longUrl); header("Content-Type: application/json; charset=UTF-8"); print json_encode($out) . "\n";
<?php /** * M29 redirector * * @package M29 * @author Ryan Finnie <*****@*****.**> * @copyright 2012 Ryan Finnie * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL version 2 */ require_once 'config.php'; require_once 'functions.php'; require_once 'M29.php'; $m29 = new M29($config); if (isset($_SERVER['PATH_INFO'])) { $url = $m29->base_url . $_SERVER['PATH_INFO']; } else { http_error(array('reason' => 'serviceError', 'message' => 'Cannot determine the short URL')); } try { $ret = $m29->process_short_url($url, true); } catch (M29Exception $e) { http_error($e->error); } header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently'); header("Location: " . $ret['long_url']); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
<?php /** * M29 URL info page * * @package M29 * @author Ryan Finnie <*****@*****.**> * @copyright 2012 Ryan Finnie * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL version 2 */ require_once 'config.php'; require_once 'functions.php'; require_once 'M29.php'; $m29 = new M29($config); if (isset($_SERVER['PATH_INFO'])) { $url = $m29->base_url . $_SERVER['PATH_INFO']; } else { http_error(array('reason' => 'serviceError', 'message' => 'Cannot determine the short URL')); } try { $ret = $m29->process_short_url($url, false); } catch (M29Exception $e) { http_error($e->error); } ?> <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"/> <title>URL Information</title> <link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700' rel='stylesheet' type='text/css'>