1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only |
4
|
|
|
* SPDX-FileCopyrightText: Copyright 2007-2016 Zarafa Deutschland GmbH |
5
|
|
|
* SPDX-FileCopyrightText: Copyright 2020-2022 grommunio GmbH |
6
|
|
|
* |
7
|
|
|
* Help function for files |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
if (!function_exists('apache_request_headers')) { |
11
|
|
|
/** |
12
|
|
|
* When using other webservers or using php as cgi in apache |
13
|
|
|
* the function apache_request_headers() is not available. |
14
|
|
|
* This function parses the environment variables to extract |
15
|
|
|
* the necessary headers for grommunio-sync. |
16
|
|
|
*/ |
17
|
|
|
function apache_request_headers() { |
18
|
|
|
$headers = []; |
19
|
|
|
foreach ($_SERVER as $key => $value) { |
20
|
|
|
if (substr($key, 0, 5) == 'HTTP_') { |
21
|
|
|
$headers[strtr(substr($key, 5), '_', '-')] = $value; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $headers; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (!function_exists('hex2bin')) { |
30
|
|
|
/** |
31
|
|
|
* Complementary function to bin2hex() which converts a hex entryid to a binary entryid. |
32
|
|
|
* Since PHP 5.4 an internal hex2bin() implementation is available. |
33
|
|
|
* |
34
|
|
|
* @param string $data the hexadecimal string |
35
|
|
|
* |
36
|
|
|
* @returns string |
37
|
|
|
*/ |
38
|
|
|
function hex2bin($data) { |
39
|
|
|
return pack("H*", $data); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!function_exists('http_response_code')) { |
44
|
|
|
/** |
45
|
|
|
* http_response_code does not exists in PHP < 5.4 |
46
|
|
|
* http://php.net/manual/en/function.http-response-code.php. |
47
|
|
|
* |
48
|
|
|
* @param null|mixed $code |
49
|
|
|
*/ |
50
|
|
|
function http_response_code($code = null) { |
51
|
|
|
if ($code !== null) { |
52
|
|
|
switch ($code) { |
53
|
|
|
case 100: |
54
|
|
|
$text = 'Continue'; |
55
|
|
|
break; |
56
|
|
|
|
57
|
|
|
case 101: |
58
|
|
|
$text = 'Switching Protocols'; |
59
|
|
|
break; |
60
|
|
|
|
61
|
|
|
case 200: |
62
|
|
|
$text = 'OK'; |
63
|
|
|
break; |
64
|
|
|
|
65
|
|
|
case 201: |
66
|
|
|
$text = 'Created'; |
67
|
|
|
break; |
68
|
|
|
|
69
|
|
|
case 202: |
70
|
|
|
$text = 'Accepted'; |
71
|
|
|
break; |
72
|
|
|
|
73
|
|
|
case 203: |
74
|
|
|
$text = 'Non-Authoritative Information'; |
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
case 204: |
78
|
|
|
$text = 'No Content'; |
79
|
|
|
break; |
80
|
|
|
|
81
|
|
|
case 205: |
82
|
|
|
$text = 'Reset Content'; |
83
|
|
|
break; |
84
|
|
|
|
85
|
|
|
case 206: |
86
|
|
|
$text = 'Partial Content'; |
87
|
|
|
break; |
88
|
|
|
|
89
|
|
|
case 300: |
90
|
|
|
$text = 'Multiple Choices'; |
91
|
|
|
break; |
92
|
|
|
|
93
|
|
|
case 301: |
94
|
|
|
$text = 'Moved Permanently'; |
95
|
|
|
break; |
96
|
|
|
|
97
|
|
|
case 302: |
98
|
|
|
$text = 'Moved Temporarily'; |
99
|
|
|
break; |
100
|
|
|
|
101
|
|
|
case 303: |
102
|
|
|
$text = 'See Other'; |
103
|
|
|
break; |
104
|
|
|
|
105
|
|
|
case 304: |
106
|
|
|
$text = 'Not Modified'; |
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
case 305: |
110
|
|
|
$text = 'Use Proxy'; |
111
|
|
|
break; |
112
|
|
|
|
113
|
|
|
case 400: |
114
|
|
|
$text = 'Bad Request'; |
115
|
|
|
break; |
116
|
|
|
|
117
|
|
|
case 401: |
118
|
|
|
$text = 'Unauthorized'; |
119
|
|
|
break; |
120
|
|
|
|
121
|
|
|
case 402: |
122
|
|
|
$text = 'Payment Required'; |
123
|
|
|
break; |
124
|
|
|
|
125
|
|
|
case 403: |
126
|
|
|
$text = 'Forbidden'; |
127
|
|
|
break; |
128
|
|
|
|
129
|
|
|
case 404: |
130
|
|
|
$text = 'Not Found'; |
131
|
|
|
break; |
132
|
|
|
|
133
|
|
|
case 405: |
134
|
|
|
$text = 'Method Not Allowed'; |
135
|
|
|
break; |
136
|
|
|
|
137
|
|
|
case 406: |
138
|
|
|
$text = 'Not Acceptable'; |
139
|
|
|
break; |
140
|
|
|
|
141
|
|
|
case 407: |
142
|
|
|
$text = 'Proxy Authentication Required'; |
143
|
|
|
break; |
144
|
|
|
|
145
|
|
|
case 408: |
146
|
|
|
$text = 'Request Time-out'; |
147
|
|
|
break; |
148
|
|
|
|
149
|
|
|
case 409: |
150
|
|
|
$text = 'Conflict'; |
151
|
|
|
break; |
152
|
|
|
|
153
|
|
|
case 410: |
154
|
|
|
$text = 'Gone'; |
155
|
|
|
break; |
156
|
|
|
|
157
|
|
|
case 411: |
158
|
|
|
$text = 'Length Required'; |
159
|
|
|
break; |
160
|
|
|
|
161
|
|
|
case 412: |
162
|
|
|
$text = 'Precondition Failed'; |
163
|
|
|
break; |
164
|
|
|
|
165
|
|
|
case 413: |
166
|
|
|
$text = 'Request Entity Too Large'; |
167
|
|
|
break; |
168
|
|
|
|
169
|
|
|
case 414: |
170
|
|
|
$text = 'Request-URI Too Large'; |
171
|
|
|
break; |
172
|
|
|
|
173
|
|
|
case 415: |
174
|
|
|
$text = 'Unsupported Media Type'; |
175
|
|
|
break; |
176
|
|
|
|
177
|
|
|
case 500: |
178
|
|
|
$text = 'Internal Server Error'; |
179
|
|
|
break; |
180
|
|
|
|
181
|
|
|
case 501: |
182
|
|
|
$text = 'Not Implemented'; |
183
|
|
|
break; |
184
|
|
|
|
185
|
|
|
case 502: |
186
|
|
|
$text = 'Bad Gateway'; |
187
|
|
|
break; |
188
|
|
|
|
189
|
|
|
case 503: |
190
|
|
|
$text = 'Service Unavailable'; |
191
|
|
|
break; |
192
|
|
|
|
193
|
|
|
case 504: |
194
|
|
|
$text = 'Gateway Time-out'; |
195
|
|
|
break; |
196
|
|
|
|
197
|
|
|
case 505: |
198
|
|
|
$text = 'HTTP Version not supported'; |
199
|
|
|
break; |
200
|
|
|
|
201
|
|
|
default: |
202
|
|
|
exit('Unknown http status code "' . htmlentities($code) . '"'); |
|
|
|
|
203
|
|
|
break; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); |
207
|
|
|
header($protocol . ' ' . $code . ' ' . $text); |
208
|
|
|
|
209
|
|
|
$GLOBALS['http_response_code'] = $code; |
210
|
|
|
} |
211
|
|
|
else { |
212
|
|
|
$code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $code; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (!function_exists('memory_get_peak_usage')) { |
220
|
|
|
/** |
221
|
|
|
* memory_get_peak_usage is not available prior to PHP 5.2. |
222
|
|
|
* This complementary function will return the value of memory_get_usage();. |
223
|
|
|
* |
224
|
|
|
* @see http://php.net/manual/en/function.memory-get-usage.php |
225
|
|
|
* @see http://php.net/manual/en/function.memory-get-peak-usage.php |
226
|
|
|
* |
227
|
|
|
* @param bool $real_usage |
228
|
|
|
*/ |
229
|
|
|
function memory_get_peak_usage($real_usage = false) { |
|
|
|
|
230
|
|
|
SLog::Write(LOGLEVEL_DEBUG, "memory_get_peak_usage() is not available on this system. The value of memory_get_usage() will be used."); |
231
|
|
|
|
232
|
|
|
return memory_get_usage(); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (!function_exists('dump')) { |
237
|
|
|
function dump($message) { |
238
|
|
|
SLog::Write(LOGLEVEL_DEBUG, $message); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.