Initial commit
This commit is contained in:
commit
03cf2a22fe
2 changed files with 185 additions and 0 deletions
40
functions.common.inc.php
Normal file
40
functions.common.inc.php
Normal file
|
@ -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);
|
||||
}
|
||||
?>
|
145
functions.http.inc.php
Normal file
145
functions.http.inc.php
Normal file
|
@ -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;
|
||||
}
|
||||
?>
|
Loading…
Reference in a new issue