strings.php 15 KB

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