strings.php 13 KB

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