AbstractProxy.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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;
  12. use Exception;
  13. /**
  14. * AbstractProxy
  15. *
  16. * Forwards a URL for shortening to shlink and stores the result.
  17. */
  18. abstract class AbstractProxy
  19. {
  20. /**
  21. * error message
  22. *
  23. * @access private
  24. * @var string
  25. */
  26. private $_error = '';
  27. /**
  28. * shortened URL
  29. *
  30. * @access private
  31. * @var string
  32. */
  33. private $_url = '';
  34. /**
  35. * constructor
  36. *
  37. * initializes and runs the proxy class
  38. *
  39. * @access public
  40. * @param string $link
  41. */
  42. public function __construct(Configuration $conf, $link)
  43. {
  44. if (!str_starts_with($link, $conf->getKey('basepath') . '?')) {
  45. $this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
  46. return;
  47. }
  48. $data = $this->_getcontents($conf, $link);
  49. if ($data == null) {
  50. $this->_error = 'Error calling proxy. Probably a configuration issue';
  51. error_log('Error calling proxy: ' . $this->_error);
  52. return;
  53. }
  54. if ($data === false) {
  55. $http_response_header = $http_response_header ?? array();
  56. $statusCode = '';
  57. if (!empty($http_response_header) && preg_match('/HTTP\/\d+\.\d+\s+(\d+)/', $http_response_header[0], $matches)) {
  58. $statusCode = $matches[1];
  59. }
  60. $this->_error = 'Error calling proxy. HTTP request failed. Status code: ' . $statusCode;
  61. error_log('Error calling proxy: ' . $this->_error);
  62. return;
  63. }
  64. try {
  65. $data = Json::decode($data);
  66. } catch (Exception $e) {
  67. $this->_error = 'Error calling proxy. Probably a configuration issue, like wrong or missing config keys.';
  68. error_log('Error calling proxy: ' . $e->getMessage());
  69. return;
  70. }
  71. $url = $this->_extractShortUrl($data);
  72. if ($url === null) {
  73. $this->_error = 'Error calling proxy. Probably a configuration issue, like wrong or missing config keys.';
  74. } else {
  75. $this->_url = $url;
  76. }
  77. }
  78. /**
  79. * Returns the (untranslated) error message
  80. *
  81. * @access public
  82. * @return string
  83. */
  84. public function getError()
  85. {
  86. return $this->_error;
  87. }
  88. /**
  89. * Returns the shortened URL
  90. *
  91. * @access public
  92. * @return string
  93. */
  94. public function getUrl()
  95. {
  96. return $this->_url;
  97. }
  98. /**
  99. * Returns true if any error has occurred
  100. *
  101. * @access public
  102. * @return bool
  103. */
  104. public function isError()
  105. {
  106. return !empty($this->_error);
  107. }
  108. /**
  109. * Abstract method to get contents from a URL.
  110. *
  111. * @param Configuration $conf
  112. * @param string $link
  113. * @return mixed
  114. */
  115. abstract protected function _getcontents(Configuration $conf, string $link);
  116. /**
  117. * Abstract method to extract the shortUrl from the response
  118. *
  119. * @param array $data
  120. * @return ?string
  121. */
  122. abstract protected function _extractShortUrl(array $data): ?string;
  123. }