/** * 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); }
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"; } elseif (isset($json['longUrlEncrypted']) && isset($json['firstKey'])) { $longUrlEncrypted_bin = $m29->base64_decode_url($json['longUrlEncrypted']); $firstKey_bin = $m29->base64_decode_url($json['firstKey']); if (isset($json['secondKey'])) { $secondKey_bin = $m29->base64_decode_url($json['secondKey']); } else { $secondKey_bin = ''; } try {