refactor shortenviayourls.php for our MVC framework

This commit is contained in:
El RIDO 2022-10-23 08:10:56 +02:00
parent 304ae76a04
commit 0dc9ab7576
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
5 changed files with 177 additions and 87 deletions

View file

@ -136,6 +136,9 @@ class Controller
case 'jsonld':
$this->_jsonld($this->_request->getParam('jsonld'));
return;
case 'yourlsproxy':
$this->_yourlsproxy($this->_request->getParam('link'));
break;
}
// output JSON or HTML
@ -378,9 +381,15 @@ class Controller
);
$page = new View;
$page->assign('CSPHEADER', $metacspheader);
$page->assign('ERROR', I18n::_($this->_error));
if ($this->_request->getOperation() === 'yourlsproxy') {
$page->assign('SHORTURL', $this->_status);
$page->draw('yourlsproxy');
return;
}
$page->assign('NAME', $this->_conf->getKey('name'));
$page->assign('BASEPATH', I18n::_($this->_conf->getKey('basepath')));
$page->assign('ERROR', I18n::_($this->_error));
$page->assign('STATUS', I18n::_($this->_status));
$page->assign('VERSION', self::VERSION);
$page->assign('DISCUSSION', $this->_conf->getKey('discussion'));
@ -405,7 +414,6 @@ class Controller
$page->assign('HTTPWARNING', $this->_conf->getKey('httpwarning'));
$page->assign('HTTPSLINK', 'https://' . $this->_request->getHost() . $this->_request->getRequestUri());
$page->assign('COMPRESSION', $this->_conf->getKey('compression'));
$page->assign('CSPHEADER', $metacspheader);
$page->draw($this->_conf->getKey('template'));
}
@ -439,6 +447,22 @@ class Controller
echo $content;
}
/**
* proxies link to YOURLS, updates status or error with response
*
* @access private
* @param string $link
*/
private function _yourlsproxy($link)
{
$yourls = new YourlsProxy($this->_conf, $link);
if ($yourls->isError()) {
$this->_error = $yourls->getError();
} else {
$this->_status = $yourls->getUrl();
}
}
/**
* prepares JSON encoded status message
*

View file

@ -120,6 +120,7 @@ class Request
if (
!array_key_exists('pasteid', $this->_params) &&
!array_key_exists('jsonld', $this->_params) &&
!array_key_exists('link', $this->_params) &&
array_key_exists('QUERY_STRING', $_SERVER) &&
!empty($_SERVER['QUERY_STRING'])
) {
@ -135,6 +136,11 @@ class Request
}
} elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) {
$this->_operation = 'jsonld';
} elseif (array_key_exists('link', $this->_params) && !empty($this->_params['link'])) {
$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
if (strpos($request_url, '/shortenviayourls?link=') !== false) {
$this->_operation = 'yourlsproxy';
}
}
}

120
lib/YourlsProxy.php Normal file
View file

@ -0,0 +1,120 @@
<?php
/**
* 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
* @version 1.4.0
*/
namespace PrivateBin;
use PrivateBin\Configuration;
/**
* YourlsProxy
*
* Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
* the result.
*/
class YourlsProxy
{
/**
* error message
*
* @access private
* @var string
*/
private $_error = '';
/**
* shortened URL
*
* @access private
* @var string
*/
private $_url = '';
/**
* constructor
*
* initializes and runs PrivateBin
*
* @access public
* @param string $link
*/
public function __construct(Configuration $conf, $link)
{
if (strpos($link, $conf->getKey('basepath') . '/?') !== false) {
// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $conf->getKey("apiurl", "yourls"));
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
'signature' => $conf->getKey("signature", "yourls"),
'format' => 'json',
'action' => 'shorturl',
'url' => $link
));
// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);
if (!($data === FALSE) && is_string($data))
{
$data = json_decode( $data, true);
if (!is_null($data) && array_key_exists('statusCode', $data)
&& array_key_exists('shorturl', $data) && ($data['statusCode'] == 200))
{
$this->_url = $data['shorturl'];
$opSuccess = TRUE;
} else {
$this->_error = 'Error parsing YOURLS response.';
}
} else {
$this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
}
} else {
$this->_error = 'Trying to shorten a URL not pointing to our PrivateBin instance.';
}
}
/**
* 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);
}
}

View file

@ -1,85 +0,0 @@
<?php
// change this, if your php files and data is outside of your webservers document root
define('PATH', '');
define('PUBLIC_PATH', __DIR__);
require PATH . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$link = $_SERVER['REQUEST_URI'];
$response = getGetData();
$arr = explode('=',$response);
$c = count ($arr);
$opSuccess = FALSE;
$errCode = 0;
$shortenedUrl = "";
$originalUrl = "";
if(($c == 2) && ($arr[0] == "link") && (strlen($arr[1]) < 256)) {
// read in configuration values
$conf = new PrivateBin\Configuration;
$originalUrl = urldecode($arr[1]);
if (startsWith($originalUrl, $conf->getKey( "basepath") . "/?")) {
// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $conf->getKey( "apiurl", "yourls"));
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
'signature' => $conf->getKey( "signature", "yourls"),
'format' => 'json',
'action' => 'shorturl',
'url' => $originalUrl
));
// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);
if (!($data === FALSE) && is_string($data))
{
$data = json_decode( $data, true);
if (!is_null($data) && array_key_exists('statusCode', $data)
&& array_key_exists('shorturl', $data) && ($data['statusCode'] == 200))
{
$shortenedUrl = $data['shorturl'];
$opSuccess = TRUE;
} else {
// error with contents of YOURLS response.
$errCode = 3;
}
} else {
// error when calling YOURLS - probably a PrivateBin configuration issue, like wrong/missing apiurl or signature
$errCode = 2;
}
} else {
// trying to shorten a URL not pointing to our PrivateBin instance.
$errCode = 1;
}
}
if ($opSuccess)
{
print("<br>Your shortened paste is <span class=\"shortensuccess\"><a href=\"$shortenedUrl\">$shortenedUrl</a></span>");
}
else
{
print("<br><span class=\"shortenerror\">Error: An error occured while trying to shorten the given URL (error code $errCode)</span>");
}
function getGetData() {
$data = http_build_query($_GET);
return $data;
}
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
?>

25
tpl/yourlsproxy.php Normal file
View file

@ -0,0 +1,25 @@
<?php
use PrivateBin\I18n;
?><!DOCTYPE html>
<html lang="<?php echo I18n::_('en'); ?>">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="<?php echo I18n::encode($CSPHEADER); ?>">
<meta name="robots" content="noindex" />
<meta name="google" content="notranslate">
<title><?php echo I18n::_($NAME); ?></title>
</head>
<body>
<?php
if (empty($ERROR)) :
?>
<p><?php echo I18n::_('Your paste is <a id="pasteurl" href="%s">%s</a> <span id="copyhint">(Hit [Ctrl]+[c] to copy)</span>', $SHORTURL, $SHORTURL); ?></p>
<?php
else:
?>
<p><?php echo I18n::_('Could not create paste: %s', $ERROR); ?></p>
<?php
endif;
?>
</body>
</html>