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