YourlsProxy.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 PrivateBin\Configuration;
  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 not pointing to our PrivateBin instance.';
  48. return;
  49. }
  50. // Init the CURL session
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_URL, $conf->getKey("apiurl", "yourls"));
  53. curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
  54. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
  55. curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
  57. 'signature' => $conf->getKey("signature", "yourls"),
  58. 'format' => 'json',
  59. 'action' => 'shorturl',
  60. 'url' => $link
  61. ));
  62. // Fetch and return content
  63. $data = curl_exec($ch);
  64. curl_close($ch);
  65. if ($data === false || !is_string($data)) {
  66. $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
  67. return;
  68. }
  69. $data = json_decode($data, true);
  70. if (
  71. !is_null($data) &&
  72. array_key_exists('statusCode', $data) &&
  73. array_key_exists('shorturl', $data) &&
  74. $data['statusCode'] == 200
  75. ) {
  76. $this->_url = $data['shorturl'];
  77. } else {
  78. $this->_error = 'Error parsing YOURLS response.';
  79. }
  80. }
  81. /**
  82. * Returns the (untranslated) error message
  83. *
  84. * @access public
  85. * @return string
  86. */
  87. public function getError()
  88. {
  89. return $this->_error;
  90. }
  91. /**
  92. * Returns the shortened URL
  93. *
  94. * @access public
  95. * @return string
  96. */
  97. public function getUrl()
  98. {
  99. return $this->_url;
  100. }
  101. /**
  102. * Returns true if any error has occurred
  103. *
  104. * @access public
  105. * @return bool
  106. */
  107. public function isError()
  108. {
  109. return !empty($this->_error);
  110. }
  111. }