YourlsProxy.php 3.1 KB

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