strings.php 13 KB

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