YourlsProxy.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.4.0
  11. */
  12. namespace PrivateBin;
  13. use Exception;
  14. use PrivateBin\Configuration;
  15. use PrivateBin\Json;
  16. /**
  17. * YourlsProxy
  18. *
  19. * Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
  20. * the result.
  21. */
  22. class YourlsProxy
  23. {
  24. /**
  25. * error message
  26. *
  27. * @access private
  28. * @var string
  29. */
  30. private $_error = '';
  31. /**
  32. * shortened URL
  33. *
  34. * @access private
  35. * @var string
  36. */
  37. private $_url = '';
  38. /**
  39. * constructor
  40. *
  41. * initializes and runs PrivateBin
  42. *
  43. * @access public
  44. * @param string $link
  45. */
  46. public function __construct(Configuration $conf, $link)
  47. {
  48. if (strpos($link, $conf->getKey('basepath') . '/?') === false) {
  49. $this->_error = 'Trying to shorten a URL not pointing to our PrivateBin instance.';
  50. return;
  51. }
  52. // Init the CURL session
  53. $ch = curl_init();
  54. curl_setopt($ch, CURLOPT_URL, $conf->getKey("apiurl", "yourls"));
  55. curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
  57. curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
  58. curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
  59. 'signature' => $conf->getKey("signature", "yourls"),
  60. 'format' => 'json',
  61. 'action' => 'shorturl',
  62. 'url' => $link
  63. ));
  64. // Fetch and return content
  65. $data = curl_exec($ch);
  66. curl_close($ch);
  67. if ($data === false || !is_string($data)) {
  68. $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
  69. return;
  70. }
  71. try {
  72. $data = Json::decode($data);
  73. } catch (Exception $e) {
  74. $this->_error = $e->getMessage();
  75. return;
  76. }
  77. if (
  78. !is_null($data) &&
  79. array_key_exists('statusCode', $data) &&
  80. array_key_exists('shorturl', $data) &&
  81. $data['statusCode'] == 200
  82. ) {
  83. $this->_url = $data['shorturl'];
  84. } else {
  85. $this->_error = 'Error parsing YOURLS response.';
  86. }
  87. }
  88. /**
  89. * Returns the (untranslated) error message
  90. *
  91. * @access public
  92. * @return string
  93. */
  94. public function getError()
  95. {
  96. return $this->_error;
  97. }
  98. /**
  99. * Returns the shortened URL
  100. *
  101. * @access public
  102. * @return string
  103. */
  104. public function getUrl()
  105. {
  106. return $this->_url;
  107. }
  108. /**
  109. * Returns true if any error has occurred
  110. *
  111. * @access public
  112. * @return bool
  113. */
  114. public function isError()
  115. {
  116. return !empty($this->_error);
  117. }
  118. }