59177427 |
1 | <?php |
7350889b |
2 | |
f1ca21bd |
3 | /** |
35586184 |
4 | * strings.php |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This code provides various string manipulation functions that are |
10 | * used by the rest of the Squirrelmail code. |
11 | * |
12 | * $Id$ |
d6c32258 |
13 | * @package squirrelmail |
35586184 |
14 | */ |
9374671f |
15 | |
35586184 |
16 | /** |
17 | * SquirrelMail version number -- DO NOT CHANGE |
18 | */ |
19 | global $version; |
509fc585 |
20 | $version = '1.5.1 [CVS]'; |
d068c0ec |
21 | |
f1ca21bd |
22 | /** |
97bdc607 |
23 | * SquirrelMail internal version number -- DO NOT CHANGE |
24 | * $sm_internal_version = array (release, major, minor) |
25 | */ |
0567e4d5 |
26 | global $SQM_INTERNAL_VERSION; |
a959e855 |
27 | $SQM_INTERNAL_VERSION = array(1,5,1); |
87d8c725 |
28 | |
87d8c725 |
29 | /** |
30 | * There can be a circular issue with includes, where the $version string is |
31 | * referenced by the include of global.php, etc. before it's defined. |
32 | * For that reason, bring in global.php AFTER we define the version strings. |
33 | */ |
34 | require_once(SM_PATH . 'functions/global.php'); |
97bdc607 |
35 | |
5cc0b70e |
36 | /** |
37 | * Wraps text at $wrap characters |
38 | * |
39 | * Has a problem with special HTML characters, so call this before |
40 | * you do character translation. |
41 | * |
42 | * Specifically, ' comes up as 5 characters instead of 1. |
43 | * This should not add newlines to the end of lines. |
8b096f0a |
44 | * |
45 | * @param string line the line of text to wrap, by ref |
46 | * @param int wrap the maximum line lenth |
47 | * @return void |
5cc0b70e |
48 | */ |
49 | function sqWordWrap(&$line, $wrap) { |
e842b215 |
50 | global $languages, $squirrelmail_language; |
51 | |
52 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
53 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
54 | if (mb_detect_encoding($line) != 'ASCII') { |
55 | $line = $languages[$squirrelmail_language]['XTRA_CODE']('wordwrap', $line, $wrap); |
56 | return; |
57 | } |
58 | } |
59 | |
5cc0b70e |
60 | ereg("^([\t >]*)([^\t >].*)?$", $line, $regs); |
61 | $beginning_spaces = $regs[1]; |
62 | if (isset($regs[2])) { |
63 | $words = explode(' ', $regs[2]); |
64 | } else { |
65 | $words = ''; |
66 | } |
f1ca21bd |
67 | |
5cc0b70e |
68 | $i = 0; |
69 | $line = $beginning_spaces; |
f1ca21bd |
70 | |
5cc0b70e |
71 | while ($i < count($words)) { |
72 | /* Force one word to be on a line (minimum) */ |
73 | $line .= $words[$i]; |
74 | $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2; |
75 | if (isset($words[$i + 1])) |
76 | $line_len += strlen($words[$i + 1]); |
77 | $i ++; |
f1ca21bd |
78 | |
5cc0b70e |
79 | /* Add more words (as long as they fit) */ |
80 | while ($line_len < $wrap && $i < count($words)) { |
81 | $line .= ' ' . $words[$i]; |
82 | $i++; |
83 | if (isset($words[$i])) |
84 | $line_len += strlen($words[$i]) + 1; |
85 | else |
86 | $line_len += 1; |
87 | } |
f1ca21bd |
88 | |
5cc0b70e |
89 | /* Skip spaces if they are the first thing on a continued line */ |
90 | while (!isset($words[$i]) && $i < count($words)) { |
91 | $i ++; |
92 | } |
f1ca21bd |
93 | |
5cc0b70e |
94 | /* Go to the next line if we have more to process */ |
95 | if ($i < count($words)) { |
e0858036 |
96 | $line .= "\n"; |
5cc0b70e |
97 | } |
98 | } |
99 | } |
100 | |
341abbd6 |
101 | /** |
102 | * Does the opposite of sqWordWrap() |
8b096f0a |
103 | * @param string body the text to un-wordwrap |
104 | * @return void |
341abbd6 |
105 | */ |
106 | function sqUnWordWrap(&$body) { |
e842b215 |
107 | global $squirrelmail_language; |
f1ca21bd |
108 | |
e842b215 |
109 | if ($squirrelmail_language == 'ja_JP') { |
110 | return; |
111 | } |
112 | |
341abbd6 |
113 | $lines = explode("\n", $body); |
114 | $body = ''; |
115 | $PreviousSpaces = ''; |
116 | $cnt = count($lines); |
117 | for ($i = 0; $i < $cnt; $i ++) { |
118 | preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs); |
119 | $CurrentSpaces = $regs[1]; |
120 | if (isset($regs[2])) { |
121 | $CurrentRest = $regs[2]; |
1e4a4feb |
122 | } else { |
f1ca21bd |
123 | $CurrentRest = ''; |
124 | } |
125 | |
341abbd6 |
126 | if ($i == 0) { |
127 | $PreviousSpaces = $CurrentSpaces; |
128 | $body = $lines[$i]; |
129 | } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */ |
130 | && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */ |
131 | && strlen($CurrentRest)) { /* and there's a line to continue with */ |
132 | $body .= ' ' . $CurrentRest; |
133 | } else { |
134 | $body .= "\n" . $lines[$i]; |
135 | $PreviousSpaces = $CurrentSpaces; |
136 | } |
137 | } |
138 | $body .= "\n"; |
139 | } |
140 | |
66239b65 |
141 | /** |
142 | * If $haystack is a full mailbox name and $needle is the mailbox |
143 | * separator character, returns the last part of the mailbox name. |
8b096f0a |
144 | * |
145 | * @param string haystack full mailbox name to search |
146 | * @param string needle the mailbox separator character |
147 | * @return string the last part of the mailbox name |
66239b65 |
148 | */ |
149 | function readShortMailboxName($haystack, $needle) { |
97b1248c |
150 | |
66239b65 |
151 | if ($needle == '') { |
97b1248c |
152 | $elem = $haystack; |
153 | } else { |
f1ca21bd |
154 | $parts = explode($needle, $haystack); |
155 | $elem = array_pop($parts); |
156 | while ($elem == '' && count($parts)) { |
157 | $elem = array_pop($parts); |
158 | } |
66239b65 |
159 | } |
97b1248c |
160 | return( $elem ); |
66239b65 |
161 | } |
3302d0d4 |
162 | |
8b096f0a |
163 | /** |
4445e6b3 |
164 | * php_self |
165 | * |
8b096f0a |
166 | * Creates an URL for the page calling this function, using either the PHP global |
167 | * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. |
168 | * |
169 | * @return string the complete url for this page |
170 | */ |
43fdb2a4 |
171 | function php_self () { |
961ca3d8 |
172 | if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) { |
173 | return $req_uri; |
43fdb2a4 |
174 | } |
f1ca21bd |
175 | |
961ca3d8 |
176 | if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) { |
f72f61d8 |
177 | |
178 | // need to add query string to end of PHP_SELF to match REQUEST_URI |
179 | // |
180 | if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) { |
181 | $php_self .= '?' . $query_string; |
182 | } |
183 | |
961ca3d8 |
184 | return $php_self; |
f1ca21bd |
185 | } |
186 | |
961ca3d8 |
187 | return ''; |
43fdb2a4 |
188 | } |
189 | |
190 | |
66239b65 |
191 | /** |
4445e6b3 |
192 | * get_location |
193 | * |
8b096f0a |
194 | * Determines the location to forward to, relative to your server. |
195 | * This is used in HTTP Location: redirects. |
66239b65 |
196 | * If this doesnt work correctly for you (although it should), you can |
8b096f0a |
197 | * remove all this code except the last two lines, and have it return |
198 | * the right URL for your site, something like: |
199 | * |
200 | * http://www.example.com/squirrelmail/ |
66239b65 |
201 | * |
8b096f0a |
202 | * @return string the base url for this SquirrelMail installation |
66239b65 |
203 | */ |
204 | function get_location () { |
f1ca21bd |
205 | |
961ca3d8 |
206 | global $imap_server_type; |
238703be |
207 | |
4deb32f1 |
208 | /* Get the path, handle virtual directories */ |
f1ca21bd |
209 | if(strpos(php_self(), '?')) { |
210 | $path = substr(php_self(), 0, strpos(php_self(), '?')); |
211 | } else { |
212 | $path = php_self(); |
213 | } |
214 | $path = substr($path, 0, strrpos($path, '/')); |
238703be |
215 | if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) { |
216 | return $full_url . $path; |
217 | } |
218 | |
66239b65 |
219 | /* Check if this is a HTTPS or regular HTTP request. */ |
220 | $proto = 'http://'; |
f1ca21bd |
221 | |
66239b65 |
222 | /* |
223 | * If you have 'SSLOptions +StdEnvVars' in your apache config |
44827b4d |
224 | * OR if you have HTTPS=on in your HTTP_SERVER_VARS |
66239b65 |
225 | * OR if you are on port 443 |
226 | */ |
227 | $getEnvVar = getenv('HTTPS'); |
228 | if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) || |
961ca3d8 |
229 | (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) || |
230 | (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) { |
8a549df2 |
231 | $proto = 'https://'; |
66239b65 |
232 | } |
f1ca21bd |
233 | |
4deb32f1 |
234 | /* Get the hostname from the Host header or server config. */ |
961ca3d8 |
235 | if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) { |
236 | if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) { |
237 | $host = ''; |
238 | } |
66239b65 |
239 | } |
f1ca21bd |
240 | |
66239b65 |
241 | $port = ''; |
242 | if (! strstr($host, ':')) { |
961ca3d8 |
243 | if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) { |
f1ca21bd |
244 | if (($server_port != 80 && $proto == 'http://') || |
961ca3d8 |
245 | ($server_port != 443 && $proto == 'https://')) { |
246 | $port = sprintf(':%d', $server_port); |
66239b65 |
247 | } |
248 | } |
249 | } |
f1ca21bd |
250 | |
8de7f698 |
251 | /* this is a workaround for the weird macosx caching that |
252 | causes Apache to return 16080 as the port number, which causes |
253 | SM to bail */ |
f1ca21bd |
254 | |
8de7f698 |
255 | if ($imap_server_type == 'macosx' && $port == ':16080') { |
256 | $port = ''; |
257 | } |
f1ca21bd |
258 | |
238703be |
259 | /* Fallback is to omit the server name and use a relative */ |
260 | /* URI, although this is not RFC 2616 compliant. */ |
261 | $full_url = ($host ? $proto . $host . $port : ''); |
262 | sqsession_register($full_url, 'sq_base_url'); |
263 | return $full_url . $path; |
66239b65 |
264 | } |
dcaf2a49 |
265 | |
9374671f |
266 | |
66239b65 |
267 | /** |
4445e6b3 |
268 | * Encrypts password |
269 | * |
8b096f0a |
270 | * These functions are used to encrypt the password before it is |
271 | * stored in a cookie. The encryption key is generated by |
272 | * OneTimePadCreate(); |
273 | * |
274 | * @param string string the (password)string to encrypt |
275 | * @param string epad the encryption key |
276 | * @return string the base64-encoded encrypted password |
66239b65 |
277 | */ |
278 | function OneTimePadEncrypt ($string, $epad) { |
279 | $pad = base64_decode($epad); |
280 | $encrypted = ''; |
281 | for ($i = 0; $i < strlen ($string); $i++) { |
282 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
283 | } |
f1ca21bd |
284 | |
66239b65 |
285 | return base64_encode($encrypted); |
286 | } |
287 | |
8b096f0a |
288 | /** |
4445e6b3 |
289 | * Decrypts a password from the cookie |
290 | * |
291 | * Decrypts a password from the cookie, encrypted by OneTimePadEncrypt. |
8b096f0a |
292 | * This uses the encryption key that is stored in the session. |
293 | * |
294 | * @param string string the string to decrypt |
295 | * @param string epad the encryption key from the session |
296 | * @return string the decrypted password |
297 | */ |
66239b65 |
298 | function OneTimePadDecrypt ($string, $epad) { |
299 | $pad = base64_decode($epad); |
300 | $encrypted = base64_decode ($string); |
301 | $decrypted = ''; |
302 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
303 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
304 | } |
f1ca21bd |
305 | |
66239b65 |
306 | return $decrypted; |
307 | } |
9374671f |
308 | |
9374671f |
309 | |
66239b65 |
310 | /** |
4445e6b3 |
311 | * Randomizes the mt_rand() function. |
312 | * |
313 | * Toss this in strings or integers and it will seed the generator |
314 | * appropriately. With strings, it is better to get them long. |
315 | * Use md5() to lengthen smaller strings. |
8b096f0a |
316 | * |
317 | * @param mixed val a value to seed the random number generator |
318 | * @return void |
66239b65 |
319 | */ |
320 | function sq_mt_seed($Val) { |
4deb32f1 |
321 | /* if mt_getrandmax() does not return a 2^n - 1 number, |
322 | this might not work well. This uses $Max as a bitmask. */ |
66239b65 |
323 | $Max = mt_getrandmax(); |
f1ca21bd |
324 | |
66239b65 |
325 | if (! is_int($Val)) { |
66239b65 |
326 | $Val = crc32($Val); |
66239b65 |
327 | } |
f1ca21bd |
328 | |
66239b65 |
329 | if ($Val < 0) { |
330 | $Val *= -1; |
331 | } |
f1ca21bd |
332 | |
66239b65 |
333 | if ($Val = 0) { |
334 | return; |
335 | } |
f1ca21bd |
336 | |
66239b65 |
337 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
338 | } |
9374671f |
339 | |
9374671f |
340 | |
66239b65 |
341 | /** |
4445e6b3 |
342 | * Init random number generator |
343 | * |
66239b65 |
344 | * This function initializes the random number generator fairly well. |
345 | * It also only initializes it once, so you don't accidentally get |
346 | * the same 'random' numbers twice in one session. |
8b096f0a |
347 | * |
348 | * @return void |
66239b65 |
349 | */ |
350 | function sq_mt_randomize() { |
66239b65 |
351 | static $randomized; |
f1ca21bd |
352 | |
66239b65 |
353 | if ($randomized) { |
354 | return; |
355 | } |
f1ca21bd |
356 | |
66239b65 |
357 | /* Global. */ |
961ca3d8 |
358 | sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER); |
359 | sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER); |
66239b65 |
360 | sq_mt_seed((int)((double) microtime() * 1000000)); |
961ca3d8 |
361 | sq_mt_seed(md5($remote_port . $remote_addr . getmypid())); |
f1ca21bd |
362 | |
66239b65 |
363 | /* getrusage */ |
364 | if (function_exists('getrusage')) { |
4deb32f1 |
365 | /* Avoid warnings with Win32 */ |
66239b65 |
366 | $dat = @getrusage(); |
367 | if (isset($dat) && is_array($dat)) { |
821a8e9c |
368 | $Str = ''; |
369 | foreach ($dat as $k => $v) |
66239b65 |
370 | { |
371 | $Str .= $k . $v; |
372 | } |
821a8e9c |
373 | sq_mt_seed(md5($Str)); |
66239b65 |
374 | } |
375 | } |
f1ca21bd |
376 | |
961ca3d8 |
377 | if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) { |
378 | sq_mt_seed(md5($unique_id)); |
0b97a708 |
379 | } |
f1ca21bd |
380 | |
66239b65 |
381 | $randomized = 1; |
382 | } |
383 | |
8b096f0a |
384 | /** |
4445e6b3 |
385 | * Creates encryption key |
386 | * |
8b096f0a |
387 | * Creates an encryption key for encrypting the password stored in the cookie. |
388 | * The encryption key itself is stored in the session. |
389 | * |
390 | * @param int length optional, length of the string to generate |
391 | * @return string the encryption key |
392 | */ |
66239b65 |
393 | function OneTimePadCreate ($length=100) { |
394 | sq_mt_randomize(); |
f1ca21bd |
395 | |
66239b65 |
396 | $pad = ''; |
397 | for ($i = 0; $i < $length; $i++) { |
398 | $pad .= chr(mt_rand(0,255)); |
399 | } |
f1ca21bd |
400 | |
66239b65 |
401 | return base64_encode($pad); |
402 | } |
9374671f |
403 | |
66239b65 |
404 | /** |
8b096f0a |
405 | * Returns a string showing the size of the message/attachment. |
406 | * |
407 | * @param int bytes the filesize in bytes |
408 | * @return string the filesize in human readable format |
66239b65 |
409 | */ |
410 | function show_readable_size($bytes) { |
411 | $bytes /= 1024; |
412 | $type = 'k'; |
f1ca21bd |
413 | |
66239b65 |
414 | if ($bytes / 1024 > 1) { |
415 | $bytes /= 1024; |
e5f1e71c |
416 | $type = 'M'; |
66239b65 |
417 | } |
f1ca21bd |
418 | |
66239b65 |
419 | if ($bytes < 10) { |
420 | $bytes *= 10; |
421 | settype($bytes, 'integer'); |
422 | $bytes /= 10; |
423 | } else { |
424 | settype($bytes, 'integer'); |
425 | } |
f1ca21bd |
426 | |
66239b65 |
427 | return $bytes . '<small> ' . $type . '</small>'; |
428 | } |
9374671f |
429 | |
66239b65 |
430 | /** |
431 | * Generates a random string from the caracter set you pass in |
432 | * |
8b096f0a |
433 | * @param int size the size of the string to generate |
434 | * @param string chars a string containing the characters to use |
435 | * @param int flags a flag to add a specific set to the characters to use: |
436 | * Flags: |
437 | * 1 = add lowercase a-z to $chars |
438 | * 2 = add uppercase A-Z to $chars |
439 | * 4 = add numbers 0-9 to $chars |
440 | * @return string the random string |
66239b65 |
441 | */ |
66239b65 |
442 | function GenerateRandomString($size, $chars, $flags = 0) { |
443 | if ($flags & 0x1) { |
444 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
445 | } |
446 | if ($flags & 0x2) { |
447 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
448 | } |
449 | if ($flags & 0x4) { |
450 | $chars .= '0123456789'; |
451 | } |
f1ca21bd |
452 | |
66239b65 |
453 | if (($size < 1) || (strlen($chars) < 1)) { |
454 | return ''; |
455 | } |
ff4f08ff |
456 | |
4deb32f1 |
457 | sq_mt_randomize(); /* Initialize the random number generator */ |
ff4f08ff |
458 | |
4deb32f1 |
459 | $String = ''; |
ff4f08ff |
460 | $j = strlen( $chars ) - 1; |
66239b65 |
461 | while (strlen($String) < $size) { |
ff4f08ff |
462 | $String .= $chars{mt_rand(0, $j)}; |
66239b65 |
463 | } |
ff4f08ff |
464 | |
66239b65 |
465 | return $String; |
466 | } |
9374671f |
467 | |
8b096f0a |
468 | /** |
469 | * Escapes special characters for use in IMAP commands. |
4445e6b3 |
470 | * |
8b096f0a |
471 | * @param string the string to escape |
472 | * @return string the escaped string |
473 | */ |
fbb76d0e |
474 | function quoteimap($str) { |
ab1df059 |
475 | return preg_replace("/([\"\\\\])/", "\\\\$1", $str); |
66239b65 |
476 | } |
1899535f |
477 | |
66239b65 |
478 | /** |
4445e6b3 |
479 | * Trims array |
480 | * |
8b096f0a |
481 | * Trims every element in the array, ie. remove the first char of each element |
482 | * @param array array the array to trim |
66239b65 |
483 | */ |
484 | function TrimArray(&$array) { |
485 | foreach ($array as $k => $v) { |
486 | global $$k; |
487 | if (is_array($$k)) { |
488 | foreach ($$k as $k2 => $v2) { |
489 | $$k[$k2] = substr($v2, 1); |
23d6bd09 |
490 | } |
66239b65 |
491 | } else { |
492 | $$k = substr($v, 1); |
23d6bd09 |
493 | } |
f1ca21bd |
494 | |
4deb32f1 |
495 | /* Re-assign back to array. */ |
66239b65 |
496 | $array[$k] = $$k; |
497 | } |
f1ca21bd |
498 | } |
23d6bd09 |
499 | |
8b096f0a |
500 | /** |
4445e6b3 |
501 | * Create compose link |
502 | * |
8b096f0a |
503 | * Returns a link to the compose-page, taking in consideration |
504 | * the compose_in_new and javascript settings. |
505 | * @param string url the URL to the compose page |
506 | * @param string text the link text, default "Compose" |
507 | * @return string a link to the compose page |
508 | */ |
21a957a9 |
509 | function makeComposeLink($url, $text = null, $target='') |
d62c4938 |
510 | { |
511 | global $compose_new_win,$javascript_on; |
512 | |
513 | if(!$text) { |
514 | $text = _("Compose"); |
515 | } |
516 | |
f72f61d8 |
517 | |
518 | // if not using "compose in new window", make |
519 | // regular link and be done with it |
d62c4938 |
520 | if($compose_new_win != '1') { |
21a957a9 |
521 | return makeInternalLink($url, $text, $target); |
d62c4938 |
522 | } |
523 | |
f72f61d8 |
524 | |
f72f61d8 |
525 | // build the compose in new window link... |
f72f61d8 |
526 | |
527 | |
528 | // if javascript is on, use onClick event to handle it |
d62c4938 |
529 | if($javascript_on) { |
530 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
531 | return '<a href="javascript:void(0)" onclick="comp_in_new(\''.$base_uri.$url.'\')">'. $text.'</a>'; |
532 | } |
533 | |
f72f61d8 |
534 | |
535 | // otherwise, just open new window using regular HTML |
d62c4938 |
536 | return makeInternalLink($url, $text, '_blank'); |
f72f61d8 |
537 | |
d62c4938 |
538 | } |
539 | |
f1ca21bd |
540 | /** |
4445e6b3 |
541 | * Print variable |
542 | * |
8b096f0a |
543 | * sm_print_r($some_variable, [$some_other_variable [, ...]]); |
4445e6b3 |
544 | * |
8b096f0a |
545 | * Debugging function - does the same as print_r, but makes sure special |
546 | * characters are converted to htmlentities first. This will allow |
547 | * values like <some@email.address> to be displayed. |
4445e6b3 |
548 | * The output is wrapped in <<pre>> and <</pre>> tags. |
8b096f0a |
549 | * |
550 | * @return void |
551 | */ |
7fe09a30 |
552 | function sm_print_r() { |
50cc40fe |
553 | ob_start(); // Buffer output |
7fe09a30 |
554 | foreach(func_get_args() as $var) { |
555 | print_r($var); |
556 | echo "\n"; |
557 | } |
50cc40fe |
558 | $buffer = ob_get_contents(); // Grab the print_r output |
559 | ob_end_clean(); // Silently discard the output & stop buffering |
f1ca21bd |
560 | print '<pre>'; |
50cc40fe |
561 | print htmlentities($buffer); |
f1ca21bd |
562 | print '</pre>'; |
50cc40fe |
563 | } |
564 | |
3ecad5e6 |
565 | /** |
566 | * version of fwrite which checks for failure |
567 | */ |
568 | function sq_fwrite($fp, $string) { |
569 | // write to file |
570 | $count = @fwrite($fp,$string); |
571 | // the number of bytes written should be the length of the string |
572 | if($count != strlen($string)) { |
573 | return FALSE; |
574 | } |
575 | |
576 | return $count; |
577 | } |
578 | |
36e1180b |
579 | /** |
580 | * sq_get_html_translation_table |
581 | * |
582 | * Returns the translation table used by sq_htmlentities() |
583 | * |
584 | * @param integer $table html translation table. Possible values (without quotes): |
deb22cec |
585 | * <ul> |
586 | * <li>HTML_ENTITIES - full html entities table defined by charset</li> |
587 | * <li>HTML_SPECIALCHARS - html special characters table</li> |
588 | * </ul> |
36e1180b |
589 | * @param integer $quote_style quote encoding style. Possible values (without quotes): |
deb22cec |
590 | * <ul> |
591 | * <li>ENT_COMPAT - (default) encode double quotes</li> |
592 | * <li>ENT_NOQUOTES - don't encode double or single quotes</li> |
593 | * <li>ENT_QUOTES - encode double and single quotes</li> |
594 | * </ul> |
36e1180b |
595 | * @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value. |
596 | * @return array html translation array |
597 | */ |
598 | function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='us-ascii') { |
599 | global $default_charset; |
600 | |
601 | if ($table == HTML_SPECIALCHARS) $charset='us-ascii'; |
602 | |
603 | // Start array with ampersand |
604 | $sq_html_ent_table = array( "&" => '&' ); |
605 | |
606 | // < and > |
607 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
608 | array("<" => '<', |
609 | ">" => '>') |
610 | ); |
611 | // double quotes |
612 | if ($quote_style == ENT_COMPAT) |
613 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
614 | array("\"" => '"') |
615 | ); |
616 | |
617 | // double and single quotes |
618 | if ($quote_style == ENT_QUOTES) |
619 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
620 | array("\"" => '"', |
621 | "'" => ''') |
622 | ); |
623 | |
624 | if ($charset=='auto') $charset=$default_charset; |
625 | |
626 | // add entities that depend on charset |
627 | switch($charset){ |
628 | case 'iso-8859-1': |
629 | include_once(SM_PATH . 'functions/htmlentities/iso-8859-1.php'); |
630 | break; |
631 | case 'utf-8': |
632 | include_once(SM_PATH . 'functions/htmlentities/utf-8.php'); |
633 | break; |
634 | case 'us-ascii': |
635 | default: |
636 | break; |
637 | } |
638 | // return table |
639 | return $sq_html_ent_table; |
640 | } |
641 | |
642 | /** |
643 | * sq_htmlentities |
644 | * |
645 | * Convert all applicable characters to HTML entities. |
646 | * Minimal php requirement - v.4.0.5 |
647 | * |
648 | * @param string $string string that has to be sanitized |
649 | * @param integer $quote_style quote encoding style. Possible values (without quotes): |
deb22cec |
650 | * <ul> |
651 | * <li>ENT_COMPAT - (default) encode double quotes</li> |
652 | * <li>ENT_NOQUOTES - don't encode double or single quotes</li> |
653 | * <li>ENT_QUOTES - encode double and single quotes</li> |
654 | * </ul> |
36e1180b |
655 | * @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value. |
656 | * @return string sanitized string |
657 | */ |
658 | function sq_htmlentities($string,$quote_style=ENT_COMPAT,$charset='us-ascii') { |
659 | // get translation table |
660 | $sq_html_ent_table=sq_get_html_translation_table(HTML_ENTITIES,$quote_style,$charset); |
661 | // convert characters |
662 | return str_replace(array_keys($sq_html_ent_table),array_values($sq_html_ent_table),$string); |
663 | } |
664 | |
43fdb2a4 |
665 | $PHP_SELF = php_self(); |
4445e6b3 |
666 | ?> |