/**
     * This function now replaces doUpgrade.php or upgrade.php
     *
     * @return void
     */
    public function doUpgrade()
    {
        // Initialize
        // setting the memory limit to 128M only if current is lower
        $memory_limit = ini_get('memory_limit');
        if (substr($memory_limit, -1) != 'G' && ((substr($memory_limit, -1) == 'M' and substr($memory_limit, 0, -1) < 128) || is_numeric($memory_limit) and intval($memory_limit) < 131072)) {
            @ini_set('memory_limit', '128M');
        }
        /* Redefine REQUEST_URI if empty (on some webservers...) */
        if (!isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI'])) {
            if (!isset($_SERVER['SCRIPT_NAME']) && isset($_SERVER['SCRIPT_FILENAME'])) {
                $_SERVER['SCRIPT_NAME'] = $_SERVER['SCRIPT_FILENAME'];
            }
            if (isset($_SERVER['SCRIPT_NAME'])) {
                if (basename($_SERVER['SCRIPT_NAME']) == 'index.php' && empty($_SERVER['QUERY_STRING'])) {
                    $_SERVER['REQUEST_URI'] = dirname($_SERVER['SCRIPT_NAME']) . '/';
                } else {
                    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
                    if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
                        $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
                    }
                }
            }
        }
        $_SERVER['REQUEST_URI'] = str_replace('//', '/', $_SERVER['REQUEST_URI']);
        define('INSTALL_VERSION', $this->install_version);
        // 1.4
        define('INSTALL_PATH', realpath($this->latestRootDir . DIRECTORY_SEPARATOR . 'install'));
        // 1.5 ...
        define('_PS_INSTALL_PATH_', INSTALL_PATH . DIRECTORY_SEPARATOR);
        define('PS_INSTALLATION_IN_PROGRESS', true);
        define('SETTINGS_FILE', $this->prodRootDir . '/config/settings.inc.php');
        define('DEFINES_FILE', $this->prodRootDir . '/config/defines.inc.php');
        define('INSTALLER__PS_BASE_URI', substr($_SERVER['REQUEST_URI'], 0, -1 * (strlen($_SERVER['REQUEST_URI']) - strrpos($_SERVER['REQUEST_URI'], '/')) - strlen(substr(dirname($_SERVER['REQUEST_URI']), strrpos(dirname($_SERVER['REQUEST_URI']), '/') + 1))));
        //	define('INSTALLER__PS_BASE_URI_ABSOLUTE', 'http://'.ToolsInstall::getHttpHost(false, true).INSTALLER__PS_BASE_URI);
        // XML Header
        // header('Content-Type: text/xml');
        $filePrefix = 'PREFIX_';
        $engineType = 'ENGINE_TYPE';
        $mysqlEngine = defined('_MYSQL_ENGINE_') ? _MYSQL_ENGINE_ : 'MyISAM';
        if (function_exists('date_default_timezone_set')) {
            date_default_timezone_set('Europe/Paris');
        }
        // if _PS_ROOT_DIR_ is defined, use it instead of "guessing" the module dir.
        if (defined('_PS_ROOT_DIR_') and !defined('_PS_MODULE_DIR_')) {
            define('_PS_MODULE_DIR_', _PS_ROOT_DIR_ . '/modules/');
        } else {
            if (!defined('_PS_MODULE_DIR_')) {
                define('_PS_MODULE_DIR_', INSTALL_PATH . '/../modules/');
            }
        }
        $upgrade_dir_php = 'upgrade/php';
        if (!file_exists(INSTALL_PATH . DIRECTORY_SEPARATOR . $upgrade_dir_php)) {
            $upgrade_dir_php = 'php';
            if (!file_exists(INSTALL_PATH . DIRECTORY_SEPARATOR . $upgrade_dir_php)) {
                $this->next = 'error';
                $this->next_desc = $this->l('/install/upgrade/php directory is missing in archive or directory');
                $this->nextQuickInfo[] = '/install/upgrade/php directory is missing in archive or directory';
                $this->nextErrors[] = '/install/upgrade/php directory is missing in archive or directory.';
                return false;
            }
        }
        define('_PS_INSTALLER_PHP_UPGRADE_DIR_', INSTALL_PATH . DIRECTORY_SEPARATOR . $upgrade_dir_php . DIRECTORY_SEPARATOR);
        //old version detection
        global $oldversion, $logger;
        $oldversion = false;
        if (file_exists(SETTINGS_FILE)) {
            include_once SETTINGS_FILE;
            // include_once(DEFINES_FILE);
            $oldversion = _PS_VERSION_;
        } else {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('The config/settings.inc.php file was not found.');
            $this->nextErrors[] = $this->l('The config/settings.inc.php file was not found.');
            return false;
        }
        if (!defined('__PS_BASE_URI__')) {
            define('__PS_BASE_URI__', realpath(dirname($_SERVER['SCRIPT_NAME'])) . '/../../');
        }
        if (!defined('_THEMES_DIR_')) {
            define('_THEMES_DIR_', __PS_BASE_URI__ . 'themes/');
        }
        $oldversion = _PS_VERSION_;
        $versionCompare = version_compare(INSTALL_VERSION, $oldversion);
        if ($versionCompare == '-1') {
            $this->next = 'error';
            $this->nextQuickInfo[] = sprintf('current version : %1$s. install version : %2$s', $oldversion, INSTALL_VERSION);
            $this->nextErrors[] = sprintf('current version : %1$s. install version : %2$s', $oldversion, INSTALL_VERSION);
            $this->nextQuickInfo[] = '[ERROR] version to install is too old ';
            $this->nextErrors[] = '[ERROR] version to install is too old ';
            return false;
        } elseif ($versionCompare == 0) {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l(sprintf('You already have the %s version.', INSTALL_VERSION));
            $this->nextErrors[] = $this->l(sprintf('You already have the %s version.', INSTALL_VERSION));
            return false;
        } elseif ($versionCompare === false) {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('There is no older version. Did you delete or rename the config/settings.inc.php file?');
            $this->nextErrors[] = $this->l('There is no older version. Did you delete or rename the config/settings.inc.php file?');
            return false;
        }
        //check DB access
        $this->db;
        error_reporting(E_ALL);
        $resultDB = MySql::tryToConnect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_, _DB_NAME_);
        if ($resultDB !== 0) {
            // $logger->logError('Invalid database configuration.');
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('Invalid database configuration');
            $this->nextErrors[] = $this->l('Invalid database configuration');
            return false;
        }
        //custom sql file creation
        $upgradeFiles = array();
        $upgrade_dir_sql = INSTALL_PATH . '/upgrade/sql';
        // if 1.4;
        if (!file_exists($upgrade_dir_sql)) {
            $upgrade_dir_sql = INSTALL_PATH . '/sql/upgrade';
        }
        if (!file_exists($upgrade_dir_sql)) {
            $this->next = 'error';
            $this->next_desc = $this->l('unable to find upgrade directory in the install path');
            return false;
        }
        if ($handle = opendir($upgrade_dir_sql)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != '.' and $file != '..') {
                    $upgradeFiles[] = str_replace(".sql", "", $file);
                }
            }
            closedir($handle);
        }
        if (empty($upgradeFiles)) {
            $this->next = 'error';
            $this->nextQuickInfo[] = sprintf($this->l('Cannot find the sql upgrade files. Please verify that the %s folder is not empty'), $upgrade_dir_sql);
            $this->nextErrors[] = sprintf($this->l('Cannot find the sql upgrade files. Please verify that the %s folder is not empty'), $upgrade_dir_sql);
            // fail 31
            return false;
        }
        natcasesort($upgradeFiles);
        $neededUpgradeFiles = array();
        $arrayVersion = explode('.', $oldversion);
        $versionNumbers = count($arrayVersion);
        if ($versionNumbers != 4) {
            $arrayVersion = array_pad($arrayVersion, 4, '0');
        }
        $oldversion = implode('.', $arrayVersion);
        foreach ($upgradeFiles as $version) {
            if (version_compare($version, $oldversion) == 1 && version_compare(INSTALL_VERSION, $version) != -1) {
                $neededUpgradeFiles[] = $version;
            }
        }
        if (empty($neededUpgradeFiles) || count($neededUpgradeFiles) === 0) {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('No upgrade is possible.');
            $this->nextErrors[] = $this->l('No upgrade is possible.');
            return false;
        }
        $sqlContentVersion = array();
        if ($this->deactivateCustomModule) {
            require_once _PS_INSTALLER_PHP_UPGRADE_DIR_ . 'deactivate_custom_modules.php';
            deactivate_custom_modules();
        }
        foreach ($neededUpgradeFiles as $version) {
            $file = $upgrade_dir_sql . DIRECTORY_SEPARATOR . $version . '.sql';
            if (!file_exists($file)) {
                $this->next = 'error';
                $this->nextQuickInfo[] = sprintf($this->l('Error while loading sql upgrade file "%s.sql".'), $version);
                $this->nextErrors[] = sprintf($this->l('Error while loading sql upgrade file "%s.sql".'), $version);
                return false;
                $logger->logError('Error while loading sql upgrade file.');
            }
            if (!($sqlContent = file_get_contents($file) . "\n")) {
                $this->next = 'error';
                $this->nextQuickInfo[] = $this->l(sprintf('Error while loading sql upgrade file %s.', $version));
                $this->nextErrors[] = $this->l(sprintf('Error while loading sql upgrade file %s.', $version));
                return false;
                $logger->logError(sprintf('Error while loading sql upgrade file %s.', $version));
            }
            $sqlContent = str_replace(array($filePrefix, $engineType), array(_DB_PREFIX_, $mysqlEngine), $sqlContent);
            $sqlContent = preg_split("/;\\s*[\r\n]+/", $sqlContent);
            $sqlContentVersion[$version] = $sqlContent;
        }
        //sql file execution
        global $requests, $warningExist;
        $requests = '';
        $warningExist = false;
        // Configuration::loadConfiguration();
        $request = '';
        foreach ($sqlContentVersion as $upgrade_file => $sqlContent) {
            foreach ($sqlContent as $query) {
                $query = trim($query);
                if (!empty($query)) {
                    /* If php code have to be executed */
                    if (strpos($query, '/* PHP:') !== false) {
                        /* Parsing php code */
                        $pos = strpos($query, '/* PHP:') + strlen('/* PHP:');
                        $phpString = substr($query, $pos, strlen($query) - $pos - strlen(' */;'));
                        $php = explode('::', $phpString);
                        preg_match('/\\((.*)\\)/', $phpString, $pattern);
                        $paramsString = trim($pattern[0], '()');
                        preg_match_all('/([^,]+),? ?/', $paramsString, $parameters);
                        if (isset($parameters[1])) {
                            $parameters = $parameters[1];
                        } else {
                            $parameters = array();
                        }
                        if (is_array($parameters)) {
                            foreach ($parameters as &$parameter) {
                                $parameter = str_replace('\'', '', $parameter);
                            }
                        }
                        // reset phpRes to a null value
                        $phpRes = null;
                        /* Call a simple function */
                        if (strpos($phpString, '::') === false) {
                            $func_name = str_replace($pattern[0], '', $php[0]);
                            if (version_compare(INSTALL_VERSION, '1.5.5.0', '=') && $func_name == 'fix_download_product_feature_active') {
                                continue;
                            }
                            if (!file_exists(_PS_INSTALLER_PHP_UPGRADE_DIR_ . strtolower($func_name) . '.php')) {
                                $this->nextQuickInfo[] = '<div class="upgradeDbError">[ERROR] ' . $upgrade_file . ' PHP - missing file ' . $query . '</div>';
                                $this->nextErrors[] = '[ERROR] ' . $upgrade_file . ' PHP - missing file ' . $query;
                                $warningExist = true;
                            } else {
                                require_once _PS_INSTALLER_PHP_UPGRADE_DIR_ . strtolower($func_name) . '.php';
                                $phpRes = call_user_func_array($func_name, $parameters);
                            }
                        } else {
                            $func_name = array($php[0], str_replace($pattern[0], '', $php[1]));
                            $this->nextQuickInfo[] = '<div class="upgradeDbError">[ERROR] ' . $upgrade_file . ' PHP - Object Method call is forbidden ( ' . $php[0] . '::' . str_replace($pattern[0], '', $php[1]) . ')</div>';
                            $this->nextErrors[] = '[ERROR] ' . $upgrade_file . ' PHP - Object Method call is forbidden (' . $php[0] . '::' . str_replace($pattern[0], '', $php[1]) . ')';
                            $warningExist = true;
                        }
                        if (isset($phpRes) && (is_array($phpRes) && !empty($phpRes['error'])) || $phpRes === false) {
                            // $this->next = 'error';
                            $this->nextQuickInfo[] = '
								<div class="upgradeDbError">
									[ERROR] PHP ' . $upgrade_file . ' ' . $query . '
									' . (empty($phpRes['error']) ? '' : $phpRes['error']) . '
									' . (empty($phpRes['msg']) ? '' : ' - ' . $phpRes['msg']) . '
								</div>';
                            $this->nextErrors[] = '
								[ERROR] PHP ' . $upgrade_file . ' ' . $query . '
								' . (empty($phpRes['error']) ? '' : $phpRes['error']) . '
								' . (empty($phpRes['msg']) ? '' : ' - ' . $phpRes['msg']);
                            $warningExist = true;
                        } else {
                            $this->nextQuickInfo[] = '<div class="upgradeDbOk">[OK] PHP ' . $upgrade_file . ' : ' . $query . '</div>';
                        }
                        if (isset($phpRes)) {
                            unset($phpRes);
                        }
                    } elseif (!$this->db->execute($query, false)) {
                        $error = $this->db->getMsgError();
                        $error_number = $this->db->getNumberError();
                        $this->nextQuickInfo[] = '
								<div class="upgradeDbError">
								[WARNING] SQL ' . $upgrade_file . '
								' . $error_number . ' in ' . $query . ': ' . $error . '</div>';
                        if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_ || !in_array($error_number, array('1050', '1060', '1061', '1062', '1091'))) {
                            $this->nextErrors[] = '[ERROR] SQL ' . $upgrade_file . ' ' . $error_number . ' in ' . $query . ': ' . $error;
                            $warningExist = true;
                        }
                    } else {
                        $this->nextQuickInfo[] = '<div class="upgradeDbOk">[OK] SQL ' . $upgrade_file . ' ' . $query . '</div>';
                    }
                }
                if (isset($query)) {
                    unset($query);
                }
            }
        }
        if ($this->next == 'error') {
            $this->next_desc = $this->l('An error happen during database upgrade');
            return false;
        }
        $this->nextQuickInfo[] = $this->l('Upgrade Db Ok');
        // no error !
        # At this point, database upgrade is over.
        # Now we need to add all previous missing settings items, and reset cache and compile directories
        $this->writeNewSettings();
        // Settings updated, compile and cache directories must be emptied
        $arrayToClean[] = $this->prodRootDir . '/tools/smarty/cache/';
        $arrayToClean[] = $this->prodRootDir . '/tools/smarty/compile/';
        $arrayToClean[] = $this->prodRootDir . '/tools/smarty_v2/cache/';
        $arrayToClean[] = $this->prodRootDir . '/tools/smarty_v2/compile/';
        if (version_compare(INSTALL_VERSION, '1.5.0.0', '>')) {
            $arrayToClean[] = $this->prodRootDir . '/cache/smarty/cache/';
            $arrayToClean[] = $this->prodRootDir . '/cache/smarty/compile/';
        }
        foreach ($arrayToClean as $dir) {
            if (!file_exists($dir)) {
                $this->nextQuickInfo[] = sprintf($this->l('[SKIP] directory "%s" doesn\'t exist and cannot be emptied.'), str_replace($this->prodRootDir, '', $dir));
                continue;
            } else {
                foreach (scandir($dir) as $file) {
                    if ($file[0] != '.' && $file != 'index.php' && $file != '.htaccess') {
                        if (is_file($dir . $file)) {
                            unlink($dir . $file);
                        } elseif (is_dir($dir . $file)) {
                            self::deleteDirectory($dir . $file);
                        }
                        $this->nextQuickInfo[] = sprintf($this->l('[cleaning cache] %s removed'), $file);
                    }
                }
            }
        }
        if (version_compare(INSTALL_VERSION, '1.5.0.0', '>')) {
            // Remove class_index Autoload cache
            if (file_exists(_PS_ROOT_DIR_ . '/cache/class_index.php')) {
                unlink(_PS_ROOT_DIR_ . '/cache/class_index.php');
            }
            if (defined('_THEME_NAME_') && $this->updateDefaultTheme && preg_match('#(default|prestashop)$#', _THEME_NAME_)) {
                $separator = addslashes(DIRECTORY_SEPARATOR);
                $file = _PS_ROOT_DIR_ . $separator . 'themes' . $separator . _THEME_NAME_ . $separator . 'cache' . $separator;
                if (file_exists($file)) {
                    foreach (scandir($file) as $cache) {
                        if ($cache[0] != '.' && $cache != 'index.php' && $cache != '.htaccess' && file_exists($file . $cache) && !is_dir($file . $cache)) {
                            if (file_exists($dir . $cache)) {
                                unlink($file . $cache);
                            }
                        }
                    }
                }
            }
            if (version_compare(_PS_VERSION_, '1.5.0.0', '<=')) {
                $dir = _PS_ROOT_DIR_ . '/controllers/';
                if (file_exists($dir)) {
                    foreach (scandir($dir) as $file) {
                        if (!is_dir($file) && $file[0] != '.' && $file != 'index.php' && $file != '.htaccess') {
                            if (file_exists($dir . basename(str_replace('.php', '', $file) . '.php'))) {
                                unlink($dir . basename($file));
                            }
                        }
                    }
                }
                $dir = _PS_ROOT_DIR_ . '/classes/';
                foreach (self::$classes14 as $class) {
                    if (file_exists($dir . basename($class) . '.php')) {
                        unlink($dir . basename($class) . '.php');
                    }
                }
                $dir = _PS_ADMIN_DIR_ . '/tabs/';
                if (file_exists($dir)) {
                    foreach (scandir($dir) as $file) {
                        if (!is_dir($file) && $file[0] != '.' && $file != 'index.php' && $file != '.htaccess') {
                            if (file_exists($dir . basename(str_replace('.php', '', $file) . '.php'))) {
                                unlink($dir . basename($file));
                            }
                        }
                    }
                }
                Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'configuration` SET value=0 WHERE name=\'PS_REWRITING_SETTINGS\'');
                if ($this->updateDefaultTheme) {
                    Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'shop` 
						SET id_theme = (SELECT id_theme FROM `' . _DB_PREFIX_ . 'theme` WHERE name LIKE \'default\' WHERE id_shop = 1 AND id_theme = 1)');
                    Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'theme` WHERE  name LIKE \'prestashop\' LIMIT 1');
                }
            }
        }
        // delete cache filesystem if activated
        if (defined('_PS_CACHE_ENABLED_') && _PS_CACHE_ENABLED_) {
            $depth = (int) $this->db->getValue('SELECT value
				FROM ' . _DB_PREFIX_ . 'configuration
				WHERE name = "PS_CACHEFS_DIRECTORY_DEPTH"');
            if ($depth) {
                if (!defined('_PS_CACHEFS_DIRECTORY_')) {
                    define('_PS_CACHEFS_DIRECTORY_', $this->prodRootDir . '/cache/cachefs/');
                }
                self::deleteDirectory(_PS_CACHEFS_DIRECTORY_, false);
                if (class_exists('CacheFs', false)) {
                    self::createCacheFsDirectories((int) $depth);
                }
            }
        }
        // we do not use class Configuration because it's not loaded;
        $this->db->execute('UPDATE `' . _DB_PREFIX_ . 'configuration`
			SET value="0" WHERE name = "PS_HIDE_OPTIMIZATION_TIS"', false);
        $this->db->execute('UPDATE `' . _DB_PREFIX_ . 'configuration`
			SET value="1" WHERE name = "PS_NEED_REBUILD_INDEX"', false);
        $this->db->execute('UPDATE `' . _DB_PREFIX_ . 'configuration`
			SET value="' . INSTALL_VERSION . '" WHERE name = "PS_VERSION_DB"', false);
        if ($warningExist) {
            $this->warning_exists = true;
            $this->nextQuickInfo[] = $this->l('Warning detected during upgrade.');
            $this->nextErrors[] = $this->l('Warning detected during upgrade.');
            $this->next_desc = $this->l('Warning detected during upgrade.');
        } else {
            $this->next_desc = $this->l('Database upgrade completed');
        }
        return true;
    }
    /**
     * This function now replaces doUpgrade.php or upgrade.php
     * 
     * @return void
     */
    public function doUpgrade()
    {
        // Initialize
        // setting the memory limit to 128M only if current is lower
        $memory_limit = ini_get('memory_limit');
        if (substr($memory_limit, -1) != 'G' and (substr($memory_limit, -1) == 'M' and substr($memory_limit, 0, -1) < 128 or is_numeric($memory_limit) and intval($memory_limit) < 131072)) {
            @ini_set('memory_limit', '128M');
        }
        /* Redefine REQUEST_URI if empty (on some webservers...) */
        if (!isset($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI'] == '') {
            $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
        }
        if ($tmp = strpos($_SERVER['REQUEST_URI'], '?')) {
            $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 0, $tmp);
        }
        $_SERVER['REQUEST_URI'] = str_replace('//', '/', $_SERVER['REQUEST_URI']);
        define('INSTALL_VERSION', $this->install_version);
        define('INSTALL_PATH', realpath($this->latestRootDir . DIRECTORY_SEPARATOR . 'install'));
        define('PS_INSTALLATION_IN_PROGRESS', true);
        define('SETTINGS_FILE', $this->prodRootDir . '/config/settings.inc.php');
        define('DEFINES_FILE', $this->prodRootDir . '/config/defines.inc.php');
        define('INSTALLER__PS_BASE_URI', substr($_SERVER['REQUEST_URI'], 0, -1 * (strlen($_SERVER['REQUEST_URI']) - strrpos($_SERVER['REQUEST_URI'], '/')) - strlen(substr(dirname($_SERVER['REQUEST_URI']), strrpos(dirname($_SERVER['REQUEST_URI']), '/') + 1))));
        //	define('INSTALLER__PS_BASE_URI_ABSOLUTE', 'http://'.ToolsInstall::getHttpHost(false, true).INSTALLER__PS_BASE_URI);
        // XML Header
        // header('Content-Type: text/xml');
        $filePrefix = 'PREFIX_';
        $engineType = 'ENGINE_TYPE';
        $mysqlEngine = defined('_MYSQL_ENGINE_') ? _MYSQL_ENGINE_ : 'MyISAM';
        if (function_exists('date_default_timezone_set')) {
            date_default_timezone_set('Europe/Paris');
        }
        // if _PS_ROOT_DIR_ is defined, use it instead of "guessing" the module dir.
        if (defined('_PS_ROOT_DIR_') and !defined('_PS_MODULE_DIR_')) {
            define('_PS_MODULE_DIR_', _PS_ROOT_DIR_ . '/modules/');
        } else {
            if (!defined('_PS_MODULE_DIR_')) {
                define('_PS_MODULE_DIR_', INSTALL_PATH . '/../modules/');
            }
        }
        // @todo upgrade_dir_php should be handled by Upgrader class
        $upgrade_dir_php = 'upgrade/php';
        if (!file_exists(INSTALL_PATH . DIRECTORY_SEPARATOR . $upgrade_dir_php)) {
            $upgrade_dir_php = 'php';
            if (!file_exists(INSTALL_PATH . DIRECTORY_SEPARATOR . $upgrade_dir_php)) {
                $this->next = 'error';
                $this->nextDesc = $this->l('php upgrade dir is not found');
                $this->nextQuickInfo[] = 'php upgrade dir is missing';
                return false;
            }
        }
        define('_PS_INSTALLER_PHP_UPGRADE_DIR_', INSTALL_PATH . DIRECTORY_SEPARATOR . $upgrade_dir_php . DIRECTORY_SEPARATOR);
        //old version detection
        global $oldversion, $logger;
        $oldversion = false;
        if (file_exists(SETTINGS_FILE)) {
            include_once SETTINGS_FILE;
            // include_once(DEFINES_FILE);
            $oldversion = _PS_VERSION_;
        } else {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('The config/settings.inc.php file was not found.');
            return false;
            die('<action result="fail" error="30" />' . "\n");
        }
        if (!defined('__PS_BASE_URI__')) {
            define('__PS_BASE_URI__', realpath(dirname($_SERVER['SCRIPT_NAME'])) . '/../../');
        }
        if (!defined('_THEMES_DIR_')) {
            define('_THEMES_DIR_', __PS_BASE_URI__ . 'themes/');
        }
        $oldversion = _PS_VERSION_;
        $versionCompare = version_compare(INSTALL_VERSION, $oldversion);
        if ($versionCompare == '-1') {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('This installer is too old');
            return false;
            // die('<action result="fail" error="27" />'."\n");
        } elseif ($versionCompare == 0) {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l(sprintf('You already have the %s version.', INSTALL_VERSION));
            return false;
            die('<action result="fail" error="28" />' . "\n");
        } elseif ($versionCompare === false) {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('There is no older version. Did you delete or rename the config/settings.inc.php file?');
            return false;
            die('<action result="fail" error="29" />' . "\n");
        }
        //check DB access
        $this->db();
        error_reporting(E_ALL);
        $resultDB = MySql::tryToConnect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_, _DB_NAME_);
        if ($resultDB !== 0) {
            // $logger->logError('Invalid database configuration.');
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('Invalid database configuration');
            return false;
            die("<action result='fail' error='" . $resultDB . "'/>\n");
        }
        //custom sql file creation
        $upgradeFiles = array();
        // @todo : upgrade/sql or sql/upgrade should be handled in the Upgrader class
        $upgrade_dir_sql = INSTALL_PATH . '/upgrade/sql';
        // if 1.4;
        if (!file_exists($upgrade_dir_sql)) {
            $upgrade_dir_sql = INSTALL_PATH . '/sql/upgrade';
        }
        if (!file_exists($upgrade_dir_sql)) {
            $this->next = 'error';
            $this->nextDesc = $this->l('unable to find upgrade directory in the install path');
            return false;
        }
        if ($handle = opendir($upgrade_dir_sql)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != '.' and $file != '..') {
                    $upgradeFiles[] = str_replace(".sql", "", $file);
                }
            }
            closedir($handle);
        }
        if (empty($upgradeFiles)) {
            $this->next = 'error';
            $this->nextQuickInfo[] = sprintf($this->l('Cannot find the sql upgrade files. Please verify that the %s folder is not empty'), $upgrade_dir_sql);
            // fail 31
            return false;
        }
        natcasesort($upgradeFiles);
        $neededUpgradeFiles = array();
        // fix : complete version number if there is not all 4 numbers
        // for example replace 1.4.3 by 1.4.3.0
        // consequences : file 1.4.3.0.sql will be skipped if oldversion = 1.4.3
        // @since 1.4.4.0
        $arrayVersion = preg_split('#\\.#', $oldversion);
        $versionNumbers = sizeof($arrayVersion);
        if ($versionNumbers != 4) {
            $arrayVersion = array_pad($arrayVersion, 4, '0');
        }
        $oldversion = implode('.', $arrayVersion);
        // end of fix
        foreach ($upgradeFiles as $version) {
            if (version_compare($version, $oldversion) == 1 and version_compare(INSTALL_VERSION, $version) != -1) {
                $neededUpgradeFiles[] = $version;
            }
        }
        if (empty($neededUpgradeFiles)) {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('No upgrade is possible.');
            return false;
            $logger->logError('No upgrade is possible.');
            die('<action result="fail" error="32" />' . "\n");
        }
        $sqlContentVersion = array();
        if (isset($_GET['customModule']) and $_GET['customModule'] == 'desactivate') {
            require_once _PS_INSTALLER_PHP_UPGRADE_DIR_ . 'deactivate_custom_modules.php';
            deactivate_custom_modules();
        }
        foreach ($neededUpgradeFiles as $version) {
            $file = $upgrade_dir_sql . DIRECTORY_SEPARATOR . $version . '.sql';
            if (!file_exists($file)) {
                $this->next = 'error';
                $this->nextQuickInfo[] = sprintf($this->l('Error while loading sql upgrade file "%s.sql".'), $version);
                return false;
                $logger->logError('Error while loading sql upgrade file.');
                die('<action result="fail" error="33" />' . "\n");
            }
            if (!($sqlContent = file_get_contents($file) . "\n")) {
                $this->next = 'error';
                $this->nextQuickInfo[] = $this->l(sprintf('Error while loading sql upgrade file %s.', $version));
                return false;
                $logger->logError(sprintf('Error while loading sql upgrade file %s.', $version));
                die('<action result="fail" error="33" />' . "\n");
            }
            $sqlContent = str_replace(array($filePrefix, $engineType), array(_DB_PREFIX_, $mysqlEngine), $sqlContent);
            $sqlContent = preg_split("/;\\s*[\r\n]+/", $sqlContent);
            $sqlContentVersion[$version] = $sqlContent;
        }
        //sql file execution
        global $requests, $warningExist;
        $requests = '';
        $warningExist = false;
        // Configuration::loadConfiguration();
        $request = '';
        foreach ($sqlContentVersion as $upgrade_file => $sqlContent) {
            foreach ($sqlContent as $query) {
                $query = trim($query);
                if (!empty($query)) {
                    /* If php code have to be executed */
                    if (strpos($query, '/* PHP:') !== false) {
                        /* Parsing php code */
                        $pos = strpos($query, '/* PHP:') + strlen('/* PHP:');
                        $phpString = substr($query, $pos, strlen($query) - $pos - strlen(' */;'));
                        $php = explode('::', $phpString);
                        preg_match('/\\((.*)\\)/', $phpString, $pattern);
                        $paramsString = trim($pattern[0], '()');
                        preg_match_all('/([^,]+),? ?/', $paramsString, $parameters);
                        if (isset($parameters[1])) {
                            $parameters = $parameters[1];
                        } else {
                            $parameters = array();
                        }
                        if (is_array($parameters)) {
                            foreach ($parameters as &$parameter) {
                                $parameter = str_replace('\'', '', $parameter);
                            }
                        }
                        // reset phpRes to a null value
                        $phpRes = null;
                        /* Call a simple function */
                        if (strpos($phpString, '::') === false) {
                            $func_name = str_replace($pattern[0], '', $php[0]);
                            if (!file_exists(_PS_INSTALLER_PHP_UPGRADE_DIR_ . strtolower($func_name) . '.php')) {
                                $this->nextQuickInfo[] = '<div class="upgradeDbError">[ERROR] ' . $upgrade_file . ' PHP - missing file ' . $query . '</div>';
                            } else {
                                require_once _PS_INSTALLER_PHP_UPGRADE_DIR_ . strtolower($func_name) . '.php';
                                $phpRes = call_user_func_array($func_name, $parameters);
                            }
                        } else {
                            $func_name = array($php[0], str_replace($pattern[0], '', $php[1]));
                            $this->nextQuickInfo[] = '<div class="upgradeDbError">[ERROR] ' . $upgrade_file . ' PHP - Object Method called ' . $php[0] . '::' . str_replace($pattern[0], '', $php[1]) . '</div>';
                        }
                        if (isset($phpRes) && (is_array($phpRes) && !empty($phpRes['error'])) || $phpRes === false) {
                            $this->next = 'error';
                            $this->nextQuickInfo[] = '<div class="upgradeDbError">[ERROR] PHP ' . $upgrade_file . ' ' . (empty($phpRes['error']) ? $query : ' ' . $phpRes['error']) . ' ' . (empty($phpRes['msg']) ? '' : ' - ' . $phpRes['msg']) . '</div>';
                        } else {
                            $this->nextQuickInfo[] = '<div class="upgradeDbOk">[OK] PHP' . $upgrade_file . ' ' . $query . '</div>';
                        }
                    } elseif (!Db::getInstance()->execute($query)) {
                        $this->next = 'error';
                        $this->nextQuickInfo[] = '<div class="upgradeDbError">[ERROR] SQL ' . $upgrade_file . ' ' . Db::getInstance()->getNumberError() . ' in ' . $query . ': ' . Db::getInstance()->getMsgError() . '</div>';
                        $warningExist = true;
                    } else {
                        $this->nextQuickInfo[] = '<div class="upgradeDbOk">[OK] SQL ' . $upgrade_file . ' ' . $query . '</div>';
                    }
                }
            }
        }
        if ($this->next == 'error') {
            $this->nextDesc = $this->l('An error happen during database upgrade');
            return false;
        }
        $this->nextQuickInfo[] = $this->l('Upgrade Db Ok');
        // no error !
        # At this point, database upgrade is over.
        # Now we need to add all previous missing settings items, and reset cache and compile directories
        $this->writeNewSettings();
        // Settings updated, compile and cache directories must be emptied
        // @todo : the list of theses directory should be available elsewhere
        $arrayToClean[] = INSTALL_PATH . '/../tools/smarty/cache/';
        $arrayToClean[] = INSTALL_PATH . '/../tools/smarty/compile/';
        $arrayToClean[] = INSTALL_PATH . '/../tools/smarty_v2/cache/';
        $arrayToClean[] = INSTALL_PATH . '/../tools/smarty_v2/compile/';
        foreach ($arrayToClean as $dir) {
            if (!file_exists($dir)) {
                $this->nextQuickInfo[] = sprintf($this->l('[SKIP] directory "%s" doesn\'t exist and cannot be emptied.'), $dir);
                continue;
            } else {
                foreach (scandir($dir) as $file) {
                    if ($file[0] != '.' and $file != 'index.php' and $file != '.htaccess') {
                        unlink($dir . $file);
                        $this->nextQuickInfo[] = sprintf($this->l('[cleaning cache] %s removed'), $file);
                    }
                }
            }
        }
        // delete cache filesystem if activated
        $depth = (int) Db::getInstance()->getValue('SELECT value 
			FROM ' . _DB_PREFIX_ . 'configuration 
			WHERE name = "PS_CACHEFS_DIRECTORY_DEPTH"');
        if ($depth) {
            Tools::deleteDirectory(_PS_CACHEFS_DIRECTORY_, false);
            CacheFs::createCacheFsDirectories((int) $depth);
        }
        // we do not use class Configuration because it's not loaded;
        Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'configuration`
			SET value="0" WHERE name = "PS_HIDE_OPTIMIZATION_TIS"');
        Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'configuration`
			SET value="1" WHERE name = "PS_NEED_REBUILD_INDEX"');
        Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'configuration`
			SET value="' . INSTALL_VERSION . '" WHERE name = "PS_VERSION_DB"');
        if ($warningExist) {
            $this->next = 'error';
            $this->nextQuickInfo[] = $this->l('Warning detected during upgrade.');
            $this->nextDesc = $this->l('Error while inserting content into the database');
            return false;
        } else {
            $this->next = 'upgradeComplete';
            $this->nextDesc = $this->l('Upgrade completed');
            return true;
        }
    }