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