aboutsummaryrefslogtreecommitdiffstats
path: root/storage-backend/lib
diff options
context:
space:
mode:
Diffstat (limited to 'storage-backend/lib')
-rw-r--r--storage-backend/lib/functions.common.inc.php48
-rw-r--r--storage-backend/lib/functions.http.inc.php169
2 files changed, 180 insertions, 37 deletions
diff --git a/storage-backend/lib/functions.common.inc.php b/storage-backend/lib/functions.common.inc.php
index b47268e..d00b555 100644
--- a/storage-backend/lib/functions.common.inc.php
+++ b/storage-backend/lib/functions.common.inc.php
@@ -15,4 +15,50 @@ function generate_uuid() {
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
-?> \ No newline at end of file
+
+function getFromArray($key, $array) {
+ if (array_key_exists($key, $array)
+ && isset($array[$key])
+ && !empty($array[$key])) {
+ return $array[$key];
+ } else {
+ return NULL;
+ }
+}
+
+function format_size($size, $precision = 2) {
+ $sizes = ['bytes', 'Kb', 'Mb', 'Gb', 'Tb'];
+ $i = 0;
+ while (1023 < $size && $i < count($sizes) - 1) {
+ $size /= 1023;
+ ++$i;
+ }
+
+ return number_format($size, $precision).' '.$sizes[$i];
+}
+
+function startsWith($haystack, $needle) {
+ $length = strlen($needle);
+ return (substr($haystack, 0, $length) === $needle);
+}
+
+function endsWith($haystack, $needle) {
+ $length = strlen($needle);
+
+ return $length === 0 || (substr($haystack, -$length) === $needle);
+}
+
+function generatePath($parts, $basePath = __DIR__) {
+ $path = $basePath;
+ if (!is_array($parts)) {
+ $parts = [$parts];
+ }
+ foreach ($parts as $part) {
+ $path .= DIRECTORY_SEPARATOR.generatePathName($part);
+ }
+ return $path;
+}
+
+function generatePathName($name) {
+ return urlencode($name);
+} \ No newline at end of file
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
+?>