strings.php 13 KB

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