1: | <?php
2: | namespace Opencart\Admin\Model\User;
3: | /**
4: | * Class Api
5: | *
6: | * @package Opencart\Admin\Model\User
7: | */
8: | class Api extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Api
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addApi(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "api` SET `username` = '" . $this->db->escape((string)$data['username']) . "', `key` = '" . $this->db->escape((string)$data['key']) . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `date_added` = NOW(), `date_modified` = NOW()");
18: |
19: | $api_id = $this->db->getLastId();
20: |
21: | if (isset($data['api_ip'])) {
22: | foreach ($data['api_ip'] as $ip) {
23: | if ($ip) {
24: | $this->addIp($api_id, $ip);
25: | }
26: | }
27: | }
28: |
29: | return $api_id;
30: | }
31: |
32: | /**
33: | * Edit Api
34: | *
35: | * @param int $api_id
36: | * @param array<string, mixed> $data
37: | *
38: | * @return void
39: | */
40: | public function editApi(int $api_id, array $data): void {
41: | $this->db->query("UPDATE `" . DB_PREFIX . "api` SET `username` = '" . $this->db->escape((string)$data['username']) . "', `key` = '" . $this->db->escape((string)$data['key']) . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `date_modified` = NOW() WHERE `api_id` = '" . (int)$api_id . "'");
42: |
43: | $this->deleteIp($api_id);
44: |
45: | if (isset($data['api_ip'])) {
46: | foreach ($data['api_ip'] as $ip) {
47: | if ($ip) {
48: | $this->addIp($api_id, $ip);
49: | }
50: | }
51: | }
52: | }
53: |
54: | /**
55: | * Delete Api
56: | *
57: | * @param int $api_id
58: | *
59: | * @return void
60: | */
61: | public function deleteApi(int $api_id): void {
62: | $this->db->query("DELETE FROM `" . DB_PREFIX . "api` WHERE `api_id` = '" . (int)$api_id . "'");
63: |
64: | }
65: |
66: | /**
67: | * Get Api
68: | *
69: | * @param int $api_id
70: | *
71: | * @return array<string, mixed>
72: | */
73: | public function getApi(int $api_id): array {
74: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `api_id` = '" . (int)$api_id . "'");
75: |
76: | return $query->row;
77: | }
78: |
79: | /**
80: | * Get Apis
81: | *
82: | * @param array<string, mixed> $data
83: | *
84: | * @return array<int, array<string, mixed>>
85: | */
86: | public function getApis(array $data = []): array {
87: | $sql = "SELECT * FROM `" . DB_PREFIX . "api`";
88: |
89: | $sort_data = [
90: | 'username',
91: | 'status',
92: | 'date_added',
93: | 'date_modified'
94: | ];
95: |
96: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
97: | $sql .= " ORDER BY " . $data['sort'];
98: | } else {
99: | $sql .= " ORDER BY `username`";
100: | }
101: |
102: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
103: | $sql .= " DESC";
104: | } else {
105: | $sql .= " ASC";
106: | }
107: |
108: | if (isset($data['start']) || isset($data['limit'])) {
109: | if ($data['start'] < 0) {
110: | $data['start'] = 0;
111: | }
112: |
113: | if ($data['limit'] < 1) {
114: | $data['limit'] = 20;
115: | }
116: |
117: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
118: | }
119: |
120: | $query = $this->db->query($sql);
121: |
122: | return $query->rows;
123: | }
124: |
125: | /**
126: | * Get Total Apis
127: | *
128: | * @return int
129: | */
130: | public function getTotalApis(): int {
131: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "api`");
132: |
133: | return (int)$query->row['total'];
134: | }
135: |
136: | /**
137: | * Add Ip
138: | *
139: | * @param int $api_id
140: | * @param string $ip
141: | *
142: | * @return void
143: | */
144: | public function addIp(int $api_id, string $ip): void {
145: | $this->db->query("INSERT INTO `" . DB_PREFIX . "api_ip` SET `api_id` = '" . (int)$api_id . "', `ip` = '" . $this->db->escape($ip) . "'");
146: | }
147: |
148: | /**
149: | * Delete Ips
150: | *
151: | * @param int $api_id
152: | *
153: | * @return void
154: | */
155: | public function deleteIps(int $api_id): void {
156: | $this->db->query("DELETE FROM `" . DB_PREFIX . "api_ip` WHERE `api_id` = '" . (int)$api_id . "'");
157: | }
158: |
159: | /**
160: | * Get Ips
161: | *
162: | * @param int $api_id
163: | *
164: | * @return array<int, string>
165: | */
166: | public function getIps(int $api_id): array {
167: | $ip_data = [];
168: |
169: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api_ip` WHERE `api_id` = '" . (int)$api_id . "'");
170: |
171: | foreach ($query->rows as $result) {
172: | $ip_data[] = $result['ip'];
173: | }
174: |
175: | return $ip_data;
176: | }
177: |
178: | /**
179: | * Add Session
180: | *
181: | * @param int $api_id
182: | * @param string $session_id
183: | * @param string $ip
184: | *
185: | * @return int
186: | */
187: | public function addSession(int $api_id, string $session_id, string $ip): int {
188: | $api_ip_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api_ip` WHERE `ip` = '" . $this->db->escape($ip) . "'");
189: |
190: | if (!$api_ip_query->num_rows) {
191: | $this->db->query("INSERT INTO `" . DB_PREFIX . "api_ip` SET `api_id` = '" . (int)$api_id . "', `ip` = '" . $this->db->escape($ip) . "'");
192: | }
193: |
194: | $this->db->query("INSERT INTO `" . DB_PREFIX . "api_session` SET `api_id` = '" . (int)$api_id . "', `session_id` = '" . $this->db->escape($session_id) . "', `ip` = '" . $this->db->escape($ip) . "', `date_added` = NOW(), `date_modified` = NOW()");
195: |
196: | return $this->db->getLastId();
197: | }
198: |
199: | /**
200: | * Get Sessions
201: | *
202: | * @param int $api_id
203: | *
204: | * @return array<int, array<string, mixed>>
205: | */
206: | public function getSessions(int $api_id): array {
207: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api_session` WHERE `api_id` = '" . (int)$api_id . "'");
208: |
209: | return $query->rows;
210: | }
211: |
212: | /**
213: | * Delete Session
214: | *
215: | * @param int $api_session_id
216: | *
217: | * @return void
218: | */
219: | public function deleteSession(int $api_session_id): void {
220: | $this->db->query("DELETE FROM `" . DB_PREFIX . "api_session` WHERE `api_session_id` = '" . (int)$api_session_id . "'");
221: | }
222: |
223: | /**
224: | * Delete Session By Session ID
225: | *
226: | * @param string $session_id
227: | *
228: | * @return void
229: | */
230: | public function deleteSessionsBySessionId(string $session_id): void {
231: | $this->db->query("DELETE FROM `" . DB_PREFIX . "api_session` WHERE `session_id` = '" . $this->db->escape($session_id) . "'");
232: | }
233: | }
234: |

OpenCart API API documentation generated by ApiGen dev-master