strings.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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;
  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. // Get the hostname from the Host header or server config.
  187. // Fallback is to omit the server name and use a relative URI,
  188. // although this is not RFC 2616 compliant.
  189. if(isset($HTTP_HOST) && !empty($HTTP_HOST)) {
  190. $location = $proto . $HTTP_HOST . $path;
  191. } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
  192. $location = $proto . $SERVER_NAME . $path;
  193. } else {
  194. $location = $path;
  195. }
  196. return $location;
  197. }
  198. function sqStripSlashes($string) {
  199. if (get_magic_quotes_gpc()) {
  200. $string = stripslashes($string);
  201. }
  202. return $string;
  203. }
  204. // These functions are used to encrypt the passowrd before it is
  205. // stored in a cookie.
  206. function OneTimePadEncrypt ($string, $pad) {
  207. for ($i = 0; $i < strlen ($string); $i++) {
  208. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  209. }
  210. return base64_encode($encrypted);
  211. }
  212. function OneTimePadDecrypt ($string, $pad) {
  213. $encrypted = base64_decode ($string);
  214. for ($i = 0; $i < strlen ($encrypted); $i++) {
  215. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  216. }
  217. return $decrypted;
  218. }
  219. // Randomize the mt_rand() function. Toss this in strings or
  220. // integers and it will seed the generator appropriately.
  221. // With strings, it is better to get them long. Use md5() to
  222. // lengthen smaller strings.
  223. function sq_mt_seed($Val)
  224. {
  225. // if mt_getrandmax() does not return a 2^n - 1 number,
  226. // this might not work well. This uses $Max as a bitmask.
  227. $Max = mt_getrandmax();
  228. if (! is_int($Val))
  229. {
  230. if (function_exists("crc32"))
  231. {
  232. $Val = crc32($Val);
  233. }
  234. else
  235. {
  236. $Str = $Val;
  237. $Pos = 0;
  238. $Val = 0;
  239. $Mask = $Max / 2;
  240. $HighBit = $Max ^ $Mask;
  241. while ($Pos < strlen($Str))
  242. {
  243. if ($Val & $HighBit)
  244. {
  245. $Val = (($Val & $Mask) << 1) + 1;
  246. }
  247. else
  248. {
  249. $Val = ($Val & $Mask) << 1;
  250. }
  251. $Val ^= $Str[$Pos];
  252. $Pos ++;
  253. }
  254. }
  255. }
  256. if ($Val < 0)
  257. $Val *= -1;
  258. if ($Val = 0)
  259. return;
  260. mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
  261. }
  262. // This function initializes the random number generator fairly well.
  263. // It also only initializes it once, so you don't accidentally get
  264. // the same 'random' numbers twice in one session.
  265. function sq_mt_randomize()
  266. {
  267. global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
  268. static $randomized;
  269. if ($randomized)
  270. return;
  271. // Global
  272. sq_mt_seed((int)((double) microtime() * 1000000));
  273. sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
  274. // getrusage
  275. if (function_exists("getrusage")) {
  276. $dat = getrusage();
  277. sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] .
  278. $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
  279. }
  280. // Apache-specific
  281. sq_mt_seed(md5($UNIQUE_ID));
  282. $randomized = 1;
  283. }
  284. function OneTimePadCreate ($length=100) {
  285. sq_mt_randomize();
  286. for ($i = 0; $i < $length; $i++) {
  287. $pad .= chr(mt_rand(0,255));
  288. }
  289. return $pad;
  290. }
  291. // Check if we have a required PHP-version. Return TRUE if we do,
  292. // or FALSE if we don't.
  293. // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
  294. // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
  295. // Does not handle betas like 4.0.1b1 or development versions
  296. function sqCheckPHPVersion($major, $minor, $release) {
  297. $ver = phpversion();
  298. eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
  299. // Parse the version string
  300. $vmajor = strval($regs[1]);
  301. $vminor = strval($regs[2]);
  302. $vrel = $regs[3];
  303. if($vrel[0] == ".")
  304. $vrel = strval(substr($vrel, 1));
  305. if($vrel[0] == "b" || $vrel[0] == "B")
  306. $vrel = - strval(substr($vrel, 1));
  307. if($vrel[0] == "r" || $vrel[0] == "R")
  308. $vrel = - strval(substr($vrel, 2))/10;
  309. // Compare major version
  310. if($vmajor < $major) return false;
  311. if($vmajor > $major) return true;
  312. // Major is the same. Compare minor
  313. if($vminor < $minor) return false;
  314. if($vminor > $minor) return true;
  315. // Major and minor is the same as the required one.
  316. // Compare release
  317. if($vrel >= 0 && $release >= 0) { // Neither are beta
  318. if($vrel < $release) return false;
  319. } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
  320. return true;
  321. } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
  322. return false;
  323. } else { // Both are beta
  324. if($vrel > $release) return false;
  325. }
  326. return true;
  327. }
  328. ?>