Merge branch 'serverside_yourls' of https://github.com/jmozd/PrivateBin into serverside_yourls

pull/997/head
El RIDO 2022-10-23 05:43:18 +02:00
commit 304ae76a04
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
3 changed files with 101 additions and 0 deletions

View File

@ -227,3 +227,15 @@ dir = PATH "data"
;bucket = "my-bucket"
;accesskey = "access key id"
;secretkey = "secret access key"
[yourls]
; don't mix this up with "urlshortener" config item:
; - when using a standard configuration, "urlshortener" will point to the YOURLS API, including access credentials, and will be part of the PrivateBin public web page (insecure!)
; - when using the parameters in this section ("signature" and "apiurl"), "urlshortener" will point to a fixed PrivateBin page ("$basepath/shortenviayourls.php") and
; that PHP will in turn call YOURLS server-side, using the URL from "apiurl" and using the "access signature" from "signature" parameter.
; (optional) the "signature" (access key) issued by YOURLS for the using account
; signature = ""
; (optional) the URL of the YOURLS API, called to shorten a PrivateBin URL
; apiurl = ""

View File

@ -93,6 +93,10 @@ class Configuration
'model_options' => array(
'dir' => 'data',
),
'yourls' => array(
'signature' => '',
'apiurl' => '',
),
);
/**

85
shortenviayourls.php Normal file
View File

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