strings.php 17 KB

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