/** * 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); }
} 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 { $ret = $m29->insert_encrypted_url($longUrlEncrypted_bin, $firstKey_bin, $secondKey_bin); } catch (M29Exception $e) { http_error($e->error, 'json'); } if ($secondKey_bin) { $out = array('kind' => 'urlshortener#url', 'id' => $ret['short_url'], 'longUrl' => $ret['long_url']); header("Content-Type: application/json; charset=UTF-8"); print json_encode($out) . "\n"; } else { $out = array('kind' => 'urlshortener#url', 'id' => $ret['short_url'], 'idIncomplete' => True); header("Content-Type: application/json; charset=UTF-8"); print json_encode($out) . "\n"; } } else { http_error(array('reason' => 'required', 'message' => 'Required parameter: longUrl', 'locationType' => 'parameter', 'location' => 'longUrl'), 'json'); }