From 6ea48160e905887e015105a63d8e8e6ea945a197 Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Sun, 15 Mar 2020 21:09:46 -0400 Subject: [PATCH] Renamed api_log2 to api_log --- api/cora/api_log.php | 84 +++++++++++++++++++++---------------------- api/cora/api_log2.php | 58 ------------------------------ api/cora/request.php | 4 +-- 3 files changed, 42 insertions(+), 104 deletions(-) delete mode 100644 api/cora/api_log2.php diff --git a/api/cora/api_log.php b/api/cora/api_log.php index 7e24e18..3d581e0 100644 --- a/api/cora/api_log.php +++ b/api/cora/api_log.php @@ -8,55 +8,51 @@ namespace cora; * * @author Jon Ziebell */ -class api_log extends crud { +final class api_log extends crud { - /** - * Insert an item into the api_log resource. Force the IP to the request IP - * and disallow overriding the timestamp. - * - * @param array $attributes The attributes to insert. - * - * @return int The ID of the inserted row. - */ - public function create($attributes) { - $attributes['request_ip'] = ip2long($_SERVER['REMOTE_ADDR']); - $attributes['user_id'] = $this->session->get_user_id(); - unset($attributes['request_timestamp']); - - // Insert using the transactionless connection. - $database = database::get_transactionless_instance(); + /** + * Insert an item into the api_log resource. Force the IP to the request IP + * and disallow overriding the timestamp. + * + * @param array $attributes The attributes to insert. + * + * @return int The ID of the inserted row. + */ + public function create($attributes) { + // Insert using the transactionless connection. + $database = database::get_transactionless_instance(); return $database->create($this->resource, $attributes); - } + } - /** - * Get the number of requests since a given timestamp for a given IP - * address. Handy for rate limiting. - * - * @param string $request_ip The IP to look at. - * @param int $timestamp The timestamp to check from. - * - * @return int The number of requests on or after $timestamp. - */ - public function get_number_requests_since($request_ip, $timestamp) { - $request_ip_escaped = $this->database->escape(ip2long($request_ip)); - $timestamp_escaped = $this->database->escape( - date('Y-m-d H:i:s', $timestamp) - ); + /** + * Get the number of requests since a given timestamp for a given IP + * address. Handy for rate limiting. + * + * @param string $ip_address The IP to look at. + * @param int $timestamp The timestamp to check from. + * + * @return int The number of requests on or after $timestamp. + */ + public function get_number_requests_since($ip_address, $timestamp) { + $ip_address_escaped = $this->database->escape(ip2long($ip_address)); + $timestamp_escaped = $this->database->escape( + date('Y-m-d H:i:s', $timestamp) + ); - $query = ' - select - count(*) `number_requests_since` - from - `api_log` - where - `request_ip` = ' . $request_ip_escaped . ' - and `request_timestamp` >= ' . $timestamp_escaped . ' - '; + $query = ' + select + count(*) `number_requests_since` + from + `api_log` + where + `ip_address` = ' . $ip_address_escaped . ' + and `timestamp` >= ' . $timestamp_escaped . ' + '; - $result = $this->database->query($query); - $row = $result->fetch_assoc(); + $result = $this->database->query($query); + $row = $result->fetch_assoc(); - return $row['number_requests_since']; - } + return $row['number_requests_since']; + } } diff --git a/api/cora/api_log2.php b/api/cora/api_log2.php deleted file mode 100644 index d00264d..0000000 --- a/api/cora/api_log2.php +++ /dev/null @@ -1,58 +0,0 @@ -create($this->resource, $attributes); - } - - /** - * Get the number of requests since a given timestamp for a given IP - * address. Handy for rate limiting. - * - * @param string $ip_address The IP to look at. - * @param int $timestamp The timestamp to check from. - * - * @return int The number of requests on or after $timestamp. - */ - public function get_number_requests_since($ip_address, $timestamp) { - $ip_address_escaped = $this->database->escape(ip2long($ip_address)); - $timestamp_escaped = $this->database->escape( - date('Y-m-d H:i:s', $timestamp) - ); - - $query = ' - select - count(*) `number_requests_since` - from - `api_log2` - where - `ip_address` = ' . $ip_address_escaped . ' - and `timestamp` >= ' . $timestamp_escaped . ' - '; - - $result = $this->database->query($query); - $row = $result->fetch_assoc(); - - return $row['number_requests_since']; - } - -} diff --git a/api/cora/request.php b/api/cora/request.php index 5737b47..c625a5a 100644 --- a/api/cora/request.php +++ b/api/cora/request.php @@ -224,7 +224,7 @@ final class request { return false; } - $api_log_resource = new api_log2(); + $api_log_resource = new api_log(); $requests_this_minute = $api_log_resource->get_number_requests_since( $_SERVER['REMOTE_ADDR'], (time() - 60) @@ -287,7 +287,7 @@ final class request { $database = database::get_instance(); $session = session::get_instance(); $setting = setting::get_instance(); - $api_log_resource = new api_log2(); + $api_log_resource = new api_log(); // If exception. if(isset($this->response['data']['error_code']) === true) {