strings.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. /* $Id$ */
  3. $strings_php = true;
  4. $fix_form_endlines = false;
  5. // Remove all slashes for form values
  6. if (get_magic_quotes_gpc())
  7. {
  8. global $REQUEST_METHOD;
  9. if ($REQUEST_METHOD == "POST")
  10. {
  11. global $HTTP_POST_VARS;
  12. RemoveSlashes($HTTP_POST_VARS);
  13. }
  14. elseif ($REQUEST_METHOD == "GET")
  15. {
  16. global $HTTP_GET_VARS;
  17. RemoveSlashes($HTTP_GET_VARS);
  18. }
  19. }
  20. // Auto-detection
  21. //
  22. // if $send (the form button's name) contains "\n" as the first char
  23. // and the script is compose.php, then trim everything. Otherwise,
  24. // we don't have to worry.
  25. global $send, $PHP_SELF;
  26. if (isset($send) && substr($send, 0, 1) == "\n" &&
  27. substr($PHP_SELF, -12) == "/compose.php")
  28. {
  29. if ($REQUEST_METHOD == "POST") {
  30. TrimArray($HTTP_POST_VARS);
  31. } else {
  32. TrimArray($HTTP_GET_VARS);
  33. }
  34. }
  35. //**************************************************************************
  36. // Trims every element in the array
  37. //**************************************************************************
  38. function TrimArray(&$array) {
  39. foreach ($array as $k => $v) {
  40. global $$k;
  41. if (is_array($$k)) {
  42. foreach ($$k as $k2 => $v2) {
  43. $$k[$k2] = substr($v2, 1);
  44. }
  45. } else {
  46. $$k = substr($v, 1);
  47. }
  48. }
  49. }
  50. //**************************************************************************
  51. // Removes slashes from every element in the array
  52. //**************************************************************************
  53. function RemoveSlashes($array)
  54. {
  55. foreach ($array as $k => $v)
  56. {
  57. global $$k;
  58. if (is_array($$k))
  59. {
  60. foreach ($$k as $k2 => $v2)
  61. {
  62. $newArray[stripslashes($k2)] = stripslashes($v2);
  63. }
  64. $$k = $newArray;
  65. }
  66. else
  67. {
  68. $$k = stripslashes($v);
  69. }
  70. }
  71. }
  72. //*************************************************************************
  73. // Count the number of occurances of $needle are in $haystack.
  74. // $needle can be a character or string, and need not occur in $haystack
  75. //*************************************************************************
  76. function countCharInString($haystack, $needle) {
  77. if ($needle == '') return 0;
  78. return count(explode($needle, $haystack));
  79. }
  80. //*************************************************************************
  81. // Read from the back of $haystack until $needle is found, or the begining
  82. // of the $haystack is reached. $needle is a single character
  83. //*************************************************************************
  84. function readShortMailboxName($haystack, $needle) {
  85. if ($needle == '') return $haystack;
  86. $parts = explode($needle, $haystack);
  87. $elem = array_pop($parts);
  88. while ($elem == '' && count($parts))
  89. {
  90. $elem = array_pop($parts);
  91. }
  92. return $elem;
  93. }
  94. //*************************************************************************
  95. // Read from the back of $haystack until $needle is found, or the begining
  96. // of the $haystack is reached. $needle is a single character
  97. //*************************************************************************
  98. function readMailboxParent($haystack, $needle) {
  99. if ($needle == '') return '';
  100. $parts = explode($needle, $haystack);
  101. $elem = array_pop($parts);
  102. while ($elem == '' && count($parts))
  103. {
  104. $elem = array_pop($parts);
  105. }
  106. return join($needle, $parts);
  107. }
  108. // Searches for the next position in a string minus white space
  109. function next_pos_minus_white ($haystack, $pos) {
  110. while (substr($haystack, $pos, 1) == ' ' ||
  111. substr($haystack, $pos, 1) == "\t" ||
  112. substr($haystack, $pos, 1) == "\n" ||
  113. substr($haystack, $pos, 1) == "\r") {
  114. if ($pos >= strlen($haystack))
  115. return -1;
  116. $pos++;
  117. }
  118. return $pos;
  119. }
  120. // Wraps text at $wrap characters
  121. // Has a problem with special HTML characters, so call this before
  122. // you do character translation.
  123. // Specifically, &#039 comes up as 5 characters instead of 1.
  124. // This should not add newlines to the end of lines.
  125. function sqWordWrap(&$line, $wrap) {
  126. ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
  127. $beginning_spaces = $regs[1];
  128. if (isset($regs[2])) {
  129. $words = explode(' ', $regs[2]);
  130. } else {
  131. $words = "";
  132. }
  133. $i = 0;
  134. $line = $beginning_spaces;
  135. while ($i < count($words)) {
  136. // Force one word to be on a line (minimum)
  137. $line .= $words[$i];
  138. $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2;
  139. if (isset($words[$i + 1]))
  140. $line_len += strlen($words[$i + 1]);
  141. $i ++;
  142. // Add more words (as long as they fit)
  143. while ($line_len < $wrap && $i < count($words)) {
  144. $line .= ' ' . $words[$i];
  145. $i++;
  146. if (isset($words[$i]))
  147. $line_len += strlen($words[$i]) + 1;
  148. else
  149. $line_len += 1;
  150. }
  151. // Skip spaces if they are the first thing on a continued line
  152. while (!isset($words[$i]) && $i < count($words)) {
  153. $i ++;
  154. }
  155. // Go to the next line if we have more to process
  156. if ($i < count($words)) {
  157. $line .= "\n" . $beginning_spaces;
  158. }
  159. }
  160. }
  161. // Does the opposite of sqWordWrap()
  162. function sqUnWordWrap(&$body)
  163. {
  164. $lines = explode("\n", $body);
  165. $body = "";
  166. $PreviousSpaces = "";
  167. for ($i = 0; $i < count($lines); $i ++)
  168. {
  169. ereg("^([\t >]*)([^\t >].*)?$", $lines[$i], $regs);
  170. $CurrentSpaces = $regs[1];
  171. if (isset($regs[2]))
  172. $CurrentRest = $regs[2];
  173. if ($i == 0)
  174. {
  175. $PreviousSpaces = $CurrentSpaces;
  176. $body = $lines[$i];
  177. }
  178. else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
  179. strlen($lines[$i - 1]) > 65 && // Over 65 characters long
  180. strlen($CurrentRest)) // and there's a line to continue with
  181. {
  182. $body .= ' ' . $CurrentRest;
  183. }
  184. else
  185. {
  186. $body .= "\n" . $lines[$i];
  187. $PreviousSpaces = $CurrentSpaces;
  188. }
  189. }
  190. $body .= "\n";
  191. }
  192. /** Returns an array of email addresses **/
  193. /* Be cautious of "user@host.com" */
  194. function parseAddrs($text) {
  195. if (trim($text) == "")
  196. return array();
  197. $text = str_replace(' ', '', $text);
  198. $text = ereg_replace('"[^"]*"', '', $text);
  199. $text = ereg_replace('\\([^\\)]*\\)', '', $text);
  200. $text = str_replace(',', ';', $text);
  201. $array = explode(';', $text);
  202. for ($i = 0; $i < count ($array); $i++) {
  203. $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]);
  204. $array[$i] = eregi_replace ("[>].*$", '', $array[$i]);
  205. }
  206. return $array;
  207. }
  208. /** Returns a line of comma separated email addresses from an array **/
  209. function getLineOfAddrs($array) {
  210. if (is_array($array)) {
  211. $to_line = implode(', ', $array);
  212. $to_line = trim(ereg_replace(', (, )+', ', ', $to_line));
  213. } else {
  214. $to_line = '';
  215. }
  216. return $to_line;
  217. }
  218. function translateText(&$body, $wrap_at, $charset) {
  219. global $where, $what; // from searching
  220. global $url_parser_php;
  221. if (!isset($url_parser_php)) {
  222. include '../functions/url_parser.php';
  223. }
  224. $body_ary = explode("\n", $body);
  225. $PriorQuotes = 0;
  226. for ($i=0; $i < count($body_ary); $i++) {
  227. $line = $body_ary[$i];
  228. if (strlen($line) - 2 >= $wrap_at) {
  229. sqWordWrap($line, $wrap_at);
  230. }
  231. $line = charset_decode($charset, $line);
  232. $line = str_replace("\t", ' ', $line);
  233. parseUrl ($line);
  234. $Quotes = 0;
  235. $pos = 0;
  236. while (1)
  237. {
  238. if ($line[$pos] == ' ')
  239. {
  240. $pos ++;
  241. }
  242. else if (strpos($line, '&gt;', $pos) === $pos)
  243. {
  244. $pos += 4;
  245. $Quotes ++;
  246. }
  247. else
  248. {
  249. break;
  250. }
  251. }
  252. if ($Quotes > 1)
  253. $line = '<FONT COLOR="FF0000">'.$line.'</FONT>';
  254. elseif ($Quotes)
  255. $line = '<FONT COLOR="800000">'.$line.'</FONT>';
  256. $body_ary[$i] = $line;
  257. }
  258. $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
  259. }
  260. /* SquirrelMail version number -- DO NOT CHANGE */
  261. $version = '1.1.0 [cvs]';
  262. function find_mailbox_name ($mailbox) {
  263. if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
  264. return $regs[1];
  265. ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
  266. return $regs[1];
  267. }
  268. // Depreciated. :-) I always wanted to say that.
  269. function replace_spaces ($string) {
  270. return str_replace(' ', '&nbsp;', $string);
  271. }
  272. function get_location () {
  273. # This determines the location to forward to relative
  274. # to your server. If this doesnt work correctly for
  275. # you (although it should), you can remove all this
  276. # code except the last two lines, and change the header()
  277. # function to look something like this, customized to
  278. # the location of SquirrelMail on your server:
  279. #
  280. # http://www.myhost.com/squirrelmail/src/login.php
  281. global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
  282. // Get the path
  283. $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
  284. // Check if this is a HTTPS or regular HTTP request
  285. $proto = 'http://';
  286. if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
  287. $proto = 'https://';
  288. }
  289. // Get the hostname from the Host header or server config.
  290. $host = '';
  291. if (isset($HTTP_HOST) && !empty($HTTP_HOST))
  292. {
  293. $host = $HTTP_HOST;
  294. }
  295. else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
  296. {
  297. $host = $SERVER_NAME;
  298. }
  299. $port = '';
  300. if (! strstr($host, ':'))
  301. {
  302. if (isset($SERVER_PORT)) {
  303. if (($SERVER_PORT != 80 && $proto == 'http://')
  304. || ($SERVER_PORT != 443 && $proto == 'https://')) {
  305. $port = sprintf(':%d', $SERVER_PORT);
  306. }
  307. }
  308. }
  309. if ($host)
  310. return $proto . $host . $port . $path;
  311. // Fallback is to omit the server name and use a relative URI,
  312. // although this is not RFC 2616 compliant.
  313. return $path;
  314. }
  315. // These functions are used to encrypt the passowrd before it is
  316. // stored in a cookie.
  317. function OneTimePadEncrypt ($string, $epad) {
  318. $pad = base64_decode($epad);
  319. $encrypted = '';
  320. for ($i = 0; $i < strlen ($string); $i++) {
  321. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  322. }
  323. return base64_encode($encrypted);
  324. }
  325. function OneTimePadDecrypt ($string, $epad) {
  326. $pad = base64_decode($epad);
  327. $encrypted = base64_decode ($string);
  328. $decrypted = '';
  329. for ($i = 0; $i < strlen ($encrypted); $i++) {
  330. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  331. }
  332. return $decrypted;
  333. }
  334. // Randomize the mt_rand() function. Toss this in strings or
  335. // integers and it will seed the generator appropriately.
  336. // With strings, it is better to get them long. Use md5() to
  337. // lengthen smaller strings.
  338. function sq_mt_seed($Val)
  339. {
  340. // if mt_getrandmax() does not return a 2^n - 1 number,
  341. // this might not work well. This uses $Max as a bitmask.
  342. $Max = mt_getrandmax();
  343. if (! is_int($Val))
  344. {
  345. if (function_exists('crc32'))
  346. {
  347. $Val = crc32($Val);
  348. }
  349. else
  350. {
  351. $Str = $Val;
  352. $Pos = 0;
  353. $Val = 0;
  354. $Mask = $Max / 2;
  355. $HighBit = $Max ^ $Mask;
  356. while ($Pos < strlen($Str))
  357. {
  358. if ($Val & $HighBit)
  359. {
  360. $Val = (($Val & $Mask) << 1) + 1;
  361. }
  362. else
  363. {
  364. $Val = ($Val & $Mask) << 1;
  365. }
  366. $Val ^= $Str[$Pos];
  367. $Pos ++;
  368. }
  369. }
  370. }
  371. if ($Val < 0)
  372. $Val *= -1;
  373. if ($Val = 0)
  374. return;
  375. mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
  376. }
  377. // This function initializes the random number generator fairly well.
  378. // It also only initializes it once, so you don't accidentally get
  379. // the same 'random' numbers twice in one session.
  380. function sq_mt_randomize()
  381. {
  382. global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
  383. static $randomized;
  384. if ($randomized)
  385. return;
  386. // Global
  387. sq_mt_seed((int)((double) microtime() * 1000000));
  388. sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
  389. // getrusage
  390. if (function_exists('getrusage')) {
  391. $dat = getrusage();
  392. $Str = '';
  393. foreach ($dat as $k => $v)
  394. {
  395. $Str .= $k . $v;
  396. }
  397. sq_mt_seed(md5($Str));
  398. }
  399. // Apache-specific
  400. sq_mt_seed(md5($UNIQUE_ID));
  401. $randomized = 1;
  402. }
  403. function OneTimePadCreate ($length=100) {
  404. sq_mt_randomize();
  405. $pad = '';
  406. for ($i = 0; $i < $length; $i++) {
  407. $pad .= chr(mt_rand(0,255));
  408. }
  409. return base64_encode($pad);
  410. }
  411. // Check if we have a required PHP-version. Return TRUE if we do,
  412. // or FALSE if we don't.
  413. // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
  414. // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
  415. // Does not handle betas like 4.0.1b1 or development versions
  416. function sqCheckPHPVersion($major, $minor, $release) {
  417. $ver = phpversion();
  418. eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
  419. // Parse the version string
  420. $vmajor = strval($regs[1]);
  421. $vminor = strval($regs[2]);
  422. $vrel = $regs[3];
  423. if($vrel[0] == ".")
  424. $vrel = strval(substr($vrel, 1));
  425. if($vrel[0] == 'b' || $vrel[0] == 'B')
  426. $vrel = - strval(substr($vrel, 1));
  427. if($vrel[0] == 'r' || $vrel[0] == 'R')
  428. $vrel = - strval(substr($vrel, 2))/10;
  429. // Compare major version
  430. if($vmajor < $major) return false;
  431. if($vmajor > $major) return true;
  432. // Major is the same. Compare minor
  433. if($vminor < $minor) return false;
  434. if($vminor > $minor) return true;
  435. // Major and minor is the same as the required one.
  436. // Compare release
  437. if($vrel >= 0 && $release >= 0) { // Neither are beta
  438. if($vrel < $release) return false;
  439. } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
  440. return true;
  441. } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
  442. return false;
  443. } else { // Both are beta
  444. if($vrel > $release) return false;
  445. }
  446. return true;
  447. }
  448. /* Returns a string showing the size of the message/attachment */
  449. function show_readable_size($bytes)
  450. {
  451. $bytes /= 1024;
  452. $type = 'k';
  453. if ($bytes / 1024 > 1)
  454. {
  455. $bytes /= 1024;
  456. $type = 'm';
  457. }
  458. if ($bytes < 10)
  459. {
  460. $bytes *= 10;
  461. settype($bytes, 'integer');
  462. $bytes /= 10;
  463. }
  464. else
  465. settype($bytes, 'integer');
  466. return $bytes . '<small>&nbsp;' . $type . '</small>';
  467. }
  468. /* Generates a random string from the caracter set you pass in
  469. *
  470. * Flags:
  471. * 1 = add lowercase a-z to $chars
  472. * 2 = add uppercase A-Z to $chars
  473. * 4 = add numbers 0-9 to $chars
  474. */
  475. function GenerateRandomString($size, $chars, $flags = 0)
  476. {
  477. if ($flags & 0x1)
  478. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  479. if ($flags & 0x2)
  480. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  481. if ($flags & 0x4)
  482. $chars .= '0123456789';
  483. if ($size < 1 || strlen($chars) < 1)
  484. return '';
  485. sq_mt_randomize(); // Initialize the random number generator
  486. $String = "";
  487. while (strlen($String) < $size) {
  488. $String .= $chars[mt_rand(0, strlen($chars))];
  489. }
  490. return $String;
  491. }
  492. function quoteIMAP($str)
  493. {
  494. return ereg_replace('(["\\])', '\\\\1', $str);
  495. }
  496. ?>