]> git.openstreetmap.org Git - nominatim.git/blob - lib/init-website.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib / init-website.php
1 <?php
2     require_once('init.php');
3
4     header('Content-type: text/html; charset=utf-8');
5
6     // check blocks in place for external servers
7     if (strpos($_SERVER["REMOTE_ADDR"],'193.63.75.') !== 0 &&
8         strpos(CONST_WhitelistedIPs, ','.$_SERVER["REMOTE_ADDR"].',') === false)
9     {
10
11         $aBucketKeys = array();
12
13         if (isset($_SERVER["HTTP_REFERER"])) $aBucketKeys[] = str_replace('www.','',strtolower(parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST)));
14         if (isset($_SERVER["REMOTE_ADDR"])) $aBucketKeys[] = $_SERVER["REMOTE_ADDR"];
15         if (isset($_GET["email"])) $aBucketKeys[] = $_GET["email"];
16
17         $fBucketVal = doBucket($aBucketKeys, 
18                         (defined('CONST_ConnectionBucket_PageType')?constant('CONST_ConnectionBucket_Cost_'.CONST_ConnectionBucket_PageType):1) + user_busy_cost(),
19                         CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
20
21         if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
22         {
23                 $m = getBucketMemcache();
24                 $iCurrentSleeping = $m->increment('sleepCounter');
25                 if (false === $iCurrentSleeping)
26                 {
27                         $m->add('sleepCounter', 0);
28                         $iCurrentSleeping = $m->increment('sleepCounter');
29                 }
30                 if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping || isBucketSleeping($aBucketKeys))
31                 {
32                         // Too many threads sleeping already.  This becomes a hard block.
33                         $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
34                 }
35                 else
36                 {
37                         setBucketSleeping($aBucketKeys, true);
38                         sleep(($fBucketVal - CONST_ConnectionBucket_WaitLimit)/CONST_ConnectionBucket_LeakRate);
39                         $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
40                         setBucketSleeping($aBucketKeys, false);
41                 }
42                 $m->decrement('sleepCounter');
43         }
44
45         if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
46         {
47                 echo "Your IP has been blocked. \n";
48                 echo "Please create a nominatim trac ticket (http://trac.openstreetmap.org/newticket?component=nominatim) to request this to be removed. \n";
49                 echo "Information on the Nominatim usage policy can be found here: http://wiki.openstreetmap.org/wiki/Nominatim#Usage_Policy \n";
50                 exit;
51         }