✏️ 正在编辑: DPDTools.php
路径:
/home/bpioifn/www/pigmentse/modules/dpdfrance/src/utils/DPDTools.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php /** * 2007-2022 PrestaShop * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@prestashop.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author DPD France S.A.S. <support.ecommerce@dpd.fr> * @copyright 2022 DPD France S.A.S. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) */ if (!defined('_PS_VERSION_')) { exit; } class DPDTools { /** * ------------------------------------------DPD INTERNAL DEBUG--------------------------------------- */ const DPD_DEBUG = false; const DPD_DEBUG_EPRINT_LOGIN = ""; const DPD_DEBUG_EPRINT_PASSWORD = ""; const DPD_DEBUG_WEBTRACE_LOGIN = ""; const DPD_DEBUG_WEBTRACE_PASSWORD = ""; /** * ------------------------------------------CUSTOM REQUEST --------------------------------------- */ const PASSTRHOUGHT_CEDI = false; /** * ------------------------------------------PRESTASHOP ORDER STATUS--------------------------------------- */ const ORDER_STATUS_AWAIT_CHECK_PAYMENT = 1; const ORDER_STATUS_PAYMENT_ACCEPTED = 2; const ORDER_STATUS_PROCESSING_IN_PROGRESS = 3; const ORDER_STATUS_SHIPPED = 4; const ORDER_STATUS_DELIVERED = 5; const ORDER_STATUS_CANCELLED = 6; const ORDER_STATUS_REFUNDED = 7; const ORDER_STATUS_PAYEMENT_ERROR = 8; /** * ------------------------------------------LINKS-------------------------------------------------------- */ const PREFIX_GOOGLE_URL = 'https://maps.google.com/maps?f=q&hl=fr&geocode=&q='; const PREFIX_RELAIS_URL = 'https://relais.dpd.fr/dpdrelais/id_'; const PREFIX_TRACEX_URL = 'https://trace.dpd.fr/tracex_'; const PREFIX_TRACES_URL = 'https://trace.dpd.fr/traces_'; const DPD_FLUX_RSS = 'https://cdn.dpd.fr/documents/rss/flux.xml'; /** * ! Webservice Eprint sur l'environnement de Production */ const DPD_WEBSERVICE_EPRINT_URL_PROD = 'https://e-station.cargonet.software/dpd-eprintwebservice/eprintwebservice.asmx?WSDL'; /** * ! Webservice Eprint sur l'environnement de TEST pour réaliser des tests avec des colis "test" */ const DPD_WEBSERVICE_EPRINT_URL_TEST = 'https://e-station-testenv.cargonet.software/eprintwebservice/eprintwebservice.asmx?WSDL'; /** * ! Webservice Webtrace sur l'environnement de PRODUCTION */ const DPD_WEBSERVICE_WEBTRACE_URL_PROD = 'http://webtrace.dpd.fr/dpd-webservices/webtrace_service.asmx?WSDL'; /** * Webservice Webtrace sur l'environnement de TEST pour réaliser des tests avec des colis "test" */ const DPD_WEBSERVICE_WEBTRACE_URL_TEST = 'https://e-station-testenv.cargonet.software/trace-service/Webtrace_Service.asmx?WSDL'; /** * ------------------------------------------DPD ORDER RETURN---------------------------------------------- */ const NO_RETURN = 0; const RETURN_ON_DEMAND = 3; const RETURN_PREPARED = 4; /** * ------------------------------------------DPD ORDER UPDATE---------------------------------------------- */ const DPD_AUTOUPDATE_DISABLED = 0; const DPD_AUTOUPDATE_ORDER_REF = 1; const DPD_AUTOUPDATE_PARCEL_NUMBER = 2; const DPD_AUTOUPDATE_ORDER_ID = 3; /** * ------------------------------------------CARGO STATUS CODE---------------------------------------------- */ /** * Consignation par EDI * * @var int */ const CARGO_CEDI = 8; /** * Entrée centre * * @var int */ const CARGO_COEC = 10; /** * Colis pris en charge * * @var int */ const CARGO_CPCH = 28; /** * Livré * * @var int */ const CARGO_REMI = 40; /** * Enlèvement * * @var int */ const CARGO_PICK = 44; /** * CODE STATUS HISTORIQUE => Colis pris en charge * * @var int */ const CARGO_HISTORIQUE_CPCH = 89; /** * Scannage mandaté * * @var int */ const CARGO_SMAN = 230; /** * Livré Destinataire Point Relais <br> * CODE REEL => REMI * * @var int */ const CARGO_LIVPT = 400; /** * ------------------------------------------POSTAL CODE---------------------------------------------------- */ /** * Liste des codes postaux en zone montagneuse * @var string[] */ const MOUNTAINS_ZONE = [ '04120', '04130', '04140', '04160', '04170', '04200', '04240', '04260', '04300', '04310', '04330', '04360', '04370', '04400', '04510', '04530', '04600', '04700', '04850', '05100', '05110', '05120', '05130', '05150', '05160', '05170', '05200', '05220', '05240', '05250', '05260', '05290', '05300', '05310', '05320', '05330', '05340', '05350', '05400', '05460', '05470', '05500', '05560', '05600', '05700', '05800', '06140', '06380', '06390', '06410', '06420', '06430', '06450', '06470', '06530', '06540', '06620', '06710', '06750', '06910', '09110', '09140', '09300', '09460', '25120', '25140', '25240', '25370', '25450', '25500', '25650', '30570', '31110', '38112', '38114', '38142', '38190', '38250', '38350', '38380', '38410', '38580', '38660', '38700', '38750', '38860', '38880', '39220', '39310', '39400', '63113', '63210', '63240', '63610', '63660', '63690', '63840', '63850', '64440', '64490', '64560', '64570', '65110', '65120', '65170', '65200', '65240', '65400', '65510', '65710', '66210', '66760', '66800', '68140', '68610', '68650', '73110', '73120', '73130', '73140', '73150', '73160', '73170', '73190', '73210', '73220', '73230', '73250', '73260', '73270', '73300', '73320', '73340', '73350', '73390', '73400', '73440', '73450', '73460', '73470', '73500', '73530', '73550', '73590', '73600', '73620', '73630', '73640', '73710', '73720', '73870', '74110', '74120', '74170', '74220', '74230', '74260', '74310', '74340', '74350', '74360', '74390', '74400', '74420', '74430', '74440', '74450', '74470', '74480', '74660', '74740', '74920', '83111', '83440', '83530', '83560', '83630', '83690', '83830', '83840', '84390', '88310', '88340', '88370', '88400', '90200', ]; /** * Liste des codes postaux en zone Iles * @var string[] */ const ISLANDS_ZONE = [ '17111', '17123', '17190', '17310', '17370', '17410', '17480', '17550', '17580', '17590', '17630', '17650', '17670', '17740', '17840', '17880', '17940', '22870', '29242', '29253', '29259', '29980', '29990', '56360', '56590', '56780', '56840', '85350', ]; /** * ------------------------------------------FUNCTIONS------------------------------------------------------ */ /** * [INSTALL] Setup new zone France * @return bool * @throws PrestaShopDatabaseException */ public static function createFranceZone(): bool { /** * ? Creation de la zone France */ $sqlCreateFranceZone = 'INSERT INTO ' . _DB_PREFIX_ . 'zone (name, active) VALUES ("France", 1)'; if (Db::getInstance()->execute($sqlCreateFranceZone) === false) { return false; } /** * ? Recuperation de la zone France */ $sqlCreatedFranceZone = 'SELECT id_zone FROM ' . _DB_PREFIX_ . 'zone WHERE name = "France"'; $resCreatedFranceZone = Db::getInstance()->ExecuteS($sqlCreatedFranceZone); if (empty($resCreatedFranceZone)) { return false; } $resCreatedFranceZone = reset($resCreatedFranceZone); /** * ? Recuperation des id de boutique */ $sqlShop = 'SELECT id_shop FROM ' . _DB_PREFIX_ . 'shop;'; $resShop = Db::getInstance()->executeS($sqlShop); /** * ? Creation de la zone shop France pour chaque boutique */ if (!empty($resShop)) { foreach ($resShop as $shop) { $zoneShopSql = ' INSERT INTO ' . _DB_PREFIX_ . 'zone_shop (id_zone, id_shop) VALUES (' . (int)$resCreatedFranceZone['id_zone'] . ', ' . (int)$shop['id_shop'] . ') '; if (Db::getInstance()->execute($zoneShopSql) === false) { return false; } } } else { return false; } /** * ? Affectation de la zone France au pays France */ foreach ($resCreatedFranceZone as $zone) { $sqlSetCountryZoneFrance = 'UPDATE ' . _DB_PREFIX_ . 'country SET id_zone=' . (int)$zone . ' WHERE iso_code ="FR" and active = 1'; if (Db::getInstance()->execute($sqlSetCountryZoneFrance) === false) { return false; } } return true; } /** * [SOAP] Initial Soap Client with userCredentials for EPrint Webservice * @return bool|SoapClient */ public static function getSoapInitEPrint() { try { $soapClient = new SoapClient( self::DPD_DEBUG === false ? self::DPD_WEBSERVICE_EPRINT_URL_PROD : self::DPD_WEBSERVICE_EPRINT_URL_TEST, [ 'connection_timeout' => 5, 'cache_wsdl' => WSDL_CACHE_NONE, 'exceptions' => true, ] ); $auth = new stdClass(); $auth->userid = self::DPD_DEBUG === false ? self::decrypt(Configuration::get('DPDFRANCE_API_LOGIN')) : self::DPD_DEBUG_EPRINT_LOGIN; $auth->password = self::DPD_DEBUG === false ? self::decrypt(Configuration::get('DPDFRANCE_API_PASSWORD')) : self::DPD_DEBUG_EPRINT_PASSWORD; $header = new SoapHeader('http://www.cargonet.software', 'UserCredentials', $auth, false); $soapClient->__setSoapHeaders($header); } catch (SoapFault $exception) { return false; } return $soapClient; } /** * [SOAP] Initial Soap Client with userCredentials for WEBTRACE Webservice * @return bool|SoapClient */ public static function getSoapTraceInit() { try { $soapClient = new SoapClient( self::DPD_DEBUG === false ? self::DPD_WEBSERVICE_WEBTRACE_URL_PROD : self::DPD_WEBSERVICE_WEBTRACE_URL_TEST, [ 'connection_timeout' => 5, 'cache_wsdl' => WSDL_CACHE_NONE, 'exceptions' => true, ] ); } catch (SoapFault $exception) { return false; } return $soapClient; } /** * [SOAP] Initial Soap Client for PUDO Webservice * @return bool|SoapClient */ public static function getSoapInitPudo() { try { $soapClient = new SoapClient( Configuration::get('DPDFRANCE_RELAIS_MYPUDO_URL'), [ 'connection_timeout' => 5, 'cache_wsdl' => WSDL_CACHE_NONE, 'exceptions' => true, ] ); } catch (SoapFault $exception) { return false; } return $soapClient; } /** * Format Weight to Float Syntax * @param string $weight * @return string */ public static function formatWeightToFloatSyntax(string $weight): string { //Search for a mixed string with numbers and letters if (preg_match('/[A-Za-z,.]+/', $weight)) { $weightNeedle = [',', '.']; $weightError = str_replace($weightNeedle, '', $weight); if (preg_match('/[A-Za-z]+/', $weightError) && preg_match('/[\d]+/', $weightError)) { $weight = "0"; } } if (strrpos($weight, ',')) { return str_replace(',', '.', $weight); } return $weight; } /** * Convert the weight according to the prestashop configuration weight unit, into kilograms * @param float $weight * @param string $weightType * @return string */ public static function convertWeightToKilograms(float $weight, string $weightType = 'kg'): string { switch ($weightType) { case ($weightType === 'g'): $weight = $weight / 1000; break; case ($weightType === 'lb'): $weight = $weight * 0.45359237; break; case ($weightType === 'oz'): $weight = $weight * 0.02834952; break; } return (string)round($weight, 2); } /** * Check the GSM validity * @param string $telDest * @return bool */ public static function checkGsmValidity(string $telDest): bool { $firstCheck = preg_match('/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,8}$/im', $telDest) ? $telDest : false; if ($firstCheck === false) { return false; } $gsmPatterns = [ '000000', '111111', '222222', '333333', '444444', '555555', '666666', '777777', '888888', '999999', '123456', '234567', '345678', '456789' ]; $telDestPattern = substr($telDest, -6); if ( !in_array($telDestPattern, $gsmPatterns) && ( /** * Austria */ preg_match('/^(\+|00)43(\s?\d{9,10})$/', $telDest) || /** * Belgium */ preg_match('/^(\+|00)32(\s?\d{8,9})$/', $telDest) || /** * Croatia */ preg_match('/^(\+|00)385(\s?\d{8,9})$/', $telDest) || /** * Czech */ preg_match('/^(\+|00)420(\s?\d{9,10})$/', $telDest) || /** * Denmark */ preg_match('/^(\+|00)45(\s?\d{8,9})$/', $telDest) || /** * Estonia */ preg_match('/^(\+|00)372(\s?\d{7,8})$/', $telDest) || /** * Finland */ preg_match('/^(\+|00)358(\s?\d{5,12})$/', $telDest) || /** * France */ preg_match('/^((\+33|0033|0)[67])(?:[ _.-]?(\d{2})){4}$/', $telDest) || /** * Germany */ preg_match('/^(\+|00)49(\s?\d{10,11})$/', $telDest) || /** * Hungary */ preg_match('/^(\+|00)36(\s?\d{8,9})$/', $telDest) || /** * Ireland */ preg_match('/^(\+|00)353(\s?\d{8,9})$/', $telDest) || /** * Italy */ preg_match('/^(\+|00)39(\s?\d{8,11})$/', $telDest) || /** * Latvia */ preg_match('/^(\+|00)371(\s?\d{8,9})$/', $telDest) || /** * Lithuania */ preg_match('/^(\+|00)370(\s?\d{8,9})$/', $telDest) || /** * Luxembourg */ preg_match('/^(\+|00)352(\s?\d{8,9})$/', $telDest) || /** * Netherlands */ preg_match('/^(\+|00)31(\s?\d{9,10})$/', $telDest) || /** * Poland */ preg_match('/^(\+|00)48(\s?\d{9,10})$/', $telDest) || /** * Portugal */ preg_match('/^(\+|00)351(\s?\d{9,10})$/', $telDest) || /** * Slovakia */ preg_match('/^(\+|00)421(\s?\d{9,10})$/', $telDest) || /** * Slovenia */ preg_match('/^(\+|00)386(\s?\d{8,9})$/', $telDest) || /** * Spain */ preg_match('/^(\+|00)34(\s?\d{9,10})$/', $telDest) || /** * Switzerland */ preg_match('/^(\+|00)41(\s?\d{9,10})$/', $telDest) || /** * Sweden */ preg_match('/^(\+|00)46(\s?\d{6,9})$/', $telDest) || /** * Uk */ preg_match('/^(\+|00)447([3456789]\d)(\s?\d{7})$/', $telDest) ) ) { return true; } else { return false; } } /** * Check if the user is authenticated * The authentication process depends on the environnement of the ajax request * @param bool $isFrontOffice Si true alors on vérifie qu'un customer ou customer[guest] est bien authentifié. Si non employee(admin). * @return bool */ public static function checkAjaxAuthValidity(bool $isFrontOffice): bool { $isAuthenticated = false; if ($isFrontOffice) { $customer = Context::getContext()->customer; if ($customer->isLogged(true)) { $isAuthenticated = true; } } else { $employeeCookie = new Cookie('psAdmin'); if ($employeeCookie->id_employee !== false && $employeeCookie->profile !== false) { $isAuthenticated = true; } } return $isAuthenticated; } /** * Check if id carrier is among the id carrier list * @param string $idCarrier * @param string $idCarrierList * @return bool */ public static function checkDeliveryService(string $idCarrier, string $idCarrierList): bool { $carrierList = explode('|', $idCarrierList); return in_array($idCarrier, $carrierList, true); } /** * Get delivery service for an order * * @param Order $order * @param int $idLang * @return string */ public static function getService($order, int $idLang): string { $orderIdCarrierQuery = new DbQuery(); $orderIdCarrierQuery->select('id_carrier') ->from('orders') ->where('id_order = '.(int)$order->id); $orderIdCarrier = Db::getInstance()->getValue($orderIdCarrierQuery); $carrierList = [ 'DPDFRANCE_CLASSIC_CARRIER_LOG' => 'CLA', 'DPDFRANCE_PREDICT_CARRIER_LOG' => 'PRE', 'DPDFRANCE_RELAIS_CARRIER_LOG' => 'REL', ]; foreach ($carrierList as $carrierListLog => $deliveryService) { if (self::checkDeliveryService( $orderIdCarrier, Configuration::get($carrierListLog, null, null, $idLang) )) { return $deliveryService; } } /** * Gestion des commandes avec un transporteur hors_dpd * Si id_carrier n'est pas dans les carrier_logs alors on identifie une commande comme hors_dpd * Si on trouve un numéro de téléphone associé à cette commande alors on associe le transporteur Predict, sinon on associe le transporteur Classic */ $orderPhoneNumberQuery = new DbQuery(); $orderPhoneNumberQuery->select('gsm_dest') ->from('dpdfrance_shipping') ->where('id_cart = '.(int)$order->id_cart.' AND id_carrier = '.(int)$order->id_carrier); $orderPhoneNumber = Db::getInstance()->getValue($orderPhoneNumberQuery); if (empty($orderPhoneNumber) === true) { $address_invoice = new Address($order->id_address_invoice, Context::getContext()->language->id); $address_delivery = new Address($order->id_address_delivery, Context::getContext()->language->id); $orderPhoneNumber = self::getPhoneNumberFromAddress($address_invoice, $address_delivery); unset($address_invoice, $address_delivery); if (!empty($orderPhoneNumber)) { $orderPhoneNumber = preg_replace('/\s+/', '', $orderPhoneNumber); /** * Si le numéro de téléphone est incorrect alors on assigne le transporteur Classic */ if (self::checkGsmValidity($orderPhoneNumber)) { return 'HDP_PRE'; } else { return 'HDP_CLA'; } } else { return 'HDP_CLA'; } } else { return 'HDP_PRE'; } } /** * Get the mobile phone ot the order * @param $address_invoice * @param $address_delivery * @return string * @throws PrestaShopExceptionCore */ public static function getPhoneNumberFromAddress($address_invoice, $address_delivery): string { if ( !is_object($address_invoice) && !is_object($address_delivery) ) { throw new PrestaShopExceptionCore(); } if (!empty($address_delivery->phone_mobile)) { return $address_delivery->phone_mobile; } if (!empty($address_invoice->phone_mobile)) { return $address_invoice->phone_mobile; } if (!empty($address_delivery->phone)) { return $address_delivery->phone; } if (!empty($address_invoice->phone)) { return $address_invoice->phone; } return ''; } /** * Converts country ISO code to DPD Station format * * @param int $idCountry * @return array|string|string[] */ public static function getIsoCodeByIdCountry(int $idCountry) { $sqlQuery = new DbQuery(); $sqlQuery->select('iso_code') ->from('country') ->where('id_country = '.(int)$idCountry); $result = Db::getInstance('_PS_USE_SQL_SLAVE_')->getRow($sqlQuery); $isoPS = [ 'DE', 'AD', 'AT', 'BE', 'BA', 'BG', 'HR', 'DK', 'ES', 'EE', 'FI', 'FR', 'GB', 'GR', 'GG', 'HU', 'IM', 'IE', 'IT', 'JE', 'LV', 'LI', 'LT', 'LU', 'MC', 'NO', 'NL', 'PL', 'PT', 'CZ', 'RO', 'RS', 'SK', 'SI', 'SE', 'CH', ]; $isoEP = [ 'D', 'AND', 'A', 'B', 'BA', 'BG', 'CRO', 'DK', 'E', 'EST', 'SF', 'F', 'GB', 'GR', 'GG', 'H', 'IM', 'IRL', 'I', 'JE', 'LET', 'LIE', 'LIT', 'L', 'F', 'N', 'NL', 'PL', 'P', 'CZ', 'RO', 'RS', 'SK', 'SLO', 'S', 'CH', ]; return in_array($result['iso_code'], $isoPS, true) ? // * IF IN ARRAY of the result (only one result) has one of the Prestashop ISO str_replace($isoPS, $isoEP, $result['iso_code']) : // * THEN Replace the Prestashop ISO Format TO DPD Station ISO Format (EUROPE) str_replace($result['iso_code'], 'INT', $result['iso_code']); // * ELSE Replace the Prestashop ISO Format TO 'INT' (intercontinental) } //TODO : Refacto function to merge getCargoFilteredIsoCodeByIdCountry() & getIsoCodeByIdCountry() together => seems possible /** * Converts country ISO code to DPD Station format * * @param int $idCountry * @return array|string|string[] */ public static function getCargoFilteredIsoCodeByIdCountry(int $idCountry) { $sqlQuery = new DbQuery(); $sqlQuery->select('iso_code') ->from('country') ->where('id_country = '.(int)$idCountry); $result = Db::getInstance('_PS_USE_SQL_SLAVE_')->getRow($sqlQuery); $isoPS = [ 'DE', 'AD', 'AT', 'BE', 'BA', 'BG', 'HR', 'DK', 'ES', 'EE', 'FI', 'FR', 'MC', 'GB', 'GR', 'HU', 'IM', 'IE', 'IT', 'LV', 'LI', 'LT', 'LU', 'NO', 'NL', 'PL', 'PT', 'CZ', 'RO', 'RS', 'SK', 'SI', 'SE', 'CH', 'UA', ]; $isoCargo = [ 'DE', 'AD', 'AT', 'BE', 'BA', 'BG', 'HR', 'DK', 'ES', 'EE', 'FI', 'FR', 'FR', 'GB', 'GR', 'HU', 'IM', 'IE', 'IT', 'LV', 'LI', 'LT', 'LU', 'NO', 'NL', 'PL', 'PT', 'CZ', 'RO', 'RS', 'SK', 'SI', 'SE', 'CH', 'UA', ]; return in_array($result['iso_code'], $isoPS, true) ? // * IF IN ARRAY of the result (only one result) has one of the Prestashop ISO str_replace($isoPS, $isoCargo, $result['iso_code']) : // * THEN Replace the Prestashop ISO Format to Cargo ISO Format str_replace($result['iso_code'], 'INT', $result['iso_code']); // * ELSE Replace the Prestashop ISO Format TO 'INT' (intercontinental) } /** * Get all orders but statuses cancelled, delivered, error * * @param int $id_shop * @return array * @throws PrestaShopDatabaseException */ public static function getAllOrders(int $id_shop): array { $id_shop = $id_shop == 0 ? 'LIKE "%"' : '= ' . $id_shop; $sql = new DbQuery(); $sql->select('id_order') ->from('orders', 'O') ->where('current_state NOT IN(' .(int)Configuration::get('DPDFRANCE_ETAPE_LIVRE', null, null, $id_shop).',' .self::ORDER_STATUS_DELIVERED.',' .self::ORDER_STATUS_CANCELLED.',' .self::ORDER_STATUS_REFUNDED.',' .self::ORDER_STATUS_PAYEMENT_ERROR. ')') ->where('O.id_shop '.$id_shop) ->orderBy('id_order DESC') ->limit(1000); $result = Db::getInstance()->executeS($sql); $orders = []; if (!empty($result)) { foreach ($result as $order) { $orders[] = (int)$order['id_order']; } } return $orders; } /** * Get delivery information from Cart Id in dpdfrance_shipping table * @param int $id_cart * @return array|bool|object|null */ public static function getDeliveryInfos(int $id_cart) { $sql = new DbQuery(); $sql->select('id_customer, id_cart, service, relay_id, company, address1, address2, postcode, city, id_country, gsm_dest') ->from('dpdfrance_shipping') ->where('id_cart = '.(int)$id_cart); return Db::getInstance()->getRow($sql); } /** * Get All France Zones * @return array|bool|mysqli_result|PDOStatement|resource|null * @throws PrestaShopDatabaseException */ public static function getAllFranceZones() { $sqlGetZone = new DbQuery(); $sqlGetZone->select('id_zone') ->from('zone') ->where('name LIKE \'%France%\''); return Db::getInstance()->executeS($sqlGetZone); } /** * Get Postal Code from an address ID * @param $id_address * @return false|mixed */ public static function getPostcodeByAddress($id_address) { $sqlQuery = new DbQuery(); $sqlQuery->select('postcode') ->from('address', 'a') ->where('a.id_address = '.(int)$id_address); $row = Db::getInstance()->getRow($sqlQuery); return !empty($row['postcode']) ? $row['postcode'] : false; } /** * Replaces accented characters and symbols * @param string $str * @param string $mode * @return array|false|string|string[]|null */ public static function stripAccents(string $str, string $mode = null) { $str = preg_replace('/[\x{00C0}\x{00C1}\x{00C2}\x{00C3}\x{00C4}\x{00C5}]/u', 'A', $str); $str = preg_replace('/[\x{0105}\x{0104}\x{00E0}\x{00E1}\x{00E2}\x{00E3}\x{00E4}\x{00E5}]/u', 'a', $str); $str = preg_replace('/[\x{00C7}\x{0106}\x{0108}\x{010A}\x{010C}]/u', 'C', $str); $str = preg_replace('/[\x{00E7}\x{0107}\x{0109}\x{010B}\x{010D}}]/u', 'c', $str); $str = preg_replace('/[\x{010E}\x{0110}]/u', 'D', $str); $str = preg_replace('/[\x{010F}\x{0111}]/u', 'd', $str); $str = preg_replace('/[\x{00C8}\x{00C9}\x{00CA}\x{00CB}\x{0112}\x{0114}\x{0116}\x{0118}\x{011A}\x{20AC}]/u', 'E', $str); $str = preg_replace('/[\x{00E8}\x{00E9}\x{00EA}\x{00EB}\x{0113}\x{0115}\x{0117}\x{0119}\x{011B}]/u', 'e', $str); $str = preg_replace('/[\x{00CC}\x{00CD}\x{00CE}\x{00CF}\x{0128}\x{012A}\x{012C}\x{012E}\x{0130}]/u', 'I', $str); $str = preg_replace('/[\x{00EC}\x{00ED}\x{00EE}\x{00EF}\x{0129}\x{012B}\x{012D}\x{012F}\x{0131}]/u', 'i', $str); $str = preg_replace('/[\x{0142}\x{0141}\x{013E}\x{013A}]/u', 'l', $str); $str = preg_replace('/[\x{00F1}\x{0148}]/u', 'n', $str); $str = preg_replace('/[\x{00D2}\x{00D3}\x{00D4}\x{00D5}\x{00D6}\x{00D8}]/u', 'O', $str); $str = preg_replace('/[\x{00F2}\x{00F3}\x{00F4}\x{00F5}\x{00F6}\x{00F8}]/u', 'o', $str); $str = preg_replace('/[\x{0159}\x{0155}]/u', 'r', $str); $str = preg_replace('/[\x{015B}\x{015A}\x{0161}]/u', 's', $str); $str = preg_replace('/[\x{00DF}]/u', 'ss', $str); $str = preg_replace('/[\x{0165}]/u', 't', $str); $str = preg_replace('/[\x{00D9}\x{00DA}\x{00DB}\x{00DC}\x{016E}\x{0170}\x{0172}]/u', 'U', $str); $str = preg_replace('/[\x{00F9}\x{00FA}\x{00FB}\x{00FC}\x{016F}\x{0171}\x{0173}]/u', 'u', $str); $str = preg_replace('/[\x{00FD}\x{00FF}]/u', 'y', $str); $str = preg_replace('/[\x{017C}\x{017A}\x{017B}\x{0179}\x{017E}]/u', 'z', $str); $str = preg_replace('/[\x{00C6}]/u', 'AE', $str); $str = preg_replace('/[\x{00E6}]/u', 'ae', $str); $str = preg_replace('/[\x{0152}]/u', 'OE', $str); $str = preg_replace('/[\x{0153}]/u', 'oe', $str); if ($mode === 'DPDStation') { $str = preg_replace('/[\x{2105}]/u', 'c/o', $str); $str = preg_replace('/[\x{2116}]/u', 'No', $str); $str = preg_replace('/[\x{0022}\x{0025}\x{0026}\x{0027}\x{00A1}\x{00A2}\x{00A3}\x{00A4}\x{00A5}\x{00A6}\x{00A7}\x{00A8}\x{00AA}\x{00AB}\x{00AC}\x{00AD}\x{00AE}\x{00AF}\x{00B0}\x{00B1}\x{00B2}\x{00B3}\x{00B4}\x{00B5}\x{00B6}\x{00B7}\x{00B8}\x{00BA}\x{00BB}\x{00BC}\x{00BD}\x{00BE}\x{00BF}\x{2019}]/u', ' ', $str); } else { $str = preg_replace('/[^\p{L}\p{N}]/u', ' ', $str); $str = Tools::strtoupper($str); } return $str; } /** * Get Rss flux DPD Informations * @return array */ public static function getDPDRssInfos(): array { // RSS stream $stream = []; $rss = @simplexml_load_string(Tools::file_get_contents(self::DPD_FLUX_RSS)); if (!empty($rss)) { if (empty($rss->channel->item)) { $stream['error'] = true; } else { foreach ($rss->channel->item as $i => $item) { $stream[$i] = [ 'category' => (string)$item->category, 'title' => (string)$item->title, 'description' => (string)$item->description, 'date' => strtotime((string)$item->pubDate), ]; } if (empty($stream)) { $stream['error'] = true; } } } else { $stream['error'] = true; } return $stream; } /** * Get the current search and relay pickup list of the user * @param int $idCart * @param int $idCustomer * @return array|bool|object|null */ public static function getCurrentRelaisDatasSearchAndRelaisList(int $idCart, int $idCustomer) { $sql = new DbQuery(); $sql->select('search_relay, relay_list') ->from('dpdfrance_relais_storage') ->where('id_cart = '.(int)$idCart) ->where('id_customer = '.(int)$idCustomer); $res = Db::getInstance()->getRow($sql); if ($res === false) { return false; } $res["search_relay"] = json_decode($res["search_relay"], true); $res["relay_list"] = json_decode($res["relay_list"], true); return $res; } /** * Set the current search and relay pickup list of the user * @param int $idCart * @param int $idCustomer * @param string $searchRelay * @param string $relayList * @return array|bool|object|null */ public static function setCurrentRelaisDatasSearchAndRelaisList( int $idCart, int $idCustomer, string $searchRelay, string $relayList ) { $sqlResult = Db::getInstance()->insert( 'dpdfrance_relais_storage', array( 'id_cart' => (int)$idCart, 'id_customer' => (int)$idCustomer, 'search_relay' => $searchRelay, 'relay_list' => $relayList ) ); return $sqlResult; } /** * Update the current search and relay pickup list of the user * @param int $idCart * @param int $idCustomer * @param string $searchRelay * @param string $relayList * @return bool|null */ public static function updateCurrentRelaisDatasSearchAndRelaisList( int $idCart, int $idCustomer, string $searchRelay, string $relayList ) { $sqlResult = Db::getInstance()->update( 'dpdfrance_relais_storage', array( 'search_relay' => $searchRelay, 'relay_list' => $relayList ), 'id_cart = '.(int)$idCart.' AND id_customer = '.(int)$idCustomer ); return $sqlResult; } /** * Delete the current search and relay pickup list of the user * @param int $idCart * @param int $idCustomer * @return bool|null */ public static function deleteCurrentRelaisDatasSearchAndRelaisList( int $idCart, int $idCustomer ): bool { $sqlResult = Db::getInstance()->delete( 'dpdfrance_relais_storage', 'id_cart = '.(int)$idCart.' AND id_customer = '.(int)$idCustomer ); return $sqlResult; } /** * Encrypt string * @param string $plainString * @return string */ public static function encrypt(string $plainString) { $key = 'DPDFrancePrestashopModule'; $ivlen = openssl_cipher_iv_length('AES-128-CBC'); $iv = openssl_random_pseudo_bytes($ivlen); $ciphertext_raw = openssl_encrypt( $plainString, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv ); $hmac = hash_hmac( 'sha256', $ciphertext_raw, $key, true ); return base64_encode($iv . $hmac . $ciphertext_raw); } /** * Decrypt string * @param string $encryptedString * @return false|string */ public static function decrypt(string $encryptedString) { if (empty($encryptedString)) { return false; } $key = 'DPDFrancePrestashopModule'; $cypherTextDec = base64_decode($encryptedString); $ivlen = openssl_cipher_iv_length('AES-128-CBC'); $iv = substr($cypherTextDec, 0, $ivlen); $hmac = substr($cypherTextDec, $ivlen, 32); $ciphertext_raw = substr($cypherTextDec, $ivlen + 32); $decrypted = openssl_decrypt($ciphertext_raw, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv); $calcmac = hash_hmac('sha256', $ciphertext_raw, $key, true); if (hash_equals($hmac, $calcmac)) { return $decrypted; } return false; } }
💾 保存文件
← 返回文件管理器