mirror of
https://github.com/beestat/app.git
synced 2025-07-09 03:04:07 -04:00
Renamed api_log2 to api_log
This commit is contained in:
parent
e0f901e968
commit
6ea48160e9
@ -8,55 +8,51 @@ namespace cora;
|
|||||||
*
|
*
|
||||||
* @author Jon Ziebell
|
* @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
|
* Insert an item into the api_log resource. Force the IP to the request IP
|
||||||
* and disallow overriding the timestamp.
|
* and disallow overriding the timestamp.
|
||||||
*
|
*
|
||||||
* @param array $attributes The attributes to insert.
|
* @param array $attributes The attributes to insert.
|
||||||
*
|
*
|
||||||
* @return int The ID of the inserted row.
|
* @return int The ID of the inserted row.
|
||||||
*/
|
*/
|
||||||
public function create($attributes) {
|
public function create($attributes) {
|
||||||
$attributes['request_ip'] = ip2long($_SERVER['REMOTE_ADDR']);
|
// Insert using the transactionless connection.
|
||||||
$attributes['user_id'] = $this->session->get_user_id();
|
$database = database::get_transactionless_instance();
|
||||||
unset($attributes['request_timestamp']);
|
|
||||||
|
|
||||||
// Insert using the transactionless connection.
|
|
||||||
$database = database::get_transactionless_instance();
|
|
||||||
return $database->create($this->resource, $attributes);
|
return $database->create($this->resource, $attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of requests since a given timestamp for a given IP
|
* Get the number of requests since a given timestamp for a given IP
|
||||||
* address. Handy for rate limiting.
|
* address. Handy for rate limiting.
|
||||||
*
|
*
|
||||||
* @param string $request_ip The IP to look at.
|
* @param string $ip_address The IP to look at.
|
||||||
* @param int $timestamp The timestamp to check from.
|
* @param int $timestamp The timestamp to check from.
|
||||||
*
|
*
|
||||||
* @return int The number of requests on or after $timestamp.
|
* @return int The number of requests on or after $timestamp.
|
||||||
*/
|
*/
|
||||||
public function get_number_requests_since($request_ip, $timestamp) {
|
public function get_number_requests_since($ip_address, $timestamp) {
|
||||||
$request_ip_escaped = $this->database->escape(ip2long($request_ip));
|
$ip_address_escaped = $this->database->escape(ip2long($ip_address));
|
||||||
$timestamp_escaped = $this->database->escape(
|
$timestamp_escaped = $this->database->escape(
|
||||||
date('Y-m-d H:i:s', $timestamp)
|
date('Y-m-d H:i:s', $timestamp)
|
||||||
);
|
);
|
||||||
|
|
||||||
$query = '
|
$query = '
|
||||||
select
|
select
|
||||||
count(*) `number_requests_since`
|
count(*) `number_requests_since`
|
||||||
from
|
from
|
||||||
`api_log`
|
`api_log`
|
||||||
where
|
where
|
||||||
`request_ip` = ' . $request_ip_escaped . '
|
`ip_address` = ' . $ip_address_escaped . '
|
||||||
and `request_timestamp` >= ' . $timestamp_escaped . '
|
and `timestamp` >= ' . $timestamp_escaped . '
|
||||||
';
|
';
|
||||||
|
|
||||||
$result = $this->database->query($query);
|
$result = $this->database->query($query);
|
||||||
$row = $result->fetch_assoc();
|
$row = $result->fetch_assoc();
|
||||||
|
|
||||||
return $row['number_requests_since'];
|
return $row['number_requests_since'];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace cora;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores a log of API requests and responses. Intended usage is to process
|
|
||||||
* the request to the end (exception or not) and then log it.
|
|
||||||
*
|
|
||||||
* @author Jon Ziebell
|
|
||||||
*/
|
|
||||||
final class api_log2 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) {
|
|
||||||
// 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 $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'];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -224,7 +224,7 @@ final class request {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$api_log_resource = new api_log2();
|
$api_log_resource = new api_log();
|
||||||
$requests_this_minute = $api_log_resource->get_number_requests_since(
|
$requests_this_minute = $api_log_resource->get_number_requests_since(
|
||||||
$_SERVER['REMOTE_ADDR'],
|
$_SERVER['REMOTE_ADDR'],
|
||||||
(time() - 60)
|
(time() - 60)
|
||||||
@ -287,7 +287,7 @@ final class request {
|
|||||||
$database = database::get_instance();
|
$database = database::get_instance();
|
||||||
$session = session::get_instance();
|
$session = session::get_instance();
|
||||||
$setting = setting::get_instance();
|
$setting = setting::get_instance();
|
||||||
$api_log_resource = new api_log2();
|
$api_log_resource = new api_log();
|
||||||
|
|
||||||
// If exception.
|
// If exception.
|
||||||
if(isset($this->response['data']['error_code']) === true) {
|
if(isset($this->response['data']['error_code']) === true) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user