1: <?php
2: namespace Opencart\Admin\Model\Extension\Opencart\Fraud;
3: /**
4: * Class Ip
5: *
6: * @package Opencart\Admin\Controller\Extension\Opencart\Fraud
7: */
8: class Ip extends \Opencart\System\Engine\Model {
9: /**
10: * Install
11: *
12: * @return void
13: */
14: public function install(): void {
15: $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "fraud_ip` (
16: `ip` varchar(40) NOT NULL,
17: `date_added` datetime NOT NULL,
18: PRIMARY KEY (`ip`)
19: ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
20: }
21:
22: /**
23: * Uninstall
24: *
25: * @return void
26: */
27: public function uninstall(): void {
28: $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "fraud_ip`");
29: }
30:
31: /**
32: * Add Ip
33: *
34: * @param string $ip
35: *
36: * @return void
37: */
38: public function addIp(string $ip): void {
39: $this->db->query("INSERT INTO `" . DB_PREFIX . "fraud_ip` SET `ip` = '" . $this->db->escape($ip) . "', `date_added` = NOW()");
40: }
41:
42: /**
43: * Remove Ip
44: *
45: * @param string $ip
46: *
47: * @return void
48: */
49: public function removeIp(string $ip): void {
50: $this->db->query("DELETE FROM `" . DB_PREFIX . "fraud_ip` WHERE `ip` = '" . $this->db->escape($ip) . "'");
51: }
52:
53: /**
54: * Get Ips
55: *
56: * @param int $start
57: * @param int $limit
58: *
59: * @return array<int, array<string, mixed>>
60: */
61: public function getIps(int $start = 0, int $limit = 10): array {
62: if ($start < 0) {
63: $start = 0;
64: }
65:
66: if ($limit < 1) {
67: $limit = 10;
68: }
69:
70: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "fraud_ip` ORDER BY `ip` ASC LIMIT " . (int)$start . "," . (int)$limit);
71:
72: return $query->rows;
73: }
74:
75: /**
76: * Get Total Ips
77: *
78: * @return int
79: */
80: public function getTotalIps(): int {
81: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "fraud_ip`");
82:
83: return (int)$query->row['total'];
84: }
85:
86: /**
87: * Get Total Ips By Ip
88: *
89: * @param string $ip
90: *
91: * @return int
92: */
93: public function getTotalIpsByIp(string $ip): int {
94: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "fraud_ip` WHERE `ip` = '" . $this->db->escape($ip) . "'");
95:
96: return (int)$query->row['total'];
97: }
98: }
99: