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