strings.php 14 KB

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