strings.php 15 KB

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