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