283 lines
11 KiB
PHP
283 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to a commercial license from Common-Services Co., Ltd.
|
|
* Use, copy, modification or distribution of this source file without written
|
|
* license agreement from the SARL SMC is strictly forbidden.
|
|
* In order to obtain a license, please contact us: contact@common-services.com
|
|
* ...........................................................................
|
|
* INFORMATION SUR LA LICENCE D'UTILISATION
|
|
*
|
|
* L'utilisation de ce fichier source est soumise a une licence commerciale
|
|
* concedee par la societe Common-Services Co., Ltd.
|
|
* Toute utilisation, reproduction, modification ou distribution du present
|
|
* fichier source sans contrat de licence ecrit de la part de la Common-Services Co. Ltd. est
|
|
* expressement interdite.
|
|
* Pour obtenir une licence, veuillez contacter Common-Services Co., Ltd. a l'adresse: contact@common-services.com
|
|
* ...........................................................................
|
|
*
|
|
* @package Colissimo Flexibilite
|
|
* @author Alexandre D.
|
|
* @copyright Copyright (c) 2011-2015 Common Services Co Ltd - 90/25 Sukhumvit 81 - 10260 Bangkok - Thailand
|
|
* @license Commercial license
|
|
* Support by mail : support.soflexibilite@common-services.com
|
|
*/
|
|
|
|
class SoFlexibiliteTools
|
|
{
|
|
|
|
public static function fileGetContents($url, $use_include_path = false, $stream_context = null, $curl_timeout = 30)
|
|
{
|
|
if ($stream_context == null && preg_match('/^https?:\/\//', $url)) {
|
|
$contextOptions = array();
|
|
|
|
if (preg_match('/^https:\/\//', $url)) {
|
|
$contextOptions = array(
|
|
'ssl' => array(
|
|
'verify_peer' => true,
|
|
'cafile' => sprintf('%s/%s', dirname(dirname(__FILE__)), 'cert/cacert.pem'),
|
|
)
|
|
);
|
|
}
|
|
|
|
$stream_context = @stream_context_create(
|
|
array('http' => array('timeout' => $curl_timeout)),
|
|
$contextOptions
|
|
);
|
|
}
|
|
|
|
if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url)) {
|
|
return file_get_contents($url, $use_include_path, $stream_context);
|
|
} elseif (function_exists('curl_init')) {
|
|
$curl = curl_init();
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $curl_timeout);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
|
|
curl_setopt($curl, CURLOPT_CAINFO, sprintf('%s/%s', dirname(dirname(__FILE__)), 'cert/cacert.pem'));
|
|
if ($stream_context != null) {
|
|
$opts = stream_context_get_options($stream_context);
|
|
if (isset($opts['http']['method']) && Tools::strtolower($opts['http']['method']) == 'post') {
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
if (isset($opts['http']['content'])) {
|
|
parse_str($opts['http']['content'], $post_data);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
|
|
}
|
|
}
|
|
}
|
|
|
|
$content = curl_exec($curl);
|
|
curl_close($curl);
|
|
|
|
return $content;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function moduleIsInstalled($module_name)
|
|
{
|
|
if (method_exists('Module', 'isInstalled')) {
|
|
return (Module::isInstalled($module_name));
|
|
} else {
|
|
Db::getInstance()->executeS(
|
|
'SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = \''.pSQL($module_name).'\''
|
|
);
|
|
return (bool)Db::getInstance()->numRows();
|
|
}
|
|
}
|
|
|
|
|
|
public static function copy($source, $destination, $stream_context = null)
|
|
{
|
|
if (is_null($stream_context) && !preg_match('/^https?:\/\//', $source)) {
|
|
return @copy($source, $destination);
|
|
}
|
|
return @file_put_contents($destination, Tools::file_get_contents($source, false, $stream_context));
|
|
}
|
|
|
|
|
|
public static function carrierIsEnabled($id_carrier)
|
|
{
|
|
return ((bool)Db::getInstance()->getValue(
|
|
'SELECT `active` FROM `'._DB_PREFIX_.'carrier` WHERE `id_carrier` = '.(int)$id_carrier
|
|
));
|
|
}
|
|
|
|
public static function getTables()
|
|
{
|
|
$tables = array();
|
|
$query = Db::getInstance()->executeS('SHOW TABLES');
|
|
foreach ($query as $rows) {
|
|
foreach ($rows as $table) {
|
|
$tables[$table] = 1;
|
|
}
|
|
}
|
|
|
|
return $tables;
|
|
}
|
|
|
|
public static function checkTablesExist($compat_type = 1)
|
|
{
|
|
$sql = null;
|
|
$tables = self::getTables();
|
|
|
|
if ($compat_type == 1 && !isset($tables[_DB_PREFIX_.'socolissimo_delivery_info'])) {
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'socolissimo_delivery_info` (
|
|
`id_cart` INT(10) NOT NULL,
|
|
`id_customer` INT(10) NOT NULL,
|
|
`delivery_mode` VARCHAR(3) NOT NULL,
|
|
`prid` TEXT(10) NOT NULL,
|
|
`prname` VARCHAR(64) NOT NULL,
|
|
`prfirstname` VARCHAR(64) NOT NULL,
|
|
`prcompladress` TEXT NOT NULL,
|
|
`pradress1` TEXT NOT NULL,
|
|
`pradress2` TEXT NOT NULL,
|
|
`pradress3` TEXT NOT NULL,
|
|
`pradress4` TEXT NOT NULL,
|
|
`przipcode` TEXT(10) NOT NULL,
|
|
`prtown` VARCHAR(64) NOT NULL,
|
|
`cecountry` VARCHAR(10) NOT NULL,
|
|
`cephonenumber` VARCHAR(15) NOT NULL,
|
|
`ceemail` VARCHAR(64) NOT NULL,
|
|
`cecompanyname` VARCHAR(64) NOT NULL,
|
|
`cedeliveryinformation` TEXT NOT NULL,
|
|
`cedoorcode1` VARCHAR(10) NOT NULL,
|
|
`cedoorcode2` VARCHAR(10) NOT NULL,
|
|
PRIMARY KEY (`id_cart`,`id_customer`)
|
|
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';
|
|
} elseif (!isset($tables[_DB_PREFIX_.'so_delivery'])) {
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'so_delivery` (
|
|
`id` INT(11) NOT NULL AUTO_INCREMENT,
|
|
`order_id` INT(11) DEFAULT NULL,
|
|
`cart_id` INT(11) DEFAULT NULL,
|
|
`point_id` INT(11) NOT NULL,
|
|
`customer_id` INT(11) NOT NULL,
|
|
`firstname` VARCHAR(38) DEFAULT NULL,
|
|
`lastname` VARCHAR(38) DEFAULT NULL,
|
|
`company` VARCHAR(38) DEFAULT NULL,
|
|
`telephone` VARCHAR(10) DEFAULT NULL,
|
|
`email` VARCHAR(64) DEFAULT NULL,
|
|
`type` VARCHAR(3) DEFAULT NULL,
|
|
`libelle` VARCHAR(50) DEFAULT NULL,
|
|
`indice` VARCHAR(70) DEFAULT NULL,
|
|
`code_postal` VARCHAR(5) DEFAULT NULL,
|
|
`commune` VARCHAR(32) DEFAULT NULL,
|
|
`pays` VARCHAR(32) NOT NULL,
|
|
`adresse1` VARCHAR(38) DEFAULT NULL,
|
|
`adresse2` VARCHAR(38) DEFAULT NULL,
|
|
`lieudit` VARCHAR(38) DEFAULT NULL,
|
|
`informations` TEXT,
|
|
`codereseau` VARCHAR(3) DEFAULT NULL,
|
|
PRIMARY KEY (`id`),
|
|
UNIQUE KEY `u_order_id` (`order_id`),
|
|
UNIQUE KEY `u_cart_id` (`cart_id`)
|
|
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=10000000;';
|
|
}
|
|
|
|
if ($sql && !Db::getInstance()->execute($sql)) {
|
|
ConfigureMessage::error(sprintf(
|
|
'%s `%s`',
|
|
'Error while creating table',
|
|
($compat_type == 1) ? _DB_PREFIX_.'socolissimo_delivery_info' : _DB_PREFIX_.'so_delivery'
|
|
));
|
|
}
|
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'soflexibilite_carrier_fee_check` (
|
|
`id_cart` INT(10) NOT NULL,
|
|
`id_customer` INT(10) NOT NULL,
|
|
`iso_code` VARCHAR(3) NOT NULL,
|
|
`timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
|
PRIMARY KEY (`id_cart`, `id_customer`)
|
|
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';
|
|
|
|
if (!Db::getInstance()->execute($sql)) {
|
|
ConfigureMessage::error(sprintf(
|
|
'%s `%s`',
|
|
'Error while creating table',
|
|
_DB_PREFIX_.'soflexibilite_carrier_fee_check'
|
|
));
|
|
}
|
|
}
|
|
|
|
public static function checkFieldsExist($compat_type = 1)
|
|
{
|
|
$tables = self::getTables();
|
|
$fields = array();
|
|
|
|
if ($compat_type == 1 && isset($tables[_DB_PREFIX_.'socolissimo_delivery_info'])) {
|
|
$query = Db::getInstance()->executeS('SHOW COLUMNS FROM `'._DB_PREFIX_.'socolissimo_delivery_info`');
|
|
foreach ($query as $row) {
|
|
$fields[$row['Field']] = 1;
|
|
}
|
|
|
|
if (!isset($fields['codereseau'])) {
|
|
$sql =
|
|
'ALTER TABLE `'._DB_PREFIX_.'socolissimo_delivery_info`
|
|
ADD `codereseau` VARCHAR(3) DEFAULT NULL';
|
|
|
|
if (!Db::getInstance()->execute($sql)) {
|
|
ConfigureMessage::error(
|
|
'Error while updating table `socolissimo_delivery_info`.'
|
|
);
|
|
} else {
|
|
ConfigureMessage::success(
|
|
'Field `codereseau` added to the table `socolissimo_delivery_info`.'
|
|
);
|
|
}
|
|
}
|
|
} elseif ($compat_type == 2 && isset($tables[_DB_PREFIX_.'so_delivery'])) {
|
|
$query = Db::getInstance()->executeS('SHOW COLUMNS FROM `'._DB_PREFIX_.'so_delivery`');
|
|
foreach ($query as $row) {
|
|
$fields[$row['Field']] = 1;
|
|
}
|
|
|
|
if (!isset($fields['codereseau'])) {
|
|
$sql = 'ALTER TABLE `'._DB_PREFIX_.'so_delivery` ADD `codereseau` VARCHAR(3) DEFAULT NULL';
|
|
if (!Db::getInstance()->execute($sql)) {
|
|
ConfigureMessage::error('Error while updating table `so_delivery`.');
|
|
} else {
|
|
ConfigureMessage::success('Field `codereseau` added to the table `so_delivery`.');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function arrayMapCastInt($v)
|
|
{
|
|
return (int)$v;
|
|
}
|
|
|
|
public static function arrayFilterNullOrLowerThanEqualZero($v)
|
|
{
|
|
return $v != null && $v != -1;
|
|
}
|
|
|
|
/**
|
|
* Replacement for array_column, only available from PHP 5.5.0
|
|
* @see http://php.net/manual/fr/function.array-column.php
|
|
* @param $array
|
|
* @param $column_name
|
|
* @return array
|
|
*/
|
|
public static function arrayColumn($array, $column_name)
|
|
{
|
|
if (function_exists('array_column')) {
|
|
return array_column($array, $column_name);
|
|
}
|
|
|
|
return array_map(
|
|
array(__CLASS__, 'arrayColumFunctionDoubleParameters'),
|
|
$array,
|
|
array_fill(0, count($array), $column_name)
|
|
);
|
|
}
|
|
|
|
private static function arrayColumFunctionDoubleParameters($element, $column_name)
|
|
{
|
|
return $element[$column_name];
|
|
}
|
|
}
|