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. /**
  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. $data = file_get_contents(
  51. $conf->getKey('apiurl', 'yourls'), false, stream_context_create(
  52. array(
  53. 'http' => array(
  54. 'method' => 'POST',
  55. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
  56. 'content' => http_build_query(
  57. array(
  58. 'signature' => $conf->getKey('signature', 'yourls'),
  59. 'format' => 'json',
  60. 'action' => 'shorturl',
  61. 'url' => $link,
  62. )
  63. ),
  64. ),
  65. )
  66. )
  67. );
  68. if ($data === false || !is_string($data)) {
  69. $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
  70. return;
  71. }
  72. try {
  73. $data = Json::decode($data);
  74. } catch (Exception $e) {
  75. $this->_error = $e->getMessage();
  76. return;
  77. }
  78. if (
  79. !is_null($data) &&
  80. array_key_exists('statusCode', $data) &&
  81. array_key_exists('shorturl', $data) &&
  82. $data['statusCode'] == 200
  83. ) {
  84. $this->_url = $data['shorturl'];
  85. } else {
  86. $this->_error = 'Error parsing YOURLS response.';
  87. }
  88. }
  89. /**
  90. * Returns the (untranslated) error message
  91. *
  92. * @access public
  93. * @return string
  94. */
  95. public function getError()
  96. {
  97. return $this->_error;
  98. }
  99. /**
  100. * Returns the shortened URL
  101. *
  102. * @access public
  103. * @return string
  104. */
  105. public function getUrl()
  106. {
  107. return $this->_url;
  108. }
  109. /**
  110. * Returns true if any error has occurred
  111. *
  112. * @access public
  113. * @return bool
  114. */
  115. public function isError()
  116. {
  117. return !empty($this->_error);
  118. }
  119. }