2015-09-27 20:34:39 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* PrivateBin
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2015-09-27 20:34:39 +02:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 13:56:52 +02:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2018-08-11 19:29:58 +02:00
|
|
|
* @version 1.2.1
|
2015-09-27 20:34:39 +02:00
|
|
|
*/
|
2016-12-12 18:43:23 +01:00
|
|
|
|
2016-12-12 18:50:00 +01:00
|
|
|
namespace PrivateBin;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2015-09-27 20:34:39 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* Request
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* parses request parameters and provides helper functions for routing
|
2016-08-15 16:45:47 +02:00
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class Request
|
2015-09-27 20:34:39 +02:00
|
|
|
{
|
2016-04-08 23:29:44 +02:00
|
|
|
/**
|
|
|
|
* MIME type for JSON
|
|
|
|
*
|
|
|
|
* @const string
|
|
|
|
*/
|
|
|
|
const MIME_JSON = 'application/json';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MIME type for HTML
|
|
|
|
*
|
|
|
|
* @const string
|
|
|
|
*/
|
|
|
|
const MIME_HTML = 'text/html';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MIME type for XHTML
|
|
|
|
*
|
|
|
|
* @const string
|
|
|
|
*/
|
|
|
|
const MIME_XHTML = 'application/xhtml+xml';
|
|
|
|
|
2015-09-27 20:34:39 +02:00
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Input stream to use for PUT parameter parsing
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-10-11 18:50:48 +02:00
|
|
|
private static $_inputStream = 'php://input';
|
2015-09-27 20:34:39 +02:00
|
|
|
|
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Operation to perform
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $_operation = 'view';
|
|
|
|
|
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Request parameters
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_params = array();
|
|
|
|
|
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* If we are in a JSON API context
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $_isJsonApi = false;
|
|
|
|
|
2019-01-21 23:06:25 +01:00
|
|
|
/**
|
|
|
|
* Return the paste ID of the current paste.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getPasteId()
|
|
|
|
{
|
2019-01-21 23:19:41 +01:00
|
|
|
// RegEx to check for valid paste ID (16 base64 chars)
|
|
|
|
$pasteIdRegEx = '/^[a-f0-9]{16}$/';
|
|
|
|
|
|
|
|
foreach ($_GET as $key => $value) {
|
|
|
|
// only return if value is empty and key matches RegEx
|
2019-01-22 00:12:02 +01:00
|
|
|
if (($value === '') and preg_match($pasteIdRegEx, $key, $match)) {
|
2019-01-21 23:19:41 +01:00
|
|
|
return $match[0];
|
2019-01-22 00:12:02 +01:00
|
|
|
}
|
2019-01-21 23:19:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 'invalid id';
|
2019-01-21 23:06:25 +01:00
|
|
|
}
|
|
|
|
|
2015-09-27 20:34:39 +02:00
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Constructor
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// decide if we are in JSON API or HTML context
|
2016-04-08 23:29:44 +02:00
|
|
|
$this->_isJsonApi = $this->_detectJsonRequest();
|
2015-09-27 20:34:39 +02:00
|
|
|
|
|
|
|
// parse parameters, depending on request type
|
2016-07-26 08:19:35 +02:00
|
|
|
switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET') {
|
2015-10-11 21:22:00 +02:00
|
|
|
case 'DELETE':
|
2015-09-27 20:34:39 +02:00
|
|
|
case 'PUT':
|
|
|
|
case 'POST':
|
2019-05-13 22:31:52 +02:00
|
|
|
$this->_params = Json::decode(
|
|
|
|
file_get_contents(self::$_inputStream)
|
|
|
|
);
|
2015-09-27 20:34:39 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->_params = $_GET;
|
|
|
|
}
|
2015-10-15 22:04:57 +02:00
|
|
|
if (
|
|
|
|
!array_key_exists('pasteid', $this->_params) &&
|
2015-10-18 14:37:58 +02:00
|
|
|
!array_key_exists('jsonld', $this->_params) &&
|
2015-10-15 22:04:57 +02:00
|
|
|
array_key_exists('QUERY_STRING', $_SERVER) &&
|
|
|
|
!empty($_SERVER['QUERY_STRING'])
|
2016-07-26 08:19:35 +02:00
|
|
|
) {
|
2019-01-21 23:06:25 +01:00
|
|
|
$this->_params['pasteid'] = $this->getPasteId();
|
2015-10-11 21:22:00 +02:00
|
|
|
}
|
2015-09-27 20:34:39 +02:00
|
|
|
|
2015-10-11 21:22:00 +02:00
|
|
|
// prepare operation, depending on current parameters
|
2015-09-27 20:34:39 +02:00
|
|
|
if (
|
2019-05-08 22:11:21 +02:00
|
|
|
array_key_exists('ct', $this->_params) &&
|
|
|
|
!empty($this->_params['ct'])
|
2016-07-26 08:19:35 +02:00
|
|
|
) {
|
2015-09-27 20:34:39 +02:00
|
|
|
$this->_operation = 'create';
|
2016-07-26 08:19:35 +02:00
|
|
|
} elseif (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid'])) {
|
|
|
|
if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken'])) {
|
2015-10-11 21:22:00 +02:00
|
|
|
$this->_operation = 'delete';
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2015-10-11 21:22:00 +02:00
|
|
|
$this->_operation = 'read';
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
} elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) {
|
2015-10-18 14:37:58 +02:00
|
|
|
$this->_operation = 'jsonld';
|
|
|
|
}
|
2015-09-27 20:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Get current operation
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOperation()
|
|
|
|
{
|
|
|
|
return $this->_operation;
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:11:21 +02:00
|
|
|
/**
|
|
|
|
* Get data of paste or comment
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getData()
|
|
|
|
{
|
|
|
|
$data = array(
|
2019-05-19 10:13:47 +02:00
|
|
|
'adata' => $this->getParam('adata'),
|
2019-05-08 22:11:21 +02:00
|
|
|
);
|
|
|
|
$required_keys = array('v', 'ct');
|
2019-05-19 10:13:47 +02:00
|
|
|
$meta = $this->getParam('meta');
|
2019-05-08 22:11:21 +02:00
|
|
|
if (empty($meta)) {
|
|
|
|
$required_keys[] = 'pasteid';
|
|
|
|
$required_keys[] = 'parentid';
|
|
|
|
} else {
|
2019-05-13 22:31:52 +02:00
|
|
|
$data['meta'] = $meta;
|
2019-05-08 22:11:21 +02:00
|
|
|
}
|
|
|
|
foreach ($required_keys as $key) {
|
|
|
|
$data[$key] = $this->getParam($key);
|
|
|
|
}
|
|
|
|
// forcing a cast to int or float
|
|
|
|
$data['v'] = $data['v'] + 0;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-09-27 20:34:39 +02:00
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Get a request parameter
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $param
|
2019-05-19 10:13:47 +02:00
|
|
|
* @param string $default
|
|
|
|
* @return string
|
2015-09-27 20:34:39 +02:00
|
|
|
*/
|
|
|
|
public function getParam($param, $default = '')
|
|
|
|
{
|
2018-01-06 10:27:58 +01:00
|
|
|
return array_key_exists($param, $this->_params) ?
|
|
|
|
$this->_params[$param] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get request URI
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRequestUri()
|
|
|
|
{
|
|
|
|
return array_key_exists('REQUEST_URI', $_SERVER) ?
|
2018-12-17 19:42:26 +01:00
|
|
|
htmlspecialchars(
|
|
|
|
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
|
|
|
|
) : '/';
|
2015-09-27 20:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* If we are in a JSON API context
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isJsonApiCall()
|
|
|
|
{
|
|
|
|
return $this->_isJsonApi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Override the default input stream source, used for unit testing
|
2015-09-27 20:34:39 +02:00
|
|
|
*
|
2015-11-09 21:39:42 +01:00
|
|
|
* @param string $input
|
2015-09-27 20:34:39 +02:00
|
|
|
*/
|
2015-10-11 18:50:48 +02:00
|
|
|
public static function setInputStream($input)
|
2015-09-27 20:34:39 +02:00
|
|
|
{
|
2015-10-11 18:50:48 +02:00
|
|
|
self::$_inputStream = $input;
|
2015-09-27 20:34:39 +02:00
|
|
|
}
|
2016-04-08 23:29:44 +02:00
|
|
|
|
|
|
|
/**
|
2017-03-25 00:58:59 +01:00
|
|
|
* Detect the clients supported media type and decide if its a JSON API call or not
|
2016-04-08 23:29:44 +02:00
|
|
|
*
|
2016-07-19 13:56:52 +02:00
|
|
|
* Adapted from: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2016-04-08 23:29:44 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function _detectJsonRequest()
|
|
|
|
{
|
|
|
|
$hasAcceptHeader = array_key_exists('HTTP_ACCEPT', $_SERVER);
|
2016-08-15 16:45:47 +02:00
|
|
|
$acceptHeader = $hasAcceptHeader ? $_SERVER['HTTP_ACCEPT'] : '';
|
2016-04-08 23:29:44 +02:00
|
|
|
|
|
|
|
// simple cases
|
|
|
|
if (
|
|
|
|
(array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
|
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
|
|
|
|
($hasAcceptHeader &&
|
|
|
|
strpos($acceptHeader, self::MIME_JSON) !== false &&
|
|
|
|
strpos($acceptHeader, self::MIME_HTML) === false &&
|
|
|
|
strpos($acceptHeader, self::MIME_XHTML) === false)
|
2016-07-26 08:19:35 +02:00
|
|
|
) {
|
2016-04-08 23:29:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// advanced case: media type negotiation
|
|
|
|
$mediaTypes = array();
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($hasAcceptHeader) {
|
2016-04-08 23:29:44 +02:00
|
|
|
$mediaTypeRanges = explode(',', trim($acceptHeader));
|
2016-07-26 08:19:35 +02:00
|
|
|
foreach ($mediaTypeRanges as $mediaTypeRange) {
|
2016-04-08 23:29:44 +02:00
|
|
|
if (preg_match(
|
|
|
|
'#(\*/\*|[a-z\-]+/[a-z\-+*]+(?:\s*;\s*[^q]\S*)*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?#',
|
|
|
|
trim($mediaTypeRange), $match
|
2016-07-26 08:19:35 +02:00
|
|
|
)) {
|
|
|
|
if (!isset($match[2])) {
|
2016-04-08 23:29:44 +02:00
|
|
|
$match[2] = '1.0';
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2016-04-08 23:29:44 +02:00
|
|
|
$match[2] = (string) floatval($match[2]);
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!isset($mediaTypes[$match[2]])) {
|
2016-04-08 23:29:44 +02:00
|
|
|
$mediaTypes[$match[2]] = array();
|
|
|
|
}
|
|
|
|
$mediaTypes[$match[2]][] = strtolower($match[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
krsort($mediaTypes);
|
2016-07-26 08:19:35 +02:00
|
|
|
foreach ($mediaTypes as $acceptedQuality => $acceptedValues) {
|
|
|
|
if ($acceptedQuality === 0.0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($acceptedValues as $acceptedValue) {
|
2016-04-08 23:29:44 +02:00
|
|
|
if (
|
|
|
|
strpos($acceptedValue, self::MIME_HTML) === 0 ||
|
|
|
|
strpos($acceptedValue, self::MIME_XHTML) === 0
|
2016-07-26 08:19:35 +02:00
|
|
|
) {
|
2016-04-08 23:29:44 +02:00
|
|
|
return false;
|
2016-07-26 08:19:35 +02:00
|
|
|
} elseif (strpos($acceptedValue, self::MIME_JSON) === 0) {
|
2016-04-08 23:29:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-11 14:15:20 +02:00
|
|
|
}
|