strings.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 sqWordWrap($passed, $wrap) {
  43. $passed = str_replace("&lt;", "<", $passed);
  44. $passed = str_replace("&gt;", ">", $passed);
  45. preg_match("/^(\s|>)+/", $passed, $regs);
  46. $beginning_spaces = $regs[0];
  47. $words = explode(" ", $passed);
  48. $i = -1;
  49. $line_len = strlen($words[0])+1;
  50. $line = "";
  51. if (count($words) > 1) {
  52. while ($i++ < count($words)) {
  53. while ($line_len < $wrap) {
  54. $line = "$line$words[$i] ";
  55. $i++;
  56. $line_len = $line_len + strlen($words[$i]) + 1;
  57. }
  58. $line_len = strlen($words[$i])+1;
  59. if ($line_len <= $wrap) {
  60. if (strlen($beginning_spaces) +2 >= $wrap)
  61. $beginning_spaces = "";
  62. if ($i < count($words)) { // don't <BR> the last line
  63. $line = "$line\n$beginning_spaces";
  64. }
  65. $line = "$line$words[$i] ";
  66. $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 1;
  67. } else {
  68. /*
  69. $endline = $words[$i];
  70. while ($line_len >= $wrap) {
  71. $bigline = substr($endline, 0, $wrap);
  72. $endline = substr($endline, $wrap, strlen($endline));
  73. $line_len = strlen($endline);
  74. $line = "$line$bigline<BR>";
  75. }
  76. */
  77. if (strlen($line) > $wrap)
  78. $line = "$line\n$words[$i]";
  79. else
  80. $line = "$line$words[$i]";
  81. $line_len = strlen($words[$i]);
  82. }
  83. }
  84. } else {
  85. $line = $words[0];
  86. }
  87. $line = str_replace(">", "&gt;", $line);
  88. $line = str_replace("<", "&lt;", $line);
  89. return $line;
  90. }
  91. /** Returns an array of email addresses **/
  92. function parseAddrs($text) {
  93. if (trim($text) == "") {
  94. return;
  95. }
  96. $text = str_replace(" ", "", $text);
  97. $text = ereg_replace( '"[^"]*"', "", $text);
  98. $text = str_replace(",", ";", $text);
  99. $array = explode(";", $text);
  100. for ($i = 0; $i < count ($array); $i++) {
  101. $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
  102. $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
  103. }
  104. return $array;
  105. }
  106. /** Returns a line of comma separated email addresses from an array **/
  107. function getLineOfAddrs($array) {
  108. if (is_array($array)) {
  109. $to_line = implode(", ", $array);
  110. $to_line = trim(ereg_replace(",,+", ",", $to_line));
  111. } else {
  112. $to_line = "";
  113. }
  114. return $to_line;
  115. }
  116. function translateText($body, $wrap_at, $charset) {
  117. global $where, $what; // from searching
  118. if (!isset($url_parser_php)) {
  119. include "../functions/url_parser.php";
  120. }
  121. $body_ary = explode("\n", $body);
  122. for ($i=0; $i < count($body_ary); $i++) {
  123. $line = $body_ary[$i];
  124. $line = charset_decode($charset, $line);
  125. $line = str_replace("\t", " ", $line);
  126. if (strlen($line) - 2 >= $wrap_at) {
  127. $line = sqWordWrap($line, $wrap_at);
  128. }
  129. $line = str_replace(" ", "&nbsp;", $line);
  130. $line = nl2br($line);
  131. $line = parseEmail ($line);
  132. $line = parseUrl ($line);
  133. $line = "^^$line"; // gotta do this because if not, strpos() returns 0
  134. // which in PHP is the same as false. Now it returns 2
  135. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  136. $line = substr($line, 2);
  137. $line = "<FONT COLOR=FF0000>$line</FONT>\n";
  138. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  139. $line = substr($line, 2);
  140. $line = "<FONT COLOR=800000>$line</FONT>\n";
  141. } else {
  142. $line = substr($line, 2);
  143. }
  144. $body_ary[$i] = "<tt>$line</tt><br>";
  145. }
  146. $body = implode("\n", $body_ary);
  147. return $body;
  148. }
  149. /* SquirrelMail version number -- DO NOT CHANGE */
  150. $version = "0.5pre2";
  151. function find_mailbox_name ($mailbox) {
  152. $mailbox = trim($mailbox);
  153. if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
  154. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  155. $pos = strrpos ($mailbox, "\"")+1;
  156. $box = substr($mailbox, $pos);
  157. } else {
  158. $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
  159. }
  160. return $box;
  161. }
  162. function replace_spaces ($string) {
  163. return str_replace(" ", "&nbsp;", $string);
  164. }
  165. function replace_escaped_spaces ($string) {
  166. return str_replace("&nbsp;", " ", $string);
  167. }
  168. function get_location () {
  169. # This determines the location to forward to relative
  170. # to your server. If this doesnt work correctly for
  171. # you (although it should), you can remove all this
  172. # code except the last two lines, and change the header()
  173. # function to look something like this, customized to
  174. # the location of SquirrelMail on your server:
  175. #
  176. # http://www.myhost.com/squirrelmail/src/login.php
  177. global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST;
  178. // Get the path
  179. $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
  180. // Check if this is a HTTPS or regular HTTP request
  181. $proto = "http://";
  182. if(isset($HTTPS) && $HTTPS == 'on' ) {
  183. $proto = "https://";
  184. }
  185. // Get the hostname from the Host header or server config.
  186. // Fallback is to omit the server name and use a relative URI,
  187. // although this is not RFC 2616 compliant.
  188. if(isset($HTTP_HOST) && !empty($HTTP_HOST)) {
  189. $location = $proto . $HTTP_HOST . $path;
  190. } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
  191. $location = $proto . $SERVER_NAME . $path;
  192. } else {
  193. $location = $path;
  194. }
  195. return $location;
  196. }
  197. function sqStripSlashes($string) {
  198. if (get_magic_quotes_gpc()) {
  199. $string = stripslashes($string);
  200. }
  201. return $string;
  202. }
  203. ?>