login.php (1415B)
1 <?php 2 class ControllerApiLogin extends Controller { 3 public function index() { 4 $this->load->language('api/login'); 5 6 $json = array(); 7 8 $this->load->model('account/api'); 9 10 // Login with API Key 11 $api_info = $this->model_account_api->login($this->request->post['username'], $this->request->post['key']); 12 13 if ($api_info) { 14 // Check if IP is allowed 15 $ip_data = array(); 16 17 $results = $this->model_account_api->getApiIps($api_info['api_id']); 18 19 foreach ($results as $result) { 20 $ip_data[] = trim($result['ip']); 21 } 22 23 if (!in_array($this->request->server['REMOTE_ADDR'], $ip_data)) { 24 $json['error']['ip'] = sprintf($this->language->get('error_ip'), $this->request->server['REMOTE_ADDR']); 25 } 26 27 if (!$json) { 28 $json['success'] = $this->language->get('text_success'); 29 30 $session = new Session($config->get('session_engine'), $registry); 31 $session->start(); 32 33 $this->model_account_api->addApiSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']); 34 35 $session->data['api_id'] = $api_info['api_id']; 36 37 // Create Token 38 $json['api_token'] = $session->getId(); 39 } else { 40 $json['error']['key'] = $this->language->get('error_key'); 41 } 42 } 43 44 $this->response->addHeader('Content-Type: application/json'); 45 $this->response->setOutput(json_encode($json)); 46 } 47 }