|
1
|
|
|
<?php |
|
2
|
|
|
class holt45 { |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
const DATA_URI_TRANSPARENT_GIF = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; |
|
5
|
|
|
const DATA_URI_TRANSPARENT_PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Check $_GET |
|
10
|
|
|
* |
|
11
|
|
|
* @example if(chk_get("s") == "a") instead of if(isset($_GET["s"]) && $_GET["s"] == "a") |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $key Get-key... |
|
14
|
|
|
* @return bool |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function chk_get($key) { |
|
|
|
|
|
|
17
|
|
|
if (!isset($_GET[$key])) { |
|
18
|
|
|
return false; |
|
19
|
|
|
} |
|
20
|
|
|
return $_GET[$key]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Check $_POST |
|
25
|
|
|
* |
|
26
|
|
|
* @example if(chk_post("s") == "a") instead of if(isset($_POST["s"]) && $_POST["s"] == "a") |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $key Post-key... |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function chk_post($key) { |
|
|
|
|
|
|
32
|
|
|
if (!isset($_POST[$key])) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
return $_POST[$key]; |
|
36
|
|
|
} |
|
37
|
|
|
/** |
|
38
|
|
|
* Assign value from $_GET |
|
39
|
|
|
* |
|
40
|
|
|
* @example $var = assign_from_get("a") instead of $var = ((!empty($_GET["s"])) ? $_GET["s"] : ""); |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function assign_from_get($key) { |
|
|
|
|
|
|
43
|
|
|
return ((!isset($_GET[$key])) ? "" : $_GET[$key]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Assign value from $_POST |
|
48
|
|
|
* |
|
49
|
|
|
* @example $var = assign_from_post("a") instead of $var = ((!empty($_POST["s"])) ? $_POST["s"] : ""); |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function assign_from_post($key) { |
|
|
|
|
|
|
52
|
|
|
return ((!isset($_POST[$key])) ? "" : $_POST[$key]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check multiple $_GET-keys |
|
57
|
|
|
* |
|
58
|
|
|
* @example if(chk_get_all(array("a","b"))) instead of if(!empty($_GET["a"]) && !empty($_GET["b"])) |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $keys |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public static function chk_get_all($keys) { |
|
|
|
|
|
|
64
|
|
|
$s = true; |
|
65
|
|
|
|
|
66
|
|
|
foreach($keys AS $key) { |
|
67
|
|
|
|
|
68
|
|
|
if (empty($_GET[$key])) { |
|
69
|
|
|
$s = false; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
return $s; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Check multiple $_POST-keys |
|
77
|
|
|
* |
|
78
|
|
|
* @example if(chk_post_all(array("a","b"))) instead of if(!empty($_POST["a"]) && !empty($_POST["b"])) |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $keys |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
public static function chk_post_all($keys) { |
|
|
|
|
|
|
84
|
|
|
$s = true; |
|
85
|
|
|
|
|
86
|
|
|
foreach($keys AS $key) { |
|
87
|
|
|
|
|
88
|
|
|
if (empty($_POST[$key])) { |
|
89
|
|
|
$s = false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
return $s; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Handle sessions - set session |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $name Session name |
|
99
|
|
|
* @param string $content Session content |
|
100
|
|
|
* @param int $expires How many seconds before the session should expire. |
|
101
|
|
|
*/ |
|
102
|
|
|
function session_set($name, $content, $expires = 86400) { |
|
|
|
|
|
|
103
|
|
|
$_SESSION[$name] = (time() + $expires).'-'.$content; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Handle sessions - check if session is set and not expired. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $name Session name |
|
110
|
|
|
* @return bool Session status |
|
111
|
|
|
*/ |
|
112
|
|
|
function session_isset($name) { |
|
|
|
|
|
|
113
|
|
|
if(isset($_SESSION[$name])) { |
|
114
|
|
|
$expires = current(explode("-",$_SESSION[$name])); |
|
115
|
|
|
if(ctype_digit($expires) && $expires > time()) { |
|
116
|
|
|
return true; |
|
117
|
|
|
|
|
118
|
|
|
} else { |
|
119
|
|
|
unset($_SESSION[$name]); |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
} else { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Handle sessions - Get session content |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $name Session name |
|
131
|
|
|
* @return string Session content |
|
132
|
|
|
*/ |
|
133
|
|
|
function session_read($name) { |
|
|
|
|
|
|
134
|
|
|
return substr(strstr($_SESSION[$name], '-'),1); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Handle sessions - Delete session |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $name Session name |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
|
|
function session_delete($name) { |
|
|
|
|
|
|
144
|
|
|
unset($_SESSION[$name]); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Convert timestamp to HTTP-date (RFC2616) |
|
149
|
|
|
* |
|
150
|
|
|
* For use in "Last-Modified" headers. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $timestamp |
|
153
|
|
|
* @return string |
|
|
|
|
|
|
154
|
|
|
*/ |
|
155
|
|
|
public static function timestamp_to_http_date($timestamp) { |
|
156
|
|
|
if($timestamp == NULL) { return NULL; } |
|
157
|
|
|
return gmdate("D, d M Y H:i:s T", strtotime($timestamp)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get client ip-address |
|
162
|
|
|
* |
|
163
|
|
|
* @return string User ip-address |
|
164
|
|
|
*/ |
|
165
|
|
|
public static function get_client_ip_address() { |
|
166
|
|
|
|
|
167
|
|
|
if (getenv('HTTP_CLIENT_IP')) |
|
168
|
|
|
return getenv('HTTP_CLIENT_IP'); |
|
169
|
|
|
else if(getenv('HTTP_X_FORWARDED_FOR')) |
|
170
|
|
|
return getenv('HTTP_X_FORWARDED_FOR'); |
|
171
|
|
|
else if(getenv('HTTP_X_FORWARDED')) |
|
172
|
|
|
return getenv('HTTP_X_FORWARDED'); |
|
173
|
|
|
else if(getenv('HTTP_FORWARDED_FOR')) |
|
174
|
|
|
return getenv('HTTP_FORWARDED_FOR'); |
|
175
|
|
|
else if(getenv('HTTP_FORWARDED')) |
|
176
|
|
|
return getenv('HTTP_FORWARDED'); |
|
177
|
|
|
else if(getenv('REMOTE_ADDR')) |
|
178
|
|
|
return getenv('REMOTE_ADDR'); |
|
179
|
|
|
else |
|
180
|
|
|
return '127.0.0.1'; // Unknown IP |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* parse url, try to correct errors and return valid url + display-url. |
|
185
|
|
|
* |
|
186
|
|
|
* @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
|
187
|
|
|
* @example gopher:/ww.example.com => gopher://www.example.com |
|
188
|
|
|
* @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
|
189
|
|
|
* @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
|
190
|
|
|
* @example .example.com/ => http://example.com/ |
|
191
|
|
|
* @example example.com =>http://example.com |
|
192
|
|
|
* @example subdomain.example.com => http://subdomain.example.com |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $url Any somewhat valid url. |
|
195
|
|
|
* @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
|
|
|
|
|
|
196
|
|
|
*/ |
|
197
|
|
|
public static function url_parser($url) { |
|
198
|
|
|
|
|
199
|
|
|
// multiple /// messes up parse_url, replace 3 or more with 2 |
|
200
|
|
|
$url = preg_replace('/(\/{2,})/','//',$url); |
|
201
|
|
|
|
|
202
|
|
|
$parse_url = parse_url($url); |
|
203
|
|
|
|
|
204
|
|
|
if(empty($parse_url["scheme"])) { |
|
205
|
|
|
$parse_url["scheme"] = "http"; |
|
206
|
|
|
} |
|
207
|
|
|
if(empty($parse_url["host"]) && !empty($parse_url["path"])) { |
|
208
|
|
|
// Strip slash from the beginning of path |
|
209
|
|
|
$parse_url["host"] = ltrim($parse_url["path"], '\/'); |
|
210
|
|
|
$parse_url["path"] = ""; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$url_array = array("url" => "", "url_display" => ""); |
|
214
|
|
|
|
|
215
|
|
|
// Check if scheme is correct |
|
216
|
|
|
if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) { |
|
217
|
|
|
$url_array["url"] .= 'http'.'://'; |
|
218
|
|
|
} else { |
|
219
|
|
|
$url_array["url"] .= $parse_url["scheme"].'://'; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
// Check if the right amount of "www" is set. |
|
223
|
|
|
$explode_host = explode(".", $parse_url["host"]); |
|
224
|
|
|
|
|
225
|
|
|
// Remove empty entries |
|
226
|
|
|
$explode_host = array_filter($explode_host); |
|
227
|
|
|
// And reassign indexes |
|
228
|
|
|
$explode_host = array_values($explode_host); |
|
229
|
|
|
|
|
230
|
|
|
// Contains subdomain |
|
231
|
|
|
if(count($explode_host) > 2) { |
|
232
|
|
|
// Check if subdomain only contains the letter w(then not any other subdomain). |
|
233
|
|
|
if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) { |
|
234
|
|
|
// Replace with "www" to avoid "ww" or "wwww", etc. |
|
235
|
|
|
$explode_host[0] = "www"; |
|
236
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
$url_array["url"] .= implode(".",$explode_host); |
|
241
|
|
|
$url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash |
|
242
|
|
|
|
|
243
|
|
|
if(!empty($parse_url["port"])) { |
|
244
|
|
|
$url_array["url"] .= ":".$parse_url["port"]; |
|
245
|
|
|
} |
|
246
|
|
|
if(!empty($parse_url["path"])) { |
|
247
|
|
|
$url_array["url"] .= $parse_url["path"]; |
|
248
|
|
|
} |
|
249
|
|
|
if(!empty($parse_url["query"])) { |
|
250
|
|
|
$url_array["url"] .= '?'.$parse_url["query"]; |
|
251
|
|
|
} |
|
252
|
|
|
if(!empty($parse_url["fragment"])) { |
|
253
|
|
|
$url_array["url"] .= '#'.$parse_url["fragment"]; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
return $url_array; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Generate a password-suggestion. |
|
262
|
|
|
* |
|
263
|
|
|
* @param int $length Length of password |
|
264
|
|
|
* @param bool $simple Limit character-set to first 33 characters. |
|
265
|
|
|
* @return string |
|
266
|
|
|
*/ |
|
267
|
|
|
public static function generate_password($length = 8, $simple = false) { |
|
268
|
|
|
$character_set = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ!#%+:=?@"; |
|
269
|
|
|
$character_set_lenght = (($simple) ? 33 : 64); |
|
270
|
|
|
|
|
271
|
|
|
$i = 0; |
|
272
|
|
|
|
|
273
|
|
|
while($i < 10) { |
|
274
|
|
|
|
|
275
|
|
|
$suggested_password = ""; |
|
276
|
|
|
|
|
277
|
|
|
for($i = 0; $i < $length; $i++) { |
|
278
|
|
|
$suggested_password .= $character_set[rand(0,($character_set_lenght-1))]; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
if(strlen(count_chars($suggested_password, 3)) > ($length-2)) { |
|
282
|
|
|
break; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $suggested_password; |
|
|
|
|
|
|
287
|
|
|
|
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Convert <textarea> to [textarea]. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $html |
|
294
|
|
|
* @return string |
|
295
|
|
|
*/ |
|
296
|
|
|
public static function textarea_encode($html) { |
|
297
|
|
|
return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Convert [textarea] to <textarea>. |
|
302
|
|
|
* |
|
303
|
|
|
* @param string $html |
|
304
|
|
|
* @return string |
|
305
|
|
|
*/ |
|
306
|
|
|
public static function textarea_decode($html) { |
|
307
|
|
|
return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Convert timestamp to "x unit" |
|
312
|
|
|
* |
|
313
|
|
|
* @param string $timestamp |
|
314
|
|
|
* @return string |
|
315
|
|
|
*/ |
|
316
|
|
|
public static function time_elapsed($timestamp) { |
|
317
|
|
|
$seconds = max((time() - strtotime($timestamp)),0); |
|
318
|
|
|
|
|
319
|
|
|
if($seconds < 60) { |
|
320
|
|
|
$number = $seconds; |
|
321
|
|
|
$text = "second"; |
|
322
|
|
|
} elseif($seconds < (60 * 60)) { |
|
323
|
|
|
$number = $seconds / 60; |
|
324
|
|
|
$text = "minute"; |
|
325
|
|
|
} elseif($seconds < (60 * 60 * 24)) { |
|
326
|
|
|
$number = $seconds / (60 * 60); |
|
327
|
|
|
$text = "hour"; |
|
328
|
|
|
} else { |
|
329
|
|
|
$number = $seconds / (60 * 60 * 24); |
|
330
|
|
|
$text = "day"; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
$number = floor($number); |
|
334
|
|
|
|
|
335
|
|
|
if($number > 1) { |
|
336
|
|
|
$text.="s"; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
return "$number $text"; |
|
340
|
|
|
} |
|
341
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.