strings.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /**
  3. * strings.php
  4. *
  5. * Copyright (c) 1999-2003 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.4.0 [CVS-DEVEL]';
  18. /**
  19. * SquirrelMail internal version number -- DO NOT CHANGE
  20. * $sm_internal_version = array (release, major, minor)
  21. */
  22. global $SQM_INTERNAL_VERSION;
  23. $SQM_INTERNAL_VERSION = array(1,4,0);
  24. /**
  25. * Wraps text at $wrap characters
  26. *
  27. * Has a problem with special HTML characters, so call this before
  28. * you do character translation.
  29. *
  30. * Specifically, &#039 comes up as 5 characters instead of 1.
  31. * This should not add newlines to the end of lines.
  32. */
  33. function sqWordWrap(&$line, $wrap) {
  34. ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
  35. $beginning_spaces = $regs[1];
  36. if (isset($regs[2])) {
  37. $words = explode(' ', $regs[2]);
  38. } else {
  39. $words = '';
  40. }
  41. $i = 0;
  42. $line = $beginning_spaces;
  43. while ($i < count($words)) {
  44. /* Force one word to be on a line (minimum) */
  45. $line .= $words[$i];
  46. $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2;
  47. if (isset($words[$i + 1]))
  48. $line_len += strlen($words[$i + 1]);
  49. $i ++;
  50. /* Add more words (as long as they fit) */
  51. while ($line_len < $wrap && $i < count($words)) {
  52. $line .= ' ' . $words[$i];
  53. $i++;
  54. if (isset($words[$i]))
  55. $line_len += strlen($words[$i]) + 1;
  56. else
  57. $line_len += 1;
  58. }
  59. /* Skip spaces if they are the first thing on a continued line */
  60. while (!isset($words[$i]) && $i < count($words)) {
  61. $i ++;
  62. }
  63. /* Go to the next line if we have more to process */
  64. if ($i < count($words)) {
  65. $line .= "\n";
  66. }
  67. }
  68. }
  69. /**
  70. * Does the opposite of sqWordWrap()
  71. */
  72. function sqUnWordWrap(&$body) {
  73. $lines = explode("\n", $body);
  74. $body = '';
  75. $PreviousSpaces = '';
  76. $cnt = count($lines);
  77. for ($i = 0; $i < $cnt; $i ++) {
  78. preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
  79. $CurrentSpaces = $regs[1];
  80. if (isset($regs[2])) {
  81. $CurrentRest = $regs[2];
  82. } else {
  83. $CurrentRest = '';
  84. }
  85. if ($i == 0) {
  86. $PreviousSpaces = $CurrentSpaces;
  87. $body = $lines[$i];
  88. } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */
  89. && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */
  90. && strlen($CurrentRest)) { /* and there's a line to continue with */
  91. $body .= ' ' . $CurrentRest;
  92. } else {
  93. $body .= "\n" . $lines[$i];
  94. $PreviousSpaces = $CurrentSpaces;
  95. }
  96. }
  97. $body .= "\n";
  98. }
  99. /**
  100. * If $haystack is a full mailbox name and $needle is the mailbox
  101. * separator character, returns the last part of the mailbox name.
  102. */
  103. function readShortMailboxName($haystack, $needle) {
  104. if ($needle == '') {
  105. $elem = $haystack;
  106. } else {
  107. $parts = explode($needle, $haystack);
  108. $elem = array_pop($parts);
  109. while ($elem == '' && count($parts)) {
  110. $elem = array_pop($parts);
  111. }
  112. }
  113. return( $elem );
  114. }
  115. /**
  116. * Returns an array of email addresses.
  117. * Be cautious of "user@host.com"
  118. */
  119. function parseAddrs($text) {
  120. if (trim($text) == '')
  121. return array();
  122. $text = str_replace(' ', '', $text);
  123. $text = ereg_replace('"[^"]*"', '', $text);
  124. $text = ereg_replace('\\([^\\)]*\\)', '', $text);
  125. $text = str_replace(',', ';', $text);
  126. $array = explode(';', $text);
  127. for ($i = 0; $i < count ($array); $i++) {
  128. $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]);
  129. $array[$i] = eregi_replace ('[>].*$', '', $array[$i]);
  130. }
  131. return $array;
  132. }
  133. /**
  134. * Returns a line of comma separated email addresses from an array.
  135. */
  136. function getLineOfAddrs($array) {
  137. if (is_array($array)) {
  138. $to_line = implode(', ', $array);
  139. $to_line = ereg_replace(', (, )+', ', ', $to_line);
  140. $to_line = trim(ereg_replace('^, ', '', $to_line));
  141. if( substr( $to_line, -1 ) == ',' )
  142. $to_line = substr( $to_line, 0, -1 );
  143. } else {
  144. $to_line = '';
  145. }
  146. return( $to_line );
  147. }
  148. function php_self () {
  149. global $PHP_SELF, $_SERVER;
  150. if (isset($_SERVER['REQUEST_URI']) && !empty($_SERVER['REQUEST_URI']) ) {
  151. return $_SERVER['REQUEST_URI'];
  152. }
  153. if (isset($PHP_SELF) && !empty($PHP_SELF)) {
  154. return $PHP_SELF;
  155. } else if (isset($_SERVER['PHP_SELF']) &&
  156. !empty($_SERVER['PHP_SELF'])) {
  157. return $_SERVER['PHP_SELF'];
  158. } else {
  159. return '';
  160. }
  161. }
  162. /**
  163. * This determines the location to forward to relative to your server.
  164. * If this doesnt work correctly for you (although it should), you can
  165. * remove all this code except the last two lines, and change the header()
  166. * function to look something like this, customized to the location of
  167. * SquirrelMail on your server:
  168. *
  169. * http://www.myhost.com/squirrelmail/src/login.php
  170. */
  171. function get_location () {
  172. global $_SERVER, $imap_server_type;
  173. /* Get the path, handle virtual directories */
  174. $path = substr(php_self(), 0, strrpos(php_self(), '/'));
  175. /* Check if this is a HTTPS or regular HTTP request. */
  176. $proto = 'http://';
  177. /*
  178. * If you have 'SSLOptions +StdEnvVars' in your apache config
  179. * OR if you have HTTPS=on in your HTTP_SERVER_VARS
  180. * OR if you are on port 443
  181. */
  182. $getEnvVar = getenv('HTTPS');
  183. if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
  184. (isset($_SERVER['HTTPS']) &&
  185. !strcasecmp($_SERVER['HTTPS'], 'on')) ||
  186. (isset($_SERVER['SERVER_PORT']) &&
  187. $_SERVER['SERVER_PORT'] == 443)) {
  188. $proto = 'https://';
  189. }
  190. /* Get the hostname from the Host header or server config. */
  191. $host = '';
  192. if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
  193. $host = $_SERVER['HTTP_HOST'];
  194. } else if (isset($_SERVER['SERVER_NAME']) &&
  195. !empty($_SERVER['SERVER_NAME'])) {
  196. }
  197. $port = '';
  198. if (! strstr($host, ':')) {
  199. if (isset($_SERVER['SERVER_PORT'])) {
  200. if (($_SERVER['SERVER_PORT'] != 80 && $proto == 'http://')
  201. || ($_SERVER['SERVER_PORT'] != 443 && $proto == 'https://')) {
  202. $port = sprintf(':%d', $_SERVER['SERVER_PORT']);
  203. }
  204. }
  205. }
  206. /* this is a workaround for the weird macosx caching that
  207. causes Apache to return 16080 as the port number, which causes
  208. SM to bail */
  209. if ($imap_server_type == 'macosx' && $port == ':16080') {
  210. $port = '';
  211. }
  212. /* Fallback is to omit the server name and use a relative */
  213. /* URI, although this is not RFC 2616 compliant. */
  214. return ($host ? $proto . $host . $port . $path : $path);
  215. }
  216. /**
  217. * These functions are used to encrypt the passowrd before it is
  218. * stored in a cookie.
  219. */
  220. function OneTimePadEncrypt ($string, $epad) {
  221. $pad = base64_decode($epad);
  222. $encrypted = '';
  223. for ($i = 0; $i < strlen ($string); $i++) {
  224. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  225. }
  226. return base64_encode($encrypted);
  227. }
  228. function OneTimePadDecrypt ($string, $epad) {
  229. $pad = base64_decode($epad);
  230. $encrypted = base64_decode ($string);
  231. $decrypted = '';
  232. for ($i = 0; $i < strlen ($encrypted); $i++) {
  233. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  234. }
  235. return $decrypted;
  236. }
  237. /**
  238. * Randomize the mt_rand() function. Toss this in strings or integers
  239. * and it will seed the generator appropriately. With strings, it is
  240. * better to get them long. Use md5() to lengthen smaller strings.
  241. */
  242. function sq_mt_seed($Val) {
  243. /* if mt_getrandmax() does not return a 2^n - 1 number,
  244. this might not work well. This uses $Max as a bitmask. */
  245. $Max = mt_getrandmax();
  246. if (! is_int($Val)) {
  247. $Val = crc32($Val);
  248. }
  249. if ($Val < 0) {
  250. $Val *= -1;
  251. }
  252. if ($Val = 0) {
  253. return;
  254. }
  255. mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
  256. }
  257. /**
  258. * This function initializes the random number generator fairly well.
  259. * It also only initializes it once, so you don't accidentally get
  260. * the same 'random' numbers twice in one session.
  261. */
  262. function sq_mt_randomize() {
  263. global $_SERVER;
  264. static $randomized;
  265. if ($randomized) {
  266. return;
  267. }
  268. /* Global. */
  269. sq_mt_seed((int)((double) microtime() * 1000000));
  270. sq_mt_seed(md5($_SERVER['REMOTE_PORT'] . $_SERVER['REMOTE_ADDR'] . getmypid()));
  271. /* getrusage */
  272. if (function_exists('getrusage')) {
  273. /* Avoid warnings with Win32 */
  274. $dat = @getrusage();
  275. if (isset($dat) && is_array($dat)) {
  276. $Str = '';
  277. foreach ($dat as $k => $v)
  278. {
  279. $Str .= $k . $v;
  280. }
  281. sq_mt_seed(md5($Str));
  282. }
  283. }
  284. if(isset($_SERVER['UNIQUE_ID'])) {
  285. sq_mt_seed(md5($_SERVER['UNIQUE_ID']));
  286. }
  287. $randomized = 1;
  288. }
  289. function OneTimePadCreate ($length=100) {
  290. sq_mt_randomize();
  291. $pad = '';
  292. for ($i = 0; $i < $length; $i++) {
  293. $pad .= chr(mt_rand(0,255));
  294. }
  295. return base64_encode($pad);
  296. }
  297. /**
  298. * Duplicate function: obsoleted. Use check_php_version.
  299. */
  300. function sqCheckPHPVersion($major, $minor, $release) {
  301. return check_php_version($major, $minor, $release);
  302. }
  303. /**
  304. * Returns a string showing the size of the message/attachment.
  305. */
  306. function show_readable_size($bytes) {
  307. $bytes /= 1024;
  308. $type = 'k';
  309. if ($bytes / 1024 > 1) {
  310. $bytes /= 1024;
  311. $type = 'm';
  312. }
  313. if ($bytes < 10) {
  314. $bytes *= 10;
  315. settype($bytes, 'integer');
  316. $bytes /= 10;
  317. } else {
  318. settype($bytes, 'integer');
  319. }
  320. return $bytes . '<small>&nbsp;' . $type . '</small>';
  321. }
  322. /**
  323. * Generates a random string from the caracter set you pass in
  324. *
  325. * Flags:
  326. * 1 = add lowercase a-z to $chars
  327. * 2 = add uppercase A-Z to $chars
  328. * 4 = add numbers 0-9 to $chars
  329. */
  330. function GenerateRandomString($size, $chars, $flags = 0) {
  331. if ($flags & 0x1) {
  332. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  333. }
  334. if ($flags & 0x2) {
  335. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  336. }
  337. if ($flags & 0x4) {
  338. $chars .= '0123456789';
  339. }
  340. if (($size < 1) || (strlen($chars) < 1)) {
  341. return '';
  342. }
  343. sq_mt_randomize(); /* Initialize the random number generator */
  344. $String = '';
  345. $j = strlen( $chars ) - 1;
  346. while (strlen($String) < $size) {
  347. $String .= $chars{mt_rand(0, $j)};
  348. }
  349. return $String;
  350. }
  351. function quoteIMAP($str) {
  352. return ereg_replace('(["\\])', '\\\\1', $str);
  353. }
  354. /**
  355. * Trims every element in the array
  356. */
  357. function TrimArray(&$array) {
  358. foreach ($array as $k => $v) {
  359. global $$k;
  360. if (is_array($$k)) {
  361. foreach ($$k as $k2 => $v2) {
  362. $$k[$k2] = substr($v2, 1);
  363. }
  364. } else {
  365. $$k = substr($v, 1);
  366. }
  367. /* Re-assign back to array. */
  368. $array[$k] = $$k;
  369. }
  370. }
  371. /**
  372. * Removes slashes from every element in the array
  373. */
  374. function RemoveSlashes(&$array) {
  375. foreach ($array as $k => $v) {
  376. global $$k;
  377. if (is_array($$k)) {
  378. foreach ($$k as $k2 => $v2) {
  379. $newArray[stripslashes($k2)] = stripslashes($v2);
  380. }
  381. $$k = $newArray;
  382. } else {
  383. $$k = stripslashes($v);
  384. }
  385. /* Re-assign back to the array. */
  386. $array[$k] = $$k;
  387. }
  388. }
  389. $PHP_SELF = php_self();
  390. ?>