| 1: | <?php
|
| 2: | namespace Opencart\Admin\Model\User;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Api extends \Opencart\System\Engine\Model {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 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: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 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: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 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: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 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: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 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: | |
| 127: | |
| 128: | |
| 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: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 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: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 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: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 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: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 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: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 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: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 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: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 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: | |