1: | <?php |
2: | namespace Opencart\Catalog\Controller\Api\Account; |
3: | /** |
4: | * Class Login |
5: | * |
6: | * @package Opencart\Catalog\Controller\Api\Account |
7: | */ |
8: | class Login extends \Opencart\System\Engine\Controller { |
9: | /** |
10: | * Opencart\Catalog\Controller\Api\Account\Login.Index |
11: | * |
12: | * @Example |
13: | * |
14: | * $url = 'https://www.yourdomain.com/index.php?route=api/account/login&language=en-gb&store_id=0'; |
15: | * |
16: | * $request_data = [ |
17: | * 'username' => 'Default', |
18: | * 'key' => '' |
19: | * ]; |
20: | * |
21: | * $curl = curl_init(); |
22: | * |
23: | * curl_setopt($curl, CURLOPT_URL, $url); |
24: | * curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
25: | * curl_setopt($curl, CURLOPT_HEADER, false); |
26: | * curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); |
27: | * curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); |
28: | * curl_setopt($curl, CURLOPT_TIMEOUT, 30); |
29: | * curl_setopt($curl, CURLOPT_POST, 1); |
30: | * curl_setopt($curl, CURLOPT_POSTFIELDS, $request_data); |
31: | * |
32: | * $response = curl_exec($curl); |
33: | * |
34: | * $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
35: | * |
36: | * curl_close($curl); |
37: | * |
38: | * if ($status == 200) { |
39: | * $api_token = json_decode($response, true); |
40: | * |
41: | * if (isset($api_token['api_token'])) { |
42: | * |
43: | * // You can now store the session cookie as a var in the your current session or some of persistent storage |
44: | * $session_id = $api_token['api_token']; |
45: | * } |
46: | * } |
47: | * |
48: | * $url = 'http://www.yourdomain.com/opencart-master/upload/index.php?route=api/sale/order.load&language=en-gb&store_id=0&order_id=1'; |
49: | * |
50: | * $curl = curl_init(); |
51: | * |
52: | * curl_setopt($curl, CURLOPT_URL, $url); |
53: | * curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
54: | * curl_setopt($curl, CURLOPT_HEADER, false); |
55: | * curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); |
56: | * curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); |
57: | * curl_setopt($curl, CURLOPT_TIMEOUT, 30); |
58: | * curl_setopt($curl, CURLOPT_POST, 1); |
59: | * curl_setopt($curl, CURLOPT_POSTFIELDS, $request_data); |
60: | * |
61: | * // Add the session cookie so we don't have to login again. |
62: | * curl_setopt($curl, CURLOPT_COOKIE, 'OCSESSID=' . $session_id); |
63: | * |
64: | * $response = curl_exec($curl); |
65: | * |
66: | * curl_close($curl); |
67: | */ |
68: | public function index(): void { |
69: | $this->load->language('api/account/login'); |
70: | |
71: | $json = []; |
72: | |
73: | $this->load->model('account/api'); |
74: | |
75: | // Login with API Key |
76: | if (!empty($this->request->post['username']) && !empty($this->request->post['key'])) { |
77: | $api_info = $this->model_account_api->login($this->request->post['username'], $this->request->post['key']); |
78: | } else { |
79: | $api_info = []; |
80: | } |
81: | |
82: | if ($api_info) { |
83: | // Check if IP is allowed |
84: | $ip_data = []; |
85: | |
86: | $results = $this->model_account_api->getIps($api_info['api_id']); |
87: | |
88: | foreach ($results as $result) { |
89: | $ip_data[] = trim($result['ip']); |
90: | } |
91: | |
92: | if (!in_array($this->request->server['REMOTE_ADDR'], $ip_data)) { |
93: | $json['error'] = sprintf($this->language->get('error_ip'), $this->request->server['REMOTE_ADDR']); |
94: | } |
95: | } else { |
96: | $json['error'] = $this->language->get('error_key'); |
97: | } |
98: | |
99: | if (!$json) { |
100: | $json['success'] = $this->language->get('text_success'); |
101: | |
102: | $session = new \Opencart\System\Library\Session($this->config->get('session_engine'), $this->registry); |
103: | $session->start(); |
104: | |
105: | $this->model_account_api->addSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']); |
106: | |
107: | $session->data['api_id'] = $api_info['api_id']; |
108: | |
109: | // Create Token |
110: | $json['api_token'] = $session->getId(); |
111: | } |
112: | |
113: | $this->response->addHeader('Content-Type: application/json'); |
114: | $this->response->setOutput(json_encode($json)); |
115: | } |
116: | } |
117: |