strings.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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.0.3 [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. function replace_spaces ($string) {
  269. return str_replace(' ', '&nbsp;', $string);
  270. }
  271. function replace_escaped_spaces ($string) {
  272. return str_replace('&nbsp;', ' ', $string);
  273. }
  274. function get_location () {
  275. # This determines the location to forward to relative
  276. # to your server. If this doesnt work correctly for
  277. # you (although it should), you can remove all this
  278. # code except the last two lines, and change the header()
  279. # function to look something like this, customized to
  280. # the location of SquirrelMail on your server:
  281. #
  282. # http://www.myhost.com/squirrelmail/src/login.php
  283. global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
  284. // Get the path
  285. $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
  286. // Check if this is a HTTPS or regular HTTP request
  287. $proto = 'http://';
  288. if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
  289. $proto = 'https://';
  290. }
  291. // Get the hostname from the Host header or server config.
  292. $host = '';
  293. if (isset($HTTP_HOST) && !empty($HTTP_HOST))
  294. {
  295. $host = $HTTP_HOST;
  296. }
  297. else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
  298. {
  299. $host = $SERVER_NAME;
  300. }
  301. $port = '';
  302. if (! strstr($host, ':'))
  303. {
  304. if (isset($SERVER_PORT)) {
  305. if (($SERVER_PORT != 80 && $proto == 'http://')
  306. || ($SERVER_PORT != 443 && $proto == 'https://')) {
  307. $port = sprintf(':%d', $SERVER_PORT);
  308. }
  309. }
  310. }
  311. if ($host)
  312. return $proto . $host . $port . $path;
  313. // Fallback is to omit the server name and use a relative URI,
  314. // although this is not RFC 2616 compliant.
  315. return $path;
  316. }
  317. // These functions are used to encrypt the passowrd before it is
  318. // stored in a cookie.
  319. function OneTimePadEncrypt ($string, $epad) {
  320. $pad = base64_decode($epad);
  321. $encrypted = '';
  322. for ($i = 0; $i < strlen ($string); $i++) {
  323. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  324. }
  325. return base64_encode($encrypted);
  326. }
  327. function OneTimePadDecrypt ($string, $epad) {
  328. $pad = base64_decode($epad);
  329. $encrypted = base64_decode ($string);
  330. $decrypted = '';
  331. for ($i = 0; $i < strlen ($encrypted); $i++) {
  332. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  333. }
  334. return $decrypted;
  335. }
  336. // Randomize the mt_rand() function. Toss this in strings or
  337. // integers and it will seed the generator appropriately.
  338. // With strings, it is better to get them long. Use md5() to
  339. // lengthen smaller strings.
  340. function sq_mt_seed($Val)
  341. {
  342. // if mt_getrandmax() does not return a 2^n - 1 number,
  343. // this might not work well. This uses $Max as a bitmask.
  344. $Max = mt_getrandmax();
  345. if (! is_int($Val))
  346. {
  347. if (function_exists('crc32'))
  348. {
  349. $Val = crc32($Val);
  350. }
  351. else
  352. {
  353. $Str = $Val;
  354. $Pos = 0;
  355. $Val = 0;
  356. $Mask = $Max / 2;
  357. $HighBit = $Max ^ $Mask;
  358. while ($Pos < strlen($Str))
  359. {
  360. if ($Val & $HighBit)
  361. {
  362. $Val = (($Val & $Mask) << 1) + 1;
  363. }
  364. else
  365. {
  366. $Val = ($Val & $Mask) << 1;
  367. }
  368. $Val ^= $Str[$Pos];
  369. $Pos ++;
  370. }
  371. }
  372. }
  373. if ($Val < 0)
  374. $Val *= -1;
  375. if ($Val = 0)
  376. return;
  377. mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
  378. }
  379. // This function initializes the random number generator fairly well.
  380. // It also only initializes it once, so you don't accidentally get
  381. // the same 'random' numbers twice in one session.
  382. function sq_mt_randomize()
  383. {
  384. global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
  385. static $randomized;
  386. if ($randomized)
  387. return;
  388. // Global
  389. sq_mt_seed((int)((double) microtime() * 1000000));
  390. sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
  391. // getrusage
  392. if (function_exists('getrusage')) {
  393. $dat = getrusage();
  394. $Str = '';
  395. foreach ($dat as $k => $v)
  396. {
  397. $Str .= $k . $v;
  398. }
  399. sq_mt_seed(md5($Str));
  400. }
  401. // Apache-specific
  402. sq_mt_seed(md5($UNIQUE_ID));
  403. $randomized = 1;
  404. }
  405. function OneTimePadCreate ($length=100) {
  406. sq_mt_randomize();
  407. $pad = '';
  408. for ($i = 0; $i < $length; $i++) {
  409. $pad .= chr(mt_rand(0,255));
  410. }
  411. return base64_encode($pad);
  412. }
  413. // Check if we have a required PHP-version. Return TRUE if we do,
  414. // or FALSE if we don't.
  415. // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
  416. // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
  417. // Does not handle betas like 4.0.1b1 or development versions
  418. function sqCheckPHPVersion($major, $minor, $release) {
  419. $ver = phpversion();
  420. eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
  421. // Parse the version string
  422. $vmajor = strval($regs[1]);
  423. $vminor = strval($regs[2]);
  424. $vrel = $regs[3];
  425. if($vrel[0] == ".")
  426. $vrel = strval(substr($vrel, 1));
  427. if($vrel[0] == 'b' || $vrel[0] == 'B')
  428. $vrel = - strval(substr($vrel, 1));
  429. if($vrel[0] == 'r' || $vrel[0] == 'R')
  430. $vrel = - strval(substr($vrel, 2))/10;
  431. // Compare major version
  432. if($vmajor < $major) return false;
  433. if($vmajor > $major) return true;
  434. // Major is the same. Compare minor
  435. if($vminor < $minor) return false;
  436. if($vminor > $minor) return true;
  437. // Major and minor is the same as the required one.
  438. // Compare release
  439. if($vrel >= 0 && $release >= 0) { // Neither are beta
  440. if($vrel < $release) return false;
  441. } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
  442. return true;
  443. } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
  444. return false;
  445. } else { // Both are beta
  446. if($vrel > $release) return false;
  447. }
  448. return true;
  449. }
  450. /* Returns a string showing the size of the message/attachment */
  451. function show_readable_size($bytes)
  452. {
  453. $bytes /= 1024;
  454. $type = 'k';
  455. if ($bytes / 1024 > 1)
  456. {
  457. $bytes /= 1024;
  458. $type = 'm';
  459. }
  460. if ($bytes < 10)
  461. {
  462. $bytes *= 10;
  463. settype($bytes, 'integer');
  464. $bytes /= 10;
  465. }
  466. else
  467. settype($bytes, 'integer');
  468. return $bytes . '<small>&nbsp;' . $type . '</small>';
  469. }
  470. /* Generates a random string from the caracter set you pass in
  471. *
  472. * Flags:
  473. * 1 = add lowercase a-z to $chars
  474. * 2 = add uppercase A-Z to $chars
  475. * 4 = add numbers 0-9 to $chars
  476. */
  477. function GenerateRandomString($size, $chars, $flags = 0)
  478. {
  479. if ($flags & 0x1)
  480. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  481. if ($flags & 0x2)
  482. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  483. if ($flags & 0x4)
  484. $chars .= '0123456789';
  485. if ($size < 1 || strlen($chars) < 1)
  486. return '';
  487. sq_mt_randomize(); // Initialize the random number generator
  488. $String = "";
  489. while (strlen($String) < $size) {
  490. $String .= $chars[mt_rand(0, strlen($chars))];
  491. }
  492. return $String;
  493. }
  494. ?>