aboutsummaryrefslogtreecommitdiffstats
path: root/storage-backend/lib/functions.http.inc.php
diff options
context:
space:
mode:
authorsteckbrief <steckbrief@chefmail.de>2018-10-17 22:08:12 +0200
committersteckbrief <steckbrief@chefmail.de>2018-10-17 22:08:12 +0200
commitbc439cd988dd308a64d0111f766716d54d3648d5 (patch)
tree335ebb5f1e2db53035e959de1a95b96456e618b4 /storage-backend/lib/functions.http.inc.php
parent840bd494f97effaede00d2bbda3d1503887f1f83 (diff)
adds common and http protocol functionalities
Diffstat (limited to 'storage-backend/lib/functions.http.inc.php')
-rw-r--r--storage-backend/lib/functions.http.inc.php169
1 files changed, 133 insertions, 36 deletions
diff --git a/storage-backend/lib/functions.http.inc.php b/storage-backend/lib/functions.http.inc.php
index c508b20..2d4b2b2 100644
--- a/storage-backend/lib/functions.http.inc.php
+++ b/storage-backend/lib/functions.http.inc.php
@@ -6,59 +6,156 @@
*
*/
+require_once('functions.common.inc.php');
+
/**
* Inspired by https://github.com/owncloud/core/blob/master/lib/private/appframework/http/request.php#L523
*/
function getServerProtocol() {
- if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
- if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
- $parts = explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO']);
- $proto = strtolower(trim($parts[0]));
- } else {
- $proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
- }
- // 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';
+ $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() {
- if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
- return strtolower($_SERVER['HTTP_X_FORWARDED_HOST']);
- }
- return strtolower($_SERVER['HTTP_HOST']);
+ $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));
+ return strtolower(substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1));
}
function sendHttpReturnCodeAndJson($code, $data) {
- if (!is_array($data)) {
- $data = ['msg' => $data];
- }
- header('Content-Type: application/json');
- sendHttpReturnCodeAndMessage($code, json_encode($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);
+ http_response_code($code);
+ exit($text);
+}
+
+function setContentType($contentType) {
+ header('Content-Type: '.$contentType);
+}
+
+function getHeaderExtensionValue($headerName) {
+ $headerName = strtoupper($headerName);
+ if (!startsWith($headerName, 'HTTP_X_')) {
+ if (!startsWith($headerName, 'HTTP_')) {
+ $headerName = str_replace('HTTP_', 'HTTP_X_', $headerName);
+ } else {
+ $headerName = 'HTTP_X_'.$headerName;
+ }
+ }
+
+ return getHeaderValue($headerName);
+}
+
+function getHeaderValue($headerName) {
+ $headerName = strtoupper($headerName);
+ if (!startsWith($headerName, 'HTTP_')) {
+ $headerName = 'HTTP_'.$headerName;
+ }
+
+ return getFromArray($headerName, $_SERVER);
+}
+
+function getFileParameter($parameterName) {
+ return getFromArray($parameterName, $_FILES);
+}
+
+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);
+}
+
+function getPostParameter($parameterName) {
+ return getFromArray($parameterName, $_POST);
}
function getOptionalPostParameter($parameterName, $default = NULL) {
- $parameter = $_POST[$parameterName];
- if (!isset($parameter) || is_null($parameter) || empty($parameter)) {
- $parameter = $default;
- }
- return $parameter;
+ $parameter = getPostParameter($parameterName);
+
+ return handleOptionalParameter($parameter, $default);
+}
+
+function getMandatoryPostParameter($parameterName, $message = '', $json = false) {
+ $parameter = getPostParameter($parameterName);
+
+ return handleMandatoryParameter($parameterName, $parameter, $message, $json);
+}
+
+function getGetParameter($parameterName) {
+ return getFromArray($parameterName, $_GET);
+}
+
+function getOptionalGetParameter($parameterName, $default = NULL) {
+ $parameter = getGetParameter($parameterName);
+
+ return handleOptionalParameter($parameter, $default);
+}
+
+function getMandatoryGetParameter($parameterName, $message = '', $json = false) {
+ $parameter = getGetParameter($parameterName);
+
+ return handleMandatoryParameter($parameterName, $parameter, $message, $json);
+}
+
+function handleOptionalParameter($parameter, $default) {
+ if (!isset($parameter) || is_null($parameter) || empty($parameter)) {
+ $parameter = $default;
+ }
+ return $parameter;
+}
+
+function handleMandatoryParameter($parameterName, $parameter, $message, $json) {
+ if (!isset($parameter) || is_null($parameter) || empty($parameter)) {
+ if (empty($message) || is_null($message)) {
+ 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;
}
-?> \ No newline at end of file
+?>