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