strings.php 12 KB

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