2017-11-25 23:30:49 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* This file contains functions to be used to
|
|
|
|
* extract information based on http request information.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once('functions.common.inc.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inspired by https://github.com/owncloud/core/blob/master/lib/private/appframework/http/request.php#L523
|
|
|
|
*/
|
|
|
|
function getServerProtocol() {
|
|
|
|
$protocol = getHeaderExtensionValue('FORWARDED_PROTO');
|
|
|
|
if (isset($protocol)) {
|
|
|
|
if (strpos($protocol, ',') !== false) {
|
|
|
|
$parts = explode(',', $protocol);
|
|
|
|
$proto = strtolower(trim($parts[0]));
|
|
|
|
} else {
|
|
|
|
$proto = strtolower($protocol);
|
|
|
|
}
|
|
|
|
// Verify that the protocol is always HTTP or HTTPS
|
|
|
|
// default to http if an invalid value is provided
|
|
|
|
return $proto === 'https' ? 'https' : 'http';
|
|
|
|
}
|
|
|
|
if (isset($_SERVER['HTTPS'])
|
|
|
|
&& $_SERVER['HTTPS'] !== null
|
|
|
|
&& $_SERVER['HTTPS'] !== 'off'
|
|
|
|
&& $_SERVER['HTTPS'] !== '') {
|
|
|
|
return 'https';
|
|
|
|
}
|
|
|
|
return 'http';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRequestHostname() {
|
|
|
|
$forwardedHost = getHeaderExtensionValue('FORWARDED_HOST');
|
|
|
|
if (isset($forwardedHost)) {
|
|
|
|
return strtolower($forwardedHost);
|
|
|
|
}
|
|
|
|
return strtolower(getHeaderValue('HOST'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRequestUriWithoutFilename() {
|
|
|
|
return strtolower(substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendHttpReturnCodeAndJson($code, $data) {
|
|
|
|
if (!is_array($data)) {
|
|
|
|
$data = ['msg' => $data];
|
|
|
|
}
|
|
|
|
|
|
|
|
setContentType('application/json');
|
|
|
|
|
|
|
|
sendHttpReturnCodeAndMessage($code, json_encode($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendHttpReturnCodeAndMessage($code, $text = '') {
|
|
|
|
http_response_code($code);
|
|
|
|
exit($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setContentType($contentType) {
|
|
|
|
header('Content-Type: '.$contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHeaderExtensionValue($headerName) {
|
|
|
|
$headerName = strtoupper($headerName);
|
2017-11-26 13:45:07 +01:00
|
|
|
if (!startsWith('HTTP_X_')) {
|
|
|
|
if (!startsWith('HTTP_')) {
|
2017-11-25 23:30:49 +01:00
|
|
|
$headerName = str_replace('HTTP_', 'HTTP_X_', $headerName);
|
|
|
|
} else {
|
|
|
|
$headerName = 'HTTP_X_'.$headerName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return getHeaderValue($headerName);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHeaderValue($headerName) {
|
|
|
|
$headerName = strtoupper($headerName);
|
2017-11-26 13:45:07 +01:00
|
|
|
if (!startsWith('HTTP_')) {
|
2017-11-25 23:30:49 +01:00
|
|
|
$headerName = 'HTTP_'.$headerName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $_SERVER[$headerName];
|
|
|
|
}
|
|
|
|
|
2017-12-02 00:13:43 +01:00
|
|
|
function getFileParameter($parameterName) {
|
|
|
|
return $_FILES[$parameterName];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionalFileParameter($parameterName, $default = NULL) {
|
|
|
|
$parameter = getFileParameter($parameterName);
|
|
|
|
|
|
|
|
return handleOptionalParameter($parameter, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMandatoryFileParameter($parameterName, $message = '', $json = false) {
|
|
|
|
$parameter = getFileParameter($parameterName);
|
|
|
|
|
|
|
|
return handleMandatoryParameter($parameterName, $parameter, $message, $json);
|
|
|
|
}
|
|
|
|
|
2017-11-25 23:30:49 +01:00
|
|
|
function getPostParameter($parameterName) {
|
2017-12-10 20:20:21 +01:00
|
|
|
if (array_key_exists($parameterName, $_POST)
|
|
|
|
&& isset($_POST[$parameterName])
|
|
|
|
&& !empty($_POST[$parameterName])) {
|
|
|
|
return $_POST[$parameterName];
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-25 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionalPostParameter($parameterName, $default = NULL) {
|
|
|
|
$parameter = getPostParameter($parameterName);
|
|
|
|
|
|
|
|
return handleOptionalParameter($parameter, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMandatoryPostParameter($parameterName, $message = '', $json = false) {
|
|
|
|
$parameter = getPostParameter($parameterName);
|
|
|
|
|
2017-11-26 13:45:07 +01:00
|
|
|
return handleMandatoryParameter($parameterName, $parameter, $message, $json);
|
2017-11-25 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getGetParameter($parameterName) {
|
2017-12-25 22:37:25 +01:00
|
|
|
if (array_key_exists($parameterName, $_GET)
|
|
|
|
&& isset($_GET[$parameterName])
|
|
|
|
&& !empty($_GET[$parameterName])) {
|
|
|
|
return $_GET[$parameterName];
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-25 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionalGetParameter($parameterName, $default = NULL) {
|
2017-12-10 20:20:21 +01:00
|
|
|
$parameter = getGetParameter($parameterName);
|
2017-11-25 23:30:49 +01:00
|
|
|
|
|
|
|
return handleOptionalParameter($parameter, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMandatoryGetParameter($parameterName, $message = '', $json = false) {
|
|
|
|
$parameter = getGetParameter($parameterName);
|
|
|
|
|
2017-11-26 13:45:07 +01:00
|
|
|
return handleMandatoryParameter($parameterName, $parameter, $message, $json);
|
2017-11-25 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleOptionalParameter($parameter, $default) {
|
|
|
|
if (!isset($parameter) || is_null($parameter) || empty($parameter)) {
|
|
|
|
$parameter = $default;
|
|
|
|
}
|
|
|
|
return $parameter;
|
|
|
|
}
|
|
|
|
|
2017-11-26 13:45:07 +01:00
|
|
|
function handleMandatoryParameter($parameterName, $parameter, $message, $json) {
|
2017-11-25 23:30:49 +01:00
|
|
|
if (!isset($parameter) || is_null($parameter) || empty($parameter)) {
|
2017-12-10 20:20:21 +01:00
|
|
|
if (empty($message) || is_null($message)) {
|
2017-11-25 23:30:49 +01:00
|
|
|
if ($json) {
|
|
|
|
$message = ['msg' => 'Missing parameter.', 'parameters' => ['missing_parameter' => $parameterName]];
|
|
|
|
} else {
|
|
|
|
$message = 'Missing mandatory parameter "'.$parameterName.'".';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$json) {
|
|
|
|
sendHttpReturnCodeAndMessage(400, $message);
|
|
|
|
} else {
|
|
|
|
sendHttpReturnCodeAndJson(400, $message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $parameter;
|
|
|
|
}
|
|
|
|
?>
|