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