YourlsProxy.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.5.2
  11. */
  12. namespace PrivateBin;
  13. use Exception;
  14. /**
  15. * YourlsProxy
  16. *
  17. * Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
  18. * the result.
  19. */
  20. class YourlsProxy
  21. {
  22. /**
  23. * error message
  24. *
  25. * @access private
  26. * @var string
  27. */
  28. private $_error = '';
  29. /**
  30. * shortened URL
  31. *
  32. * @access private
  33. * @var string
  34. */
  35. private $_url = '';
  36. /**
  37. * constructor
  38. *
  39. * initializes and runs PrivateBin
  40. *
  41. * @access public
  42. * @param string $link
  43. */
  44. public function __construct(Configuration $conf, $link)
  45. {
  46. if (strpos($link, $conf->getKey('basepath') . '?') === false) {
  47. $this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
  48. return;
  49. }
  50. $yourls_api_url = $conf->getKey('apiurl', 'yourls');
  51. if (empty($yourls_api_url)) {
  52. $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
  53. return;
  54. }
  55. $data = file_get_contents(
  56. $yourls_api_url, false, stream_context_create(
  57. array(
  58. 'http' => array(
  59. 'method' => 'POST',
  60. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
  61. 'content' => http_build_query(
  62. array(
  63. 'signature' => $conf->getKey('signature', 'yourls'),
  64. 'format' => 'json',
  65. 'action' => 'shorturl',
  66. 'url' => $link,
  67. )
  68. ),
  69. ),
  70. )
  71. )
  72. );
  73. try {
  74. $data = Json::decode($data);
  75. } catch (Exception $e) {
  76. $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
  77. error_log('Error calling YOURLS: ' . $e->getMessage());
  78. return;
  79. }
  80. if (
  81. !is_null($data) &&
  82. array_key_exists('statusCode', $data) &&
  83. $data['statusCode'] == 200 &&
  84. array_key_exists('shorturl', $data)
  85. ) {
  86. $this->_url = $data['shorturl'];
  87. } else {
  88. $this->_error = 'Error parsing YOURLS response.';
  89. }
  90. }
  91. /**
  92. * Returns the (untranslated) error message
  93. *
  94. * @access public
  95. * @return string
  96. */
  97. public function getError()
  98. {
  99. return $this->_error;
  100. }
  101. /**
  102. * Returns the shortened URL
  103. *
  104. * @access public
  105. * @return string
  106. */
  107. public function getUrl()
  108. {
  109. return $this->_url;
  110. }
  111. /**
  112. * Returns true if any error has occurred
  113. *
  114. * @access public
  115. * @return bool
  116. */
  117. public function isError()
  118. {
  119. return !empty($this->_error);
  120. }
  121. }