4
0

stats.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?
  2. #### stats.php
  3. #
  4. ## a little script to relay stats to your normal homepage
  5. #
  6. # Configuration
  7. # the server where stats.mod is running (you can use "localhost" if
  8. # the stats.php is located on the same server)
  9. $STATSERVER = "broken.eggheads.org";
  10. # the port
  11. $STATPORT = 8033;
  12. # time to wait before the script gives up
  13. $STATTIMEOUT = 10;
  14. ###################################################################
  15. ###################################################################
  16. #
  17. ## Don't touch anything below unless you know what you are doing!
  18. #
  19. #
  20. #
  21. $MAXLEN = 4096;
  22. $statpath = $HTTP_GET_VARS["statpath"];
  23. $password = $HTTP_POST_VARS["password"];
  24. $languages = $HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"];
  25. $useragent = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];
  26. $xforwardedfor = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
  27. $remoteaddr = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  28. $cookies = $HTTP_SERVER_VARS["HTTP_COOKIE"];
  29. $scriptname = $HTTP_SERVER_VARS["SCRIPT_NAME"];
  30. $postparams = "";
  31. if ($statpath == "") {
  32. $statpath = $HTTP_SERVER_VARS["PATH_INFO"];
  33. if ($statpath == "") {
  34. $statpath = "/";
  35. }
  36. }
  37. function my_array_keys ($arr, $term="") {
  38. $t = array();
  39. while (list($k,$v) = each($arr)) {
  40. if ($term && $v != $term)
  41. continue;
  42. $t[] = $k;
  43. }
  44. return $t;
  45. }
  46. if ($password != "") {
  47. $keys = my_array_keys($HTTP_POST_VARS);
  48. for ($i = 0; $i < count($keys); $i++) {
  49. $param = urlencode($HTTP_POST_VARS[$keys[$i]]);
  50. $postparams .= "&$keys[$i]=$param";
  51. }
  52. $postparams = substr($postparams, 1);
  53. }
  54. function transform_url($url) {
  55. global $scriptname;
  56. global $statpath;
  57. if (!strcasecmp(substr($url, 0, 7), "http://") ||
  58. !strcasecmp(substr($url, 0, 7), "mailto:") ||
  59. !strcasecmp(substr($url, 0, 6), "ftp://")) {
  60. // global URL, don't touch it
  61. $retstr = $url;
  62. } else if ($url[0] == "/") {
  63. // absolute URL, simply remap it
  64. $retstr = "$scriptname$url";
  65. } else {
  66. // relative URL, process any '../'s
  67. $parts = explode("/", $url);
  68. $backs = 0;
  69. $newurl = "";
  70. for ($i = 0; $i < count($parts); $i++) {
  71. if (!strcasecmp($parts[$i], "..")) {
  72. $backs++;
  73. } else {
  74. $tmp = "$newurl/$parts[$i]";
  75. $newurl = $tmp;
  76. }
  77. }
  78. $newurl = substr($newurl, 1);
  79. $parts = explode("/", $statpath);
  80. $newpath = "";
  81. for ($i = 0; $i < (count($parts) - $backs - 1); $i++) {
  82. $tmp = "${newpath}/$parts[$i]";
  83. $newpath = $tmp;
  84. }
  85. $newpath = substr($newpath, 1);
  86. $retstr = "$scriptname$newpath/$newurl";
  87. }
  88. return $retstr;
  89. }
  90. function do_transform($line) {
  91. $words = explode(" ", $line);
  92. for ($i = 0; $i < count($words); $i++) {
  93. if (!strcasecmp(substr($words[$i], 0, 5), "href=")) {
  94. $urlq = strstr($words[$i], "\"");
  95. $urlq = substr($urlq, 1);
  96. $urlparts = explode("\"", $urlq);
  97. $rest = $urlparts[1];
  98. $newurl = transform_url($urlparts[0]);
  99. echo "href=\"$newurl\"$rest ";
  100. } else if (!strcasecmp(substr($words[$i], 0, 7), "action=")) {
  101. $urlq = strstr($words[$i], "\"");
  102. $urlq = substr($urlq, 1);
  103. $urlparts = explode("\"", $urlq);
  104. $rest = $urlparts[1];
  105. $newurl = transform_url($urlparts[0]);
  106. echo "action=\"$newurl\"$rest ";
  107. } else {
  108. echo "$words[$i] ";
  109. }
  110. }
  111. }
  112. $httpbody = false;
  113. $idx = fsockopen($STATSERVER, $STATPORT, $errno, $errstr, $STATTIMEOUT);
  114. if (!$idx) {
  115. echo "<html><head><title>$errno: $errstr</title></head>";
  116. echo "<body><H1>Connection to statistics server FAILED!</H1><br>";
  117. echo "$errno: $errstr";
  118. } else {
  119. if ($postparams == "") {
  120. fputs ($idx, "GET $statpath HTTP/1.0\r\n");
  121. } else {
  122. $len = strlen($postparams);
  123. fputs ($idx, "POST $statpath HTTP/1.0\r\n");
  124. fputs ($idx, "Content-Length: $len\r\n");
  125. }
  126. if ($languages != "") {
  127. fputs ($idx, "Accept-Language: $languages\r\n");
  128. }
  129. if ($useragent != "") {
  130. fputs ($idx, "User-Agent: $useragent\r\n");
  131. }
  132. if ($remoteaddr != "") {
  133. fputs ($idx, "X-Relayed-For: $remoteaddr\r\n");
  134. }
  135. if ($xforwardedfor != "") {
  136. fputs ($idx, "X-Forwarded-For: $xforwardedfor\r\n");
  137. }
  138. if ($cookies != "") {
  139. fputs ($idx, "Cookie: $cookies\r\n");
  140. }
  141. fputs ($idx, "\r\n");
  142. if ($postparams != "") {
  143. fputs ($idx, $postparams);
  144. }
  145. while (!feof($idx)) {
  146. $buf = fgets($idx, $MAXLEN);
  147. if ($httpbody) {
  148. do_transform($buf);
  149. } else if ($buf == "" || $buf == "\n") {
  150. $httpbody = true;
  151. } else {
  152. $buf = eregi_replace("Location: ", "Location: $scriptname?statpath=", $buf);
  153. header($buf);
  154. }
  155. }
  156. fclose($idx);
  157. }
  158. ########### old regex experiments
  159. // echo eregi_replace("href=\"(\/.*\/)*\"/", "relay.php?statpath=", $buf);
  160. // echo eregi_replace("href=\"(http\:\/\/{0,0})", "relay.php?statpath=", $buf);
  161. // $buf = eregi_replace("href=\"", "href=\"relay.php?statpath=", $buf);
  162. // $buf = eregi_replace("(href=\")([^\"]*)(\")", "href=\"$scriptname.php?path=$statpath{transform_remote_url(\\2)}\"", $buf);
  163. // $buf = eregi_replace("(href=\"){1,}", "href=\"$scriptname?statpath=$statpath", $buf);
  164. // $buf = eregi_replace("(href=\")[[:alpha:]]*[^\ ]*http\:\/\/", "href=\"http://", $buf);
  165. // echo $buf;
  166. // echo pregi_replace("href=\"(^http)", "href=\"relay.php?statpath=", $buf);
  167. // for ($i = 0; $i < strlen($buf); $i++) {
  168. // if (!strncasecmp($buf[$i], "href=\"", 6) && !(!strncasecmp($buf[$i], "href=\"http://", 13)) {
  169. // echo "href=\"$scriptname?statpath=$statpath/";
  170. // $i = $i + 5;
  171. // } else {
  172. // echo $buf[i];
  173. // }
  174. // }
  175. // echo eregi_replace("(href\=\")(http\:\/\/){,0}", "href=\"$scriptname?statpath=", $buf);
  176. // echo $buf;
  177. ?>