1
0

YourlsProxy.php 3.3 KB

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