strings.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. // Removed parseEmail and integrated it into parseUrl
  132. // This line is no longer needed.
  133. // $line = parseEmail ($line);
  134. $line = parseUrl ($line);
  135. $test_line = str_replace('&nbsp;', '', $line);
  136. if (strpos($test_line, '&gt;&gt;') === 0) {
  137. $line = "<FONT COLOR=FF0000>$line</FONT>\n";
  138. } else if (strpos($test_line, '&gt;') === 0) {
  139. $line = "<FONT COLOR=800000>$line</FONT>\n";
  140. }
  141. if ($line)
  142. {
  143. $line = '<tt>' . $line . '</tt>';
  144. }
  145. $body_ary[$i] = $line . '<br>';
  146. }
  147. $body = implode("\n", $body_ary);
  148. return $body;
  149. }
  150. /* SquirrelMail version number -- DO NOT CHANGE */
  151. $version = "0.5";
  152. function find_mailbox_name ($mailbox) {
  153. $mailbox = trim($mailbox);
  154. if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
  155. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  156. $pos = strrpos ($mailbox, "\"")+1;
  157. $box = substr($mailbox, $pos);
  158. } else {
  159. $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
  160. }
  161. return $box;
  162. }
  163. function replace_spaces ($string) {
  164. return str_replace(" ", "&nbsp;", $string);
  165. }
  166. function replace_escaped_spaces ($string) {
  167. return str_replace("&nbsp;", " ", $string);
  168. }
  169. function get_location () {
  170. # This determines the location to forward to relative
  171. # to your server. If this doesnt work correctly for
  172. # you (although it should), you can remove all this
  173. # code except the last two lines, and change the header()
  174. # function to look something like this, customized to
  175. # the location of SquirrelMail on your server:
  176. #
  177. # http://www.myhost.com/squirrelmail/src/login.php
  178. global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST;
  179. // Get the path
  180. $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
  181. // Check if this is a HTTPS or regular HTTP request
  182. $proto = "http://";
  183. if(isset($HTTPS) && $HTTPS == 'on' ) {
  184. $proto = "https://";
  185. }
  186. // Get the hostname from the Host header or server config.
  187. // Fallback is to omit the server name and use a relative URI,
  188. // although this is not RFC 2616 compliant.
  189. if(isset($HTTP_HOST) && !empty($HTTP_HOST)) {
  190. $location = $proto . $HTTP_HOST . $path;
  191. } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
  192. $location = $proto . $SERVER_NAME . $path;
  193. } else {
  194. $location = $path;
  195. }
  196. return $location;
  197. }
  198. function sqStripSlashes($string) {
  199. if (get_magic_quotes_gpc()) {
  200. $string = stripslashes($string);
  201. }
  202. return $string;
  203. }
  204. // These functions are used to encrypt the passowrd before it is
  205. // stored in a cookie.
  206. function OneTimePadEncrypt ($string, $pad) {
  207. for ($i = 0; $i < strlen ($string); $i++) {
  208. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  209. }
  210. return base64_encode($encrypted);
  211. }
  212. function OneTimePadDecrypt ($string, $pad) {
  213. $encrypted = base64_decode ($string);
  214. for ($i = 0; $i < strlen ($encrypted); $i++) {
  215. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  216. }
  217. return $decrypted;
  218. }
  219. function OneTimePadCreate ($length=100) {
  220. global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
  221. // Entropy gathering
  222. if (function_exists("crc32")) {
  223. $seed1 = (double) microtime() * 1000000;
  224. $seed2 = md5($REMOTE_PORT . $REMOTE_ADDR . $UNIQUE_ID);
  225. if (function_exists("getrusage")) {
  226. $dat = getrusage();
  227. $seed3 = md5($dat["ru_nswap"].$dat["ru_majflt"].$dat["ru_utime.tv_sec"].$dat["ru_utime.tv_usec"].getmypid());
  228. } else {
  229. $seed3 = getmypid();
  230. }
  231. $seed = crc32($seed1)*1000000 + crc32($seed2)*10000 + crc32($seed3);
  232. } else {
  233. $seed = (double) microtime() * 1000000;
  234. }
  235. srand ($seed);
  236. for ($i = 0; $i < $length; $i++) {
  237. $pad .= chr(rand(0,255));
  238. }
  239. return $pad;
  240. }
  241. // Check if we have a required PHP-version. Return TRUE if we do,
  242. // or FALSE if we don't.
  243. // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
  244. // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
  245. // Does not handle betas like 4.0.1b1 or development versions
  246. function sqCheckPHPVersion($major, $minor, $release) {
  247. $ver = phpversion();
  248. eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
  249. // Parse the version string
  250. $vmajor = strval($regs[1]);
  251. $vminor = strval($regs[2]);
  252. $vrel = $regs[3];
  253. if($vrel[0] == ".")
  254. $vrel = strval(substr($vrel, 1));
  255. if($vrel[0] == "b" || $vrel[0] == "B")
  256. $vrel = - strval(substr($vrel, 1));
  257. if($vrel[0] == "r" || $vrel[0] == "R")
  258. $vrel = - strval(substr($vrel, 2))/10;
  259. // Compare major and minor
  260. if($vmajor < $major) return false;
  261. if($vminor < $minor) return false;
  262. // Compare release
  263. if($vrel >= 0 && $release >= 0) { // Neither are beta
  264. if($vrel < $release) return false;
  265. } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
  266. return true;
  267. } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
  268. return false;
  269. } else { // Both are beta
  270. if($vrel > $release) return false;
  271. }
  272. return true;
  273. }
  274. ?>