strings.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. function sqWordWrap($passed, $wrap) {
  43. $passed = str_replace("&lt;", "<", $passed);
  44. $passed = str_replace("&gt;", ">", $passed);
  45. preg_match("/^(\s|>)+/", $passed, $regs);
  46. $beginning_spaces = $regs[0];
  47. $words = explode(" ", $passed);
  48. $i = -1;
  49. $line_len = strlen($words[0])+1;
  50. $line = "";
  51. if (count($words) > 1) {
  52. while ($i++ < count($words)) {
  53. while ($line_len < $wrap) {
  54. $line = "$line$words[$i] ";
  55. $i++;
  56. $line_len = $line_len + strlen($words[$i]) + 1;
  57. }
  58. $line_len = strlen($words[$i])+1;
  59. if ($line_len <= $wrap) {
  60. if (strlen($beginning_spaces) +2 >= $wrap)
  61. $beginning_spaces = "";
  62. if ($i < count($words)) { // don't <BR> the last line
  63. $line = "$line\n$beginning_spaces";
  64. }
  65. $line = "$line$words[$i] ";
  66. $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 1;
  67. } else {
  68. /*
  69. $endline = $words[$i];
  70. while ($line_len >= $wrap) {
  71. $bigline = substr($endline, 0, $wrap);
  72. $endline = substr($endline, $wrap, strlen($endline));
  73. $line_len = strlen($endline);
  74. $line = "$line$bigline<BR>";
  75. }
  76. */
  77. if (strlen($line) > $wrap)
  78. $line = "$line\n$words[$i]";
  79. else
  80. $line = "$line$words[$i]";
  81. $line_len = strlen($words[$i]);
  82. }
  83. }
  84. } else {
  85. $line = $words[0];
  86. }
  87. $line = str_replace(">", "&gt;", $line);
  88. $line = str_replace("<", "&lt;", $line);
  89. return $line;
  90. }
  91. /** Returns an array of email addresses **/
  92. function parseAddrs($text) {
  93. if (trim($text) == "") {
  94. return;
  95. }
  96. $text = str_replace(" ", "", $text);
  97. $text = ereg_replace( '"[^"]*"', "", $text);
  98. $text = str_replace(",", ";", $text);
  99. $array = explode(";", $text);
  100. for ($i = 0; $i < count ($array); $i++) {
  101. $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
  102. $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
  103. }
  104. return $array;
  105. }
  106. /** Returns a line of comma separated email addresses from an array **/
  107. function getLineOfAddrs($array) {
  108. if (is_array($array)) {
  109. $to_line = implode(", ", $array);
  110. $to_line = trim(ereg_replace(",,+", ",", $to_line));
  111. } else {
  112. $to_line = "";
  113. }
  114. return $to_line;
  115. }
  116. function translateText($body, $wrap_at, $charset) {
  117. global $where, $what; // from searching
  118. if (!isset($url_parser_php)) {
  119. include "../functions/url_parser.php";
  120. }
  121. $body_ary = explode("\n", $body);
  122. for ($i=0; $i < count($body_ary); $i++) {
  123. $line = $body_ary[$i];
  124. $line = charset_decode($charset, $line);
  125. $line = str_replace("\t", ' ', $line);
  126. if (strlen($line) - 2 >= $wrap_at) {
  127. $line = sqWordWrap($line, $wrap_at);
  128. }
  129. $line = str_replace(' ', '&nbsp;', $line);
  130. $line = nl2br($line);
  131. // Removed parseEmail and integrated it into parseUrl
  132. // This line is no longer needed.
  133. // $line = parseEmail ($line);
  134. $line = parseUrl ($line);
  135. $test_line = str_replace('&nbsp;', '', $line);
  136. if (strpos($test_line, '&gt;&gt;') === 0) {
  137. $line = "<FONT COLOR=FF0000>$line</FONT>\n";
  138. } else if (strpos($test_line, '&gt;') === 0) {
  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. return $body;
  149. }
  150. /* SquirrelMail version number -- DO NOT CHANGE */
  151. $version = "0.5";
  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. ?>