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 $aBucketKeys = array();
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"];
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);
25 if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
27 $m = getBucketMemcache();
28 $iCurrentSleeping = $m->increment('sleepCounter');
29 if (false === $iCurrentSleeping)
31 $m->add('sleepCounter', 0);
32 $iCurrentSleeping = $m->increment('sleepCounter');
34 if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping || isBucketSleeping($aBucketKeys))
36 // Too many threads sleeping already. This becomes a hard block.
37 $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
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);
46 $m->decrement('sleepCounter');
49 if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
51 header("HTTP/1.0 429 Too Many Requests");
52 echo "Your IP has been blocked. \n";
53 echo CONST_BlockMessage;
57 header('Content-type: text/html; charset=utf-8');