mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2026-04-18 21:48:24 -04:00
chore: moved proxy classes to different folder and namespaces
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
<?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\Proxy;
|
||||
|
||||
use Exception;
|
||||
use PrivateBin\Configuration;
|
||||
use PrivateBin\Json;
|
||||
|
||||
/**
|
||||
* AbstractProxy
|
||||
*
|
||||
* Forwards a URL for shortening 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 Configuration $conf
|
||||
* @param string $link
|
||||
*/
|
||||
public function __construct(Configuration $conf, string $link)
|
||||
{
|
||||
if (!str_starts_with($link, $conf->getKey('basepath') . '?')) {
|
||||
$this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
|
||||
return;
|
||||
}
|
||||
|
||||
$proxyUrl = $this->_getProxyUrl($conf);
|
||||
|
||||
if (empty($proxyUrl)) {
|
||||
$this->_error = 'Error calling proxy. Probably a configuration issue, like missing api url';
|
||||
error_log($this->_error);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = file_get_contents($proxyUrl, false,
|
||||
stream_context_create(
|
||||
array(
|
||||
'http' => $this->_getProxyPayload($conf, $link),
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if ($data == null) {
|
||||
$this->_error = 'Error calling proxy. Probably a configuration issue';
|
||||
error_log($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($this->_error);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$jsonData = 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($jsonData);
|
||||
|
||||
if ($url === null) {
|
||||
$this->_error = 'Error calling proxy. Probably a configuration issue, like wrong or missing config keys.';
|
||||
error_log('Error calling proxy: ' . $data);
|
||||
} 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 the payload to send to the URL Shortener
|
||||
*
|
||||
* @access protected
|
||||
* @param Configuration $conf
|
||||
* @param string $link
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function _getProxyPayload(Configuration $conf, string $link): array;
|
||||
|
||||
/**
|
||||
* Abstract method to extract the shortUrl from the response
|
||||
*
|
||||
* @param array $data
|
||||
* @return ?string
|
||||
*/
|
||||
abstract protected function _extractShortUrl(array $data): ?string;
|
||||
|
||||
/**
|
||||
* Abstract method to get the proxy URL
|
||||
*
|
||||
* @param Configuration $conf
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function _getProxyUrl(Configuration $conf): string;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?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\Proxy;
|
||||
|
||||
use PrivateBin\Configuration;
|
||||
use PrivateBin\Json;
|
||||
|
||||
/**
|
||||
* ShlinkProxy
|
||||
*
|
||||
* Forwards a URL for shortening to shlink and stores the result.
|
||||
*/
|
||||
class ShlinkProxy extends AbstractProxy
|
||||
{
|
||||
/**
|
||||
* Overrides the abstract parent function to get the proxy URL.
|
||||
*
|
||||
* @param Configuration $conf
|
||||
* @return string
|
||||
*/
|
||||
protected function _getProxyUrl(Configuration $conf): string
|
||||
{
|
||||
return $conf->getKey('apiurl', 'shlink');
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the abstract parent function to get contents from Shlink API.
|
||||
*
|
||||
* @access protected
|
||||
* @param Configuration $conf
|
||||
* @param string $link
|
||||
* @return array
|
||||
*/
|
||||
protected function _getProxyPayload(Configuration $conf, string $link): array
|
||||
{
|
||||
$shlink_api_key = $conf->getKey('apikey', 'shlink');
|
||||
|
||||
$body = array(
|
||||
'longUrl' => $link,
|
||||
);
|
||||
|
||||
return array(
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/json\r\n" .
|
||||
'X-Api-Key: ' . $shlink_api_key . "\r\n",
|
||||
'content' => Json::encode($body),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the short URL from the shlink API response.
|
||||
*
|
||||
* @access protected
|
||||
* @param array $data
|
||||
* @return ?string
|
||||
*/
|
||||
protected function _extractShortUrl(array $data): ?string
|
||||
{
|
||||
if (
|
||||
array_key_exists('shortUrl', $data)
|
||||
) {
|
||||
return $data['shortUrl'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?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\Proxy;
|
||||
|
||||
use PrivateBin\Configuration;
|
||||
|
||||
/**
|
||||
* YourlsProxy
|
||||
*
|
||||
* Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
|
||||
* the result.
|
||||
*/
|
||||
class YourlsProxy extends AbstractProxy
|
||||
{
|
||||
/**
|
||||
* Overrides the abstract parent function to get the proxy URL.
|
||||
*
|
||||
* @param Configuration $conf
|
||||
* @return string
|
||||
*/
|
||||
protected function _getProxyUrl(Configuration $conf): string
|
||||
{
|
||||
return $conf->getKey('apiurl', 'yourls');
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the abstract parent function to get contents from YOURLS API.
|
||||
*
|
||||
* @access protected
|
||||
* @param Configuration $conf
|
||||
* @param string $link
|
||||
* @return array
|
||||
*/
|
||||
protected function _getProxyPayload(Configuration $conf, string $link): array
|
||||
{
|
||||
return array(
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
|
||||
'content' => http_build_query(
|
||||
array(
|
||||
'signature' => $conf->getKey('signature', 'yourls'),
|
||||
'format' => 'json',
|
||||
'action' => 'shorturl',
|
||||
'url' => $link,
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the short URL from the YOURLS API response.
|
||||
*
|
||||
* @access protected
|
||||
* @param array $data
|
||||
* @return ?string
|
||||
*/
|
||||
protected function _extractShortUrl(array $data): ?string
|
||||
{
|
||||
if (
|
||||
array_key_exists('statusCode', $data) &&
|
||||
$data['statusCode'] == 200 &&
|
||||
array_key_exists('shorturl', $data)
|
||||
) {
|
||||
return $data['shorturl'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user