Updated some things to say 0.5pre1
[squirrelmail.git] / functions / strings.php
CommitLineData
59177427 1<?php
7ce342dc 2
d068c0ec 3 $strings_php = true;
4
3302d0d4 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)
12 $count++;
13 }
14 return $count;
15 }
16
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) {
91f999f6 22 if (substr($haystack, -1) == $needle)
23 $haystack = substr($haystack, 0, strlen($haystack) - 1);
24
d29aac0e 25 if (strrpos($haystack, $needle)) {
d92b6f31 26 $pos = strrpos($haystack, $needle) + 1;
27 $data = substr($haystack, $pos, strlen($haystack));
28 } else {
29 $data = $haystack;
3302d0d4 30 }
d92b6f31 31 return $data;
3302d0d4 32 }
33
8467bf00 34 // Wraps text at $wrap characters
35 function wordWrap($passed, $wrap) {
a8648d75 36 $passed = str_replace("&gt;", ">", $passed);
37 $passed = str_replace("&lt;", "<", $passed);
38
8467bf00 39 $words = explode(" ", trim($passed));
40 $i = 0;
41 $line_len = strlen($words[$i])+1;
42 $line = "";
43 while ($i < count($words)) {
44 while ($line_len < $wrap) {
a8648d75 45 $line = "$line$words[$i] ";
8467bf00 46 $i++;
47 $line_len = $line_len + strlen($words[$i])+1;
e550d551 48 }
8467bf00 49 $line_len = strlen($words[$i])+1;
b8ea4ed6 50 if ($line_len < $wrap) {
51 if ($i < count($words)) // don't <BR> the last line
a8648d75 52 $line = "$line\n";
b8ea4ed6 53 } else {
54 $endline = $words[$i];
55 while ($line_len >= $wrap) {
56 $bigline = substr($endline, 0, $wrap);
57 $endline = substr($endline, $wrap, strlen($endline));
58 $line_len = strlen($endline);
59 $line = "$line$bigline<BR>";
60 }
61 $line = "$line$endline<BR>";
62 $i++;
63 }
e550d551 64 }
a8648d75 65
66 $line = str_replace(">", "&gt;", $line);
67 $line = str_replace("<", "&lt;", $line);
8467bf00 68 return $line;
e550d551 69 }
40ee9452 70
71 /** Returns an array of email addresses **/
72 function parseAddrs($text) {
7831268e 73 if (trim($text) == "") {
74 return;
75 }
40ee9452 76 $text = str_replace(" ", "", $text);
77 $text = str_replace(",", ";", $text);
78 $array = explode(";", $text);
af9404d7 79 for ($i = 0; $i < count ($array); $i++) {
80 $array[$i] = eregi_replace ("^.*\<", "", $array[$i]);
81 $array[$i] = eregi_replace ("\>.*$", "", $array[$i]);
82 }
40ee9452 83 return $array;
84 }
85
86 /** Returns a line of comma separated email addresses from an array **/
87 function getLineOfAddrs($array) {
88 $to_line = "";
89 for ($i = 0; $i < count($array); $i++) {
90 if ($to_line)
91 $to_line = "$to_line, $array[$i]";
92 else
93 $to_line = "$array[$i]";
94 }
95 return $to_line;
96 }
7ce342dc 97
17ce8467 98 function translateText($body, $wrap_at, $charset) {
43fcef5c 99 include ("../functions/url_parser.php");
a8648d75 100 /** Add any parsing you want to in here */
a8648d75 101 $body_ary = explode("\n", $body);
102
103 for ($i = 0; $i < count($body_ary); $i++) {
104 $line = $body_ary[$i];
105 $line = "^^$line";
106
17ce8467 107 //$line = str_replace(">", "&gt;", $line);
108 //$line = str_replace("<", "&lt;", $line);
109 //$line = htmlspecialchars($line);
a8648d75 110
111 if (strlen($line) >= $wrap_at) // -2 because of the ^^ at the beginning
112 $line = wordWrap($line, $wrap_at);
113
17ce8467 114 $line = charset_decode($charset, $line);
115
a8648d75 116 $line = str_replace(" ", "&nbsp;", $line);
117 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
118 $line = nl2br($line);
119
120 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
121 $line = substr($line, 2, strlen($line));
122 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
123 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
124 $line = substr($line, 2, strlen($line));
125 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
126 } else {
127 $line = substr($line, 2, strlen($line));
128 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
129 }
130
175e7218 131 $line = parseEmail ($line);
43fcef5c 132 $line = parseUrl ($line);
a8648d75 133 $new_body[$i] = "$line";
134 }
135 $bdy = implode("\n", $new_body);
136 return $bdy;
78509c54 137 }
138
7ce342dc 139 /* SquirrelMail version number -- DO NOT CHANGE */
cf59dc94 140 $version = "0.5pre1";
d29aac0e 141
142
143 function find_mailbox_name ($mailbox) {
144 $mailbox = trim($mailbox);
145 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
146 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
147 $pos = strrpos ($mailbox, "\"")+1;
148 $box = substr($mailbox, $pos);
149 } else {
150 $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
151 }
152 return $box;
153 }
154
155 function replace_spaces ($string) {
156 return str_replace(" ", "&nbsp;", $string);
157 }
158
159 function replace_escaped_spaces ($string) {
160 return str_replace("&nbsp;", " ", $string);
161 }
3302d0d4 162?>