AbstractProxy.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Proxy;
  12. use Exception;
  13. use PrivateBin\Configuration;
  14. use PrivateBin\Json;
  15. /**
  16. * AbstractProxy
  17. *
  18. * Forwards a URL for shortening and stores the result.
  19. */
  20. abstract class AbstractProxy
  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 the proxy class
  40. *
  41. * @access public
  42. * @param Configuration $conf
  43. * @param string $link
  44. */
  45. public function __construct(Configuration $conf, string $link)
  46. {
  47. if (!str_starts_with($link, $conf->getKey('basepath') . '?')) {
  48. $this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
  49. return;
  50. }
  51. $proxyUrl = $this->_getProxyUrl($conf);
  52. if (empty($proxyUrl)) {
  53. $this->_error = 'Error calling proxy. Probably a configuration issue, like missing api url';
  54. error_log($this->_error);
  55. return;
  56. }
  57. $data = file_get_contents($proxyUrl, false,
  58. stream_context_create(
  59. array(
  60. 'http' => $this->_getProxyPayload($conf, $link),
  61. )
  62. )
  63. );
  64. if ($data == null) {
  65. $this->_error = 'Error calling proxy. Probably a configuration issue';
  66. error_log($this->_error);
  67. return;
  68. }
  69. if ($data === false) {
  70. $http_response_header = $http_response_header ?? array();
  71. $statusCode = '';
  72. if (!empty($http_response_header) && preg_match('/HTTP\/\d+\.\d+\s+(\d+)/', $http_response_header[0], $matches)) {
  73. $statusCode = $matches[1];
  74. }
  75. $this->_error = 'Error calling proxy. HTTP request failed. Status code: ' . $statusCode;
  76. error_log($this->_error);
  77. return;
  78. }
  79. try {
  80. $jsonData = Json::decode($data);
  81. } catch (Exception $e) {
  82. $this->_error = 'Error calling proxy. Probably a configuration issue, like wrong or missing config keys.';
  83. error_log('Error calling proxy: ' . $e->getMessage());
  84. return;
  85. }
  86. $url = $this->_extractShortUrl($jsonData);
  87. if ($url === null) {
  88. $this->_error = 'Error calling proxy. Probably a configuration issue, like wrong or missing config keys.';
  89. error_log('Error calling proxy: ' . $data);
  90. } else {
  91. $this->_url = $url;
  92. }
  93. }
  94. /**
  95. * Returns the (untranslated) error message
  96. *
  97. * @access public
  98. * @return string
  99. */
  100. public function getError()
  101. {
  102. return $this->_error;
  103. }
  104. /**
  105. * Returns the shortened URL
  106. *
  107. * @access public
  108. * @return string
  109. */
  110. public function getUrl()
  111. {
  112. return $this->_url;
  113. }
  114. /**
  115. * Returns true if any error has occurred
  116. *
  117. * @access public
  118. * @return bool
  119. */
  120. public function isError()
  121. {
  122. return !empty($this->_error);
  123. }
  124. /**
  125. * Abstract method to get the payload to send to the URL Shortener
  126. *
  127. * @access protected
  128. * @param Configuration $conf
  129. * @param string $link
  130. * @return array
  131. */
  132. abstract protected function _getProxyPayload(Configuration $conf, string $link): array;
  133. /**
  134. * Abstract method to extract the shortUrl from the response
  135. *
  136. * @param array $data
  137. * @return ?string
  138. */
  139. abstract protected function _extractShortUrl(array $data): ?string;
  140. /**
  141. * Abstract method to get the proxy URL
  142. *
  143. * @param Configuration $conf
  144. * @return string
  145. */
  146. abstract protected function _getProxyUrl(Configuration $conf): string;
  147. }