ajax = true; } public function displayAjax() { if ($this->validate() === true) { try { $pudoAddress = $this->getPudoAddress($this->getPudoCode()); if ($pudoAddress != null) { $response = ['success' => true, 'data' => $pudoAddress]; } else { $response = ['success' => false, 'message' => 'Undefined error']; } } catch (Exception $e) { $response = ['success' => false, 'message' => $e->getMessage()]; } die(json_encode($response)); } else { die(json_encode(['success' => false, 'message' => 'Undefined error'])); } } private function validate(): bool { if ($_SERVER['REQUEST_METHOD'] !== 'GET') { die(json_encode(['success' => false, 'message' => 'Method not allowed'])); } if ($this->getCsrf() != Tools::getToken(false)) { die(json_encode(['success' => false, 'message' => 'Invalid token.'])); } if ($this->getToken() != sha1(_COOKIE_KEY_ . 'dpdshipping')) { die(json_encode(['success' => false, 'message' => 'Invalid token'])); } if (empty($this->getPudoCode())) { die(json_encode(['success' => false, 'message' => 'Invalid params'])); } return true; } /** * @return false|mixed */ public function getCsrf() { return Tools::getValue('dpdshipping_csrf'); } /** * @return false|mixed */ public function getToken() { return Tools::getValue('dpdshipping_token'); } /** * @return false|mixed */ public function getPudoCode() { return Tools::getValue('dpdshipping_pudo_code'); } public function getPudoAddress($pudoCode) { if (empty($pudoCode)) { return null; } $ch = curl_init(); $url = sprintf(Config::DPD_PUDO_WS_URL, $pudoCode); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate, sdch, br'); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding: gzip, deflate, sdch, br', 'Accept-Language: en-US,en;q=0.8', 'Cache-Control: max-age=0', 'Connection: keep-alive', 'Host: mypudo.dpd.com.pl', 'Upgrade-Insecure-Requests: 1', 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', ]); $result = curl_exec($ch); curl_close($ch); if (!$result) { return null; } $xml = new SimpleXMLElement($result); if (!isset($xml->PUDO_ITEMS) || !isset($xml->PUDO_ITEMS->PUDO_ITEM)) { return null; } return implode(', ', [ $xml->PUDO_ITEMS->PUDO_ITEM->ADDRESS1, $xml->PUDO_ITEMS->PUDO_ITEM->ZIPCODE, $xml->PUDO_ITEMS->PUDO_ITEM->CITY, $xml->PUDO_ITEMS->PUDO_ITEM->PUDO_ID]); } }