strings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. global $color; // color theme
  155. include '../functions/url_parser.php';
  156. $body_ary = explode("\n", $body);
  157. $PriorQuotes = 0;
  158. for ($i=0; $i < count($body_ary); $i++) {
  159. $line = $body_ary[$i];
  160. if (strlen($line) - 2 >= $wrap_at) {
  161. sqWordWrap($line, $wrap_at);
  162. }
  163. $line = charset_decode($charset, $line);
  164. $line = str_replace("\t", ' ', $line);
  165. parseUrl ($line);
  166. $Quotes = 0;
  167. $pos = 0;
  168. while (1)
  169. {
  170. if ($line[$pos] == ' ')
  171. {
  172. $pos ++;
  173. }
  174. else if (strpos($line, '&gt;', $pos) === $pos)
  175. {
  176. $pos += 4;
  177. $Quotes ++;
  178. }
  179. else
  180. {
  181. break;
  182. }
  183. }
  184. if ($Quotes > 1) {
  185. if (! isset($color[13]))
  186. $color[13] = '#FF0000';
  187. $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>';
  188. } elseif ($Quotes) {
  189. if (! isset($color[14]))
  190. $color[14] = '#800000';
  191. $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>';
  192. }
  193. $body_ary[$i] = $line;
  194. }
  195. $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
  196. }
  197. /* SquirrelMail version number -- DO NOT CHANGE */
  198. global $version;
  199. $version = '1.1.3 [cvs]';
  200. function find_mailbox_name ($mailbox) {
  201. if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
  202. return $regs[1];
  203. ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
  204. return $regs[1];
  205. }
  206. function get_location () {
  207. # This determines the location to forward to relative
  208. # to your server. If this doesnt work correctly for
  209. # you (although it should), you can remove all this
  210. # code except the last two lines, and change the header()
  211. # function to look something like this, customized to
  212. # the location of SquirrelMail on your server:
  213. #
  214. # http://www.myhost.com/squirrelmail/src/login.php
  215. global $PHP_SELF, $SERVER_NAME, $HTTP_HOST, $SERVER_PORT,
  216. $HTTP_SERVER_VARS;
  217. // Get the path
  218. $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
  219. // Check if this is a HTTPS or regular HTTP request
  220. $proto = 'http://';
  221. // If you have 'SSLOptions +StdEnvVars' in your apache config
  222. // OR if you have HTTPS in your HTTP_SERVER_VARS
  223. // OR if you are on port 443
  224. $getEnvVar = getenv('HTTPS');
  225. if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
  226. (isset($HTTP_SERVER_VARS['HTTPS'])) ||
  227. (isset($HTTP_SERVER_VARS['SERVER_PORT']) &&
  228. $HTTP_SERVER_VARS['SERVER_PORT'] == 443)) {
  229. $proto = 'https://';
  230. }
  231. // Get the hostname from the Host header or server config.
  232. $host = '';
  233. if (isset($HTTP_HOST) && !empty($HTTP_HOST))
  234. {
  235. $host = $HTTP_HOST;
  236. }
  237. else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
  238. {
  239. $host = $SERVER_NAME;
  240. }
  241. $port = '';
  242. if (! strstr($host, ':'))
  243. {
  244. if (isset($SERVER_PORT)) {
  245. if (($SERVER_PORT != 80 && $proto == 'http://')
  246. || ($SERVER_PORT != 443 && $proto == 'https://')) {
  247. $port = sprintf(':%d', $SERVER_PORT);
  248. }
  249. }
  250. }
  251. if ($host)
  252. return $proto . $host . $port . $path;
  253. // Fallback is to omit the server name and use a relative URI,
  254. // although this is not RFC 2616 compliant.
  255. return $path;
  256. }
  257. // These functions are used to encrypt the passowrd before it is
  258. // stored in a cookie.
  259. function OneTimePadEncrypt ($string, $epad) {
  260. $pad = base64_decode($epad);
  261. $encrypted = '';
  262. for ($i = 0; $i < strlen ($string); $i++) {
  263. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  264. }
  265. return base64_encode($encrypted);
  266. }
  267. function OneTimePadDecrypt ($string, $epad) {
  268. $pad = base64_decode($epad);
  269. $encrypted = base64_decode ($string);
  270. $decrypted = '';
  271. for ($i = 0; $i < strlen ($encrypted); $i++) {
  272. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  273. }
  274. return $decrypted;
  275. }
  276. // Randomize the mt_rand() function. Toss this in strings or
  277. // integers and it will seed the generator appropriately.
  278. // With strings, it is better to get them long. Use md5() to
  279. // lengthen smaller strings.
  280. function sq_mt_seed($Val)
  281. {
  282. // if mt_getrandmax() does not return a 2^n - 1 number,
  283. // this might not work well. This uses $Max as a bitmask.
  284. $Max = mt_getrandmax();
  285. if (! is_int($Val))
  286. {
  287. if (function_exists('crc32'))
  288. {
  289. $Val = crc32($Val);
  290. }
  291. else
  292. {
  293. $Str = $Val;
  294. $Pos = 0;
  295. $Val = 0;
  296. $Mask = $Max / 2;
  297. $HighBit = $Max ^ $Mask;
  298. while ($Pos < strlen($Str))
  299. {
  300. if ($Val & $HighBit)
  301. {
  302. $Val = (($Val & $Mask) << 1) + 1;
  303. }
  304. else
  305. {
  306. $Val = ($Val & $Mask) << 1;
  307. }
  308. $Val ^= $Str[$Pos];
  309. $Pos ++;
  310. }
  311. }
  312. }
  313. if ($Val < 0)
  314. $Val *= -1;
  315. if ($Val = 0)
  316. return;
  317. mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
  318. }
  319. // This function initializes the random number generator fairly well.
  320. // It also only initializes it once, so you don't accidentally get
  321. // the same 'random' numbers twice in one session.
  322. function sq_mt_randomize()
  323. {
  324. global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
  325. static $randomized;
  326. if ($randomized)
  327. return;
  328. // Global
  329. sq_mt_seed((int)((double) microtime() * 1000000));
  330. sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
  331. // getrusage
  332. if (function_exists('getrusage')) {
  333. // Avoid warnings with Win32
  334. $dat = @getrusage();
  335. if (isset($dat) && is_array($dat))
  336. {
  337. $Str = '';
  338. foreach ($dat as $k => $v)
  339. {
  340. $Str .= $k . $v;
  341. }
  342. sq_mt_seed(md5($Str));
  343. }
  344. }
  345. // Apache-specific
  346. sq_mt_seed(md5($UNIQUE_ID));
  347. $randomized = 1;
  348. }
  349. function OneTimePadCreate ($length=100) {
  350. sq_mt_randomize();
  351. $pad = '';
  352. for ($i = 0; $i < $length; $i++) {
  353. $pad .= chr(mt_rand(0,255));
  354. }
  355. return base64_encode($pad);
  356. }
  357. // Check if we have a required PHP-version. Return TRUE if we do,
  358. // or FALSE if we don't.
  359. // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
  360. // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
  361. // Does not handle betas like 4.0.1b1 or development versions
  362. function sqCheckPHPVersion($major, $minor, $release) {
  363. $ver = phpversion();
  364. eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
  365. // Parse the version string
  366. $vmajor = strval($regs[1]);
  367. $vminor = strval($regs[2]);
  368. $vrel = $regs[3];
  369. if($vrel[0] == ".")
  370. $vrel = strval(substr($vrel, 1));
  371. if($vrel[0] == 'b' || $vrel[0] == 'B')
  372. $vrel = - strval(substr($vrel, 1));
  373. if($vrel[0] == 'r' || $vrel[0] == 'R')
  374. $vrel = - strval(substr($vrel, 2))/10;
  375. // Compare major version
  376. if($vmajor < $major) return false;
  377. if($vmajor > $major) return true;
  378. // Major is the same. Compare minor
  379. if($vminor < $minor) return false;
  380. if($vminor > $minor) return true;
  381. // Major and minor is the same as the required one.
  382. // Compare release
  383. if($vrel >= 0 && $release >= 0) { // Neither are beta
  384. if($vrel < $release) return false;
  385. } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
  386. return true;
  387. } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
  388. return false;
  389. } else { // Both are beta
  390. if($vrel > $release) return false;
  391. }
  392. return true;
  393. }
  394. /* Returns a string showing the size of the message/attachment */
  395. function show_readable_size($bytes)
  396. {
  397. $bytes /= 1024;
  398. $type = 'k';
  399. if ($bytes / 1024 > 1)
  400. {
  401. $bytes /= 1024;
  402. $type = 'm';
  403. }
  404. if ($bytes < 10)
  405. {
  406. $bytes *= 10;
  407. settype($bytes, 'integer');
  408. $bytes /= 10;
  409. }
  410. else
  411. settype($bytes, 'integer');
  412. return $bytes . '<small>&nbsp;' . $type . '</small>';
  413. }
  414. /* Generates a random string from the caracter set you pass in
  415. *
  416. * Flags:
  417. * 1 = add lowercase a-z to $chars
  418. * 2 = add uppercase A-Z to $chars
  419. * 4 = add numbers 0-9 to $chars
  420. */
  421. function GenerateRandomString($size, $chars, $flags = 0)
  422. {
  423. if ($flags & 0x1)
  424. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  425. if ($flags & 0x2)
  426. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  427. if ($flags & 0x4)
  428. $chars .= '0123456789';
  429. if ($size < 1 || strlen($chars) < 1)
  430. return '';
  431. sq_mt_randomize(); // Initialize the random number generator
  432. $String = "";
  433. while (strlen($String) < $size) {
  434. $String .= $chars[mt_rand(0, strlen($chars))];
  435. }
  436. return $String;
  437. }
  438. function quoteIMAP($str)
  439. {
  440. return ereg_replace('(["\\])', '\\\\1', $str);
  441. }
  442. ?>