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 | |
217 | // inner loop, (obviously) handles wrapping up to |
218 | // the next newline |
219 | while ($pos < $nextNewline) { |
220 | // skip over initial spaces |
221 | while (($pos < $nextNewline) && (ctype_space ($body{$pos}))) { |
222 | $pos++; |
223 | } |
224 | |
bb977394 |
225 | |
c9d61baf |
226 | // if this is a short line then just append it and continue outer loop |
6eaf5320 |
227 | if (($outStringCol + $nextNewline - $pos) <= ($wrap - $citeLevel - 1) ) { |
c9d61baf |
228 | // if this is the final line in the input string then include |
229 | // any trailing newlines |
6eaf5320 |
230 | // echo substr($body,$pos,$wrap). "<br />"; |
c9d61baf |
231 | if (($nextNewline + 1 == $length) && ($body{$nextNewline} == "\n")) { |
232 | $nextNewline++; |
233 | } |
234 | |
bb977394 |
235 | // trim trailing spaces |
236 | $lastRealChar = $nextNewline; |
237 | while (($lastRealChar > $pos && $lastRealChar < $length) && (ctype_space ($body{$lastRealChar}))) { |
238 | $lastRealChar--; |
239 | } |
6eaf5320 |
240 | |
bb977394 |
241 | // decide if appending the short string is what we want |
242 | if (($nextNewline < $length && $body{$nextNewline} == "\n") && |
243 | isset($lastRealChar)) { |
6eaf5320 |
244 | |
245 | //check the first word: |
bb977394 |
246 | $mypos = $nextNewline+1; |
247 | while (($mypos < $length) && ($body{$mypos} == '>')) { |
248 | $mypos++; |
249 | // skip over any spaces interleaved among the cite markers |
250 | while (($mypos < $length) && ($body{$mypos} == ' ')) { |
6eaf5320 |
251 | $mypos++; |
bb977394 |
252 | } |
253 | } |
254 | /* |
255 | $ldnspacecnt = 0; |
256 | if ($mypos == $nextNewline+1) { |
257 | while (($mypos < $length) && ($body{$mypos} == ' ')) { |
258 | $ldnspacecnt++; |
6eaf5320 |
259 | } |
bb977394 |
260 | } |
261 | */ |
262 | |
263 | $firstword = substr($body,$mypos,strpos($body,' ',$mypos) - $mypos); |
264 | |
265 | //if ($dowrap || $ldnspacecnt > 1 || ($firstword && ( |
266 | if (!$smartwrap || $firstword && ( |
267 | $firstword{0} == '-' || |
6eaf5320 |
268 | $firstword{0} == '+' || |
269 | $firstword{0} == '*' || |
bb977394 |
270 | $firstword{0} == strtoupper($firstword{0}) || |
6eaf5320 |
271 | strpos($firstword,':'))) { |
bb977394 |
272 | $outString .= substr($body,$pos,($lastRealChar - $pos+1)); |
273 | $outStringCol += ($lastRealChar - $pos); |
274 | sqMakeNewLine($outString,$citeLevel,$outStringCol); |
275 | $nextNewline++; |
276 | $pos = $nextNewline; |
277 | $outStringCol--; |
278 | continue; |
279 | } |
6eaf5320 |
280 | |
c9d61baf |
281 | } |
bb977394 |
282 | |
c9d61baf |
283 | $outString .= substr ($body, $pos, ($lastRealChar - $pos + 1)); |
284 | $outStringCol += ($lastRealChar - $pos); |
285 | $pos = $nextNewline + 1; |
286 | continue; |
287 | } |
bb977394 |
288 | |
c9d61baf |
289 | $eol = $pos + $wrap - $citeLevel - $outStringCol; |
290 | // eol is the tentative end of line. |
291 | // look backwards for there for a whitespace to break at. |
292 | // if it's already less than our current position then |
293 | // our current line is already too long, break immediately |
294 | // and restart outer loop |
295 | if ($eol <= $pos) { |
6eaf5320 |
296 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
c9d61baf |
297 | continue; |
298 | } |
299 | |
300 | // start looking backwards for whitespace to break at. |
301 | $breakPoint = $eol; |
302 | while (($breakPoint > $pos) && (! ctype_space ($body{$breakPoint}))) { |
303 | $breakPoint--; |
304 | } |
305 | |
306 | // if we didn't find a breakpoint by looking backward then we |
307 | // need to figure out what to do about that |
308 | if ($breakPoint == $pos) { |
309 | // if we are not at the beginning then end this line |
310 | // and start a new loop |
311 | if ($outStringCol > ($citeLevel + 1)) { |
312 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
313 | continue; |
314 | } else { |
315 | // just hard break here. most likely we are breaking |
316 | // a really long URL. could also try searching |
317 | // forward for a break point, which is what Mozilla |
318 | // does. don't bother for now. |
319 | $breakPoint = $eol; |
320 | } |
321 | } |
322 | |
323 | // special case: maybe we should have wrapped last |
324 | // time. if the first breakpoint here makes the |
325 | // current line too long and there is already text on |
326 | // the current line, break and loop again if at |
327 | // beginning of current line, don't force break |
328 | $SLOP = 6; |
329 | if ((($outStringCol + ($breakPoint - $pos)) > ($wrap + $SLOP)) && ($outStringCol > ($citeLevel + 1))) { |
330 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
331 | continue; |
332 | } |
333 | |
334 | // skip newlines or whitespace at the beginning of the string |
335 | $substring = substr ($body, $pos, ($breakPoint - $pos)); |
336 | $substring = rtrim ($substring); // do rtrim and ctype_space have the same ideas about whitespace? |
337 | $outString .= $substring; |
338 | $outStringCol += strlen ($substring); |
339 | // advance past the whitespace which caused the wrap |
340 | $pos = $breakPoint; |
341 | while (($pos < $length) && (ctype_space ($body{$pos}))) { |
342 | $pos++; |
343 | } |
344 | if ($pos < $length) { |
345 | sqMakeNewLine ($outString, $citeLevel, $outStringCol); |
346 | } |
347 | } |
348 | } |
349 | |
350 | return $outString; |
351 | } |
352 | |
5cc0b70e |
353 | /** |
354 | * Wraps text at $wrap characters |
355 | * |
356 | * Has a problem with special HTML characters, so call this before |
357 | * you do character translation. |
358 | * |
359 | * Specifically, ' comes up as 5 characters instead of 1. |
360 | * This should not add newlines to the end of lines. |
8b096f0a |
361 | * |
362 | * @param string line the line of text to wrap, by ref |
363 | * @param int wrap the maximum line lenth |
364 | * @return void |
5cc0b70e |
365 | */ |
366 | function sqWordWrap(&$line, $wrap) { |
e842b215 |
367 | global $languages, $squirrelmail_language; |
368 | |
369 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
370 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
371 | if (mb_detect_encoding($line) != 'ASCII') { |
372 | $line = $languages[$squirrelmail_language]['XTRA_CODE']('wordwrap', $line, $wrap); |
373 | return; |
374 | } |
375 | } |
376 | |
5cc0b70e |
377 | ereg("^([\t >]*)([^\t >].*)?$", $line, $regs); |
378 | $beginning_spaces = $regs[1]; |
379 | if (isset($regs[2])) { |
380 | $words = explode(' ', $regs[2]); |
381 | } else { |
382 | $words = ''; |
383 | } |
f1ca21bd |
384 | |
5cc0b70e |
385 | $i = 0; |
386 | $line = $beginning_spaces; |
f1ca21bd |
387 | |
5cc0b70e |
388 | while ($i < count($words)) { |
389 | /* Force one word to be on a line (minimum) */ |
390 | $line .= $words[$i]; |
391 | $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2; |
392 | if (isset($words[$i + 1])) |
393 | $line_len += strlen($words[$i + 1]); |
394 | $i ++; |
f1ca21bd |
395 | |
5cc0b70e |
396 | /* Add more words (as long as they fit) */ |
397 | while ($line_len < $wrap && $i < count($words)) { |
398 | $line .= ' ' . $words[$i]; |
399 | $i++; |
400 | if (isset($words[$i])) |
401 | $line_len += strlen($words[$i]) + 1; |
402 | else |
403 | $line_len += 1; |
404 | } |
f1ca21bd |
405 | |
5cc0b70e |
406 | /* Skip spaces if they are the first thing on a continued line */ |
407 | while (!isset($words[$i]) && $i < count($words)) { |
408 | $i ++; |
409 | } |
f1ca21bd |
410 | |
5cc0b70e |
411 | /* Go to the next line if we have more to process */ |
412 | if ($i < count($words)) { |
e0858036 |
413 | $line .= "\n"; |
5cc0b70e |
414 | } |
415 | } |
416 | } |
417 | |
341abbd6 |
418 | /** |
419 | * Does the opposite of sqWordWrap() |
8b096f0a |
420 | * @param string body the text to un-wordwrap |
421 | * @return void |
341abbd6 |
422 | */ |
423 | function sqUnWordWrap(&$body) { |
e842b215 |
424 | global $squirrelmail_language; |
f1ca21bd |
425 | |
e842b215 |
426 | if ($squirrelmail_language == 'ja_JP') { |
427 | return; |
428 | } |
429 | |
341abbd6 |
430 | $lines = explode("\n", $body); |
431 | $body = ''; |
432 | $PreviousSpaces = ''; |
433 | $cnt = count($lines); |
434 | for ($i = 0; $i < $cnt; $i ++) { |
435 | preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs); |
436 | $CurrentSpaces = $regs[1]; |
437 | if (isset($regs[2])) { |
438 | $CurrentRest = $regs[2]; |
1e4a4feb |
439 | } else { |
f1ca21bd |
440 | $CurrentRest = ''; |
441 | } |
442 | |
341abbd6 |
443 | if ($i == 0) { |
444 | $PreviousSpaces = $CurrentSpaces; |
445 | $body = $lines[$i]; |
446 | } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */ |
447 | && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */ |
448 | && strlen($CurrentRest)) { /* and there's a line to continue with */ |
449 | $body .= ' ' . $CurrentRest; |
450 | } else { |
451 | $body .= "\n" . $lines[$i]; |
452 | $PreviousSpaces = $CurrentSpaces; |
453 | } |
454 | } |
455 | $body .= "\n"; |
456 | } |
457 | |
66239b65 |
458 | /** |
459 | * If $haystack is a full mailbox name and $needle is the mailbox |
460 | * separator character, returns the last part of the mailbox name. |
8b096f0a |
461 | * |
462 | * @param string haystack full mailbox name to search |
463 | * @param string needle the mailbox separator character |
464 | * @return string the last part of the mailbox name |
66239b65 |
465 | */ |
466 | function readShortMailboxName($haystack, $needle) { |
97b1248c |
467 | |
66239b65 |
468 | if ($needle == '') { |
97b1248c |
469 | $elem = $haystack; |
470 | } else { |
f1ca21bd |
471 | $parts = explode($needle, $haystack); |
472 | $elem = array_pop($parts); |
473 | while ($elem == '' && count($parts)) { |
474 | $elem = array_pop($parts); |
475 | } |
66239b65 |
476 | } |
97b1248c |
477 | return( $elem ); |
66239b65 |
478 | } |
3302d0d4 |
479 | |
8b096f0a |
480 | /** |
4445e6b3 |
481 | * php_self |
482 | * |
8b096f0a |
483 | * Creates an URL for the page calling this function, using either the PHP global |
484 | * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. |
485 | * |
486 | * @return string the complete url for this page |
487 | */ |
43fdb2a4 |
488 | function php_self () { |
961ca3d8 |
489 | if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) { |
490 | return $req_uri; |
43fdb2a4 |
491 | } |
f1ca21bd |
492 | |
961ca3d8 |
493 | if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) { |
f72f61d8 |
494 | |
495 | // need to add query string to end of PHP_SELF to match REQUEST_URI |
496 | // |
497 | if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) { |
498 | $php_self .= '?' . $query_string; |
499 | } |
500 | |
961ca3d8 |
501 | return $php_self; |
f1ca21bd |
502 | } |
503 | |
961ca3d8 |
504 | return ''; |
43fdb2a4 |
505 | } |
506 | |
507 | |
66239b65 |
508 | /** |
4445e6b3 |
509 | * get_location |
510 | * |
8b096f0a |
511 | * Determines the location to forward to, relative to your server. |
512 | * This is used in HTTP Location: redirects. |
66239b65 |
513 | * If this doesnt work correctly for you (although it should), you can |
8b096f0a |
514 | * remove all this code except the last two lines, and have it return |
515 | * the right URL for your site, something like: |
516 | * |
517 | * http://www.example.com/squirrelmail/ |
66239b65 |
518 | * |
8b096f0a |
519 | * @return string the base url for this SquirrelMail installation |
66239b65 |
520 | */ |
521 | function get_location () { |
f1ca21bd |
522 | |
961ca3d8 |
523 | global $imap_server_type; |
238703be |
524 | |
4deb32f1 |
525 | /* Get the path, handle virtual directories */ |
f1ca21bd |
526 | if(strpos(php_self(), '?')) { |
527 | $path = substr(php_self(), 0, strpos(php_self(), '?')); |
528 | } else { |
529 | $path = php_self(); |
530 | } |
531 | $path = substr($path, 0, strrpos($path, '/')); |
238703be |
532 | if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) { |
533 | return $full_url . $path; |
534 | } |
535 | |
66239b65 |
536 | /* Check if this is a HTTPS or regular HTTP request. */ |
537 | $proto = 'http://'; |
f1ca21bd |
538 | |
66239b65 |
539 | /* |
540 | * If you have 'SSLOptions +StdEnvVars' in your apache config |
44827b4d |
541 | * OR if you have HTTPS=on in your HTTP_SERVER_VARS |
66239b65 |
542 | * OR if you are on port 443 |
543 | */ |
544 | $getEnvVar = getenv('HTTPS'); |
545 | if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) || |
961ca3d8 |
546 | (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) || |
547 | (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) { |
8a549df2 |
548 | $proto = 'https://'; |
66239b65 |
549 | } |
f1ca21bd |
550 | |
4deb32f1 |
551 | /* Get the hostname from the Host header or server config. */ |
961ca3d8 |
552 | if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) { |
553 | if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) { |
554 | $host = ''; |
555 | } |
66239b65 |
556 | } |
f1ca21bd |
557 | |
66239b65 |
558 | $port = ''; |
559 | if (! strstr($host, ':')) { |
961ca3d8 |
560 | if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) { |
f1ca21bd |
561 | if (($server_port != 80 && $proto == 'http://') || |
961ca3d8 |
562 | ($server_port != 443 && $proto == 'https://')) { |
563 | $port = sprintf(':%d', $server_port); |
66239b65 |
564 | } |
565 | } |
566 | } |
f1ca21bd |
567 | |
8de7f698 |
568 | /* this is a workaround for the weird macosx caching that |
569 | causes Apache to return 16080 as the port number, which causes |
570 | SM to bail */ |
f1ca21bd |
571 | |
8de7f698 |
572 | if ($imap_server_type == 'macosx' && $port == ':16080') { |
573 | $port = ''; |
574 | } |
f1ca21bd |
575 | |
238703be |
576 | /* Fallback is to omit the server name and use a relative */ |
577 | /* URI, although this is not RFC 2616 compliant. */ |
578 | $full_url = ($host ? $proto . $host . $port : ''); |
579 | sqsession_register($full_url, 'sq_base_url'); |
580 | return $full_url . $path; |
66239b65 |
581 | } |
dcaf2a49 |
582 | |
9374671f |
583 | |
66239b65 |
584 | /** |
4445e6b3 |
585 | * Encrypts password |
586 | * |
8b096f0a |
587 | * These functions are used to encrypt the password before it is |
588 | * stored in a cookie. The encryption key is generated by |
589 | * OneTimePadCreate(); |
590 | * |
591 | * @param string string the (password)string to encrypt |
592 | * @param string epad the encryption key |
593 | * @return string the base64-encoded encrypted password |
66239b65 |
594 | */ |
595 | function OneTimePadEncrypt ($string, $epad) { |
596 | $pad = base64_decode($epad); |
597 | $encrypted = ''; |
598 | for ($i = 0; $i < strlen ($string); $i++) { |
599 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
600 | } |
f1ca21bd |
601 | |
66239b65 |
602 | return base64_encode($encrypted); |
603 | } |
604 | |
8b096f0a |
605 | /** |
4445e6b3 |
606 | * Decrypts a password from the cookie |
607 | * |
608 | * Decrypts a password from the cookie, encrypted by OneTimePadEncrypt. |
8b096f0a |
609 | * This uses the encryption key that is stored in the session. |
610 | * |
611 | * @param string string the string to decrypt |
612 | * @param string epad the encryption key from the session |
613 | * @return string the decrypted password |
614 | */ |
66239b65 |
615 | function OneTimePadDecrypt ($string, $epad) { |
616 | $pad = base64_decode($epad); |
617 | $encrypted = base64_decode ($string); |
618 | $decrypted = ''; |
619 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
620 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
621 | } |
f1ca21bd |
622 | |
66239b65 |
623 | return $decrypted; |
624 | } |
9374671f |
625 | |
9374671f |
626 | |
66239b65 |
627 | /** |
4445e6b3 |
628 | * Randomizes the mt_rand() function. |
629 | * |
c9d61baf |
630 | * Toss this in strings or integers and it will seed the generator |
631 | * appropriately. With strings, it is better to get them long. |
4445e6b3 |
632 | * Use md5() to lengthen smaller strings. |
8b096f0a |
633 | * |
634 | * @param mixed val a value to seed the random number generator |
635 | * @return void |
66239b65 |
636 | */ |
637 | function sq_mt_seed($Val) { |
4deb32f1 |
638 | /* if mt_getrandmax() does not return a 2^n - 1 number, |
639 | this might not work well. This uses $Max as a bitmask. */ |
66239b65 |
640 | $Max = mt_getrandmax(); |
f1ca21bd |
641 | |
66239b65 |
642 | if (! is_int($Val)) { |
66239b65 |
643 | $Val = crc32($Val); |
66239b65 |
644 | } |
f1ca21bd |
645 | |
66239b65 |
646 | if ($Val < 0) { |
647 | $Val *= -1; |
648 | } |
f1ca21bd |
649 | |
66239b65 |
650 | if ($Val = 0) { |
651 | return; |
652 | } |
f1ca21bd |
653 | |
66239b65 |
654 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
655 | } |
9374671f |
656 | |
9374671f |
657 | |
66239b65 |
658 | /** |
4445e6b3 |
659 | * Init random number generator |
660 | * |
66239b65 |
661 | * This function initializes the random number generator fairly well. |
662 | * It also only initializes it once, so you don't accidentally get |
663 | * the same 'random' numbers twice in one session. |
8b096f0a |
664 | * |
665 | * @return void |
66239b65 |
666 | */ |
667 | function sq_mt_randomize() { |
66239b65 |
668 | static $randomized; |
f1ca21bd |
669 | |
66239b65 |
670 | if ($randomized) { |
671 | return; |
672 | } |
f1ca21bd |
673 | |
66239b65 |
674 | /* Global. */ |
961ca3d8 |
675 | sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER); |
676 | sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER); |
66239b65 |
677 | sq_mt_seed((int)((double) microtime() * 1000000)); |
961ca3d8 |
678 | sq_mt_seed(md5($remote_port . $remote_addr . getmypid())); |
f1ca21bd |
679 | |
66239b65 |
680 | /* getrusage */ |
681 | if (function_exists('getrusage')) { |
4deb32f1 |
682 | /* Avoid warnings with Win32 */ |
66239b65 |
683 | $dat = @getrusage(); |
684 | if (isset($dat) && is_array($dat)) { |
821a8e9c |
685 | $Str = ''; |
686 | foreach ($dat as $k => $v) |
66239b65 |
687 | { |
688 | $Str .= $k . $v; |
689 | } |
821a8e9c |
690 | sq_mt_seed(md5($Str)); |
66239b65 |
691 | } |
692 | } |
f1ca21bd |
693 | |
961ca3d8 |
694 | if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) { |
695 | sq_mt_seed(md5($unique_id)); |
0b97a708 |
696 | } |
f1ca21bd |
697 | |
66239b65 |
698 | $randomized = 1; |
699 | } |
700 | |
8b096f0a |
701 | /** |
4445e6b3 |
702 | * Creates encryption key |
703 | * |
8b096f0a |
704 | * Creates an encryption key for encrypting the password stored in the cookie. |
705 | * The encryption key itself is stored in the session. |
706 | * |
707 | * @param int length optional, length of the string to generate |
708 | * @return string the encryption key |
709 | */ |
66239b65 |
710 | function OneTimePadCreate ($length=100) { |
711 | sq_mt_randomize(); |
f1ca21bd |
712 | |
66239b65 |
713 | $pad = ''; |
714 | for ($i = 0; $i < $length; $i++) { |
715 | $pad .= chr(mt_rand(0,255)); |
716 | } |
f1ca21bd |
717 | |
66239b65 |
718 | return base64_encode($pad); |
719 | } |
9374671f |
720 | |
66239b65 |
721 | /** |
8b096f0a |
722 | * Returns a string showing the size of the message/attachment. |
723 | * |
724 | * @param int bytes the filesize in bytes |
725 | * @return string the filesize in human readable format |
66239b65 |
726 | */ |
727 | function show_readable_size($bytes) { |
728 | $bytes /= 1024; |
729 | $type = 'k'; |
f1ca21bd |
730 | |
66239b65 |
731 | if ($bytes / 1024 > 1) { |
732 | $bytes /= 1024; |
e5f1e71c |
733 | $type = 'M'; |
66239b65 |
734 | } |
f1ca21bd |
735 | |
66239b65 |
736 | if ($bytes < 10) { |
737 | $bytes *= 10; |
738 | settype($bytes, 'integer'); |
739 | $bytes /= 10; |
740 | } else { |
741 | settype($bytes, 'integer'); |
742 | } |
f1ca21bd |
743 | |
66239b65 |
744 | return $bytes . '<small> ' . $type . '</small>'; |
745 | } |
9374671f |
746 | |
66239b65 |
747 | /** |
748 | * Generates a random string from the caracter set you pass in |
749 | * |
8b096f0a |
750 | * @param int size the size of the string to generate |
751 | * @param string chars a string containing the characters to use |
752 | * @param int flags a flag to add a specific set to the characters to use: |
753 | * Flags: |
754 | * 1 = add lowercase a-z to $chars |
755 | * 2 = add uppercase A-Z to $chars |
756 | * 4 = add numbers 0-9 to $chars |
757 | * @return string the random string |
66239b65 |
758 | */ |
66239b65 |
759 | function GenerateRandomString($size, $chars, $flags = 0) { |
760 | if ($flags & 0x1) { |
761 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
762 | } |
763 | if ($flags & 0x2) { |
764 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
765 | } |
766 | if ($flags & 0x4) { |
767 | $chars .= '0123456789'; |
768 | } |
f1ca21bd |
769 | |
66239b65 |
770 | if (($size < 1) || (strlen($chars) < 1)) { |
771 | return ''; |
772 | } |
ff4f08ff |
773 | |
4deb32f1 |
774 | sq_mt_randomize(); /* Initialize the random number generator */ |
ff4f08ff |
775 | |
4deb32f1 |
776 | $String = ''; |
ff4f08ff |
777 | $j = strlen( $chars ) - 1; |
66239b65 |
778 | while (strlen($String) < $size) { |
ff4f08ff |
779 | $String .= $chars{mt_rand(0, $j)}; |
66239b65 |
780 | } |
ff4f08ff |
781 | |
66239b65 |
782 | return $String; |
783 | } |
9374671f |
784 | |
8b096f0a |
785 | /** |
786 | * Escapes special characters for use in IMAP commands. |
4445e6b3 |
787 | * |
8b096f0a |
788 | * @param string the string to escape |
789 | * @return string the escaped string |
790 | */ |
fbb76d0e |
791 | function quoteimap($str) { |
ab1df059 |
792 | return preg_replace("/([\"\\\\])/", "\\\\$1", $str); |
66239b65 |
793 | } |
1899535f |
794 | |
66239b65 |
795 | /** |
4445e6b3 |
796 | * Trims array |
797 | * |
8b096f0a |
798 | * Trims every element in the array, ie. remove the first char of each element |
799 | * @param array array the array to trim |
66239b65 |
800 | */ |
801 | function TrimArray(&$array) { |
802 | foreach ($array as $k => $v) { |
803 | global $$k; |
804 | if (is_array($$k)) { |
805 | foreach ($$k as $k2 => $v2) { |
806 | $$k[$k2] = substr($v2, 1); |
23d6bd09 |
807 | } |
66239b65 |
808 | } else { |
809 | $$k = substr($v, 1); |
23d6bd09 |
810 | } |
f1ca21bd |
811 | |
4deb32f1 |
812 | /* Re-assign back to array. */ |
66239b65 |
813 | $array[$k] = $$k; |
814 | } |
f1ca21bd |
815 | } |
23d6bd09 |
816 | |
8b096f0a |
817 | /** |
4445e6b3 |
818 | * Create compose link |
819 | * |
8b096f0a |
820 | * Returns a link to the compose-page, taking in consideration |
821 | * the compose_in_new and javascript settings. |
822 | * @param string url the URL to the compose page |
823 | * @param string text the link text, default "Compose" |
824 | * @return string a link to the compose page |
825 | */ |
21a957a9 |
826 | function makeComposeLink($url, $text = null, $target='') |
d62c4938 |
827 | { |
828 | global $compose_new_win,$javascript_on; |
829 | |
830 | if(!$text) { |
831 | $text = _("Compose"); |
832 | } |
833 | |
f72f61d8 |
834 | |
c9d61baf |
835 | // if not using "compose in new window", make |
f72f61d8 |
836 | // regular link and be done with it |
d62c4938 |
837 | if($compose_new_win != '1') { |
21a957a9 |
838 | return makeInternalLink($url, $text, $target); |
d62c4938 |
839 | } |
840 | |
f72f61d8 |
841 | |
c9d61baf |
842 | // build the compose in new window link... |
f72f61d8 |
843 | |
844 | |
845 | // if javascript is on, use onClick event to handle it |
d62c4938 |
846 | if($javascript_on) { |
847 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
848 | return '<a href="javascript:void(0)" onclick="comp_in_new(\''.$base_uri.$url.'\')">'. $text.'</a>'; |
849 | } |
850 | |
f72f61d8 |
851 | |
852 | // otherwise, just open new window using regular HTML |
d62c4938 |
853 | return makeInternalLink($url, $text, '_blank'); |
f72f61d8 |
854 | |
d62c4938 |
855 | } |
856 | |
f1ca21bd |
857 | /** |
4445e6b3 |
858 | * Print variable |
859 | * |
8b096f0a |
860 | * sm_print_r($some_variable, [$some_other_variable [, ...]]); |
4445e6b3 |
861 | * |
8b096f0a |
862 | * Debugging function - does the same as print_r, but makes sure special |
863 | * characters are converted to htmlentities first. This will allow |
864 | * values like <some@email.address> to be displayed. |
4445e6b3 |
865 | * The output is wrapped in <<pre>> and <</pre>> tags. |
8b096f0a |
866 | * |
867 | * @return void |
868 | */ |
7fe09a30 |
869 | function sm_print_r() { |
50cc40fe |
870 | ob_start(); // Buffer output |
7fe09a30 |
871 | foreach(func_get_args() as $var) { |
872 | print_r($var); |
873 | echo "\n"; |
874 | } |
50cc40fe |
875 | $buffer = ob_get_contents(); // Grab the print_r output |
876 | ob_end_clean(); // Silently discard the output & stop buffering |
f1ca21bd |
877 | print '<pre>'; |
50cc40fe |
878 | print htmlentities($buffer); |
f1ca21bd |
879 | print '</pre>'; |
50cc40fe |
880 | } |
881 | |
3ecad5e6 |
882 | /** |
883 | * version of fwrite which checks for failure |
884 | */ |
885 | function sq_fwrite($fp, $string) { |
c9d61baf |
886 | // write to file |
887 | $count = @fwrite($fp,$string); |
888 | // the number of bytes written should be the length of the string |
889 | if($count != strlen($string)) { |
890 | return FALSE; |
891 | } |
892 | |
893 | return $count; |
3ecad5e6 |
894 | } |
895 | |
36e1180b |
896 | /** |
897 | * sq_get_html_translation_table |
898 | * |
899 | * Returns the translation table used by sq_htmlentities() |
900 | * |
901 | * @param integer $table html translation table. Possible values (without quotes): |
deb22cec |
902 | * <ul> |
903 | * <li>HTML_ENTITIES - full html entities table defined by charset</li> |
904 | * <li>HTML_SPECIALCHARS - html special characters table</li> |
905 | * </ul> |
36e1180b |
906 | * @param integer $quote_style quote encoding style. Possible values (without quotes): |
c9d61baf |
907 | * <ul> |
deb22cec |
908 | * <li>ENT_COMPAT - (default) encode double quotes</li> |
909 | * <li>ENT_NOQUOTES - don't encode double or single quotes</li> |
910 | * <li>ENT_QUOTES - encode double and single quotes</li> |
c9d61baf |
911 | * </ul> |
36e1180b |
912 | * @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value. |
913 | * @return array html translation array |
914 | */ |
915 | function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='us-ascii') { |
916 | global $default_charset; |
917 | |
918 | if ($table == HTML_SPECIALCHARS) $charset='us-ascii'; |
919 | |
920 | // Start array with ampersand |
921 | $sq_html_ent_table = array( "&" => '&' ); |
922 | |
923 | // < and > |
924 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
c9d61baf |
925 | array("<" => '<', |
926 | ">" => '>') |
927 | ); |
36e1180b |
928 | // double quotes |
929 | if ($quote_style == ENT_COMPAT) |
930 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
c9d61baf |
931 | array("\"" => '"') |
932 | ); |
36e1180b |
933 | |
934 | // double and single quotes |
935 | if ($quote_style == ENT_QUOTES) |
936 | $sq_html_ent_table = array_merge($sq_html_ent_table, |
c9d61baf |
937 | array("\"" => '"', |
938 | "'" => ''') |
939 | ); |
36e1180b |
940 | |
941 | if ($charset=='auto') $charset=$default_charset; |
942 | |
943 | // add entities that depend on charset |
944 | switch($charset){ |
945 | case 'iso-8859-1': |
946 | include_once(SM_PATH . 'functions/htmlentities/iso-8859-1.php'); |
947 | break; |
948 | case 'utf-8': |
949 | include_once(SM_PATH . 'functions/htmlentities/utf-8.php'); |
950 | break; |
951 | case 'us-ascii': |
952 | default: |
953 | break; |
954 | } |
955 | // return table |
956 | return $sq_html_ent_table; |
957 | } |
958 | |
959 | /** |
960 | * sq_htmlentities |
961 | * |
962 | * Convert all applicable characters to HTML entities. |
963 | * Minimal php requirement - v.4.0.5 |
964 | * |
965 | * @param string $string string that has to be sanitized |
966 | * @param integer $quote_style quote encoding style. Possible values (without quotes): |
c9d61baf |
967 | * <ul> |
deb22cec |
968 | * <li>ENT_COMPAT - (default) encode double quotes</li> |
969 | * <li>ENT_NOQUOTES - don't encode double or single quotes</li> |
970 | * <li>ENT_QUOTES - encode double and single quotes</li> |
c9d61baf |
971 | * </ul> |
36e1180b |
972 | * @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value. |
973 | * @return string sanitized string |
974 | */ |
975 | function sq_htmlentities($string,$quote_style=ENT_COMPAT,$charset='us-ascii') { |
976 | // get translation table |
977 | $sq_html_ent_table=sq_get_html_translation_table(HTML_ENTITIES,$quote_style,$charset); |
978 | // convert characters |
979 | return str_replace(array_keys($sq_html_ent_table),array_values($sq_html_ent_table),$string); |
980 | } |
981 | |
43fdb2a4 |
982 | $PHP_SELF = php_self(); |
4445e6b3 |
983 | ?> |