diff --git a/classes/class_cache.php b/classes/class_cache.php index d77aa1a1..203d86b6 100644 --- a/classes/class_cache.php +++ b/classes/class_cache.php @@ -41,7 +41,8 @@ class CACHE extends Memcache { 'query_lock_*', 'top10votes_*', // 'similar_albums_*', - 'users_snatched_*' + 'users_snatched_*', + 'ajax_requests_*' ); public $CanClear = false; @@ -148,6 +149,12 @@ public function delete_value($Key) { } $this->Time+=(microtime(true)-$StartTime)*1000; } + + public function increment_value($Key,$Value=1) { + $StartTime=microtime(true); + $this->increment($Key,$Value); + $this->Time+=(microtime(true)-$StartTime)*1000; + } //---------- memcachedb functions ----------// diff --git a/sections/ajax/index.php b/sections/ajax/index.php index b0999d00..9c1e72d8 100644 --- a/sections/ajax/index.php +++ b/sections/ajax/index.php @@ -10,8 +10,33 @@ enforce_login(); +/* AJAX_LIMIT = array(x,y) = 'x' requests every 'y' seconds. + e.g. array(5,10) = 5 requests every 10 seconds */ +$AJAX_LIMIT = array(5,10); +$Limited_Pages = array('tcomments','user','forum','top10','browse','usersearch','requests','artist','inbox','subscriptions','bookmarks','announcements','notifications','request','better','similar_artists','userhistory','votefavorite','wiki'); + header('Content-Type: application/json; charset=utf-8'); +// Enforce rate limiting everywhere except info.php +if (isset($_GET['action']) && in_array($_GET['action'],$Limited_Pages)) { + if (!$userrequests = $Cache->get_value('ajax_requests_'.$UserID)) { + $userrequests = 0; + $Cache->cache_value('ajax_requests_'.$UserID,'0',$AJAX_LIMIT[1]); + } + if ($userrequests > $AJAX_LIMIT[0]) { + print json_encode( + array( + 'status' => 'failure', + 'response' => 'Rate limit exceeded.' + ) + ); + + die(); + } else { + $Cache->increment_value('ajax_requests_'.$UserID); + } +} + switch ($_GET['action']) { // things that (may be) used on the site case 'upload_section': @@ -41,6 +66,9 @@ case 'torrentgroup': require('torrentgroup.php'); break; + case 'torrentgroupalbumart': // so the album art script can function without breaking the ratelimit + require(SERVER_ROOT . '/sections/ajax/torrentgroupalbumart.php'); + break; case 'tcomments': require(SERVER_ROOT . '/sections/ajax/tcomments.php'); break; diff --git a/sections/ajax/torrentgroupalbumart.php b/sections/ajax/torrentgroupalbumart.php new file mode 100644 index 00000000..0fb68725 --- /dev/null +++ b/sections/ajax/torrentgroupalbumart.php @@ -0,0 +1,21 @@ + 'success', + 'response' => array( + 'wikiImage' => $Image + ) + ) +); diff --git a/sections/donate/donate.php b/sections/donate/donate.php index f1d88959..f6c2bb6f 100644 --- a/sections/donate/donate.php +++ b/sections/donate/donate.php @@ -72,7 +72,7 @@ -