strings.php 15 KB

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