refactor: added AbstractProxy base class for shortener proxies

This commit is contained in:
Karthik Kasturi 2025-08-15 23:28:44 +01:00
parent 714e455479
commit 4a39a2ad0f
12 changed files with 221 additions and 216 deletions

139
lib/AbstractProxy.php Normal file
View file

@ -0,0 +1,139 @@
<?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;
}

View file

@ -149,10 +149,10 @@ class Controller
$this->_jsonld($this->_request->getParam('jsonld'));
return;
case 'yourlsproxy':
$this->_yourlsproxy($this->_request->getParam('link'));
$this->_shortenerproxy($this->_request->getParam('link'), YourlsProxy::class);
break;
case 'shlinkproxy':
$this->_shlinkproxy($this->_request->getParam('link'));
$this->_shortenerproxy($this->_request->getParam('link'), ShlinkProxy::class);
break;
}
@ -454,12 +454,12 @@ class Controller
$page->assign('NAME', $this->_conf->getKey('name'));
if ($this->_request->getOperation() === 'yourlsproxy') {
$page->assign('SHORTURL', $this->_status);
$page->draw('yourlsproxy');
$page->draw('shortenerproxy');
return;
}
if ($this->_request->getOperation() === 'shlinkproxy') {
$page->assign('SHORTURL', $this->_status);
$page->draw('shlinkproxy');
$page->draw('shortenerproxy');
return;
}
$page->assign('BASEPATH', I18n::_($this->_conf->getKey('basepath')));
@ -536,34 +536,23 @@ class Controller
}
/**
* proxies link to YOURLS, updates status or error with response
* Proxies a link using the specified proxy class, and updates the status or error with the response.
*
* @access private
* @param string $link
* @param string $link The link to be proxied.
* @param string $proxyClass The fully qualified class name of the proxy to use.
*/
private function _yourlsproxy($link)
private function _shortenerproxy($link, $proxyClass)
{
$yourls = new YourlsProxy($this->_conf, $link);
if ($yourls->isError()) {
$this->_error = $yourls->getError();
} else {
$this->_status = $yourls->getUrl();
if (!is_subclass_of($proxyClass, AbstractProxy::class)) {
$this->_error = 'Invalid proxy class.';
return;
}
}
/**
* proxies link to SHLINK, updates status or error with response
*
* @access private
* @param string $link
*/
private function _shlinkproxy($link)
{
$shlink = new ShlinkProxy($this->_conf, $link);
if ($shlink->isError()) {
$this->_error = $shlink->getError();
$proxy = new $proxyClass($this->_conf, $link);
if ($proxy->isError()) {
$this->_error = $proxy->getError();
} else {
$this->_status = $shlink->getUrl();
$this->_status = $proxy->getUrl();
}
}

View file

@ -11,130 +11,74 @@
namespace PrivateBin;
use Exception;
/**
* ShlinkProxy
*
* Forwards a URL for shortening to shlink and stores the result.
*/
class ShlinkProxy
class ShlinkProxy extends AbstractProxy
{
/**
* error message
*
* @access private
* @var string
*/
private $_error = '';
/**
* shortened URL
*
* @access private
* @var string
*/
private $_url = '';
/**
* constructor
*
* initializes and runs PrivateBin
* initializes and runs ShlinkProxy
*
* @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;
}
parent::__construct($conf, $link);
}
/**
* Overrides the abstract parent function to get contents from Shlink API.
*
* @access protected
* @return string
*/
protected function _getcontents(Configuration $conf, string $link)
{
$shlink_api_url = $conf->getKey('apiurl', 'shlink');
$shlink_api_key = $conf->getKey('apikey', 'shlink');
if (empty($shlink_api_url) || empty($shlink_api_key)) {
$this->_error = 'Error calling Shlink. Probably a configuration issue, like wrong or missing "apiurl" or "apikey".';
return;
}
$data = file_get_contents(
$body = array(
'longUrl' => $link,
);
return file_get_contents(
$shlink_api_url, false, stream_context_create(
array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n" .
'X-Api-Key: ' . $shlink_api_key . "\r\n",
'content' => json_encode(
array(
'longUrl' => $link,
)
),
'content' => Json::encode($body),
),
)
)
);
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 shlink. HTTP request failed for URL ' . $shlink_api_url . '. Status code: ' . $statusCode;
error_log('Error calling shlink: HTTP request failed for URL ' . $shlink_api_url . '. Status code: ' . $statusCode);
return;
}
try {
$data = Json::decode($data);
} catch (Exception $e) {
$this->_error = 'Error calling shlink. Probably a configuration issue, like wrong or missing "apiurl" or "apikey".';
error_log('Error calling shlink: ' . $e->getMessage());
return;
}
}
/**
* Extracts the short URL from the shlink API response.
*
* @access protected
* @param array $data
* @return ?string
*/
protected function _extractShortUrl(array $data): ?string
{
if (
!is_null($data) &&
// array_key_exists('statusCode', $data) &&
// $data['statusCode'] == 200 &&
array_key_exists('shortUrl', $data)
) {
$this->_url = $data['shortUrl'];
} else {
$this->_error = 'Error parsing shlink response.';
return $data['shortUrl'];
}
}
/**
* 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);
return null;
}
}

View file

@ -11,54 +11,42 @@
namespace PrivateBin;
use Exception;
/**
* YourlsProxy
*
* Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
* the result.
*/
class YourlsProxy
class YourlsProxy extends AbstractProxy
{
/**
* error message
*
* @access private
* @var string
*/
private $_error = '';
/**
* shortened URL
*
* @access private
* @var string
*/
private $_url = '';
/**
* constructor
*
* initializes and runs PrivateBin
* initializes and runs YourlsProxy
*
* @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;
}
parent::__construct($conf, $link);
}
/**
* Overrides the abstract parent function to get contents from YOURLS API.
*
* @access protected
* @return string
*/
protected function _getcontents(Configuration $conf, string $link)
{
$yourls_api_url = $conf->getKey('apiurl', 'yourls');
if (empty($yourls_api_url)) {
$this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
return;
return null;
}
$data = file_get_contents(
return file_get_contents(
$yourls_api_url, false, stream_context_create(
array(
'http' => array(
@ -76,56 +64,25 @@ class YourlsProxy
)
)
);
try {
$data = Json::decode($data);
} catch (Exception $e) {
$this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
error_log('Error calling YOURLS: ' . $e->getMessage());
return;
}
}
/**
* Extracts the short URL from the YOURLS API response.
*
* @access protected
* @param array $data
* @return ?string
*/
protected function _extractShortUrl(array $data): ?string
{
if (
!is_null($data) &&
array_key_exists('statusCode', $data) &&
$data['statusCode'] == 200 &&
array_key_exists('shorturl', $data)
) {
$this->_url = $data['shorturl'];
} else {
$this->_error = 'Error parsing YOURLS response.';
return $data['shorturl'];
}
}
/**
* 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);
return null;
}
}