strings.php 14 KB

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