5 //*************************************************************************
6 // Count the number of occurances of $needle are in $haystack.
7 //*************************************************************************
8 function countCharInString($haystack, $needle) {
9 $len = strlen($haystack);
10 for ($i = 0; $i < $len; $i++
) {
11 if ($haystack[$i] == $needle)
17 //*************************************************************************
18 // Read from the back of $haystack until $needle is found, or the begining
19 // of the $haystack is reached.
20 //*************************************************************************
21 function readShortMailboxName($haystack, $needle) {
22 if (substr($haystack, -1) == $needle)
23 $haystack = substr($haystack, 0, strlen($haystack) - 1);
25 if (strrpos($haystack, $needle)) {
26 $pos = strrpos($haystack, $needle) +
1;
27 $data = substr($haystack, $pos, strlen($haystack));
34 // Searches for the next position in a string minus white space
35 function next_pos_minus_white ($haystack, $pos) {
36 while (substr($haystack, $pos, 1) == " " ||
37 substr($haystack, $pos, 1) == "\t" ||
38 substr($haystack, $pos, 1) == "\n" ||
39 substr($haystack, $pos, 1) == "\r") {
40 if ($pos >= strlen($haystack))
47 // Wraps text at $wrap characters
48 function sqWordWrap($passed, $wrap) {
49 $passed = str_replace("<", "<", $passed);
50 $passed = str_replace(">", ">", $passed);
52 preg_match("/^(\s|>)+/", $passed, $regs);
53 $beginning_spaces = $regs[0];
55 $words = explode(" ", $passed);
57 $line_len = strlen($words[0])+
1;
59 if (count($words) > 1) {
60 while ($i++
< count($words)) {
61 while ($line_len < $wrap) {
62 $line = "$line$words[$i] ";
64 $line_len = $line_len +
strlen($words[$i]) +
1;
66 $line_len = strlen($words[$i])+
1;
67 if ($line_len <= $wrap) {
68 if (strlen($beginning_spaces) +
2 >= $wrap)
69 $beginning_spaces = "";
70 if ($i < count($words)) { // don't <BR> the last line
71 $line = "$line\n$beginning_spaces";
73 $line = "$line$words[$i] ";
74 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
1;
77 $endline = $words[$i];
78 while ($line_len >= $wrap) {
79 $bigline = substr($endline, 0, $wrap);
80 $endline = substr($endline, $wrap, strlen($endline));
81 $line_len = strlen($endline);
82 $line = "$line$bigline<BR>";
85 if (strlen($line) > $wrap)
86 $line = "$line\n$words[$i]";
88 $line = "$line$words[$i]";
89 $line_len = strlen($words[$i]);
96 $line = str_replace(">", ">", $line);
97 $line = str_replace("<", "<", $line);
101 /** Returns an array of email addresses **/
102 function parseAddrs($text) {
103 if (trim($text) == "") {
106 $text = str_replace(" ", "", $text);
107 $text = ereg_replace( '"[^"]*"', "", $text);
108 $text = str_replace(",", ";", $text);
109 $array = explode(";", $text);
110 for ($i = 0; $i < count ($array); $i++
) {
111 $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
112 $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
117 /** Returns a line of comma separated email addresses from an array **/
118 function getLineOfAddrs($array) {
119 if (is_array($array)) {
120 $to_line = implode(", ", $array);
121 $to_line = trim(ereg_replace(",,+", ",", $to_line));
128 function translateText($body, $wrap_at, $charset) {
129 global $where, $what; // from searching
131 if (!isset($url_parser_php)) {
132 include "../functions/url_parser.php";
135 $body_ary = explode("\n", $body);
136 for ($i=0; $i < count($body_ary); $i++
) {
137 $line = $body_ary[$i];
138 $line = charset_decode($charset, $line);
139 $line = str_replace("\t", ' ', $line);
141 if (strlen($line) - 2 >= $wrap_at) {
142 $line = sqWordWrap($line, $wrap_at);
145 $line = str_replace(' ', ' ', $line);
146 $line = nl2br($line);
148 // Removed parseEmail and integrated it into parseUrl
149 // This line is no longer needed.
150 // $line = parseEmail ($line);
151 $line = parseUrl ($line);
153 $test_line = str_replace(' ', '', $line);
154 if (strpos($test_line, '>>') === 0) {
155 $line = "<FONT COLOR=FF0000>$line</FONT>\n";
156 } else if (strpos($test_line, '>') === 0) {
157 $line = "<FONT COLOR=800000>$line</FONT>\n";
162 $line = '<tt>' . $line . '</tt>';
165 $body_ary[$i] = $line . '<br>';
167 $body = implode("\n", $body_ary);
172 /* SquirrelMail version number -- DO NOT CHANGE */
176 function find_mailbox_name ($mailbox) {
177 $mailbox = trim($mailbox);
178 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
179 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
180 $pos = strrpos ($mailbox, "\"")+
1;
181 $box = substr($mailbox, $pos);
183 $box = substr($mailbox, strrpos($mailbox, " ")+
1, strlen($mailbox));
188 function replace_spaces ($string) {
189 return str_replace(" ", " ", $string);
192 function replace_escaped_spaces ($string) {
193 return str_replace(" ", " ", $string);
196 function get_location () {
197 # This determines the location to forward to relative
198 # to your server. If this doesnt work correctly for
199 # you (although it should), you can remove all this
200 # code except the last two lines, and change the header()
201 # function to look something like this, customized to
202 # the location of SquirrelMail on your server:
204 # http://www.myhost.com/squirrelmail/src/login.php
206 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST;
209 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
211 // Check if this is a HTTPS or regular HTTP request
213 if(isset($HTTPS) && $HTTPS == 'on' ) {
217 // Get the hostname from the Host header or server config.
218 // Fallback is to omit the server name and use a relative URI,
219 // although this is not RFC 2616 compliant.
220 if(isset($HTTP_HOST) && !empty($HTTP_HOST)) {
221 $location = $proto . $HTTP_HOST . $path;
222 } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
223 $location = $proto . $SERVER_NAME . $path;
230 function sqStripSlashes($string) {
231 if (get_magic_quotes_gpc()) {
232 $string = stripslashes($string);
238 // These functions are used to encrypt the passowrd before it is
239 // stored in a cookie.
240 function OneTimePadEncrypt ($string, $pad) {
241 for ($i = 0; $i < strlen ($string); $i++
) {
242 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
245 return base64_encode($encrypted);
248 function OneTimePadDecrypt ($string, $pad) {
249 $encrypted = base64_decode ($string);
251 for ($i = 0; $i < strlen ($encrypted); $i++
) {
252 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
258 function OneTimePadCreate ($length=100) {
259 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
262 if (function_exists("crc32")) {
263 $seed1 = (double) microtime() * 1000000;
264 $seed2 = md5($REMOTE_PORT . $REMOTE_ADDR . $UNIQUE_ID);
265 if (function_exists("getrusage")) {
267 $seed3 = md5($dat["ru_nswap"].$dat["ru_majflt"].$dat["ru_utime.tv_sec"].$dat["ru_utime.tv_usec"].getmypid());
272 $seed = crc32($seed1)*1000000 +
crc32($seed2)*10000 +
crc32($seed3);
274 $seed = (double) microtime() * 1000000;
279 for ($i = 0; $i < $length; $i++
) {
280 $pad .= chr(rand(0,255));
286 // Check if we have a required PHP-version. Return TRUE if we do,
287 // or FALSE if we don't.
288 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
289 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
290 // Does not handle betas like 4.0.1b1 or development versions
291 function sqCheckPHPVersion($major, $minor, $release) {
294 eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
296 // Parse the version string
297 $vmajor = strval($regs[1]);
298 $vminor = strval($regs[2]);
301 $vrel = strval(substr($vrel, 1));
302 if($vrel[0] == "b" ||
$vrel[0] == "B")
303 $vrel = - strval(substr($vrel, 1));
304 if($vrel[0] == "r" ||
$vrel[0] == "R")
305 $vrel = - strval(substr($vrel, 2))/10;
307 // Compare major version
308 if($vmajor < $major) return false;
309 if($vmajor > $major) return true;
311 // Major is the same. Compare minor
312 if($vminor < $minor) return false;
313 if($vminor > $minor) return true;
315 // Major and minor is the same as the required one.
317 if($vrel >= 0 && $release >= 0) { // Neither are beta
318 if($vrel < $release) return false;
319 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
321 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
323 } else { // Both are beta
324 if($vrel > $release) return false;