2022-10-23 08:10:56 +02:00
|
|
|
<?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
|
2022-12-24 05:52:07 +01:00
|
|
|
* @version 1.5.1
|
2022-10-23 08:10:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PrivateBin;
|
|
|
|
|
2022-10-23 08:21:37 +02:00
|
|
|
use Exception;
|
2022-10-23 08:10:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2022-10-29 07:54:42 +02:00
|
|
|
if (strpos($link, $conf->getKey('basepath') . '?') === false) {
|
2022-10-23 10:50:18 +02:00
|
|
|
$this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
|
2022-10-23 08:16:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-10-23 08:10:56 +02:00
|
|
|
|
2022-10-23 13:09:54 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2022-10-23 09:05:17 +02:00
|
|
|
$data = file_get_contents(
|
2022-10-23 13:09:54 +02:00
|
|
|
$yourls_api_url, false, stream_context_create(
|
2022-10-23 09:05:17 +02:00
|
|
|
array(
|
|
|
|
'http' => 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',
|
2022-10-23 09:16:55 +02:00
|
|
|
'url' => $link,
|
2022-10-23 09:05:17 +02:00
|
|
|
)
|
2022-10-23 09:16:55 +02:00
|
|
|
),
|
|
|
|
),
|
2022-10-23 09:05:17 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2022-10-23 08:21:37 +02:00
|
|
|
try {
|
|
|
|
$data = Json::decode($data);
|
|
|
|
} catch (Exception $e) {
|
2022-10-23 13:09:54 +02:00
|
|
|
$this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
|
|
|
|
error_log('Error calling YOURLS: ' . $e->getMessage());
|
2022-10-23 08:21:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-23 08:16:05 +02:00
|
|
|
if (
|
|
|
|
!is_null($data) &&
|
|
|
|
array_key_exists('statusCode', $data) &&
|
2022-10-23 13:09:54 +02:00
|
|
|
$data['statusCode'] == 200 &&
|
|
|
|
array_key_exists('shorturl', $data)
|
2022-10-23 08:16:05 +02:00
|
|
|
) {
|
|
|
|
$this->_url = $data['shorturl'];
|
2022-10-23 08:10:56 +02:00
|
|
|
} else {
|
2022-10-23 08:16:05 +02:00
|
|
|
$this->_error = 'Error parsing YOURLS response.';
|
2022-10-23 08:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|