francis94c /
ci-rest
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | defined('BASEPATH') OR exit('No direct script access allowed'); |
||
| 4 | |||
| 5 | class RESTModel extends CI_Model { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * [private description] |
||
| 9 | * @var [type] |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 10 | */ |
||
| 11 | private $users_table; |
||
| 12 | /** |
||
| 13 | * [private description] |
||
| 14 | * @var [type] |
||
|
0 ignored issues
–
show
|
|||
| 15 | */ |
||
| 16 | private $users_id_column; |
||
| 17 | /** |
||
| 18 | * [private description] |
||
| 19 | * @var [type] |
||
|
0 ignored issues
–
show
|
|||
| 20 | */ |
||
| 21 | private $users_email_column; |
||
| 22 | /** |
||
| 23 | * [private description] |
||
| 24 | * @var [type] |
||
|
0 ignored issues
–
show
|
|||
| 25 | */ |
||
| 26 | private $users_username_column; |
||
| 27 | /** |
||
| 28 | * [private description] |
||
| 29 | * @var [type] |
||
|
0 ignored issues
–
show
|
|||
| 30 | */ |
||
| 31 | private $users_password_column; |
||
| 32 | /** |
||
| 33 | * [private description] |
||
| 34 | * @var [type] |
||
|
0 ignored issues
–
show
|
|||
| 35 | */ |
||
| 36 | private $api_key_table; |
||
| 37 | /** |
||
| 38 | * [private description] |
||
| 39 | * @var [type] |
||
|
0 ignored issues
–
show
|
|||
| 40 | */ |
||
| 41 | private $api_key_column; |
||
| 42 | /** |
||
| 43 | * [private description] |
||
| 44 | * @var [type] |
||
|
0 ignored issues
–
show
|
|||
| 45 | */ |
||
| 46 | private $api_key_limit_column; |
||
| 47 | /** |
||
| 48 | * [init description] |
||
| 49 | * @param array $config [description] |
||
| 50 | */ |
||
| 51 | public function init(array $config):void { |
||
| 52 | $this->users_table = $config['users_table']; |
||
| 53 | $this->users_id_column = $config['users_id_column']; |
||
| 54 | $this->users_email_column = $config['users_email_column']; |
||
| 55 | $this->users_username_column = $config['users_username_column']; |
||
| 56 | $this->users_password_column = $config['users_password_column']; |
||
| 57 | $this->api_key_table = $config['api_key_table']; |
||
| 58 | $this->api_key_column = $config['api_key_column']; |
||
| 59 | $this->api_key_limit_column = $config['api_key_limit_column']; |
||
| 60 | } |
||
| 61 | /** |
||
| 62 | * [basic_auth description] |
||
| 63 | * @param object $context [description] |
||
| 64 | * @param string $username [description] |
||
| 65 | * @param string $password [description] |
||
| 66 | * @return bool [description] |
||
| 67 | */ |
||
| 68 | public function basicAuth(object &$context, string $username, string $password):bool { |
||
| 69 | // Basic Checks. |
||
| 70 | if ($this->users_table == null || $this->users_email_column == null || $this->users_password_column == null) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | // Database Query. |
||
| 74 | if ($this->users_id_column != null) { |
||
| 75 | $this->db->select($this->users_id_column); |
||
| 76 | } |
||
| 77 | $this->db->select($this->users_password_column); |
||
| 78 | $this->db->from($this->users_table); |
||
| 79 | $this->db->where($this->users_email_column, $username); |
||
| 80 | if ($this->users_username_column != null) { |
||
| 81 | $this->db->or_where($this->users_username_column, $username); |
||
| 82 | } |
||
| 83 | $query = $this->db->get(); |
||
| 84 | if ($query->num_rows() == 0) return false; |
||
| 85 | // Authenticate. |
||
| 86 | if (password_verify($password, $query->result()[0]->{$this->users_password_column})) { |
||
| 87 | if ($this->users_id_column != null) $context->userId = $query->result()[0]->{$this->users_id_column}; |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * [getAPIKeyData description] |
||
| 95 | * @param string $apiKey [description] |
||
| 96 | * @return array [description] |
||
| 97 | */ |
||
| 98 | public function getAPIKeyData(string $apiKey):?object { |
||
| 99 | // Preliminary Check. |
||
| 100 | if ($this->api_key_table == null || $this->api_key_column == null) return null; |
||
| 101 | // Query. |
||
| 102 | $this->db->from($this->api_key_table); |
||
| 103 | $this->db->where($this->api_key_column, $apiKey); |
||
| 104 | $query = $this->db->get(); |
||
| 105 | // Process Result. |
||
| 106 | return $query->num_rows() > 0 ? $query->result()[0] : null; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * [truncateRatelimitData description] |
||
| 111 | * @return bool [description] |
||
| 112 | */ |
||
| 113 | public function truncateRatelimitData():bool { |
||
| 114 | return $this->db->simple_query('DELETE FROM rest_api_rate_limit WHERE start < (NOW() - INTERVAL 1 HOUR)'); |
||
| 115 | } |
||
| 116 | /** |
||
| 117 | * [getLimitData description] |
||
| 118 | * @param string $client [description] |
||
| 119 | * @param string $group [description] |
||
| 120 | * @return [type] [description] |
||
|
0 ignored issues
–
show
|
|||
| 121 | */ |
||
| 122 | public function getLimitData(string $client, string $group):?array { |
||
| 123 | $sql = 'SELECT count, start, (`start` + INTERVAL (1 - TIMESTAMPDIFF(HOUR, UTC_TIMESTAMP(), NOW())) HOUR) AS reset_epoch FROM rest_api_rate_limit WHERE client = ? AND _group = ?'; |
||
| 124 | $query = $this->db->query($sql, [$client, $group]); |
||
| 125 | if (!is_scalar($query) && $query->num_rows() > 0) return $query->result_array()[0]; |
||
| 126 | return null; |
||
| 127 | } |
||
| 128 | /** |
||
| 129 | * [insertLimitData description] |
||
| 130 | * @param string $client [description] |
||
| 131 | * @param string $group [description] |
||
| 132 | * @return bool [description] |
||
| 133 | */ |
||
| 134 | public function insertLimitData(string $client, string $group):bool { |
||
| 135 | $sql = 'INSERT INTO rest_api_rate_limit (client, _group) VALUES (?, ?) ON DUPLICATE KEY UPDATE count = count + 1'; |
||
| 136 | return $this->db->query($sql, [$client, $group]); |
||
| 137 | } |
||
| 138 | } |
||
| 139 |