d809e6f5a5b1ab9d2b96644248f8857cd56ea3e6
[brisk.git] / web / Obj / proxyscan.phh
1 <?php
2 /**
3  *      Proxy Detector v0.1
4  *              copyrights by: Daantje Eeltink (me@daantje.nl)
5  *                                              http://www.daantje.nl
6  *
7  *              first build: Mon Sep 18 21:43:48 CEST 2006
8  *              last build: Tue Sep 19 10:37:12 CEST 2006
9  *
10  *      Description:
11  *              This class can detect if a visitor uses a proxy server by scanning the
12  *              headers returned by the user client. When the user uses a proxy server,
13  *              most of the proxy servers alter the header. The header is returned to
14  *              PHP in the array $_SERVER.
15  *
16  *      License:
17  *              GPL v2 licence. (http://www.gnu.org/copyleft/gpl.txt)
18  *
19  *      Support:
20  *              If you like this class and find it usefull, please donate one or two
21  *              coins to my PayPal account me@daantje.nl
22  *
23  *      Todo:
24  *              Add open proxy black list scan.
25  */
26
27 class proxy_detector {
28
29   /**
30    * CONSTRUCTOR
31    *    Set defaults...
32    */
33   function proxy_detector(){
34     $this->config = array();
35     $this->lastLog = "";
36
37     //set default headers
38     $this->scan_headers = array(
39                                 'HTTP_VIA',
40                                 'HTTP_X_FORWARDED_FOR',
41                                 'HTTP_FORWARDED_FOR',
42                                 'HTTP_X_FORWARDED',
43                                 'HTTP_FORWARDED',
44                                 'HTTP_CLIENT_IP',
45                                 'HTTP_FORWARDED_FOR_IP',
46                                 'VIA',
47                                 'X_FORWARDED_FOR',
48                                 'FORWARDED_FOR',
49                                 'X_FORWARDED',
50                                 'FORWARDED',
51                                 'CLIENT_IP',
52                                 'FORWARDED_FOR_IP',
53                                 'HTTP_PROXY_CONNECTION'
54                                 );
55   }
56
57   function exists_in_rbl($remote) {
58     $rbls = array('http.dnsbl.sorbs.net', 'misc.dnsbl.sorbs.net');
59     //    $remote = $_SERVER['REMOTE_ADDR'];
60     // $remote = '213.134.170.206';
61     // $remote = '64.34.166.71';
62     
63     if (preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/",
64                    $remote, $matches)) {
65       foreach ($rbls as $rbl) {
66         $rblhost = $matches[4] . "." . $matches[3] . "." .
67           $matches[2] . "." . $matches[1] . "." . $rbl;
68         
69         $resolved = gethostbyname($rblhost);
70         // echo "RBL ".$rblhost."<br>";
71         if ($resolved != $rblhost) {
72           return true;
73         }
74       }
75     }
76     return false;
77   }
78   
79   /**
80    * VOID setHeader( STRING $trigger )
81    *    Set new header trigger...
82    */
83   function setHeader($trigger){
84     $this->scan_headers[] = $trigger;
85   }
86
87
88   /**
89    * ARRAY $triggers = getHeaders( VOID )
90    *    Get all triggers in one array
91    */
92   function getHeaders(){
93     return $this->scan_headers;
94   }
95
96
97   /**
98    * VOID setConfig( STRING $key,  STRING $value)
99    *    Set config line...
100    */
101   function setConfig($key,$value){
102     $this->config[$key] = $value;
103   }
104
105
106   /**
107    * MIXED $config = getConfig( [STRING $key] )
108    *    Get all config in one array, or only one config value as a string.
109    */
110   function getConfig($key=''){
111     if($key)
112       return $this->config[$key];
113     else
114       return $this->config;
115   }
116
117
118   /**
119    * STRING $log = getLog( VOID )
120    *    Get last logged information. Only works AFTER calling detect()!
121    */
122   function getLog(){
123     return $this->lastLog;
124   }
125
126
127   /**
128    * BOOL $proxy = detect( VOID )
129    *    Start detection and return true if a proxy server is detected...
130    */
131   function detect(){
132     $log = "";
133
134
135     if ($this->exists_in_rbl($_SERVER['REMOTE_ADDR']) == TRUE)
136       return (TRUE);
137
138     //scan all headers
139     foreach($this->scan_headers as $i){
140       //proxy detected? lets log...
141       if($_SERVER[$i])
142         $log.= "trigger $i: ".$_SERVER[$i]."\n";
143     }
144
145     //let's do something...
146     if($log){
147       $log = $this->lastLog = date("Y-m-d H:i:s")."\nDetected proxy server: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])." ({$_SERVER['REMOTE_ADDR']})\n".$log;
148
149       //mail message
150       if($this->getConfig('MAIL_ALERT_TO'))
151         mail($this->getConfig('MAIL_ALERT_TO'),"Proxy detected at {$_SERVER['REQUEST_URI']}",$log);
152
153       //write to file
154       $f = $this->getConfig('LOG_FILE');
155       if($f){
156         if(is_writable($f)){
157           $fp = fopen($f,'a');
158           fwrite($fp,"$log\n");
159           fclose($fp);
160         }else{
161           die("<strong>Fatal Error:</strong> Couldn't write to file: '<strong>$f</strong>'<br>Please check if the path exists and is writable for the webserver or php...");
162         }
163       }
164
165       //done
166       return true;
167     }
168
169     //nope, no proxy was logged...
170     return false;
171   }
172 }
173
174 //init class
175 function is_proxy()
176 {
177   $proxy = new proxy_detector();
178   
179   //start detect
180   if($proxy->detect()) {
181     //returned true, lets die...
182     echo "<br><br><div style=\"text-align:center;\"><h1>Proxy detected</h1><br><br>";
183     echo "Please disable your proxy server in your browser preferences or internet settings, and try again.<br><br></div>";
184     
185     //parse logged info
186     echo nl2br($proxy->getLog());
187     
188     //some credits...
189     // echo "<hr><strong>proxy detector v0.1</strong> - &copy;2006 <a href=\"http://www.daantje.nl\" target=\"_blank\">daantje.nl</a>";
190     
191     //and do nothing anymore! (but not in my example)
192     return (TRUE);
193   }
194   else
195     return (FALSE);
196 }
197
198 ?>