diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-11-25 23:30:49 +0100 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-11-25 23:30:49 +0100 |
commit | 03cf2a22fe227633cf901dc1d9cf1024b8d07b59 (patch) | |
tree | 7a5a4e2162e088bf745161cb65d0a852e3e51010 |
Initial commit
-rw-r--r-- | functions.common.inc.php | 40 | ||||
-rw-r--r-- | functions.http.inc.php | 145 |
2 files changed, 185 insertions, 0 deletions
diff --git a/functions.common.inc.php b/functions.common.inc.php new file mode 100644 index 0000000..5be37b6 --- /dev/null +++ b/functions.common.inc.php @@ -0,0 +1,40 @@ +<?php +/* + * This file contains functions commonly used. + */ + +/** + * Copied from http://rogerstringer.com/2013/11/15/generate-uuids-php/ + */ +function generate_uuid() { + return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', + mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), + mt_rand( 0, 0xffff ), + mt_rand( 0, 0x0fff ) | 0x4000, + mt_rand( 0, 0x3fff ) | 0x8000, + mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) + ); +} + +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); +} +?> diff --git a/functions.http.inc.php b/functions.http.inc.php new file mode 100644 index 0000000..234e404 --- /dev/null +++ b/functions.http.inc.php @@ -0,0 +1,145 @@ +<?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); + if (!startsWith('HTTP_X_') { + if (!startsWith('HTTP_') { + $headerName = str_replace('HTTP_', 'HTTP_X_', $headerName); + } else { + $headerName = 'HTTP_X_'.$headerName; + } + } + + return getHeaderValue($headerName); +} + +function getHeaderValue($headerName) { + $headerName = strtoupper($headerName); + if (!startsWith('HTTP_') { + $headerName = 'HTTP_'.$headerName; + } + + return $_SERVER[$headerName]; +} + +function getPostParameter($parameterName) { + return $_POST[$parameterName]; +} + +function getOptionalPostParameter($parameterName, $default = NULL) { + $parameter = getPostParameter($parameterName); + + return handleOptionalParameter($parameter, $default); +} + +function getMandatoryPostParameter($parameterName, $message = '', $json = false) { + $parameter = getPostParameter($parameterName); + + return handleMandatoryParameter($parameter, $message, $json); +} + +function getGetParameter($parameterName) { + return $_GET[$parameterName]; +} + +function getOptionalGetParameter($parameterName, $default = NULL) { + $parameter = getPostParameter($parameterName); + + return handleOptionalParameter($parameter, $default); +} + +function getMandatoryGetParameter($parameterName, $message = '', $json = false) { + $parameter = getGetParameter($parameterName); + + return handleMandatoryParameter($parameter, $message, $json); +} + +function handleOptionalParameter($parameter, $default) { + if (!isset($parameter) || is_null($parameter) || empty($parameter)) { + $parameter = $default; + } + return $parameter; +} + +function handleMandatoryParameter($parameter, $message, $json) { + if (!isset($parameter) || is_null($parameter) || empty($parameter)) { + if (empty($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; +} +?> |