YourlsProxy.php 3.1 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.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. $data = file_get_contents(
  53. $conf->getKey('apiurl', 'yourls'), false, stream_context_create(
  54. array(
  55. 'http' => array(
  56. 'method' => 'POST',
  57. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
  58. 'content' => http_build_query(
  59. array(
  60. 'signature' => $conf->getKey('signature', 'yourls'),
  61. 'format' => 'json',
  62. 'action' => 'shorturl',
  63. 'url' => $link
  64. )
  65. )
  66. )
  67. )
  68. )
  69. );
  70. if ($data === false || !is_string($data)) {
  71. $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
  72. return;
  73. }
  74. try {
  75. $data = Json::decode($data);
  76. } catch (Exception $e) {
  77. $this->_error = $e->getMessage();
  78. return;
  79. }
  80. if (
  81. !is_null($data) &&
  82. array_key_exists('statusCode', $data) &&
  83. array_key_exists('shorturl', $data) &&
  84. $data['statusCode'] == 200
  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. }