| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php declare(strict_types=1);
- /**
- * PrivateBin
- *
- * a zero-knowledge paste bin
- *
- * @link https://github.com/PrivateBin/PrivateBin
- * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
- * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
- */
- namespace PrivateBin;
- use Exception;
- /**
- * AbstractProxy
- *
- * Forwards a URL for shortening to shlink and stores the result.
- */
- abstract class AbstractProxy
- {
- /**
- * error message
- *
- * @access private
- * @var string
- */
- private $_error = '';
- /**
- * shortened URL
- *
- * @access private
- * @var string
- */
- private $_url = '';
- /**
- * constructor
- *
- * initializes and runs the proxy class
- *
- * @access public
- * @param string $link
- */
- public function __construct(Configuration $conf, $link)
- {
- if (!str_starts_with($link, $conf->getKey('basepath') . '?')) {
- $this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
- return;
- }
- $data = $this->_getcontents($conf, $link);
- if ($data == null) {
- $this->_error = 'Error calling proxy. Probably a configuration issue';
- error_log('Error calling proxy: ' . $this->_error);
- return;
- }
- if ($data === false) {
- $http_response_header = $http_response_header ?? array();
- $statusCode = '';
- if (!empty($http_response_header) && preg_match('/HTTP\/\d+\.\d+\s+(\d+)/', $http_response_header[0], $matches)) {
- $statusCode = $matches[1];
- }
- $this->_error = 'Error calling proxy. HTTP request failed. Status code: ' . $statusCode;
- error_log('Error calling proxy: ' . $this->_error);
- return;
- }
- try {
- $data = Json::decode($data);
- } catch (Exception $e) {
- $this->_error = 'Error calling proxy. Probably a configuration issue, like wrong or missing config keys.';
- error_log('Error calling proxy: ' . $e->getMessage());
- return;
- }
- $url = $this->_extractShortUrl($data);
- if ($url === null) {
- $this->_error = 'Error calling proxy. Probably a configuration issue, like wrong or missing config keys.';
- } else {
- $this->_url = $url;
- }
- }
- /**
- * Returns the (untranslated) error message
- *
- * @access public
- * @return string
- */
- public function getError()
- {
- return $this->_error;
- }
- /**
- * Returns the shortened URL
- *
- * @access public
- * @return string
- */
- public function getUrl()
- {
- return $this->_url;
- }
- /**
- * Returns true if any error has occurred
- *
- * @access public
- * @return bool
- */
- public function isError()
- {
- return !empty($this->_error);
- }
- /**
- * Abstract method to get contents from a URL.
- *
- * @param Configuration $conf
- * @param string $link
- * @return mixed
- */
- abstract protected function _getcontents(Configuration $conf, string $link);
- /**
- * Abstract method to extract the shortUrl from the response
- *
- * @param array $data
- * @return ?string
- */
- abstract protected function _extractShortUrl(array $data): ?string;
- }
|