2 require_once('init.php');
4 if (CONST_NoAccessControl)
6 header("Access-Control-Allow-Origin: *");
7 header("Access-Control-Allow-Methods: OPTIONS,GET");
8 if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
10 header("Access-Control-Allow-Headers: ".$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
13 if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
15 if (CONST_ClosedForIndexing && strpos(CONST_ClosedForIndexingExceptionIPs, ','.$_SERVER["REMOTE_ADDR"].',') === false)
17 echo "Closed for re-indexing...";
21 $aBucketKeys = array();
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"];
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);
31 if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
33 $m = getBucketMemcache();
34 $iCurrentSleeping = $m->increment('sleepCounter');
35 if (false === $iCurrentSleeping)
37 $m->add('sleepCounter', 0);
38 $iCurrentSleeping = $m->increment('sleepCounter');
40 if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping || isBucketSleeping($aBucketKeys))
42 // Too many threads sleeping already. This becomes a hard block.
43 $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
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);
52 $m->decrement('sleepCounter');
55 if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
57 echo "Your IP has been blocked. \n";
58 echo CONST_BlockMessage;
62 header('Content-type: text/html; charset=utf-8');