2 require_once('init.php');
3 require_once('website.php');
5 if (CONST_NoAccessControl)
7 header("Access-Control-Allow-Origin: *");
8 header("Access-Control-Allow-Methods: OPTIONS,GET");
9 if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
11 header("Access-Control-Allow-Headers: ".$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
14 if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
16 $aBucketKeys = array();
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"];
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);
26 if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
28 $m = getBucketMemcache();
29 $iCurrentSleeping = $m->increment('sleepCounter');
30 if (false === $iCurrentSleeping)
32 $m->add('sleepCounter', 0);
33 $iCurrentSleeping = $m->increment('sleepCounter');
35 if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping || isBucketSleeping($aBucketKeys))
37 // Too many threads sleeping already. This becomes a hard block.
38 $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
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);
47 $m->decrement('sleepCounter');
50 if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
52 header("HTTP/1.0 429 Too Many Requests");
53 echo "Your IP has been blocked. \n";
54 echo CONST_BlockMessage;
58 header('Content-type: text/html; charset=utf-8');