1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
<?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 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);
}
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($parameterName, $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($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)) {
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;
}
?>
|