]> git.openstreetmap.org Git - nominatim.git/blob - lib/init-website.php
move looksLikeLatLonPair into lib.php, basic PHP test suite using phpunit
[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         if (CONST_ClosedForIndexing && strpos(CONST_ClosedForIndexingExceptionIPs, ','.$_SERVER["REMOTE_ADDR"].',') === false)
16         {
17                 echo "Closed for re-indexing...";
18                 exit;
19         }
20
21         $aBucketKeys = array();
22
23         if (isset($_SERVER["HTTP_REFERER"])) $aBucketKeys[] = str_replace('www.','',strtolower(parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST)));
24         if (isset($_SERVER["REMOTE_ADDR"])) $aBucketKeys[] = $_SERVER["REMOTE_ADDR"];
25         if (isset($_GET["email"])) $aBucketKeys[] = $_GET["email"];
26
27         $fBucketVal = doBucket($aBucketKeys, 
28                         (defined('CONST_ConnectionBucket_PageType')?constant('CONST_ConnectionBucket_Cost_'.CONST_ConnectionBucket_PageType):1) + user_busy_cost(),
29                         CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
30
31         if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
32         {
33                 $m = getBucketMemcache();
34                 $iCurrentSleeping = $m->increment('sleepCounter');
35                 if (false === $iCurrentSleeping)
36                 {
37                         $m->add('sleepCounter', 0);
38                         $iCurrentSleeping = $m->increment('sleepCounter');
39                 }
40                 if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping || isBucketSleeping($aBucketKeys))
41                 {
42                         // Too many threads sleeping already.  This becomes a hard block.
43                         $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
44                 }
45                 else
46                 {
47                         setBucketSleeping($aBucketKeys, true);
48                         sleep(($fBucketVal - CONST_ConnectionBucket_WaitLimit)/CONST_ConnectionBucket_LeakRate);
49                         $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
50                         setBucketSleeping($aBucketKeys, false);
51                 }
52                 $m->decrement('sleepCounter');
53         }
54
55         if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
56         {
57                 header("HTTP/1.0 429 Too Many Requests");
58                 echo "Your IP has been blocked. \n";
59                 echo CONST_BlockMessage;
60                 exit;
61         }
62
63         header('Content-type: text/html; charset=utf-8');
64