1: | <?php
|
2: | namespace Opencart\Admin\Controller\User;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Api extends \Opencart\System\Engine\Controller {
|
9: | |
10: | |
11: | |
12: | |
13: |
|
14: | public function index(): void {
|
15: | $this->load->language('user/api');
|
16: |
|
17: | $this->document->setTitle($this->language->get('heading_title'));
|
18: |
|
19: | $url = '';
|
20: |
|
21: | if (isset($this->request->get['sort'])) {
|
22: | $url .= '&sort=' . $this->request->get['sort'];
|
23: | }
|
24: |
|
25: | if (isset($this->request->get['order'])) {
|
26: | $url .= '&order=' . $this->request->get['order'];
|
27: | }
|
28: |
|
29: | if (isset($this->request->get['page'])) {
|
30: | $url .= '&page=' . $this->request->get['page'];
|
31: | }
|
32: |
|
33: | $data['breadcrumbs'] = [];
|
34: |
|
35: | $data['breadcrumbs'][] = [
|
36: | 'text' => $this->language->get('text_home'),
|
37: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
|
38: | ];
|
39: |
|
40: | $data['breadcrumbs'][] = [
|
41: | 'text' => $this->language->get('heading_title'),
|
42: | 'href' => $this->url->link('user/api', 'user_token=' . $this->session->data['user_token'] . $url)
|
43: | ];
|
44: |
|
45: | $data['add'] = $this->url->link('user/api.form', 'user_token=' . $this->session->data['user_token'] . $url);
|
46: | $data['delete'] = $this->url->link('user/api.delete', 'user_token=' . $this->session->data['user_token']);
|
47: |
|
48: | $data['list'] = $this->getList();
|
49: |
|
50: | $data['user_token'] = $this->session->data['user_token'];
|
51: |
|
52: | $data['header'] = $this->load->controller('common/header');
|
53: | $data['column_left'] = $this->load->controller('common/column_left');
|
54: | $data['footer'] = $this->load->controller('common/footer');
|
55: |
|
56: | $this->response->setOutput($this->load->view('user/api', $data));
|
57: | }
|
58: |
|
59: | |
60: | |
61: | |
62: | |
63: |
|
64: | public function list(): void {
|
65: | $this->load->language('user/api');
|
66: |
|
67: | $this->response->setOutput($this->getList());
|
68: | }
|
69: |
|
70: | |
71: | |
72: | |
73: | |
74: |
|
75: | protected function getList(): string {
|
76: | if (isset($this->request->get['sort'])) {
|
77: | $sort = (string)$this->request->get['sort'];
|
78: | } else {
|
79: | $sort = 'username';
|
80: | }
|
81: |
|
82: | if (isset($this->request->get['order'])) {
|
83: | $order = (string)$this->request->get['order'];
|
84: | } else {
|
85: | $order = 'ASC';
|
86: | }
|
87: |
|
88: | if (isset($this->request->get['page'])) {
|
89: | $page = (int)$this->request->get['page'];
|
90: | } else {
|
91: | $page = 1;
|
92: | }
|
93: |
|
94: | $url = '';
|
95: |
|
96: | if (isset($this->request->get['sort'])) {
|
97: | $url .= '&sort=' . $this->request->get['sort'];
|
98: | }
|
99: |
|
100: | if (isset($this->request->get['order'])) {
|
101: | $url .= '&order=' . $this->request->get['order'];
|
102: | }
|
103: |
|
104: | if (isset($this->request->get['page'])) {
|
105: | $url .= '&page=' . $this->request->get['page'];
|
106: | }
|
107: |
|
108: | $data['action'] = $this->url->link('user/api.list', 'user_token=' . $this->session->data['user_token'] . $url);
|
109: |
|
110: | $data['apis'] = [];
|
111: |
|
112: | $filter_data = [
|
113: | 'sort' => $sort,
|
114: | 'order' => $order,
|
115: | 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
|
116: | 'limit' => $this->config->get('config_pagination_admin')
|
117: | ];
|
118: |
|
119: | $this->load->model('user/api');
|
120: |
|
121: | $results = $this->model_user_api->getApis($filter_data);
|
122: |
|
123: | foreach ($results as $result) {
|
124: | $data['apis'][] = [
|
125: | 'api_id' => $result['api_id'],
|
126: | 'username' => $result['username'],
|
127: | 'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
|
128: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
|
129: | 'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
|
130: | 'edit' => $this->url->link('user/api.form', 'user_token=' . $this->session->data['user_token'] . '&api_id=' . $result['api_id'] . $url)
|
131: | ];
|
132: | }
|
133: |
|
134: | $url = '';
|
135: |
|
136: | if ($order == 'ASC') {
|
137: | $url .= '&order=DESC';
|
138: | } else {
|
139: | $url .= '&order=ASC';
|
140: | }
|
141: |
|
142: | $data['sort_username'] = $this->url->link('user/api.list', 'user_token=' . $this->session->data['user_token'] . '&sort=username' . $url);
|
143: | $data['sort_status'] = $this->url->link('user/api.list', 'user_token=' . $this->session->data['user_token'] . '&sort=status' . $url);
|
144: | $data['sort_date_added'] = $this->url->link('user/api.list', 'user_token=' . $this->session->data['user_token'] . '&sort=date_added' . $url);
|
145: | $data['sort_date_modified'] = $this->url->link('user/api.list', 'user_token=' . $this->session->data['user_token'] . '&sort=date_modified' . $url);
|
146: |
|
147: | $url = '';
|
148: |
|
149: | if (isset($this->request->get['sort'])) {
|
150: | $url .= '&sort=' . $this->request->get['sort'];
|
151: | }
|
152: |
|
153: | if (isset($this->request->get['order'])) {
|
154: | $url .= '&order=' . $this->request->get['order'];
|
155: | }
|
156: |
|
157: | $user_total = $this->model_user_api->getTotalApis();
|
158: |
|
159: | $data['pagination'] = $this->load->controller('common/pagination', [
|
160: | 'total' => $user_total,
|
161: | 'page' => $page,
|
162: | 'limit' => $this->config->get('config_pagination_admin'),
|
163: | 'url' => $this->url->link('user/api.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
|
164: | ]);
|
165: |
|
166: | $data['results'] = sprintf($this->language->get('text_pagination'), ($user_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($user_total - $this->config->get('config_pagination_admin'))) ? $user_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $user_total, ceil($user_total / $this->config->get('config_pagination_admin')));
|
167: |
|
168: | $data['sort'] = $sort;
|
169: | $data['order'] = $order;
|
170: |
|
171: | return $this->load->view('user/api_list', $data);
|
172: | }
|
173: |
|
174: | |
175: | |
176: | |
177: | |
178: |
|
179: | public function form(): void {
|
180: | $this->load->language('user/api');
|
181: |
|
182: | $this->document->setTitle($this->language->get('heading_title'));
|
183: |
|
184: | $data['text_form'] = !isset($this->request->get['api_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
|
185: | $data['text_ip'] = sprintf($this->language->get('text_ip'), $this->request->server['REMOTE_ADDR']);
|
186: |
|
187: | if (isset($this->request->get['api_id'])) {
|
188: | $data['api_id'] = $this->request->get['api_id'];
|
189: | } else {
|
190: | $data['api_id'] = 0;
|
191: | }
|
192: |
|
193: | $url = '';
|
194: |
|
195: | if (isset($this->request->get['sort'])) {
|
196: | $url .= '&sort=' . $this->request->get['sort'];
|
197: | }
|
198: |
|
199: | if (isset($this->request->get['order'])) {
|
200: | $url .= '&order=' . $this->request->get['order'];
|
201: | }
|
202: |
|
203: | if (isset($this->request->get['page'])) {
|
204: | $url .= '&page=' . $this->request->get['page'];
|
205: | }
|
206: |
|
207: | $data['breadcrumbs'] = [];
|
208: |
|
209: | $data['breadcrumbs'][] = [
|
210: | 'text' => $this->language->get('text_home'),
|
211: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
|
212: | ];
|
213: |
|
214: | $data['breadcrumbs'][] = [
|
215: | 'text' => $this->language->get('heading_title'),
|
216: | 'href' => $this->url->link('user/api', 'user_token=' . $this->session->data['user_token'] . $url)
|
217: | ];
|
218: |
|
219: | $data['save'] = $this->url->link('user/api.save', 'user_token=' . $this->session->data['user_token']);
|
220: | $data['back'] = $this->url->link('user/api', 'user_token=' . $this->session->data['user_token'] . $url);
|
221: |
|
222: | if (isset($this->request->get['api_id'])) {
|
223: | $this->load->model('user/api');
|
224: |
|
225: | $api_info = $this->model_user_api->getApi($this->request->get['api_id']);
|
226: | }
|
227: |
|
228: | if (isset($this->request->get['api_id'])) {
|
229: | $data['api_id'] = (int)$this->request->get['api_id'];
|
230: | } else {
|
231: | $data['api_id'] = 0;
|
232: | }
|
233: |
|
234: | if (!empty($api_info)) {
|
235: | $data['username'] = $api_info['username'];
|
236: | } else {
|
237: | $data['username'] = '';
|
238: | }
|
239: |
|
240: | if (!empty($api_info)) {
|
241: | $data['key'] = $api_info['key'];
|
242: | } else {
|
243: | $data['key'] = '';
|
244: | }
|
245: |
|
246: | if (!empty($api_info)) {
|
247: | $data['status'] = $api_info['status'];
|
248: | } else {
|
249: | $data['status'] = 0;
|
250: | }
|
251: |
|
252: |
|
253: | if (!empty($api_info)) {
|
254: | $data['api_ips'] = $this->model_user_api->getIps($this->request->get['api_id']);
|
255: | } else {
|
256: | $data['api_ips'] = [];
|
257: | }
|
258: |
|
259: |
|
260: | $data['api_sessions'] = [];
|
261: |
|
262: | if (!empty($api_info)) {
|
263: | $results = $this->model_user_api->getSessions($this->request->get['api_id']);
|
264: |
|
265: | foreach ($results as $result) {
|
266: | $data['api_sessions'][] = [
|
267: | 'api_session_id' => $result['api_session_id'],
|
268: | 'session_id' => $result['session_id'],
|
269: | 'ip' => $result['ip'],
|
270: | 'date_added' => date($this->language->get('datetime_format'), strtotime($result['date_added'])),
|
271: | 'date_modified' => date($this->language->get('datetime_format'), strtotime($result['date_modified']))
|
272: | ];
|
273: | }
|
274: | }
|
275: |
|
276: | $data['user_token'] = $this->session->data['user_token'];
|
277: |
|
278: | $data['header'] = $this->load->controller('common/header');
|
279: | $data['column_left'] = $this->load->controller('common/column_left');
|
280: | $data['footer'] = $this->load->controller('common/footer');
|
281: |
|
282: | $this->response->setOutput($this->load->view('user/api_form', $data));
|
283: | }
|
284: |
|
285: | |
286: | |
287: | |
288: | |
289: |
|
290: | public function save(): void {
|
291: | $this->load->language('user/api');
|
292: |
|
293: | $json = [];
|
294: |
|
295: | if (!$this->user->hasPermission('modify', 'user/api')) {
|
296: | $json['error']['warning'] = $this->language->get('error_permission');
|
297: | }
|
298: |
|
299: | if ((oc_strlen($this->request->post['username']) < 3) || (oc_strlen($this->request->post['username']) > 64)) {
|
300: | $json['error']['username'] = $this->language->get('error_username');
|
301: | }
|
302: |
|
303: | if ((oc_strlen($this->request->post['key']) < 64) || (oc_strlen($this->request->post['key']) > 256)) {
|
304: | $json['error']['key'] = $this->language->get('error_key');
|
305: | }
|
306: |
|
307: | if (!isset($json['error']['warning']) && !isset($this->request->post['api_ip'])) {
|
308: | $json['error']['warning'] = $this->language->get('error_ip');
|
309: | }
|
310: |
|
311: | if (!$json) {
|
312: | $this->load->model('user/api');
|
313: |
|
314: | if (!$this->request->post['api_id']) {
|
315: | $json['api_id'] = $this->model_user_api->addApi($this->request->post);
|
316: | } else {
|
317: | $this->model_user_api->editApi($this->request->post['api_id'], $this->request->post);
|
318: | }
|
319: |
|
320: | $json['success'] = $this->language->get('text_success');
|
321: | }
|
322: |
|
323: | $this->response->addHeader('Content-Type: application/json');
|
324: | $this->response->setOutput(json_encode($json));
|
325: | }
|
326: |
|
327: | |
328: | |
329: | |
330: | |
331: |
|
332: | public function delete(): void {
|
333: | $this->load->language('user/api');
|
334: |
|
335: | $json = [];
|
336: |
|
337: | if (isset($this->request->post['selected'])) {
|
338: | $selected = $this->request->post['selected'];
|
339: | } else {
|
340: | $selected = [];
|
341: | }
|
342: |
|
343: | if (!$this->user->hasPermission('modify', 'user/api')) {
|
344: | $json['error'] = $this->language->get('error_permission');
|
345: | }
|
346: |
|
347: | if (!$json) {
|
348: | $this->load->model('user/api');
|
349: |
|
350: | foreach ($selected as $api_id) {
|
351: | $this->model_user_api->deleteApi($api_id);
|
352: | }
|
353: |
|
354: | $json['success'] = $this->language->get('text_success');
|
355: | }
|
356: |
|
357: | $this->response->addHeader('Content-Type: application/json');
|
358: | $this->response->setOutput(json_encode($json));
|
359: | }
|
360: |
|
361: | |
362: | |
363: | |
364: | |
365: |
|
366: | public function deleteSession(): void {
|
367: | $this->load->language('user/api');
|
368: |
|
369: | $json = [];
|
370: |
|
371: | if (!$this->user->hasPermission('modify', 'user/api')) {
|
372: | $json['error'] = $this->language->get('error_permission');
|
373: | }
|
374: |
|
375: | if (!$json) {
|
376: | $this->load->model('user/api');
|
377: |
|
378: | $this->model_user_api->deleteSession($this->request->get['api_session_id']);
|
379: |
|
380: | $json['success'] = $this->language->get('text_success');
|
381: | }
|
382: |
|
383: | $this->response->addHeader('Content-Type: application/json');
|
384: | $this->response->setOutput(json_encode($json));
|
385: | }
|
386: | }
|
387: | |