59177427 |
1 | <?php |
7350889b |
2 | |
f1ca21bd |
3 | /** |
35586184 |
4 | * strings.php |
5 | * |
35586184 |
6 | * This code provides various string manipulation functions that are |
598294a7 |
7 | * used by the rest of the SquirrelMail code. |
35586184 |
8 | * |
47ccfad4 |
9 | * @copyright © 1999-2006 The SquirrelMail Project Team |
4b4abf93 |
10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
11 | * @version $Id$ |
d6c32258 |
12 | * @package squirrelmail |
35586184 |
13 | */ |
9374671f |
14 | |
c9d61baf |
15 | /** |
16 | * Appends citation markers to the string. |
17 | * Also appends a trailing space. |
18 | * |
19 | * @author Justus Pendleton |
31310ecd |
20 | * @param string $str The string to append to |
21 | * @param int $citeLevel the number of markers to append |
c9d61baf |
22 | * @return null |
31310ecd |
23 | * @since 1.5.1 |
c9d61baf |
24 | */ |
25 | function sqMakeCite (&$str, $citeLevel) { |
26 | for ($i = 0; $i < $citeLevel; $i++) { |
27 | $str .= '>'; |
28 | } |
29 | if ($citeLevel != 0) { |
30 | $str .= ' '; |
31 | } |
32 | } |
33 | |
34 | /** |
35 | * Create a newline in the string, adding citation |
36 | * markers to the newline as necessary. |
37 | * |
38 | * @author Justus Pendleton |
31310ecd |
39 | * @param string $str the string to make a newline in |
40 | * @param int $citeLevel the citation level the newline is at |
41 | * @param int $column starting column of the newline |
c9d61baf |
42 | * @return null |
31310ecd |
43 | * @since 1.5.1 |
c9d61baf |
44 | */ |
45 | function sqMakeNewLine (&$str, $citeLevel, &$column) { |
46 | $str .= "\n"; |
47 | $column = 0; |
48 | if ($citeLevel > 0) { |
49 | sqMakeCite ($str, $citeLevel); |
50 | $column = $citeLevel + 1; |
51 | } else { |
52 | $column = 0; |
53 | } |
54 | } |
55 | |
5e7ae713 |
56 | /** |
57 | * Checks for spaces in strings - only used if PHP doesn't have native ctype support |
58 | * |
326727cf |
59 | * You might be able to rewrite the function by adding short evaluation form. |
5e7ae713 |
60 | * |
61 | * possible problems: |
62 | * - iso-2022-xx charsets - hex 20 might be part of other symbol. I might |
63 | * be wrong. 0x20 is not used in iso-2022-jp. I haven't checked iso-2022-kr |
64 | * and iso-2022-cn mappings. |
65 | * |
66 | * - no-break space ( ) - it is 8bit symbol, that depends on charset. |
67 | * there are at least three different charset groups that have nbsp in |
68 | * different places. |
69 | * |
70 | * I don't see any charset/nbsp options in php ctype either. |
71 | * |
72 | * @param string $string tested string |
326727cf |
73 | * @return bool true when only whitespace symbols are present in test string |
31310ecd |
74 | * @since 1.5.1 |
5e7ae713 |
75 | */ |
76 | function sm_ctype_space($string) { |
c7aff938 |
77 | if ( preg_match('/^[\x09-\x0D]|^\x20/', $string) || $string=='') { |
78 | return true; |
79 | } else { |
80 | return false; |
81 | } |
5e7ae713 |
82 | } |
83 | |
c9d61baf |
84 | /** |
85 | * Wraps text at $wrap characters. While sqWordWrap takes |
86 | * a single line of text and wraps it, this function works |
87 | * on the entire corpus at once, this allows it to be a little |
88 | * bit smarter and when and how to wrap. |
89 | * |
90 | * @author Justus Pendleton |
31310ecd |
91 | * @param string $body the entire body of text |
92 | * @param int $wrap the maximum line length |
c9d61baf |
93 | * @return string the wrapped text |
31310ecd |
94 | * @since 1.5.1 |
c9d61baf |
95 | */ |
96 | function &sqBodyWrap (&$body, $wrap) { |
5e7ae713 |
97 | //check for ctype support, and fake it if it doesn't exist |
98 | if (!function_exists('ctype_space')) { |
99 | function ctype_space ($string) { |
100 | return sm_ctype_space($string); |
101 | } |
102 | } |
103 | |
c9d61baf |
104 | // the newly wrapped text |
105 | $outString = ''; |
106 | // current column since the last newline in the outstring |
107 | $outStringCol = 0; |
98abf408 |
108 | $length = sq_strlen($body); |
c9d61baf |
109 | // where we are in the original string |
110 | $pos = 0; |
111 | // the number of >>> citation markers we are currently at |
112 | $citeLevel = 0; |
113 | |
114 | // the main loop, whenever we start a newline of input text |
115 | // we start from here |
116 | while ($pos < $length) { |
117 | // we're at the beginning of a line, get the new cite level |
118 | $newCiteLevel = 0; |
119 | |
98abf408 |
120 | while (($pos < $length) && (sq_substr($body,$pos,1) == '>')) { |
c9d61baf |
121 | $newCiteLevel++; |
122 | $pos++; |
123 | |
124 | // skip over any spaces interleaved among the cite markers |
98abf408 |
125 | while (($pos < $length) && (sq_substr($body,$pos,1) == ' ')) { |
bb977394 |
126 | |
c9d61baf |
127 | $pos++; |
bb977394 |
128 | |
c9d61baf |
129 | } |
130 | if ($pos >= $length) { |
131 | break; |
132 | } |
133 | } |
134 | |
135 | // special case: if this is a blank line then maintain it |
136 | // (i.e. try to preserve original paragraph breaks) |
137 | // unless they occur at the very beginning of the text |
98abf408 |
138 | if ((sq_substr($body,$pos,1) == "\n" ) && (sq_strlen($outString) != 0)) { |
139 | $outStringLast = $outString{sq_strlen($outString) - 1}; |
c9d61baf |
140 | if ($outStringLast != "\n") { |
141 | $outString .= "\n"; |
142 | } |
143 | sqMakeCite ($outString, $newCiteLevel); |
144 | $outString .= "\n"; |
145 | $pos++; |
146 | $outStringCol = 0; |
147 | continue; |
148 | } |
149 | |
150 | // if the cite level has changed, then start a new line |
151 | // with the new cite level. |
152 | if (($citeLevel != $newCiteLevel) && ($pos > ($newCiteLevel + 1)) && ($outStringCol != 0)) { |
153 | sqMakeNewLine ($outString, 0, $outStringCol); |
154 | } |
155 | |
156 | $citeLevel = $newCiteLevel; |
157 | |
158 | // prepend the quote level if necessary |
159 | if ($outStringCol == 0) { |
160 | sqMakeCite ($outString, $citeLevel); |
161 | // if we added a citation then move the column |
162 | // out by citelevel + 1 (the cite markers + the space) |
163 | $outStringCol = $citeLevel + ($citeLevel ? 1 : 0); |
164 | } else if ($outStringCol > $citeLevel) { |
165 | // not a cite and we're not at the beginning of a line |
166 | // in the output. add a space to separate the new text |
167 | // from previous text. |
168 | $outString .= ' '; |
169 | $outStringCol++; |
170 | } |
171 | |
172 | // find the next newline -- we don't want to go further than that |
98abf408 |
173 | $nextNewline = sq_strpos ($body, "\n", $pos); |
c9d61baf |
174 | if ($nextNewline === FALSE) { |
175 | $nextNewline = $length; |
176 | } |
177 | |
178 | // Don't wrap unquoted lines at all. For now the textarea |
179 | // will work fine for this. Maybe revisit this later though |
180 | // (for completeness more than anything else, I think) |
181 | if ($citeLevel == 0) { |
98abf408 |
182 | $outString .= sq_substr ($body, $pos, ($nextNewline - $pos)); |
c9d61baf |
183 | $outStringCol = $nextNewline - $pos; |
184 | if ($nextNewline != $length) { |
185 | sqMakeNewLine ($outString, 0, $outStringCol); |
186 | } |
187 | $pos = $nextNewline + 1; |
188 | continue; |
189 | } |
bb977394 |
190 | /** |
191 | * Set this to false to stop appending short strings to previous lines |
192 | */ |
193 | $smartwrap = true; |
c9d61baf |
194 | // inner loop, (obviously) handles wrapping up to |
195 | // the next newline |
196 | while ($pos < $nextNewline) { |
197 | // skip over initial spaces |
98abf408 |
198 | while (($pos < $nextNewline) && (ctype_space (sq_substr($body,$pos,1)))) { |
c9d61baf |
199 | $pos++; |
200 | } |
c9d61baf |
201 | // if this is a short line then just append it and continue outer loop |
6eaf5320 |
202 | if (($outStringCol + $nextNewline - $pos) <= ($wrap - $citeLevel - 1) ) { |
c9d61baf |
203 | // if this is the final line in the input string then include |
204 | // any trailing newlines |
6eaf5320 |
205 | // echo substr($body,$pos,$wrap). "<br />"; |
98abf408 |
206 | if (($nextNewline + 1 == $length) && (sq_substr($body,$nextNewline,1) == "\n")) { |
c9d61baf |
207 | $nextNewline++; |
208 | } |
209 | |
bb977394 |
210 | // trim trailing spaces |
211 | $lastRealChar = $nextNewline; |
98abf408 |
212 | while (($lastRealChar > $pos && $lastRealChar < $length) && (ctype_space (sq_substr($body,$lastRealChar,1)))) { |
bb977394 |
213 | $lastRealChar--; |
214 | } |
bb977394 |
215 | // decide if appending the short string is what we want |
98abf408 |
216 | if (($nextNewline < $length && sq_substr($body,$nextNewline,1) == "\n") && |
bb977394 |
217 | isset($lastRealChar)) { |
be86a35a |
218 | $mypos = $pos; |
219 | //check the first word: |
98abf408 |
220 | while (($mypos < $length) && (sq_substr($body,$mypos,1) == '>')) { |
bb977394 |
221 | $mypos++; |
222 | // skip over any spaces interleaved among the cite markers |
98abf408 |
223 | while (($mypos < $length) && (sq_substr($body,$mypos,1) == ' ')) { |
6eaf5320 |
224 | $mypos++; |
bb977394 |
225 | } |
226 | } |
227 | /* |
228 | $ldnspacecnt = 0; |
229 | if ($mypos == $nextNewline+1) { |
230 | while (($mypos < $length) && ($body{$mypos} == ' ')) { |
231 | $ldnspacecnt++; |
6eaf5320 |
232 | } |
bb977394 |
233 | } |
234 | */ |
235 | |
98abf408 |
236 | $firstword = sq_substr($body,$mypos,sq_strpos($body,' ',$mypos) - $mypos); |
bb977394 |
237 | //if ($dowrap || $ldnspacecnt > 1 || ($firstword && ( |
238 | if (!$smartwrap || $firstword && ( |
239 | $firstword{0} == '-' || |
6eaf5320 |
240 | $firstword{0} == '+' || |
241 | $firstword{0} == '*' || |
98abf408 |
242 | sq_substr($firstword,0,1) == sq_strtoupper(sq_substr($firstword,0,1)) || |
6eaf5320 |
243 | strpos($firstword,':'))) { |
98abf408 |
244 | $outString .= sq_substr($body,$pos,($lastRealChar - $pos+1)); |
bb977394 |
245 | $outStringCol += ($lastRealChar - $pos); |
246 | sqMakeNewLine($outString,$citeLevel,$outStringCol); |
247 | $nextNewline++; |
248 | $pos = $nextNewline; |
249 | $outStringCol--; |
250 | continue; |
251 | } |
6eaf5320 |
252 | |
c9d61baf |
253 | } |
bb977394 |
254 | |
98abf408 |
255 | $outString .= sq_substr ($body, $pos, ($lastRealChar - $pos + 1)); |
c9d61baf |
256 | $outStringCol += ($lastRealChar - $pos); |
257 | $pos = $nextNewline + 1; |
258 | continue; |
259 | } |
bb977394 |
260 | |
c9d61baf |
261 | $eol = $pos + $wrap - $citeLevel - $outStringCol; |
262 | // eol is the tentative end of line. |
263 | // look backwards for there for a whitespace to break at. |
264 | // if it's already less than our current position then |
265 | // our current line is already too long, break immediately |
266 | // and restart outer loop |
267 | if ($eol <= $pos) { |
6eaf5320 |
268 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
c9d61baf |
269 | continue; |
270 | } |
271 | |
272 | // start looking backwards for whitespace to break at. |
273 | $breakPoint = $eol; |
98abf408 |
274 | while (($breakPoint > $pos) && (! ctype_space (sq_substr($body,$breakPoint,1)))) { |
c9d61baf |
275 | $breakPoint--; |
276 | } |
277 | |
278 | // if we didn't find a breakpoint by looking backward then we |
279 | // need to figure out what to do about that |
280 | if ($breakPoint == $pos) { |
281 | // if we are not at the beginning then end this line |
282 | // and start a new loop |
283 | if ($outStringCol > ($citeLevel + 1)) { |
284 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
285 | continue; |
286 | } else { |
287 | // just hard break here. most likely we are breaking |
288 | // a really long URL. could also try searching |
289 | // forward for a break point, which is what Mozilla |
290 | // does. don't bother for now. |
291 | $breakPoint = $eol; |
292 | } |
293 | } |
294 | |
295 | // special case: maybe we should have wrapped last |
296 | // time. if the first breakpoint here makes the |
297 | // current line too long and there is already text on |
298 | // the current line, break and loop again if at |
299 | // beginning of current line, don't force break |
300 | $SLOP = 6; |
301 | if ((($outStringCol + ($breakPoint - $pos)) > ($wrap + $SLOP)) && ($outStringCol > ($citeLevel + 1))) { |
302 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
303 | continue; |
304 | } |
305 | |
306 | // skip newlines or whitespace at the beginning of the string |
98abf408 |
307 | $substring = sq_substr ($body, $pos, ($breakPoint - $pos)); |
c9d61baf |
308 | $substring = rtrim ($substring); // do rtrim and ctype_space have the same ideas about whitespace? |
309 | $outString .= $substring; |
98abf408 |
310 | $outStringCol += sq_strlen ($substring); |
c9d61baf |
311 | // advance past the whitespace which caused the wrap |
312 | $pos = $breakPoint; |
98abf408 |
313 | while (($pos < $length) && (ctype_space (sq_substr($body,$pos,1)))) { |
c9d61baf |
314 | $pos++; |
315 | } |
316 | if ($pos < $length) { |
317 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
318 | } |
319 | } |
320 | } |
321 | |
322 | return $outString; |
323 | } |
324 | |
5cc0b70e |
325 | /** |
326 | * Wraps text at $wrap characters |
327 | * |
328 | * Has a problem with special HTML characters, so call this before |
329 | * you do character translation. |
330 | * |
17886554 |
331 | * Specifically, &#039; comes up as 5 characters instead of 1. |
5cc0b70e |
332 | * This should not add newlines to the end of lines. |
8b096f0a |
333 | * |
31310ecd |
334 | * @param string $line the line of text to wrap, by ref |
335 | * @param int $wrap the maximum line lenth |
336 | * @param string $charset name of charset used in $line string. Available since v.1.5.1. |
8b096f0a |
337 | * @return void |
31310ecd |
338 | * @since 1.0 |
5cc0b70e |
339 | */ |
c7aff938 |
340 | function sqWordWrap(&$line, $wrap, $charset='') { |
e842b215 |
341 | global $languages, $squirrelmail_language; |
342 | |
17886554 |
343 | // Use custom wrapping function, if translation provides it |
e842b215 |
344 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
1b45fe31 |
345 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap')) { |
e842b215 |
346 | if (mb_detect_encoding($line) != 'ASCII') { |
1b45fe31 |
347 | $line = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap', $line, $wrap); |
e842b215 |
348 | return; |
349 | } |
350 | } |
351 | |
5cc0b70e |
352 | ereg("^([\t >]*)([^\t >].*)?$", $line, $regs); |
353 | $beginning_spaces = $regs[1]; |
354 | if (isset($regs[2])) { |
355 | $words = explode(' ', $regs[2]); |
356 | } else { |
357 | $words = ''; |
358 | } |
f1ca21bd |
359 | |
5cc0b70e |
360 | $i = 0; |
361 | $line = $beginning_spaces; |
f1ca21bd |
362 | |
5cc0b70e |
363 | while ($i < count($words)) { |
364 | /* Force one word to be on a line (minimum) */ |
365 | $line .= $words[$i]; |
c7aff938 |
366 | $line_len = strlen($beginning_spaces) + sq_strlen($words[$i],$charset) + 2; |
5cc0b70e |
367 | if (isset($words[$i + 1])) |
c7aff938 |
368 | $line_len += sq_strlen($words[$i + 1],$charset); |
5cc0b70e |
369 | $i ++; |
f1ca21bd |
370 | |
5cc0b70e |
371 | /* Add more words (as long as they fit) */ |
372 | while ($line_len < $wrap && $i < count($words)) { |
373 | $line .= ' ' . $words[$i]; |
374 | $i++; |
375 | if (isset($words[$i])) |
c7aff938 |
376 | $line_len += sq_strlen($words[$i],$charset) + 1; |
5cc0b70e |
377 | else |
378 | $line_len += 1; |
379 | } |
f1ca21bd |
380 | |
5cc0b70e |
381 | /* Skip spaces if they are the first thing on a continued line */ |
382 | while (!isset($words[$i]) && $i < count($words)) { |
383 | $i ++; |
384 | } |
f1ca21bd |
385 | |
5cc0b70e |
386 | /* Go to the next line if we have more to process */ |
387 | if ($i < count($words)) { |
e0858036 |
388 | $line .= "\n"; |
5cc0b70e |
389 | } |
390 | } |
391 | } |
392 | |
341abbd6 |
393 | /** |
394 | * Does the opposite of sqWordWrap() |
31310ecd |
395 | * @param string $body the text to un-wordwrap |
8b096f0a |
396 | * @return void |
31310ecd |
397 | * @since 1.0 |
341abbd6 |
398 | */ |
399 | function sqUnWordWrap(&$body) { |
e842b215 |
400 | global $squirrelmail_language; |
f1ca21bd |
401 | |
e842b215 |
402 | if ($squirrelmail_language == 'ja_JP') { |
403 | return; |
404 | } |
405 | |
341abbd6 |
406 | $lines = explode("\n", $body); |
407 | $body = ''; |
408 | $PreviousSpaces = ''; |
409 | $cnt = count($lines); |
410 | for ($i = 0; $i < $cnt; $i ++) { |
411 | preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs); |
412 | $CurrentSpaces = $regs[1]; |
413 | if (isset($regs[2])) { |
414 | $CurrentRest = $regs[2]; |
1e4a4feb |
415 | } else { |
f1ca21bd |
416 | $CurrentRest = ''; |
417 | } |
418 | |
341abbd6 |
419 | if ($i == 0) { |
420 | $PreviousSpaces = $CurrentSpaces; |
421 | $body = $lines[$i]; |
422 | } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */ |
423 | && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */ |
424 | && strlen($CurrentRest)) { /* and there's a line to continue with */ |
425 | $body .= ' ' . $CurrentRest; |
426 | } else { |
427 | $body .= "\n" . $lines[$i]; |
428 | $PreviousSpaces = $CurrentSpaces; |
429 | } |
430 | } |
431 | $body .= "\n"; |
432 | } |
433 | |
66239b65 |
434 | /** |
435 | * If $haystack is a full mailbox name and $needle is the mailbox |
436 | * separator character, returns the last part of the mailbox name. |
8b096f0a |
437 | * |
438 | * @param string haystack full mailbox name to search |
439 | * @param string needle the mailbox separator character |
440 | * @return string the last part of the mailbox name |
31310ecd |
441 | * @since 1.0 |
66239b65 |
442 | */ |
443 | function readShortMailboxName($haystack, $needle) { |
97b1248c |
444 | |
66239b65 |
445 | if ($needle == '') { |
97b1248c |
446 | $elem = $haystack; |
447 | } else { |
f1ca21bd |
448 | $parts = explode($needle, $haystack); |
449 | $elem = array_pop($parts); |
450 | while ($elem == '' && count($parts)) { |
451 | $elem = array_pop($parts); |
452 | } |
66239b65 |
453 | } |
97b1248c |
454 | return( $elem ); |
66239b65 |
455 | } |
3302d0d4 |
456 | |
a9a7cda1 |
457 | |
66239b65 |
458 | /** |
4445e6b3 |
459 | * get_location |
460 | * |
8b096f0a |
461 | * Determines the location to forward to, relative to your server. |
462 | * This is used in HTTP Location: redirects. |
66239b65 |
463 | * If this doesnt work correctly for you (although it should), you can |
8b096f0a |
464 | * remove all this code except the last two lines, and have it return |
465 | * the right URL for your site, something like: |
466 | * |
467 | * http://www.example.com/squirrelmail/ |
66239b65 |
468 | * |
8b096f0a |
469 | * @return string the base url for this SquirrelMail installation |
31310ecd |
470 | * @since 1.0 |
66239b65 |
471 | */ |
472 | function get_location () { |
f1ca21bd |
473 | |
961ca3d8 |
474 | global $imap_server_type; |
238703be |
475 | |
4deb32f1 |
476 | /* Get the path, handle virtual directories */ |
f1ca21bd |
477 | if(strpos(php_self(), '?')) { |
478 | $path = substr(php_self(), 0, strpos(php_self(), '?')); |
479 | } else { |
480 | $path = php_self(); |
481 | } |
482 | $path = substr($path, 0, strrpos($path, '/')); |
238703be |
483 | if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) { |
484 | return $full_url . $path; |
485 | } |
486 | |
66239b65 |
487 | /* Check if this is a HTTPS or regular HTTP request. */ |
488 | $proto = 'http://'; |
f1ca21bd |
489 | |
66239b65 |
490 | /* |
491 | * If you have 'SSLOptions +StdEnvVars' in your apache config |
44827b4d |
492 | * OR if you have HTTPS=on in your HTTP_SERVER_VARS |
66239b65 |
493 | * OR if you are on port 443 |
494 | */ |
495 | $getEnvVar = getenv('HTTPS'); |
496 | if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) || |
961ca3d8 |
497 | (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) || |
498 | (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) { |
8a549df2 |
499 | $proto = 'https://'; |
66239b65 |
500 | } |
f1ca21bd |
501 | |
4deb32f1 |
502 | /* Get the hostname from the Host header or server config. */ |
961ca3d8 |
503 | if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) { |
504 | if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) { |
505 | $host = ''; |
506 | } |
66239b65 |
507 | } |
f1ca21bd |
508 | |
66239b65 |
509 | $port = ''; |
510 | if (! strstr($host, ':')) { |
961ca3d8 |
511 | if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) { |
f1ca21bd |
512 | if (($server_port != 80 && $proto == 'http://') || |
961ca3d8 |
513 | ($server_port != 443 && $proto == 'https://')) { |
514 | $port = sprintf(':%d', $server_port); |
66239b65 |
515 | } |
516 | } |
517 | } |
f1ca21bd |
518 | |
8de7f698 |
519 | /* this is a workaround for the weird macosx caching that |
520 | causes Apache to return 16080 as the port number, which causes |
521 | SM to bail */ |
f1ca21bd |
522 | |
8de7f698 |
523 | if ($imap_server_type == 'macosx' && $port == ':16080') { |
524 | $port = ''; |
525 | } |
f1ca21bd |
526 | |
238703be |
527 | /* Fallback is to omit the server name and use a relative */ |
528 | /* URI, although this is not RFC 2616 compliant. */ |
529 | $full_url = ($host ? $proto . $host . $port : ''); |
530 | sqsession_register($full_url, 'sq_base_url'); |
531 | return $full_url . $path; |
66239b65 |
532 | } |
dcaf2a49 |
533 | |
9374671f |
534 | |
66239b65 |
535 | /** |
4445e6b3 |
536 | * Encrypts password |
537 | * |
8b096f0a |
538 | * These functions are used to encrypt the password before it is |
539 | * stored in a cookie. The encryption key is generated by |
540 | * OneTimePadCreate(); |
541 | * |
31310ecd |
542 | * @param string $string the (password)string to encrypt |
543 | * @param string $epad the encryption key |
8b096f0a |
544 | * @return string the base64-encoded encrypted password |
31310ecd |
545 | * @since 1.0 |
66239b65 |
546 | */ |
547 | function OneTimePadEncrypt ($string, $epad) { |
548 | $pad = base64_decode($epad); |
432db2fc |
549 | |
550 | if (strlen($pad)>0) { |
551 | // make sure that pad is longer than string |
552 | while (strlen($string)>strlen($pad)) { |
553 | $pad.=$pad; |
554 | } |
555 | } else { |
556 | // FIXME: what should we do when $epad is not base64 encoded or empty. |
557 | } |
558 | |
66239b65 |
559 | $encrypted = ''; |
560 | for ($i = 0; $i < strlen ($string); $i++) { |
561 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
562 | } |
f1ca21bd |
563 | |
66239b65 |
564 | return base64_encode($encrypted); |
565 | } |
566 | |
8b096f0a |
567 | /** |
4445e6b3 |
568 | * Decrypts a password from the cookie |
569 | * |
570 | * Decrypts a password from the cookie, encrypted by OneTimePadEncrypt. |
8b096f0a |
571 | * This uses the encryption key that is stored in the session. |
572 | * |
31310ecd |
573 | * @param string $string the string to decrypt |
574 | * @param string $epad the encryption key from the session |
8b096f0a |
575 | * @return string the decrypted password |
31310ecd |
576 | * @since 1.0 |
8b096f0a |
577 | */ |
66239b65 |
578 | function OneTimePadDecrypt ($string, $epad) { |
579 | $pad = base64_decode($epad); |
432db2fc |
580 | |
581 | if (strlen($pad)>0) { |
582 | // make sure that pad is longer than string |
583 | while (strlen($string)>strlen($pad)) { |
584 | $pad.=$pad; |
585 | } |
586 | } else { |
587 | // FIXME: what should we do when $epad is not base64 encoded or empty. |
588 | } |
589 | |
66239b65 |
590 | $encrypted = base64_decode ($string); |
591 | $decrypted = ''; |
592 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
593 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
594 | } |
f1ca21bd |
595 | |
66239b65 |
596 | return $decrypted; |
597 | } |
9374671f |
598 | |
9374671f |
599 | |
66239b65 |
600 | /** |
4445e6b3 |
601 | * Randomizes the mt_rand() function. |
602 | * |
c9d61baf |
603 | * Toss this in strings or integers and it will seed the generator |
604 | * appropriately. With strings, it is better to get them long. |
4445e6b3 |
605 | * Use md5() to lengthen smaller strings. |
8b096f0a |
606 | * |
31310ecd |
607 | * @param mixed $val a value to seed the random number generator. mixed = integer or string. |
8b096f0a |
608 | * @return void |
31310ecd |
609 | * @since 1.0 |
66239b65 |
610 | */ |
611 | function sq_mt_seed($Val) { |
4deb32f1 |
612 | /* if mt_getrandmax() does not return a 2^n - 1 number, |
613 | this might not work well. This uses $Max as a bitmask. */ |
66239b65 |
614 | $Max = mt_getrandmax(); |
f1ca21bd |
615 | |
66239b65 |
616 | if (! is_int($Val)) { |
66239b65 |
617 | $Val = crc32($Val); |
66239b65 |
618 | } |
f1ca21bd |
619 | |
66239b65 |
620 | if ($Val < 0) { |
621 | $Val *= -1; |
622 | } |
f1ca21bd |
623 | |
8d2155e5 |
624 | if ($Val == 0) { |
66239b65 |
625 | return; |
626 | } |
f1ca21bd |
627 | |
66239b65 |
628 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
629 | } |
9374671f |
630 | |
9374671f |
631 | |
66239b65 |
632 | /** |
4445e6b3 |
633 | * Init random number generator |
634 | * |
66239b65 |
635 | * This function initializes the random number generator fairly well. |
636 | * It also only initializes it once, so you don't accidentally get |
637 | * the same 'random' numbers twice in one session. |
8b096f0a |
638 | * |
639 | * @return void |
31310ecd |
640 | * @since 1.0 |
66239b65 |
641 | */ |
642 | function sq_mt_randomize() { |
66239b65 |
643 | static $randomized; |
f1ca21bd |
644 | |
66239b65 |
645 | if ($randomized) { |
646 | return; |
647 | } |
f1ca21bd |
648 | |
66239b65 |
649 | /* Global. */ |
961ca3d8 |
650 | sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER); |
651 | sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER); |
66239b65 |
652 | sq_mt_seed((int)((double) microtime() * 1000000)); |
961ca3d8 |
653 | sq_mt_seed(md5($remote_port . $remote_addr . getmypid())); |
f1ca21bd |
654 | |
66239b65 |
655 | /* getrusage */ |
656 | if (function_exists('getrusage')) { |
4deb32f1 |
657 | /* Avoid warnings with Win32 */ |
66239b65 |
658 | $dat = @getrusage(); |
659 | if (isset($dat) && is_array($dat)) { |
821a8e9c |
660 | $Str = ''; |
661 | foreach ($dat as $k => $v) |
66239b65 |
662 | { |
663 | $Str .= $k . $v; |
664 | } |
821a8e9c |
665 | sq_mt_seed(md5($Str)); |
66239b65 |
666 | } |
667 | } |
f1ca21bd |
668 | |
961ca3d8 |
669 | if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) { |
670 | sq_mt_seed(md5($unique_id)); |
0b97a708 |
671 | } |
f1ca21bd |
672 | |
66239b65 |
673 | $randomized = 1; |
674 | } |
675 | |
8b096f0a |
676 | /** |
4445e6b3 |
677 | * Creates encryption key |
678 | * |
8b096f0a |
679 | * Creates an encryption key for encrypting the password stored in the cookie. |
680 | * The encryption key itself is stored in the session. |
681 | * |
31310ecd |
682 | * Pad must be longer or equal to encoded string length in 1.4.4/1.5.0 and older. |
683 | * @param int $length optional, length of the string to generate |
8b096f0a |
684 | * @return string the encryption key |
31310ecd |
685 | * @since 1.0 |
8b096f0a |
686 | */ |
66239b65 |
687 | function OneTimePadCreate ($length=100) { |
688 | sq_mt_randomize(); |
f1ca21bd |
689 | |
66239b65 |
690 | $pad = ''; |
691 | for ($i = 0; $i < $length; $i++) { |
692 | $pad .= chr(mt_rand(0,255)); |
693 | } |
f1ca21bd |
694 | |
66239b65 |
695 | return base64_encode($pad); |
696 | } |
9374671f |
697 | |
66239b65 |
698 | /** |
8b096f0a |
699 | * Returns a string showing the size of the message/attachment. |
700 | * |
31310ecd |
701 | * @param int $bytes the filesize in bytes |
8b096f0a |
702 | * @return string the filesize in human readable format |
31310ecd |
703 | * @since 1.0 |
66239b65 |
704 | */ |
705 | function show_readable_size($bytes) { |
706 | $bytes /= 1024; |
707 | $type = 'k'; |
f1ca21bd |
708 | |
66239b65 |
709 | if ($bytes / 1024 > 1) { |
710 | $bytes /= 1024; |
e5f1e71c |
711 | $type = 'M'; |
66239b65 |
712 | } |
f1ca21bd |
713 | |
66239b65 |
714 | if ($bytes < 10) { |
715 | $bytes *= 10; |
716 | settype($bytes, 'integer'); |
717 | $bytes /= 10; |
718 | } else { |
719 | settype($bytes, 'integer'); |
720 | } |
f1ca21bd |
721 | |
91c27aee |
722 | return $bytes . ' ' . $type; |
66239b65 |
723 | } |
9374671f |
724 | |
66239b65 |
725 | /** |
c7aff938 |
726 | * Generates a random string from the character set you pass in |
66239b65 |
727 | * |
31310ecd |
728 | * @param int $size the length of the string to generate |
729 | * @param string $chars a string containing the characters to use |
730 | * @param int $flags a flag to add a specific set to the characters to use: |
8b096f0a |
731 | * Flags: |
732 | * 1 = add lowercase a-z to $chars |
733 | * 2 = add uppercase A-Z to $chars |
734 | * 4 = add numbers 0-9 to $chars |
735 | * @return string the random string |
31310ecd |
736 | * @since 1.0 |
66239b65 |
737 | */ |
66239b65 |
738 | function GenerateRandomString($size, $chars, $flags = 0) { |
739 | if ($flags & 0x1) { |
740 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
741 | } |
742 | if ($flags & 0x2) { |
743 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
744 | } |
745 | if ($flags & 0x4) { |
746 | $chars .= '0123456789'; |
747 | } |
f1ca21bd |
748 | |
66239b65 |
749 | if (($size < 1) || (strlen($chars) < 1)) { |
750 | return ''; |
751 | } |
ff4f08ff |
752 | |
4deb32f1 |
753 | sq_mt_randomize(); /* Initialize the random number generator */ |
ff4f08ff |
754 | |
4deb32f1 |
755 | $String = ''; |
ff4f08ff |
756 | $j = strlen( $chars ) - 1; |
66239b65 |
757 | while (strlen($String) < $size) { |
ff4f08ff |
758 | $String .= $chars{mt_rand(0, $j)}; |
66239b65 |
759 | } |
ff4f08ff |
760 | |
66239b65 |
761 | return $String; |
762 | } |
9374671f |
763 | |
8b096f0a |
764 | /** |
765 | * Escapes special characters for use in IMAP commands. |
4445e6b3 |
766 | * |
31310ecd |
767 | * @param string $str the string to escape |
8b096f0a |
768 | * @return string the escaped string |
31310ecd |
769 | * @since 1.0.3 |
8b096f0a |
770 | */ |
fbb76d0e |
771 | function quoteimap($str) { |
ab1df059 |
772 | return preg_replace("/([\"\\\\])/", "\\\\$1", $str); |
66239b65 |
773 | } |
1899535f |
774 | |
66239b65 |
775 | /** |
4445e6b3 |
776 | * Trims array |
777 | * |
8b096f0a |
778 | * Trims every element in the array, ie. remove the first char of each element |
31310ecd |
779 | * @param array $array the array to trim |
780 | * @since 1.2.0 |
66239b65 |
781 | */ |
782 | function TrimArray(&$array) { |
783 | foreach ($array as $k => $v) { |
784 | global $$k; |
785 | if (is_array($$k)) { |
786 | foreach ($$k as $k2 => $v2) { |
787 | $$k[$k2] = substr($v2, 1); |
23d6bd09 |
788 | } |
66239b65 |
789 | } else { |
790 | $$k = substr($v, 1); |
23d6bd09 |
791 | } |
f1ca21bd |
792 | |
4deb32f1 |
793 | /* Re-assign back to array. */ |
66239b65 |
794 | $array[$k] = $$k; |
795 | } |
f1ca21bd |
796 | } |
23d6bd09 |
797 | |
8b096f0a |
798 | /** |
4445e6b3 |
799 | * Create compose link |
800 | * |
8b096f0a |
801 | * Returns a link to the compose-page, taking in consideration |
802 | * the compose_in_new and javascript settings. |
31310ecd |
803 | * @param string $url the URL to the compose page |
804 | * @param string $text the link text, default "Compose" |
805 | * @param string $target (since 1.4.3) url target |
8b096f0a |
806 | * @return string a link to the compose page |
31310ecd |
807 | * @since 1.4.2 |
8b096f0a |
808 | */ |
d0b119a5 |
809 | function makeComposeLink($url, $text = null, $target='') { |
810 | global $compose_new_win,$javascript_on, $compose_width, $compose_height; |
d62c4938 |
811 | |
812 | if(!$text) { |
813 | $text = _("Compose"); |
814 | } |
815 | |
c9d61baf |
816 | // if not using "compose in new window", make |
f72f61d8 |
817 | // regular link and be done with it |
d62c4938 |
818 | if($compose_new_win != '1') { |
21a957a9 |
819 | return makeInternalLink($url, $text, $target); |
d62c4938 |
820 | } |
821 | |
c9d61baf |
822 | // build the compose in new window link... |
f72f61d8 |
823 | |
824 | |
c435f076 |
825 | // if javascript is on, use onclick event to handle it |
d62c4938 |
826 | if($javascript_on) { |
827 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
cdc4d881 |
828 | $compuri = SM_BASE_URI.$url; |
d0b119a5 |
829 | return "<a href=\"javascript:void(0)\" onclick=\"comp_in_new('$compuri','$compose_width','$compose_height')\">$text</a>"; |
d62c4938 |
830 | } |
831 | |
f72f61d8 |
832 | // otherwise, just open new window using regular HTML |
d62c4938 |
833 | return makeInternalLink($url, $text, '_blank'); |
834 | } |
835 | |
f1ca21bd |
836 | /** |
4445e6b3 |
837 | * Print variable |
838 | * |
8b096f0a |
839 | * sm_print_r($some_variable, [$some_other_variable [, ...]]); |
4445e6b3 |
840 | * |
8b096f0a |
841 | * Debugging function - does the same as print_r, but makes sure special |
842 | * characters are converted to htmlentities first. This will allow |
843 | * values like <some@email.address> to be displayed. |
4445e6b3 |
844 | * The output is wrapped in <<pre>> and <</pre>> tags. |
69567a46 |
845 | * Since 1.4.2 accepts unlimited number of arguments. |
846 | * @since 1.4.1 |
8b096f0a |
847 | * @return void |
848 | */ |
7fe09a30 |
849 | function sm_print_r() { |
50cc40fe |
850 | ob_start(); // Buffer output |
7fe09a30 |
851 | foreach(func_get_args() as $var) { |
852 | print_r($var); |
853 | echo "\n"; |
3896acce |
854 | // php has get_class_methods function that can print class methods |
855 | if (is_object($var)) { |
856 | // get class methods if $var is object |
857 | $aMethods=get_class_methods(get_class($var)); |
351ee7ca |
858 | // make sure that $aMethods is array and array is not empty |
859 | if (is_array($aMethods) && $aMethods!=array()) { |
3896acce |
860 | echo "Object methods:\n"; |
861 | foreach($aMethods as $method) { |
862 | echo '* ' . $method . "\n"; |
863 | } |
864 | } |
865 | echo "\n"; |
866 | } |
7fe09a30 |
867 | } |
50cc40fe |
868 | $buffer = ob_get_contents(); // Grab the print_r output |
869 | ob_end_clean(); // Silently discard the output & stop buffering |
7297873e |
870 | print '<div align="left"><pre>'; |
50cc40fe |
871 | print htmlentities($buffer); |
7297873e |
872 | print '</pre></div>'; |
50cc40fe |
873 | } |
874 | |
3ecad5e6 |
875 | /** |
876 | * version of fwrite which checks for failure |
31310ecd |
877 | * @param resource $fp |
878 | * @param string $string |
879 | * @return number of written bytes. false on failure |
880 | * @since 1.4.3 |
3ecad5e6 |
881 | */ |
882 | function sq_fwrite($fp, $string) { |
c9d61baf |
883 | // write to file |
884 | $count = @fwrite($fp,$string); |
885 | // the number of bytes written should be the length of the string |
886 | if($count != strlen($string)) { |
887 | return FALSE; |
888 | } |
889 | |
890 | return $count; |
3ecad5e6 |
891 | } |
892 | |
36e1180b |
893 | /** |
894 | * sq_get_html_translation_table |
895 | * |
896 | * Returns the translation table used by sq_htmlentities() |
897 | * |
898 | * @param integer $table html translation table. Possible values (without quotes): |
deb22cec |
899 | * <ul> |
900 | * <li>HTML_ENTITIES - full html entities table defined by charset</li> |
901 | * <li>HTML_SPECIALCHARS - html special characters table</li> |
902 | * </ul> |
36e1180b |
903 | * @param integer $quote_style quote encoding style. Possible values (without quotes): |
c9d61baf |
904 | * <ul> |
deb22cec |
905 | * <li>ENT_COMPAT - (default) encode double quotes</li> |
906 | * <li>ENT_NOQUOTES - don't encode double or single quotes</li> |
907 | * <li>ENT_QUOTES - encode double and single quotes</li> |
c9d61baf |
908 | * </ul> |
36e1180b |
909 | * @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value. |
910 | * @return array html translation array |
31310ecd |
911 | * @since 1.5.1 |
36e1180b |
912 | */ |
913 | function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='us-ascii') { |
914 | global $default_charset; |
915 | |
916 | if ($table == HTML_SPECIALCHARS) $charset='us-ascii'; |
917 | |
918 | // Start array with ampersand |
919 | $sq_html_ent_table = array( "&" => '&' ); |
920 | |
921 | // < and > |
922 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
c9d61baf |
923 | array("<" => '<', |
924 | ">" => '>') |
925 | ); |
36e1180b |
926 | // double quotes |
927 | if ($quote_style == ENT_COMPAT) |
928 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
c9d61baf |
929 | array("\"" => '"') |
930 | ); |
36e1180b |
931 | |
932 | // double and single quotes |
933 | if ($quote_style == ENT_QUOTES) |
934 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
c9d61baf |
935 | array("\"" => '"', |
936 | "'" => ''') |
937 | ); |
36e1180b |
938 | |
939 | if ($charset=='auto') $charset=$default_charset; |
940 | |
941 | // add entities that depend on charset |
942 | switch($charset){ |
943 | case 'iso-8859-1': |
944 | include_once(SM_PATH . 'functions/htmlentities/iso-8859-1.php'); |
945 | break; |
946 | case 'utf-8': |
947 | include_once(SM_PATH . 'functions/htmlentities/utf-8.php'); |
948 | break; |
949 | case 'us-ascii': |
950 | default: |
951 | break; |
952 | } |
953 | // return table |
954 | return $sq_html_ent_table; |
955 | } |
956 | |
957 | /** |
958 | * sq_htmlentities |
959 | * |
960 | * Convert all applicable characters to HTML entities. |
17886554 |
961 | * Minimal php requirement - v.4.0.5. |
962 | * |
963 | * Function is designed for people that want to use full power of htmlentities() in |
964 | * i18n environment. |
36e1180b |
965 | * |
966 | * @param string $string string that has to be sanitized |
967 | * @param integer $quote_style quote encoding style. Possible values (without quotes): |
c9d61baf |
968 | * <ul> |
deb22cec |
969 | * <li>ENT_COMPAT - (default) encode double quotes</li> |
970 | * <li>ENT_NOQUOTES - don't encode double or single quotes</li> |
971 | * <li>ENT_QUOTES - encode double and single quotes</li> |
c9d61baf |
972 | * </ul> |
36e1180b |
973 | * @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value. |
974 | * @return string sanitized string |
31310ecd |
975 | * @since 1.5.1 |
36e1180b |
976 | */ |
977 | function sq_htmlentities($string,$quote_style=ENT_COMPAT,$charset='us-ascii') { |
978 | // get translation table |
979 | $sq_html_ent_table=sq_get_html_translation_table(HTML_ENTITIES,$quote_style,$charset); |
980 | // convert characters |
981 | return str_replace(array_keys($sq_html_ent_table),array_values($sq_html_ent_table),$string); |
982 | } |
983 | |
b54acf3f |
984 | /** |
985 | * Tests if string contains 8bit symbols. |
986 | * |
987 | * If charset is not set, function defaults to default_charset. |
91e0dccc |
988 | * $default_charset global must be set correctly if $charset is |
b54acf3f |
989 | * not used. |
990 | * @param string $string tested string |
991 | * @param string $charset charset used in a string |
992 | * @return bool true if 8bit symbols are detected |
17886554 |
993 | * @since 1.5.1 and 1.4.4 |
b54acf3f |
994 | */ |
995 | function sq_is8bit($string,$charset='') { |
996 | global $default_charset; |
997 | |
998 | if ($charset=='') $charset=$default_charset; |
999 | |
1000 | /** |
1001 | * Don't use \240 in ranges. Sometimes RH 7.2 doesn't like it. |
17886554 |
1002 | * Don't use \200-\237 for iso-8859-x charsets. This range |
b54acf3f |
1003 | * stores control symbols in those charsets. |
1004 | * Use preg_match instead of ereg in order to avoid problems |
1005 | * with mbstring overloading |
1006 | */ |
1007 | if (preg_match("/^iso-8859/i",$charset)) { |
1008 | $needle='/\240|[\241-\377]/'; |
1009 | } else { |
1010 | $needle='/[\200-\237]|\240|[\241-\377]/'; |
1011 | } |
1012 | return preg_match("$needle",$string); |
1013 | } |
1014 | |
1015 | /** |
1016 | * Replacement of mb_list_encodings function |
1017 | * |
1018 | * This function provides replacement for function that is available only |
1019 | * in php 5.x. Function does not test all mbstring encodings. Only the ones |
1020 | * that might be used in SM translations. |
1021 | * |
17886554 |
1022 | * Supported strings are stored in session in order to reduce number of |
b54acf3f |
1023 | * mb_internal_encoding function calls. |
1024 | * |
91e0dccc |
1025 | * If you want to test all mbstring encodings - fill $list_of_encodings |
b54acf3f |
1026 | * array. |
17886554 |
1027 | * @return array list of encodings supported by php mbstring extension |
7fe4b6ed |
1028 | * @since 1.5.1 and 1.4.6 |
b54acf3f |
1029 | */ |
1030 | function sq_mb_list_encodings() { |
1031 | if (! function_exists('mb_internal_encoding')) |
1032 | return array(); |
1033 | |
31310ecd |
1034 | // php 5+ function |
1035 | if (function_exists('mb_list_encodings')) { |
1036 | $ret = mb_list_encodings(); |
1037 | array_walk($ret,'sq_lowercase_array_vals'); |
1038 | return $ret; |
1039 | } |
1040 | |
b54acf3f |
1041 | // don't try to test encodings, if they are already stored in session |
1042 | if (sqgetGlobalVar('mb_supported_encodings',$mb_supported_encodings,SQ_SESSION)) |
1043 | return $mb_supported_encodings; |
1044 | |
1045 | // save original encoding |
1046 | $orig_encoding=mb_internal_encoding(); |
1047 | |
1048 | $list_of_encoding=array( |
1049 | 'pass', |
1050 | 'auto', |
1051 | 'ascii', |
1052 | 'jis', |
1053 | 'utf-8', |
1054 | 'sjis', |
1055 | 'euc-jp', |
1056 | 'iso-8859-1', |
1057 | 'iso-8859-2', |
1058 | 'iso-8859-7', |
1059 | 'iso-8859-9', |
1060 | 'iso-8859-15', |
1061 | 'koi8-r', |
1062 | 'koi8-u', |
1063 | 'big5', |
1064 | 'gb2312', |
98abf408 |
1065 | 'gb18030', |
b54acf3f |
1066 | 'windows-1251', |
1067 | 'windows-1255', |
1068 | 'windows-1256', |
1069 | 'tis-620', |
1070 | 'iso-2022-jp', |
ba40ff8b |
1071 | 'euc-cn', |
b54acf3f |
1072 | 'euc-kr', |
ba40ff8b |
1073 | 'euc-tw', |
1074 | 'uhc', |
b54acf3f |
1075 | 'utf7-imap'); |
1076 | |
1077 | $supported_encodings=array(); |
1078 | |
1079 | foreach ($list_of_encoding as $encoding) { |
1080 | // try setting encodings. suppress warning messages |
1081 | if (@mb_internal_encoding($encoding)) |
1082 | $supported_encodings[]=$encoding; |
1083 | } |
1084 | |
1085 | // restore original encoding |
1086 | mb_internal_encoding($orig_encoding); |
1087 | |
1088 | // register list in session |
1089 | sqsession_register($supported_encodings,'mb_supported_encodings'); |
1090 | |
1091 | return $supported_encodings; |
1092 | } |
1093 | |
31310ecd |
1094 | /** |
1095 | * Callback function used to lowercase array values. |
1096 | * @param string $val array value |
1097 | * @param mixed $key array key |
7fe4b6ed |
1098 | * @since 1.5.1 and 1.4.6 |
31310ecd |
1099 | */ |
1100 | function sq_lowercase_array_vals(&$val,$key) { |
1101 | $val = strtolower($val); |
1102 | } |
1103 | |
1104 | |
c7aff938 |
1105 | /** |
1106 | * Function returns number of characters in string. |
1107 | * |
1108 | * Returned number might be different from number of bytes in string, |
91c27aee |
1109 | * if $charset is multibyte charset. Detection depends on mbstring |
98abf408 |
1110 | * functions. If mbstring does not support tested multibyte charset, |
91c27aee |
1111 | * vanilla string length function is used. |
c7aff938 |
1112 | * @param string $str string |
1113 | * @param string $charset charset |
7fe4b6ed |
1114 | * @since 1.5.1 and 1.4.6 |
91c27aee |
1115 | * @return integer number of characters in string |
c7aff938 |
1116 | */ |
31310ecd |
1117 | function sq_strlen($str, $charset=null){ |
c7aff938 |
1118 | // default option |
31310ecd |
1119 | if (is_null($charset)) return strlen($str); |
1120 | |
1121 | // lowercase charset name |
1122 | $charset=strtolower($charset); |
c7aff938 |
1123 | |
1124 | // use automatic charset detection, if function call asks for it |
1125 | if ($charset=='auto') { |
37780b3e |
1126 | global $default_charset, $squirrelmail_language; |
c7aff938 |
1127 | set_my_charset(); |
1128 | $charset=$default_charset; |
37780b3e |
1129 | if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; |
c7aff938 |
1130 | } |
1131 | |
98abf408 |
1132 | // Use mbstring only with listed charsets |
1133 | $aList_of_mb_charsets=array('utf-8','big5','gb2312','gb18030','euc-jp','euc-cn','euc-tw','euc-kr'); |
c7aff938 |
1134 | |
1135 | // calculate string length according to charset |
98abf408 |
1136 | if (in_array($charset,$aList_of_mb_charsets) && in_array($charset,sq_mb_list_encodings())) { |
1137 | $real_length = mb_strlen($str,$charset); |
c7aff938 |
1138 | } else { |
91c27aee |
1139 | // own strlen detection code is removed because missing strpos, |
98abf408 |
1140 | // strtoupper and substr implementations break string wrapping. |
c7aff938 |
1141 | $real_length=strlen($str); |
1142 | } |
1143 | return $real_length; |
1144 | } |
1145 | |
17886554 |
1146 | /** |
1147 | * string padding with multibyte support |
1148 | * |
1149 | * @link http://www.php.net/str_pad |
1150 | * @param string $string original string |
1151 | * @param integer $width padded string width |
1152 | * @param string $pad padding symbols |
91c27aee |
1153 | * @param integer $padtype padding type |
17886554 |
1154 | * (internal php defines, see str_pad() description) |
1155 | * @param string $charset charset used in original string |
1156 | * @return string padded string |
1157 | */ |
1158 | function sq_str_pad($string, $width, $pad, $padtype, $charset='') { |
1159 | |
1160 | $charset = strtolower($charset); |
1161 | $padded_string = ''; |
1162 | |
1163 | switch ($charset) { |
1164 | case 'utf-8': |
1165 | case 'big5': |
1166 | case 'gb2312': |
1167 | case 'euc-kr': |
1168 | /* |
1169 | * all multibyte charsets try to increase width value by |
1170 | * adding difference between number of bytes and real length |
1171 | */ |
1172 | $width = $width - sq_strlen($string,$charset) + strlen($string); |
1173 | default: |
1174 | $padded_string=str_pad($string,$width,$pad,$padtype); |
1175 | } |
1176 | return $padded_string; |
1177 | } |
98abf408 |
1178 | |
1179 | /** |
1180 | * Wrapper that is used to switch between vanilla and multibyte substr |
1181 | * functions. |
1182 | * @param string $string |
1183 | * @param integer $start |
1184 | * @param integer $length |
1185 | * @param string $charset |
1186 | * @return string |
1187 | * @since 1.5.1 |
1188 | * @link http://www.php.net/substr |
1189 | * @link http://www.php.net/mb_substr |
1190 | */ |
1191 | function sq_substr($string,$start,$length,$charset='auto') { |
1192 | // use automatic charset detection, if function call asks for it |
1193 | if ($charset=='auto') { |
37780b3e |
1194 | global $default_charset, $squirrelmail_language; |
98abf408 |
1195 | set_my_charset(); |
1196 | $charset=$default_charset; |
37780b3e |
1197 | if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; |
98abf408 |
1198 | } |
1199 | $charset = strtolower($charset); |
91c27aee |
1200 | if (function_exists('mb_internal_encoding') && |
98abf408 |
1201 | in_array($charset,sq_mb_list_encodings())) { |
1202 | return mb_substr($string,$start,$length,$charset); |
1203 | } |
1204 | // TODO: add mbstring independent code |
1205 | |
1206 | // use vanilla string functions as last option |
1207 | return substr($string,$start,$length); |
1208 | } |
1209 | |
1210 | /** |
1211 | * Wrapper that is used to switch between vanilla and multibyte strpos |
1212 | * functions. |
1213 | * @param string $haystack |
1214 | * @param mixed $needle |
1215 | * @param integer $offset |
1216 | * @param string $charset |
1217 | * @return string |
1218 | * @since 1.5.1 |
1219 | * @link http://www.php.net/strpos |
1220 | * @link http://www.php.net/mb_strpos |
1221 | */ |
1222 | function sq_strpos($haystack,$needle,$offset,$charset='auto') { |
1223 | // use automatic charset detection, if function call asks for it |
1224 | if ($charset=='auto') { |
37780b3e |
1225 | global $default_charset, $squirrelmail_language; |
98abf408 |
1226 | set_my_charset(); |
1227 | $charset=$default_charset; |
37780b3e |
1228 | if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; |
98abf408 |
1229 | } |
1230 | $charset = strtolower($charset); |
91c27aee |
1231 | if (function_exists('mb_internal_encoding') && |
98abf408 |
1232 | in_array($charset,sq_mb_list_encodings())) { |
1233 | return mb_strpos($haystack,$needle,$offset,$charset); |
1234 | } |
1235 | // TODO: add mbstring independent code |
1236 | |
1237 | // use vanilla string functions as last option |
1238 | return strpos($haystack,$needle,$offset); |
1239 | } |
1240 | |
1241 | /** |
1242 | * Wrapper that is used to switch between vanilla and multibyte strtoupper |
1243 | * functions. |
1244 | * @param string $string |
1245 | * @param string $charset |
1246 | * @return string |
1247 | * @since 1.5.1 |
1248 | * @link http://www.php.net/strtoupper |
1249 | * @link http://www.php.net/mb_strtoupper |
1250 | */ |
1251 | function sq_strtoupper($string,$charset='auto') { |
1252 | // use automatic charset detection, if function call asks for it |
1253 | if ($charset=='auto') { |
37780b3e |
1254 | global $default_charset,$squirrelmail_language; |
98abf408 |
1255 | set_my_charset(); |
1256 | $charset=$default_charset; |
37780b3e |
1257 | if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; |
98abf408 |
1258 | } |
1259 | $charset = strtolower($charset); |
91c27aee |
1260 | if (function_exists('mb_strtoupper') && |
98abf408 |
1261 | in_array($charset,sq_mb_list_encodings())) { |
1262 | return mb_strtoupper($string,$charset); |
1263 | } |
1264 | // TODO: add mbstring independent code |
1265 | |
1266 | // use vanilla string functions as last option |
1267 | return strtoupper($string); |
1268 | } |
a24cf710 |
1269 | |
1270 | /** |
1271 | * Counts 8bit bytes in string |
1272 | * @param string $string tested string |
1273 | * @return integer number of 8bit bytes |
1274 | */ |
1275 | function sq_count8bit($string) { |
1276 | $count=0; |
1277 | for ($i=0; $i<strlen($string); $i++) { |
1278 | if (ord($string[$i]) > 127) $count++; |
1279 | } |
1280 | return $count; |
1281 | } |
7f62aaef |
1282 | |
a9a7cda1 |
1283 | ?> |