]> git.openstreetmap.org Git - nominatim.git/blob - lib/init-website.php
Merge remote-tracking branch 'markus/master' into master
[nominatim.git] / lib / init-website.php
1 <?php
2         require_once('init.php');
3
4         if (CONST_NoAccessControl)
5         {
6                 header("Access-Control-Allow-Origin: *");
7                 header("Access-Control-Allow-Methods: OPTIONS,GET");
8                 if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
9                 {
10                         header("Access-Control-Allow-Headers: ".$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
11                 }
12         }
13         if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
14
15         $aBucketKeys = array();
16
17         if (isset($_SERVER["HTTP_REFERER"])) $aBucketKeys[] = str_replace('www.','',strtolower(parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST)));
18         if (isset($_SERVER["REMOTE_ADDR"])) $aBucketKeys[] = $_SERVER["REMOTE_ADDR"];
19         if (isset($_GET["email"])) $aBucketKeys[] = $_GET["email"];
20
21         $fBucketVal = doBucket($aBucketKeys, 
22                         (defined('CONST_ConnectionBucket_PageType')?constant('CONST_ConnectionBucket_Cost_'.CONST_ConnectionBucket_PageType):1) + user_busy_cost(),
23                         CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
24
25         if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
26         {
27                 $m = getBucketMemcache();
28                 $iCurrentSleeping = $m->increment('sleepCounter');
29                 if (false === $iCurrentSleeping)
30                 {
31                         $m->add('sleepCounter', 0);
32                         $iCurrentSleeping = $m->increment('sleepCounter');
33                 }
34                 if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping || isBucketSleeping($aBucketKeys))
35                 {
36                         // Too many threads sleeping already.  This becomes a hard block.
37                         $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
38                 }
39                 else
40                 {
41                         setBucketSleeping($aBucketKeys, true);
42                         sleep(($fBucketVal - CONST_ConnectionBucket_WaitLimit)/CONST_ConnectionBucket_LeakRate);
43                         $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
44                         setBucketSleeping($aBucketKeys, false);
45                 }
46                 $m->decrement('sleepCounter');
47         }
48
49         if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
50         {
51                 header("HTTP/1.0 429 Too Many Requests");
52                 echo "Your IP has been blocked. \n";
53                 echo CONST_BlockMessage;
54                 exit;
55         }
56
57         header('Content-type: text/html; charset=utf-8');
58