strings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /* $Id$ */
  3. $strings_php = true;
  4. //*************************************************************************
  5. // Count the number of occurances of $needle are in $haystack.
  6. //*************************************************************************
  7. function countCharInString($haystack, $needle) {
  8. $haystack = ereg_replace("[^$needle]","",$haystack);
  9. return strlen($haystack);
  10. }
  11. //*************************************************************************
  12. // Read from the back of $haystack until $needle is found, or the begining
  13. // of the $haystack is reached. $needle is a single character
  14. //*************************************************************************
  15. function readShortMailboxName($haystack, $needle) {
  16. if ($needle == "") return $haystack;
  17. if ($needle == ".") $needle = "\.";
  18. ereg("([^$needle]+)$needle?$", $haystack, $regs);
  19. return $regs[1];
  20. }
  21. //*************************************************************************
  22. // Read from the back of $haystack until $needle is found, or the begining
  23. // of the $haystack is reached. $needle is a single character
  24. //*************************************************************************
  25. function readMailboxParent($haystack, $needle) {
  26. if ($needle == ".") $needle = "\.";
  27. ereg("^(.+)$needle([^$needle]+)$needle?$", $haystack, $regs);
  28. return $regs[1];
  29. }
  30. // Searches for the next position in a string minus white space
  31. function next_pos_minus_white ($haystack, $pos) {
  32. while (substr($haystack, $pos, 1) == " " ||
  33. substr($haystack, $pos, 1) == "\t" ||
  34. substr($haystack, $pos, 1) == "\n" ||
  35. substr($haystack, $pos, 1) == "\r") {
  36. if ($pos >= strlen($haystack))
  37. return -1;
  38. $pos++;
  39. }
  40. return $pos;
  41. }
  42. // Wraps text at $wrap characters
  43. // Has a problem with special HTML characters, so call this before
  44. // you do character translation.
  45. // Specifically, &#039 comes up as 5 characters instead of 1.
  46. // This should not add newlines to the end of lines.
  47. function sqWordWrap(&$line, $wrap) {
  48. preg_match("/^([\s>]*)([^\s>].*)?$/", $line, $regs);
  49. $beginning_spaces = $regs[1];
  50. $words = explode(" ", $regs[2]);
  51. $i = 0;
  52. $line = $beginning_spaces;
  53. while ($i < count($words)) {
  54. // Force one word to be on a line (minimum)
  55. $line .= $words[$i];
  56. $line_len = strlen($beginning_spaces) + strlen($words[$i]) +
  57. strlen($words[$i + 1]) + 2;
  58. $i ++;
  59. // Add more words (as long as they fit)
  60. while ($line_len < $wrap && $i < count($words)) {
  61. $line .= ' ' . $words[$i];
  62. $i++;
  63. $line_len += strlen($words[$i]) + 1;
  64. }
  65. // Skip spaces if they are the first thing on a continued line
  66. while (!$words[$i] && $i < count($words)) {
  67. $i ++;
  68. }
  69. // Go to the next line if we have more to process
  70. if ($i < count($words)) {
  71. $line .= "\n$beginning_spaces";
  72. }
  73. }
  74. }
  75. // Does the opposite of sqWordWrap()
  76. function sqUnWordWrap(&$body)
  77. {
  78. $lines = explode("\n", $body);
  79. $body = "";
  80. $PreviousSpaces = "";
  81. for ($i = 0; $i < count($lines); $i ++)
  82. {
  83. preg_match("/^([\s>]*)([^\s>].*)?$/", $lines[$i], $regs);
  84. $CurrentSpaces = $regs[1];
  85. $CurrentRest = $regs[2];
  86. if ($i == 0)
  87. {
  88. $PreviousSpaces = $CurrentSpaces;
  89. $body = $lines[$i];
  90. }
  91. else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
  92. strlen($lines[$i - 1]) > 65 && // Over 65 characters long
  93. strlen($CurrentRest)) // and there's a line to continue with
  94. {
  95. $body .= ' ' . $CurrentRest;
  96. }
  97. else
  98. {
  99. $body .= "\n" . $lines[$i];
  100. $PreviousSpaces = $CurrentSpaces;
  101. }
  102. }
  103. $body .= "\n";
  104. }
  105. /** Returns an array of email addresses **/
  106. /* Be cautious of "user@host.com" */
  107. function parseAddrs($text) {
  108. if (trim($text) == "")
  109. return array();
  110. $text = str_replace(" ", "", $text);
  111. $text = ereg_replace('"[^"]*"', "", $text);
  112. $text = ereg_replace("\([^\)]*\)", "", $text);
  113. $text = str_replace(",", ";", $text);
  114. $array = explode(";", $text);
  115. for ($i = 0; $i < count ($array); $i++) {
  116. $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
  117. $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
  118. }
  119. return $array;
  120. }
  121. /** Returns a line of comma separated email addresses from an array **/
  122. function getLineOfAddrs($array) {
  123. if (is_array($array)) {
  124. $to_line = implode(", ", $array);
  125. $to_line = trim(ereg_replace(",,+", ",", $to_line));
  126. } else {
  127. $to_line = "";
  128. }
  129. return $to_line;
  130. }
  131. function translateText(&$body, $wrap_at, $charset) {
  132. global $where, $what; // from searching
  133. global $url_parser_php;
  134. if (!isset($url_parser_php)) {
  135. include "../functions/url_parser.php";
  136. }
  137. $body_ary = explode("\n", $body);
  138. $PriorQuotes = 0;
  139. for ($i=0; $i < count($body_ary); $i++) {
  140. $line = $body_ary[$i];
  141. if (strlen($line) - 2 >= $wrap_at) {
  142. sqWordWrap($line, $wrap_at);
  143. }
  144. $line = charset_decode($charset, $line);
  145. $line = str_replace("\t", ' ', $line);
  146. parseUrl ($line);
  147. $Quotes = 0;
  148. $pos = 0;
  149. while (1)
  150. {
  151. if ($line[$pos] == ' ')
  152. {
  153. $pos ++;
  154. }
  155. else if (strpos($line, '&gt;', $pos) === $pos)
  156. {
  157. $pos += 4;
  158. $Quotes ++;
  159. }
  160. else
  161. {
  162. break;
  163. }
  164. }
  165. if ($Quotes > 1)
  166. $line = "<FONT COLOR=FF0000>$line</FONT>";
  167. elseif ($Quotes)
  168. $line = "<FONT COLOR=800000>$line</FONT>";
  169. $body_ary[$i] = $line;
  170. }
  171. $body = "<pre>" . implode("\n", $body_ary) . "</pre>";
  172. }
  173. /* SquirrelMail version number -- DO NOT CHANGE */
  174. var $version = "1.0pre3 [cvs]";
  175. function find_mailbox_name ($mailbox) {
  176. /*
  177. $mailbox = trim($mailbox);
  178. if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
  179. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  180. $pos = strrpos ($mailbox, "\"")+1;
  181. $box = substr($mailbox, $pos);
  182. } else {
  183. $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
  184. }
  185. return $box;
  186. */
  187. if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
  188. return $regs[1];
  189. ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
  190. return $regs[1];
  191. }
  192. function replace_spaces ($string) {
  193. return str_replace(" ", "&nbsp;", $string);
  194. }
  195. function replace_escaped_spaces ($string) {
  196. return str_replace("&nbsp;", " ", $string);
  197. }
  198. function get_location () {
  199. # This determines the location to forward to relative
  200. # to your server. If this doesnt work correctly for
  201. # you (although it should), you can remove all this
  202. # code except the last two lines, and change the header()
  203. # function to look something like this, customized to
  204. # the location of SquirrelMail on your server:
  205. #
  206. # http://www.myhost.com/squirrelmail/src/login.php
  207. global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
  208. // Get the path
  209. $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
  210. // Check if this is a HTTPS or regular HTTP request
  211. $proto = "http://";
  212. if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
  213. $proto = "https://";
  214. }
  215. // Get the hostname from the Host header or server config.
  216. $host = "";
  217. if (isset($HTTP_HOST) && !empty($HTTP_HOST))
  218. {
  219. $host = $HTTP_HOST;
  220. }
  221. else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
  222. {
  223. $host = $SERVER_NAME;
  224. }
  225. $port = '';
  226. if (! strstr($host, ':'))
  227. {
  228. if (isset($SERVER_PORT)) {
  229. if (($SERVER_PORT != 80 && $proto == "http://")
  230. || ($SERVER_PORT != 443 && $proto == "https://")) {
  231. $port = sprintf(':%d', $SERVER_PORT);
  232. }
  233. }
  234. }
  235. if ($host)
  236. return $proto . $host . $port . $path;
  237. // Fallback is to omit the server name and use a relative URI,
  238. // although this is not RFC 2616 compliant.
  239. return $path;
  240. }
  241. function sqStripSlashes($string) {
  242. if (get_magic_quotes_gpc()) {
  243. $string = stripslashes($string);
  244. }
  245. return $string;
  246. }
  247. // These functions are used to encrypt the passowrd before it is
  248. // stored in a cookie.
  249. function OneTimePadEncrypt ($string, $epad) {
  250. $pad = base64_decode($epad);
  251. for ($i = 0; $i < strlen ($string); $i++) {
  252. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  253. }
  254. return base64_encode($encrypted);
  255. }
  256. function OneTimePadDecrypt ($string, $epad) {
  257. $pad = base64_decode($epad);
  258. $encrypted = base64_decode ($string);
  259. for ($i = 0; $i < strlen ($encrypted); $i++) {
  260. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  261. }
  262. return $decrypted;
  263. }
  264. // Randomize the mt_rand() function. Toss this in strings or
  265. // integers and it will seed the generator appropriately.
  266. // With strings, it is better to get them long. Use md5() to
  267. // lengthen smaller strings.
  268. function sq_mt_seed($Val)
  269. {
  270. // if mt_getrandmax() does not return a 2^n - 1 number,
  271. // this might not work well. This uses $Max as a bitmask.
  272. $Max = mt_getrandmax();
  273. if (! is_int($Val))
  274. {
  275. if (function_exists("crc32"))
  276. {
  277. $Val = crc32($Val);
  278. }
  279. else
  280. {
  281. $Str = $Val;
  282. $Pos = 0;
  283. $Val = 0;
  284. $Mask = $Max / 2;
  285. $HighBit = $Max ^ $Mask;
  286. while ($Pos < strlen($Str))
  287. {
  288. if ($Val & $HighBit)
  289. {
  290. $Val = (($Val & $Mask) << 1) + 1;
  291. }
  292. else
  293. {
  294. $Val = ($Val & $Mask) << 1;
  295. }
  296. $Val ^= $Str[$Pos];
  297. $Pos ++;
  298. }
  299. }
  300. }
  301. if ($Val < 0)
  302. $Val *= -1;
  303. if ($Val = 0)
  304. return;
  305. mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
  306. }
  307. // This function initializes the random number generator fairly well.
  308. // It also only initializes it once, so you don't accidentally get
  309. // the same 'random' numbers twice in one session.
  310. function sq_mt_randomize()
  311. {
  312. global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
  313. static $randomized;
  314. if ($randomized)
  315. return;
  316. // Global
  317. sq_mt_seed((int)((double) microtime() * 1000000));
  318. sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
  319. // getrusage
  320. if (function_exists("getrusage")) {
  321. $dat = getrusage();
  322. sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] .
  323. $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
  324. }
  325. // Apache-specific
  326. sq_mt_seed(md5($UNIQUE_ID));
  327. $randomized = 1;
  328. }
  329. function OneTimePadCreate ($length=100) {
  330. sq_mt_randomize();
  331. for ($i = 0; $i < $length; $i++) {
  332. $pad .= chr(mt_rand(0,255));
  333. }
  334. return base64_encode($pad);
  335. }
  336. // Check if we have a required PHP-version. Return TRUE if we do,
  337. // or FALSE if we don't.
  338. // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
  339. // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
  340. // Does not handle betas like 4.0.1b1 or development versions
  341. function sqCheckPHPVersion($major, $minor, $release) {
  342. $ver = phpversion();
  343. eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
  344. // Parse the version string
  345. $vmajor = strval($regs[1]);
  346. $vminor = strval($regs[2]);
  347. $vrel = $regs[3];
  348. if($vrel[0] == ".")
  349. $vrel = strval(substr($vrel, 1));
  350. if($vrel[0] == "b" || $vrel[0] == "B")
  351. $vrel = - strval(substr($vrel, 1));
  352. if($vrel[0] == "r" || $vrel[0] == "R")
  353. $vrel = - strval(substr($vrel, 2))/10;
  354. // Compare major version
  355. if($vmajor < $major) return false;
  356. if($vmajor > $major) return true;
  357. // Major is the same. Compare minor
  358. if($vminor < $minor) return false;
  359. if($vminor > $minor) return true;
  360. // Major and minor is the same as the required one.
  361. // Compare release
  362. if($vrel >= 0 && $release >= 0) { // Neither are beta
  363. if($vrel < $release) return false;
  364. } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
  365. return true;
  366. } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
  367. return false;
  368. } else { // Both are beta
  369. if($vrel > $release) return false;
  370. }
  371. return true;
  372. }
  373. /* Returns a string showing the size of the message/attachment */
  374. function show_readable_size($bytes)
  375. {
  376. $bytes /= 1024;
  377. $type = 'k';
  378. if ($bytes / 1024 > 1)
  379. {
  380. $bytes /= 1024;
  381. $type = 'm';
  382. }
  383. if ($bytes < 10)
  384. {
  385. $bytes *= 10;
  386. settype($bytes, "integer");
  387. $bytes /= 10;
  388. }
  389. else
  390. settype($bytes, "integer");
  391. return $bytes . '<small>&nbsp;' . $type . '</small>';
  392. }
  393. /* Generates a random string from the caracter set you pass in
  394. *
  395. * Flags:
  396. * 1 = add lowercase a-z to $chars
  397. * 2 = add uppercase A-Z to $chars
  398. * 4 = add numbers 0-9 to $chars
  399. */
  400. function GenerateRandomString($size, $chars, $flags = 0)
  401. {
  402. if ($flags & 0x1)
  403. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  404. if ($flags & 0x2)
  405. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  406. if ($flags & 0x4)
  407. $chars .= '0123456789';
  408. if ($size < 1 || strlen($chars) < 1)
  409. return "";
  410. sq_mt_randomize(); // Initialize the random number generator
  411. while (strlen($String) < $size) {
  412. $String .= $chars[mt_rand(0, strlen($chars))];
  413. }
  414. return $String;
  415. }
  416. ?>