shortenviayourls.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // change this, if your php files and data is outside of your webservers document root
  3. define('PATH', '');
  4. define('PUBLIC_PATH', __DIR__);
  5. require PATH . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
  6. $link = $_SERVER['REQUEST_URI'];
  7. $response = getGetData();
  8. $arr = explode('=',$response);
  9. $c = count ($arr);
  10. $opSuccess = FALSE;
  11. $errCode = 0;
  12. $shortenedUrl = "";
  13. $originalUrl = "";
  14. if(($c == 2) && ($arr[0] == "link") && (strlen($arr[1]) < 256)) {
  15. // read in configuration values
  16. $conf = new PrivateBin\Configuration;
  17. $originalUrl = urldecode($arr[1]);
  18. if (startsWith($originalUrl, $conf->getKey( "basepath") . "/?")) {
  19. // Init the CURL session
  20. $ch = curl_init();
  21. curl_setopt($ch, CURLOPT_URL, $conf->getKey( "apiurl", "yourls"));
  22. curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
  24. curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
  25. curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
  26. 'signature' => $conf->getKey( "signature", "yourls"),
  27. 'format' => 'json',
  28. 'action' => 'shorturl',
  29. 'url' => $originalUrl
  30. ));
  31. // Fetch and return content
  32. $data = curl_exec($ch);
  33. curl_close($ch);
  34. if (!($data === FALSE) && is_string($data))
  35. {
  36. $data = json_decode( $data, true);
  37. if (!is_null($data) && array_key_exists('statusCode', $data)
  38. && array_key_exists('shorturl', $data) && ($data['statusCode'] == 200))
  39. {
  40. $shortenedUrl = $data['shorturl'];
  41. $opSuccess = TRUE;
  42. } else {
  43. // error with contents of YOURLS response.
  44. $errCode = 3;
  45. }
  46. } else {
  47. // error when calling YOURLS - probably a PrivateBin configuration issue, like wrong/missing apiurl or signature
  48. $errCode = 2;
  49. }
  50. } else {
  51. // trying to shorten a URL not pointing to our PrivateBin instance.
  52. $errCode = 1;
  53. }
  54. }
  55. if ($opSuccess)
  56. {
  57. print("<br>Your shortened paste is <span class=\"shortensuccess\"><a href=\"$shortenedUrl\">$shortenedUrl</a></span>");
  58. }
  59. else
  60. {
  61. print("<br><span class=\"shortenerror\">Error: An error occured while trying to shorten the given URL (error code $errCode)</span>");
  62. }
  63. function getGetData() {
  64. $data = http_build_query($_GET);
  65. return $data;
  66. }
  67. function startsWith($haystack, $needle)
  68. {
  69. $length = strlen($needle);
  70. return (substr($haystack, 0, $length) === $needle);
  71. }
  72. ?>