From ba3adf97f1cd93af42a7be91e00b1823dbfc2064 Mon Sep 17 00:00:00 2001 From: Darko Date: Mon, 8 Jun 2015 10:34:26 +0200 Subject: [PATCH] Refactor getApirequest function. --- newznab/controllers/Users.php | 40 +++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/newznab/controllers/Users.php b/newznab/controllers/Users.php index 273381f20..4a0ab586f 100644 --- a/newznab/controllers/Users.php +++ b/newznab/controllers/Users.php @@ -920,13 +920,18 @@ class Users return $this->pdo->queryOneRow("select * from userroles where isdefault = 1"); } - public function getApiRequests($userid) + /** + * Get the quantity of API requests in the last day for the user_id. + * + * @param int $userID + * + * @return array|bool + */ + public function getApiRequests($userID) { - //clear old requests - //$this->pdo->queryExec(sprintf("DELETE FROM userrequests WHERE (userid = %d AND timestamp < DATE_SUB(NOW(), INTERVAL 1 DAY))", $userid)); - $this->pdo->queryExec(sprintf("DELETE FROM userrequests WHERE timestamp < DATE_SUB(NOW(), INTERVAL 1 DAY)", $userid)); - $sql = sprintf("select COUNT(id) as num, TIME_TO_SEC(TIMEDIFF(DATE_ADD(MIN(TIMESTAMP), INTERVAL 1 DAY), NOW())) AS nextrequest FROM userrequests WHERE userid = %d AND timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)", $userid); - return $this->pdo->queryOneRow($sql); + // Clear old requests. + $this->clearApiRequests($userID); + return $this->pdo->queryOneRow(sprintf('SELECT COUNT(id) AS num FROM userrequests WHERE userid = %d', $userID)); } /** @@ -948,6 +953,29 @@ class Users ); } + /** + * Delete api requests older than a day. + * + * @param int|bool $userID + * int The users ID. + * bool false do all user ID's.. + * + * @return void + */ + protected function clearApiRequests($userID) + { + if ($userID === false) { + $this->pdo->queryExec('DELETE FROM userrequests WHERE timestamp < DATE_SUB(NOW(), INTERVAL 1 DAY)'); + } else { + $this->pdo->queryExec( + sprintf( + 'DELETE FROM userrequests WHERE userid = %d AND timestamp < DATE_SUB(NOW(), INTERVAL 1 DAY)', + $userID + ) + ); + } + } + /** * deletes old rows from the userrequest and userdownloads tables. * if site->userdownloadpurgedays set to 0 then all release history is removed but