strings.php 13 KB

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