strings.php 12 KB

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