strings.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. $strings_php = true;
  3. //*************************************************************************
  4. // Count the number of occurances of $needle are in $haystack.
  5. //*************************************************************************
  6. function countCharInString($haystack, $needle) {
  7. $len = strlen($haystack);
  8. for ($i = 0; $i < $len; $i++) {
  9. if ($haystack[$i] == $needle)
  10. $count++;
  11. }
  12. return $count;
  13. }
  14. //*************************************************************************
  15. // Read from the back of $haystack until $needle is found, or the begining
  16. // of the $haystack is reached.
  17. //*************************************************************************
  18. function readShortMailboxName($haystack, $needle) {
  19. if (substr($haystack, -1) == $needle)
  20. $haystack = substr($haystack, 0, strlen($haystack) - 1);
  21. if (strrpos($haystack, $needle)) {
  22. $pos = strrpos($haystack, $needle) + 1;
  23. $data = substr($haystack, $pos, strlen($haystack));
  24. } else {
  25. $data = $haystack;
  26. }
  27. return $data;
  28. }
  29. // Searches for the next position in a string minus white space
  30. function next_pos_minus_white ($haystack, $pos) {
  31. while (substr($haystack, $pos, 1) == " " ||
  32. substr($haystack, $pos, 1) == "\t" ||
  33. substr($haystack, $pos, 1) == "\n" ||
  34. substr($haystack, $pos, 1) == "\r") {
  35. if ($pos >= strlen($haystack))
  36. return -1;
  37. $pos++;
  38. }
  39. return $pos;
  40. }
  41. // Wraps text at $wrap characters
  42. function wordWrap($passed, $wrap) {
  43. $passed = str_replace("&gt;", ">", $passed);
  44. $passed = str_replace("&lt;", "<", $passed);
  45. $words = explode(" ", trim($passed));
  46. $i = 0;
  47. $line_len = strlen($words[$i])+1;
  48. $line = "";
  49. while ($i < count($words)) {
  50. while ($line_len < $wrap) {
  51. $line = "$line$words[$i] ";
  52. $i++;
  53. $line_len = $line_len + strlen($words[$i])+1;
  54. }
  55. $line_len = strlen($words[$i])+1;
  56. if ($line_len < $wrap) {
  57. if ($i < count($words)) // don't <BR> the last line
  58. $line = "$line\n";
  59. } else {
  60. $endline = $words[$i];
  61. while ($line_len >= $wrap) {
  62. $bigline = substr($endline, 0, $wrap);
  63. $endline = substr($endline, $wrap, strlen($endline));
  64. $line_len = strlen($endline);
  65. $line = "$line$bigline<BR>";
  66. }
  67. $line = "$line$endline<BR>";
  68. $i++;
  69. }
  70. }
  71. $line = str_replace(">", "&gt;", $line);
  72. $line = str_replace("<", "&lt;", $line);
  73. return $line;
  74. }
  75. /** Returns an array of email addresses **/
  76. function parseAddrs($text) {
  77. if (trim($text) == "") {
  78. return;
  79. }
  80. $text = str_replace(" ", "", $text);
  81. $text = str_replace(",", ";", $text);
  82. $array = explode(";", $text);
  83. for ($i = 0; $i < count ($array); $i++) {
  84. $array[$i] = eregi_replace ("^.*\<", "", $array[$i]);
  85. $array[$i] = eregi_replace ("\>.*$", "", $array[$i]);
  86. }
  87. return $array;
  88. }
  89. /** Returns a line of comma separated email addresses from an array **/
  90. function getLineOfAddrs($array) {
  91. $to_line = "";
  92. for ($i = 0; $i < count($array); $i++) {
  93. if ($to_line)
  94. $to_line = "$to_line, $array[$i]";
  95. else
  96. $to_line = "$array[$i]";
  97. }
  98. return $to_line;
  99. }
  100. function translateText($body, $wrap_at, $charset) {
  101. include ("../functions/url_parser.php");
  102. /** Add any parsing you want to in here */
  103. $body_ary = explode("\n", $body);
  104. for ($i = 0; $i < count($body_ary); $i++) {
  105. $line = $body_ary[$i];
  106. $line = "^^$line";
  107. //$line = str_replace(">", "&gt;", $line);
  108. //$line = str_replace("<", "&lt;", $line);
  109. //$line = htmlspecialchars($line);
  110. if (strlen($line) >= $wrap_at) // -2 because of the ^^ at the beginning
  111. $line = wordWrap($line, $wrap_at);
  112. $line = charset_decode($charset, $line);
  113. $line = str_replace(" ", "&nbsp;", $line);
  114. $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
  115. $line = nl2br($line);
  116. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  117. $line = substr($line, 2, strlen($line));
  118. $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
  119. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  120. $line = substr($line, 2, strlen($line));
  121. $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
  122. } else {
  123. $line = substr($line, 2, strlen($line));
  124. $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
  125. }
  126. $line = parseEmail ($line);
  127. $line = parseUrl ($line);
  128. $new_body[$i] = "$line";
  129. }
  130. $bdy = implode("\n", $new_body);
  131. return $bdy;
  132. }
  133. /* SquirrelMail version number -- DO NOT CHANGE */
  134. $version = "0.5pre1";
  135. function find_mailbox_name ($mailbox) {
  136. $mailbox = trim($mailbox);
  137. if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
  138. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  139. $pos = strrpos ($mailbox, "\"")+1;
  140. $box = substr($mailbox, $pos);
  141. } else {
  142. $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
  143. }
  144. return $box;
  145. }
  146. function replace_spaces ($string) {
  147. return str_replace(" ", "&nbsp;", $string);
  148. }
  149. function replace_escaped_spaces ($string) {
  150. return str_replace("&nbsp;", " ", $string);
  151. }
  152. ?>