strings.php 15 KB

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