X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fstrings.php;h=1cf6be0d74acb36f46161f7e33b6679a68757e63;hb=fa9b646f78e3eb7204718fcff06c11cb059fb6b0;hp=c9c6b9b29b32124d9343b43a6e998331e0f44804;hpb=175e7218bffd68523c8c32a7fae9bde6b5d5c952;p=squirrelmail.git
diff --git a/functions/strings.php b/functions/strings.php
index c9c6b9b2..1cf6be0d 100644
--- a/functions/strings.php
+++ b/functions/strings.php
@@ -1,159 +1,589 @@
-
+ $v) {
+ global $$k;
+ if (is_array($$k)) {
+ foreach ($$k as $k2 => $v2) {
+ $$k[$k2] = substr($v2, $trimChars);
+ }
+ } else {
+ $$k = substr($v, $trimChars);
+ }
+ }
+ }
+
+
+ //**************************************************************************
+ // Removes slashes from every element in the array
+ //**************************************************************************
+ function RemoveSlashes($array)
+ {
+ foreach ($array as $k => $v)
+ {
+ global $$k;
+ if (is_array($$k))
+ {
+ foreach ($$k as $k2 => $v2)
+ {
+ $newArray[stripslashes($k2)] = stripslashes($v2);
+ }
+ $$k = $newArray;
+ }
+ else
+ {
+ $$k = stripslashes($v);
+ }
+ }
+ }
+
//*************************************************************************
// Count the number of occurances of $needle are in $haystack.
+ // $needle can be a character or string, and need not occur in $haystack
//*************************************************************************
function countCharInString($haystack, $needle) {
- $len = strlen($haystack);
- for ($i = 0; $i < $len; $i++) {
- if ($haystack[$i] == $needle)
- $count++;
- }
- return $count;
+ if ($needle == '') return 0;
+ return count(explode($needle, $haystack));
}
//*************************************************************************
// Read from the back of $haystack until $needle is found, or the begining
- // of the $haystack is reached.
+ // of the $haystack is reached. $needle is a single character
//*************************************************************************
function readShortMailboxName($haystack, $needle) {
- if (substr($haystack, -1) == $needle)
- $haystack = substr($haystack, 0, strlen($haystack) - 1);
+ if ($needle == '') return $haystack;
+ $parts = explode($needle, $haystack);
+ $elem = array_pop($parts);
+ while ($elem == '' && count($parts))
+ {
+ $elem = array_pop($parts);
+ }
+ return $elem;
+ }
- if (strrpos($haystack, $needle)) {
- $pos = strrpos($haystack, $needle) + 1;
- $data = substr($haystack, $pos, strlen($haystack));
- } else {
- $data = $haystack;
+ //*************************************************************************
+ // Read from the back of $haystack until $needle is found, or the begining
+ // of the $haystack is reached. $needle is a single character
+ //*************************************************************************
+ function readMailboxParent($haystack, $needle) {
+ if ($needle == '') return '';
+ $parts = explode($needle, $haystack);
+ $elem = array_pop($parts);
+ while ($elem == '' && count($parts))
+ {
+ $elem = array_pop($parts);
}
- return $data;
+ return join($needle, $parts);
+ }
+
+ // Searches for the next position in a string minus white space
+ function next_pos_minus_white ($haystack, $pos) {
+ while (substr($haystack, $pos, 1) == ' ' ||
+ substr($haystack, $pos, 1) == "\t" ||
+ substr($haystack, $pos, 1) == "\n" ||
+ substr($haystack, $pos, 1) == "\r") {
+ if ($pos >= strlen($haystack))
+ return -1;
+ $pos++;
+ }
+ return $pos;
}
// Wraps text at $wrap characters
- function wordWrap($passed, $wrap) {
- $passed = str_replace(">", ">", $passed);
- $passed = str_replace("<", "<", $passed);
+ // Has a problem with special HTML characters, so call this before
+ // you do character translation.
+ // Specifically, ' comes up as 5 characters instead of 1.
+ // This should not add newlines to the end of lines.
+ function sqWordWrap(&$line, $wrap) {
+ ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
+ $beginning_spaces = $regs[1];
+ if (isset($regs[2])) {
+ $words = explode(' ', $regs[2]);
+ } else {
+ $words = "";
+ }
- $words = explode(" ", trim($passed));
$i = 0;
- $line_len = strlen($words[$i])+1;
- $line = "";
+ $line = $beginning_spaces;
+
while ($i < count($words)) {
- while ($line_len < $wrap) {
- $line = "$line$words[$i] ";
+ // Force one word to be on a line (minimum)
+ $line .= $words[$i];
+ $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2;
+ if (isset($words[$i + 1]))
+ $line_len += strlen($words[$i + 1]);
+ $i ++;
+
+ // Add more words (as long as they fit)
+ while ($line_len < $wrap && $i < count($words)) {
+ $line .= ' ' . $words[$i];
$i++;
- $line_len = $line_len + strlen($words[$i])+1;
+ if (isset($words[$i]))
+ $line_len += strlen($words[$i]) + 1;
+ else
+ $line_len += 1;
}
- $line_len = strlen($words[$i])+1;
- if ($line_len < $wrap) {
- if ($i < count($words)) // don't
the last line
- $line = "$line\n";
- } else {
- $endline = $words[$i];
- while ($line_len >= $wrap) {
- $bigline = substr($endline, 0, $wrap);
- $endline = substr($endline, $wrap, strlen($endline));
- $line_len = strlen($endline);
- $line = "$line$bigline
";
- }
- $line = "$line$endline
";
- $i++;
+
+ // Skip spaces if they are the first thing on a continued line
+ while (!isset($words[$i]) && $i < count($words)) {
+ $i ++;
}
- }
- $line = str_replace(">", ">", $line);
- $line = str_replace("<", "<", $line);
- return $line;
+ // Go to the next line if we have more to process
+ if ($i < count($words)) {
+ $line .= "\n" . $beginning_spaces;
+ }
+ }
+ }
+
+
+ // Does the opposite of sqWordWrap()
+ function sqUnWordWrap(&$body)
+ {
+ $lines = explode("\n", $body);
+ $body = "";
+ $PreviousSpaces = "";
+ for ($i = 0; $i < count($lines); $i ++)
+ {
+ ereg("^([\t >]*)([^\t >].*)?$", $lines[$i], $regs);
+ $CurrentSpaces = $regs[1];
+ if (isset($regs[2]))
+ $CurrentRest = $regs[2];
+ if ($i == 0)
+ {
+ $PreviousSpaces = $CurrentSpaces;
+ $body = $lines[$i];
+ }
+ else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
+ strlen($lines[$i - 1]) > 65 && // Over 65 characters long
+ strlen($CurrentRest)) // and there's a line to continue with
+ {
+ $body .= ' ' . $CurrentRest;
+ }
+ else
+ {
+ $body .= "\n" . $lines[$i];
+ $PreviousSpaces = $CurrentSpaces;
+ }
+ }
+ $body .= "\n";
}
+
/** Returns an array of email addresses **/
+ /* Be cautious of "user@host.com" */
function parseAddrs($text) {
- if (trim($text) == "") {
- return;
- }
- $text = str_replace(" ", "", $text);
- $text = str_replace(",", ";", $text);
- $array = explode(";", $text);
+ if (trim($text) == "")
+ return array();
+ $text = str_replace(' ', '', $text);
+ $text = ereg_replace('"[^"]*"', '', $text);
+ $text = ereg_replace('\\([^\\)]*\\)', '', $text);
+ $text = str_replace(',', ';', $text);
+ $array = explode(';', $text);
+ for ($i = 0; $i < count ($array); $i++) {
+ $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]);
+ $array[$i] = eregi_replace ("[>].*$", '', $array[$i]);
+ }
return $array;
}
/** Returns a line of comma separated email addresses from an array **/
function getLineOfAddrs($array) {
- $to_line = "";
- for ($i = 0; $i < count($array); $i++) {
- if ($to_line)
- $to_line = "$to_line, $array[$i]";
- else
- $to_line = "$array[$i]";
+ if (is_array($array)) {
+ $to_line = implode(', ', $array);
+ $to_line = trim(ereg_replace(', (, )+', ', ', $to_line));
+ } else {
+ $to_line = '';
}
return $to_line;
}
- function translateText($body, $wrap_at, $charset) {
- include ("../functions/url_parser.php");
- /** Add any parsing you want to in here */
- $body = trim($body);
- $body_ary = explode("\n", $body);
+ function translateText(&$body, $wrap_at, $charset) {
+ global $where, $what; // from searching
+ global $url_parser_php;
- for ($i = 0; $i < count($body_ary); $i++) {
+ if (!isset($url_parser_php)) {
+ include '../functions/url_parser.php';
+ }
+
+ $body_ary = explode("\n", $body);
+ $PriorQuotes = 0;
+ for ($i=0; $i < count($body_ary); $i++) {
$line = $body_ary[$i];
- $line = "^^$line";
+ if (strlen($line) - 2 >= $wrap_at) {
+ sqWordWrap($line, $wrap_at);
+ }
+ $line = charset_decode($charset, $line);
+ $line = str_replace("\t", ' ', $line);
+
+ parseUrl ($line);
+
+ $Quotes = 0;
+ $pos = 0;
+ while (1)
+ {
+ if ($line[$pos] == ' ')
+ {
+ $pos ++;
+ }
+ else if (strpos($line, '>', $pos) === $pos)
+ {
+ $pos += 4;
+ $Quotes ++;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ if ($Quotes > 1)
+ $line = ''.$line.'';
+ elseif ($Quotes)
+ $line = ''.$line.'';
+
+ $body_ary[$i] = $line;
+ }
+ $body = '
' . implode("\n", $body_ary) . ''; + } - //$line = str_replace(">", ">", $line); - //$line = str_replace("<", "<", $line); - //$line = htmlspecialchars($line); + /* SquirrelMail version number -- DO NOT CHANGE */ + $version = '1.1.0 [cvs]'; - if (strlen($line) >= $wrap_at) // -2 because of the ^^ at the beginning - $line = wordWrap($line, $wrap_at); - $line = charset_decode($charset, $line); + function find_mailbox_name ($mailbox) { + if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs)) + return $regs[1]; + ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs); + return $regs[1]; - $line = str_replace(" ", " ", $line); - $line = str_replace("\t", " ", $line); - $line = nl2br($line); + } - if (strpos(trim(str_replace(" ", "", $line)), ">>") == 2) { - $line = substr($line, 2, strlen($line)); - $line = "$line