diff --git a/.github/workflows/e2e_nightly_upgrade.yml b/.github/workflows/e2e_nightly_upgrade.yml index 9b96f76b6..71b3abcfe 100755 --- a/.github/workflows/e2e_nightly_upgrade.yml +++ b/.github/workflows/e2e_nightly_upgrade.yml @@ -16,12 +16,12 @@ jobs: runs-on: ubuntu-18.04 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - name: Setup Node - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: - node-version: 12 + node-version: 16 - name: Install dependencies shell: bash @@ -61,7 +61,7 @@ jobs: steps: # Setup PrestaShop and Install Autoupgrade module - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - run: | cp -r .github .github_dev @@ -94,9 +94,9 @@ jobs: # Install Module with user interface - name: Setup Node - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: - node-version: 12 + node-version: 16 - name: Install dependencies run: npm install @@ -162,7 +162,7 @@ jobs: fail-fast: false steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - uses: actions/download-artifact@v2 name: Download reports @@ -173,7 +173,7 @@ jobs: - name: Setup Node uses: actions/setup-node@v2 with: - node-version: 12 + node-version: 16 - name: Install dependencies shell: bash @@ -206,7 +206,7 @@ jobs: fail-fast: false steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - uses: actions/download-artifact@v2 name: Download report diff --git a/.github/workflows/nightly_upgrade.yml b/.github/workflows/nightly_upgrade.yml index 637b60dee..9d058ac60 100644 --- a/.github/workflows/nightly_upgrade.yml +++ b/.github/workflows/nightly_upgrade.yml @@ -8,7 +8,7 @@ jobs: name: Set up matrix runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - uses: shivammathur/setup-php@v2 with: php-version: 7.4 @@ -22,12 +22,10 @@ jobs: strategy: fail-fast: false matrix: - from: ['8.0.4', '1.7.6.9', '1.7.7.0', '1.6.1.24'] + from: ['8.0.4', '1.7.6.9', '1.7.7.0'] ps-versions: ${{ fromJson(needs.get_matrix.outputs.matrix) }} branch: ['dev', 'master'] exclude: - - from: '1.6.1.24' - ps-versions: {branch: 'develop'} - from: '1.7.6.9' ps-versions: {branch: 'develop'} - from: '1.7.7.0' @@ -39,7 +37,7 @@ jobs: outputs: result: ${{ steps.export-result.outputs.result }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - if: ${{ matrix.branch != 'dev' }} run: | cp -r .github .github_dev @@ -76,7 +74,7 @@ jobs: matrix: ps-versions: ${{ fromJson(needs.get_matrix.outputs.matrix) }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - uses: actions/download-artifact@v2 with: path: ./artifacts/ diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 983d1620a..0f1125fad 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2.0.0 + uses: actions/checkout@v3.1.0 - name: PHP syntax checker 5.6 uses: prestashop/github-action-php-lint/5.6@master @@ -50,10 +50,10 @@ jobs: php-version: '5.6' - name: Checkout - uses: actions/checkout@v2.0.0 + uses: actions/checkout@v3.1.0 - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: vendor key: php-${{ hashFiles('composer.lock') }} @@ -76,7 +76,7 @@ jobs: with: php-version: ${{ matrix.php-versions }} extensions: mbstring, intl, gd, xml, dom, json, fileinfo, curl, zip, iconv, ext-zip - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 with: fetch-depth: 0 @@ -89,21 +89,21 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - presta-versions: ['1.6.1.18', '1.7.2.5', '1.7.3.4', '1.7.4.4', '1.7.5.1', '1.7.6', '1.7.7', '1.7.8', '8.0.0', 'latest'] + presta-versions: ['1.7.2.5', '1.7.3.4', '1.7.4.4', '1.7.5.1', '1.7.6', '1.7.7', '1.7.8', '8.0.0', 'latest'] steps: - name: Checkout - uses: actions/checkout@v2.0.0 + uses: actions/checkout@v3.1.0 # Add vendor folder in cache to make next builds faster - name: Cache vendor folder - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: vendor key: php-${{ hashFiles('composer.lock') }} # Add composer local folder in cache to make next builds faster - name: Cache composer folder - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.composer/cache key: php-composer-cache diff --git a/.github/workflows/upgrade.yml b/.github/workflows/upgrade.yml index bc5bf1adb..97a00f1ab 100644 --- a/.github/workflows/upgrade.yml +++ b/.github/workflows/upgrade.yml @@ -4,14 +4,14 @@ jobs: upgrade: strategy: matrix: - from: ['1.6.1.11', '1.7.6.9', '1.7.6.1', '1.7.7.0'] + from: ['1.7.6.9', '1.7.6.1', '1.7.7.0'] ps-versions: - channel: minor - channel: major runs-on: ubuntu-latest name: Upgrade steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - name: Composer Install run: composer install - name: Upgrade & Rollback diff --git a/AdminSelfUpgrade.php b/AdminSelfUpgrade.php index 045980143..a65355dee 100755 --- a/AdminSelfUpgrade.php +++ b/AdminSelfUpgrade.php @@ -522,21 +522,6 @@ public function display() parent::display(); } - /** - * @deprecated - * Method allowing errors on very old tabs to be displayed. - * On the next major of this module, use an admin controller and get rid of this. - * - * This method is called by functions.php available in the admin root folder. - */ - public function displayErrors() - { - if (empty($this->_errors)) { - return; - } - echo implode(' - ', $this->_errors); - } - /** * Adapter for trans calls, existing only on PS 1.7. * Making them available for PS 1.6 as well. diff --git a/ajax-upgradetabconfig.php b/ajax-upgradetabconfig.php index 35c4bbfab..139033ffd 100644 --- a/ajax-upgradetabconfig.php +++ b/ajax-upgradetabconfig.php @@ -63,7 +63,6 @@ function autoupgrade_init_container($callerFilePath) } define('AUTOUPGRADE_MODULE_DIR', _PS_MODULE_DIR_ . 'autoupgrade' . DIRECTORY_SEPARATOR); - require_once AUTOUPGRADE_MODULE_DIR . 'functions.php'; require_once AUTOUPGRADE_MODULE_DIR . 'vendor/autoload.php'; $dir = Tools14::safeOutput(Tools14::getValue('dir')); diff --git a/autoupgrade.php b/autoupgrade.php index d86da025b..7c13cad1b 100644 --- a/autoupgrade.php +++ b/autoupgrade.php @@ -36,7 +36,7 @@ public function __construct() $this->name = 'autoupgrade'; $this->tab = 'administration'; $this->author = 'PrestaShop'; - $this->version = '4.16.4'; + $this->version = '5.0.0'; $this->need_instance = 1; $this->bootstrap = true; @@ -159,7 +159,7 @@ public function hookDashboardZoneOne($params) public function getContent() { global $cookie; - header('Location: index.php?tab=AdminSelfUpgrade&token=' . md5(pSQL(_COOKIE_KEY_ . 'AdminSelfUpgrade' . (int) Tab::getIdFromClassName('AdminSelfUpgrade') . (int) $cookie->id_employee))); + header('Location: index.php?controller=AdminSelfUpgrade&token=' . md5(pSQL(_COOKIE_KEY_ . 'AdminSelfUpgrade' . (int) Tab::getIdFromClassName('AdminSelfUpgrade') . (int) $cookie->id_employee))); exit; } diff --git a/classes/PrestashopConfiguration.php b/classes/PrestashopConfiguration.php index d5fc75d1a..863664df9 100644 --- a/classes/PrestashopConfiguration.php +++ b/classes/PrestashopConfiguration.php @@ -76,6 +76,7 @@ public function getPrestaShopVersion() $this->psRootDir . '/config/settings.inc.php', $this->psRootDir . '/config/autoload.php', $this->psRootDir . '/app/AppKernel.php', + $this->psRootDir . '/src/Core/Version.php', ]; foreach ($files as $file) { if (!file_exists($file)) { diff --git a/classes/TaskRunner/Miscellaneous/CompareReleases.php b/classes/TaskRunner/Miscellaneous/CompareReleases.php index d452b2b77..5b7719517 100644 --- a/classes/TaskRunner/Miscellaneous/CompareReleases.php +++ b/classes/TaskRunner/Miscellaneous/CompareReleases.php @@ -31,7 +31,7 @@ use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask; /** - * get the list of all modified and deleted files between current version + * This class gets the list of all modified and deleted files between current version * and target version (according to channel configuration). */ class CompareReleases extends AbstractTask @@ -61,6 +61,8 @@ public function run() $version = $upgrader->version_num; } + // Get list of differences between these two versions. The differences will be fetched from a local + // XML file if it exists, or from PrestaShop API. $diffFileList = $upgrader->getDiffFilesList(_PS_VERSION_, $version); if (!is_array($diffFileList)) { $this->nextParams['status'] = 'error'; diff --git a/classes/TaskRunner/Upgrade/UpgradeDb.php b/classes/TaskRunner/Upgrade/UpgradeDb.php index 2a5392d33..990bc640b 100644 --- a/classes/TaskRunner/Upgrade/UpgradeDb.php +++ b/classes/TaskRunner/Upgrade/UpgradeDb.php @@ -29,11 +29,9 @@ use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask; use PrestaShop\Module\AutoUpgrade\UpgradeException; -use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader16; use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader17; use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader80; use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader81; -use PrestaShop\Module\AutoUpgrade\UpgradeTools\SettingsFileWriter; class UpgradeDb extends AbstractTask { @@ -61,10 +59,6 @@ public function run() public function getCoreUpgrader() { - if (version_compare($this->container->getState()->getInstallVersion(), '1.7', '<')) { - return new CoreUpgrader16($this->container, $this->logger); - } - if (version_compare($this->container->getState()->getInstallVersion(), '8', '<')) { return new CoreUpgrader17($this->container, $this->logger); } @@ -85,7 +79,6 @@ public function init() // Migrating settings file $this->container->initPrestaShopAutoloader(); - (new SettingsFileWriter($this->translator))->migrateSettingsFile($this->logger); parent::init(); } } diff --git a/classes/TaskRunner/Upgrade/UpgradeFiles.php b/classes/TaskRunner/Upgrade/UpgradeFiles.php index f22251861..eae4e77ea 100644 --- a/classes/TaskRunner/Upgrade/UpgradeFiles.php +++ b/classes/TaskRunner/Upgrade/UpgradeFiles.php @@ -38,7 +38,7 @@ class UpgradeFiles extends AbstractTask public function run() { - // The first call must init the list of files be upgraded + // The first call must init the list of files be upgraded. if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::FILES_TO_UPGRADE_LIST)) { return $this->warmUp(); } @@ -47,6 +47,8 @@ public function run() $this->destUpgradePath = $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH); $this->next = 'upgradeFiles'; + + // Now we load the list of files to be upgraded, prepared previously by warmUp method. $filesToUpgrade = $this->container->getFileConfigurationStorage()->load(UpgradeFileNames::FILES_TO_UPGRADE_LIST); if (!is_array($filesToUpgrade)) { $this->next = 'error'; @@ -68,6 +70,8 @@ public function run() } $file = array_pop($filesToUpgrade); + + // Note - upgrade this file means do whatever is needed for that file to be in the final state, delete included. if (!$this->upgradeThisFile($file)) { // put the file back to the begin of the list $this->next = 'error'; @@ -119,7 +123,7 @@ protected function listFilesToUpgrade($dir) continue; } $list[] = str_replace($this->container->getProperty(UpgradeContainer::LATEST_PATH), '', $fullPath); - if (is_dir($fullPath) && strpos($dir . DIRECTORY_SEPARATOR . $file, 'install') === false) { + if (is_dir($fullPath)) { $list = array_merge($list, $this->listFilesToUpgrade($fullPath)); } } @@ -137,9 +141,15 @@ public function upgradeThisFile($file) // translations_custom and mails_custom list are currently not used // later, we could handle customization with some kind of diff functions // for now, just copy $file in str_replace($this->latestRootDir,_PS_ROOT_DIR_) + + // The path to the file from the upgrade archive $orig = $this->container->getProperty(UpgradeContainer::LATEST_PATH) . $file; + + // The path to the file in our prestashop directory $dest = $this->destUpgradePath . $file; + // Skip files that we want to avoid touching. They may be already excluded from the list from before, + // but again, as a safety precaution. if ($this->container->getFilesystemAdapter()->isFileSkipped($file, $dest, 'upgrade')) { $this->logger->debug($this->translator->trans('%s ignored', [$file], 'Modules.Autoupgrade.Admin')); @@ -221,6 +231,7 @@ public function upgradeThisFile($file) */ protected function warmUp() { + // Get path to the folder with release we will use to upgrade and check if it's valid $newReleasePath = $this->container->getProperty(UpgradeContainer::LATEST_PATH); if (!$this->container->getFilesystemAdapter()->isReleaseValid($newReleasePath)) { $this->logger->error($this->translator->trans('Could not assert the folder %s contains a valid PrestaShop release, exiting.', [$newReleasePath], 'Modules.Autoupgrade.Admin')); @@ -230,42 +241,57 @@ protected function warmUp() return false; } + // Replace the name of the admin folder inside the release to match our admin folder name $admin_dir = str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . DIRECTORY_SEPARATOR, '', $this->container->getProperty(UpgradeContainer::PS_ADMIN_PATH)); if (file_exists($newReleasePath . DIRECTORY_SEPARATOR . 'admin')) { rename($newReleasePath . DIRECTORY_SEPARATOR . 'admin', $newReleasePath . DIRECTORY_SEPARATOR . $admin_dir); } elseif (file_exists($newReleasePath . DIRECTORY_SEPARATOR . 'admin-dev')) { rename($newReleasePath . DIRECTORY_SEPARATOR . 'admin-dev', $newReleasePath . DIRECTORY_SEPARATOR . $admin_dir); } + + // Rename develop installer directory, it would be ignored anyway because it's present in getFilesToIgnoreOnUpgrade() if (file_exists($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev')) { rename($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev', $newReleasePath . DIRECTORY_SEPARATOR . 'install'); } - // list saved in UpgradeFileNames::toUpgradeFileList - // get files differences (previously generated) - $admin_dir = trim(str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), '', $this->container->getProperty(UpgradeContainer::PS_ADMIN_PATH)), DIRECTORY_SEPARATOR); + // Now, we will get the list of changed and removed files between the versions. This was generated previously by + // CompareReleases task. $filepath_list_diff = $this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_DIFF_LIST; $list_files_diff = []; + + // We check if that file exists first and load it if (file_exists($filepath_list_diff)) { $list_files_diff = $this->container->getFileConfigurationStorage()->load(UpgradeFileNames::FILES_DIFF_LIST); - // only keep list of files to delete. The modified files will be listed with _listFilesToUpgrade + // $list_files_diff now contains an array with a list of changed and deleted files. + // We only keep list of files to delete. The modified files will be listed with listFilesToUpgrade below. $list_files_diff = $list_files_diff['deleted']; + + // Admin folder name in this deleted files list is standard /admin/. + // We will need to change it to our own admin folder name. + $admin_dir = trim(str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), '', $this->container->getProperty(UpgradeContainer::PS_ADMIN_PATH)), DIRECTORY_SEPARATOR); foreach ($list_files_diff as $k => $path) { if (preg_match('#autoupgrade#', $path)) { unset($list_files_diff[$k]); - } else { - $list_files_diff[$k] = str_replace('/' . 'admin', '/' . $admin_dir, $path); + } elseif (substr($path, 0, 6) === '/admin') { + // Please make sure that the condition to check if the string starts with /admin stays here, because it was replacing + // admin even in the middle of a path, not deleting some files as a result. + // Also, do not use DIRECTORY_SEPARATOR, keep forward slash, because the path come from the XML standardized. + $list_files_diff[$k] = '/' . $admin_dir . substr($path, 6); } - } // do not replace by DIRECTORY_SEPARATOR + } } + // Now, we get the list of files that are either new or must be modified $list_files_to_upgrade = $this->listFilesToUpgrade($newReleasePath); if (false === $list_files_to_upgrade) { return false; } - // also add files to remove + // Add our previously created list of deleted files $list_files_to_upgrade = array_reverse(array_merge($list_files_diff, $list_files_to_upgrade)); + // Now, some files should be updated as an absolute last step, if they are present in the list, + // we will put them to the end of it. $filesToMoveToTheEnd = [ DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php', DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'ClassLoader.php', @@ -287,7 +313,8 @@ protected function warmUp() } } - // save in a serialized array in UpgradeFileNames::toUpgradeFileList + // Save in a serialized array in UpgradeFileNames::toUpgradeFileList, to be later used by the upgrade step itself above, + // after run() is called. $this->container->getFileConfigurationStorage()->save($list_files_to_upgrade, UpgradeFileNames::FILES_TO_UPGRADE_LIST); $total_files_to_upgrade = count($list_files_to_upgrade); diff --git a/classes/Tools14.php b/classes/Tools14.php index 585808c65..de9b0fb15 100755 --- a/classes/Tools14.php +++ b/classes/Tools14.php @@ -29,84 +29,14 @@ use Tab; +/** + * Useful collection of utilities that are guaranteed to work on every PHP and PrestaShop version supported. + */ class Tools14 { - protected static $file_exists_cache = []; protected static $_forceCompile; protected static $_caching; - /** - * Random password generator. - * - * @param int $length Desired length (optional) - * - * @return string Password - */ - public static function passwdGen($length = 8) - { - $str = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; - for ($i = 0, $passwd = ''; $i < $length; ++$i) { - $passwd .= self::substr($str, mt_rand(0, self::strlen($str) - 1), 1); - } - - return $passwd; - } - - /** - * Redirect user to another page. - * - * @param string $url Desired URL - * @param string $baseUri Base URI (optional) - */ - public static function redirect($url, $baseUri = __PS_BASE_URI__) - { - if (strpos($url, 'http://') === false && strpos($url, 'https://') === false) { - global $link; - if (strpos($url, $baseUri) !== false && strpos($url, $baseUri) == 0) { - $url = substr($url, strlen($baseUri)); - } - $explode = explode('?', $url, 2); - // don't use ssl if url is home page - // used when logout for example - $useSSL = !empty($url); - $url = $link->getPageLink($explode[0], $useSSL); - if (isset($explode[1])) { - $url .= '?' . $explode[1]; - } - $baseUri = ''; - } - - if (isset($_SERVER['HTTP_REFERER']) && ($url == $_SERVER['HTTP_REFERER'])) { - header('Location: ' . $_SERVER['HTTP_REFERER']); - } else { - header('Location: ' . $baseUri . $url); - } - exit; - } - - /** - * Redirect url wich allready PS_BASE_URI. - * - * @param string $url Desired URL - */ - public static function redirectLink($url) - { - if (!preg_match('@^https?://@i', $url)) { - global $link; - if (strpos($url, __PS_BASE_URI__) !== false && strpos($url, __PS_BASE_URI__) == 0) { - $url = substr($url, strlen(__PS_BASE_URI__)); - } - $explode = explode('?', $url, 2); - $url = $link->getPageLink($explode[0]); - if (isset($explode[1])) { - $url .= '?' . $explode[1]; - } - } - - header('Location: ' . $url); - exit; - } - /** * Redirect user to another admin page. * @@ -118,22 +48,9 @@ public static function redirectAdmin($url) exit; } - /** - * getProtocol return the set protocol according to configuration (http[s]). - * - * @param bool true if require ssl - * - * @return string (http|https) - */ - public static function getProtocol($use_ssl = null) - { - return null !== $use_ssl && $use_ssl ? 'https://' : 'http://'; - } - /** * getHttpHost return the current host used, with the protocol (http or https) if $http is true * This function should not be used to choose http or https domain name. - * Use Tools14::getShopDomain() or Tools14::getShopDomainSsl instead. * * @param bool $http * @param bool $entities @@ -153,135 +70,6 @@ public static function getHttpHost($http = false, $entities = false) return $host; } - /** - * getShopDomain returns domain name according to configuration and ignoring ssl. - * - * @param bool $http if true, return domain name with protocol - * @param bool $entities if true, - * - * @return string domain - */ - public static function getShopDomain($http = false, $entities = false) - { - if (!($domain = Configuration::get('PS_SHOP_DOMAIN'))) { - $domain = self::getHttpHost(); - } - if ($entities) { - $domain = htmlspecialchars($domain, ENT_COMPAT, 'UTF-8'); - } - if ($http) { - $domain = 'http://' . $domain; - } - - return $domain; - } - - /** - * getShopDomainSsl returns domain name according to configuration and depending on ssl activation. - * - * @param bool $http if true, return domain name with protocol - * @param bool $entities if true, - * - * @return string domain - */ - public static function getShopDomainSsl($http = false, $entities = false) - { - if (!($domain = Configuration::get('PS_SHOP_DOMAIN_SSL'))) { - $domain = self::getHttpHost(); - } - if ($entities) { - $domain = htmlspecialchars($domain, ENT_COMPAT, 'UTF-8'); - } - if ($http) { - $domain = (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://') . $domain; - } - - return $domain; - } - - /** - * Get the server variable SERVER_NAME. - * - * @return string server name - */ - public static function getServerName() - { - if (isset($_SERVER['HTTP_X_FORWARDED_SERVER']) && $_SERVER['HTTP_X_FORWARDED_SERVER']) { - return $_SERVER['HTTP_X_FORWARDED_SERVER']; - } - - return $_SERVER['SERVER_NAME']; - } - - /** - * Get the server variable REMOTE_ADDR, or the first ip of HTTP_X_FORWARDED_FOR (when using proxy). - * - * @return string $remote_addr ip of client - */ - public static function getRemoteAddr() - { - // This condition is necessary when using CDN, don't remove it. - if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && (!isset($_SERVER['REMOTE_ADDR']) || preg_match('/^127\..*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^172\.16.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^192\.168\.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^10\..*/i', trim($_SERVER['REMOTE_ADDR'])))) { - if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')) { - $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); - - return $ips[0]; - } else { - return $_SERVER['HTTP_X_FORWARDED_FOR']; - } - } - - return $_SERVER['REMOTE_ADDR']; - } - - /** - * Check if the current page use SSL connection on not. - * - * @return bool uses SSL - */ - public static function usingSecureMode() - { - if (isset($_SERVER['HTTPS'])) { - return $_SERVER['HTTPS'] == 1 || strtolower($_SERVER['HTTPS']) == 'on'; - } - // $_SERVER['SSL'] exists only in some specific configuration - if (isset($_SERVER['SSL'])) { - return $_SERVER['SSL'] == 1 || strtolower($_SERVER['SSL']) == 'on'; - } - - return false; - } - - /** - * Get the current url prefix protocol (https/http). - * - * @return string protocol - */ - public static function getCurrentUrlProtocolPrefix() - { - if (self::usingSecureMode()) { - return 'https://'; - } else { - return 'http://'; - } - } - - /** - * Secure an URL referrer. - * - * @param string $referrer URL referrer - * - * @return secured referrer - */ - public static function secureReferrer($referrer) - { - if (preg_match('/^http[s]?:\/\/' . self::getServerName() . '(:' . _PS_SSL_PORT_ . ')?\/.*$/Ui', $referrer)) { - return $referrer; - } - - return __PS_BASE_URI__; - } - /** * Get a value from $_POST / $_GET * if unavailable, take a default value. @@ -305,236 +93,6 @@ public static function getValue($key, $defaultValue = false) return !is_string($ret) ? $ret : stripslashes($ret); } - public static function getIsset($key) - { - if (!isset($key) || empty($key) || !is_string($key)) { - return false; - } - - return isset($_POST[$key]) ? true : (isset($_GET[$key]) ? true : false); - } - - /** - * Change language in cookie while clicking on a flag. - * - * @return string iso code - */ - public static function setCookieLanguage() - { - global $cookie; - - /* If language does not exist or is disabled, erase it */ - if ($cookie->id_lang) { - $lang = new Language((int) $cookie->id_lang); - if (!Validate::isLoadedObject($lang) || !$lang->active) { - $cookie->id_lang = null; - } - } - - /* Automatically detect language if not already defined */ - if (!$cookie->id_lang && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $array = explode(',', self::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])); - if (self::strlen($array[0]) > 2) { - $tab = explode('-', $array[0]); - $string = $tab[0]; - } else { - $string = $array[0]; - } - if (Validate::isLanguageIsoCode($string)) { - $lang = new Language((int) (Language::getIdByIso($string))); - if (Validate::isLoadedObject($lang) && $lang->active) { - $cookie->id_lang = (int) ($lang->id); - } - } - } - - /* If language file not present, you must use default language file */ - if (!$cookie->id_lang || !Validate::isUnsignedId($cookie->id_lang)) { - $cookie->id_lang = (int) (Configuration::get('PS_LANG_DEFAULT')); - } - - $iso = Language::getIsoById((int) $cookie->id_lang); - @include_once _PS_THEME_DIR_ . 'lang/' . $iso . '.php'; - - return $iso; - } - - /** - * Set cookie id_lang. - */ - public static function switchLanguage() - { - global $cookie; - - if ($id_lang = (int) (self::getValue('id_lang')) && Validate::isUnsignedId($id_lang)) { - $cookie->id_lang = $id_lang; - } - } - - /** - * Set cookie currency from POST or default currency. - * - * @return Currency object - */ - public static function setCurrency() - { - global $cookie; - - if (self::isSubmit('SubmitCurrency')) { - if (isset($_POST['id_currency']) && is_numeric($_POST['id_currency'])) { - $currency = Currency::getCurrencyInstance((int) ($_POST['id_currency'])); - if (is_object($currency) && $currency->id && !$currency->deleted) { - $cookie->id_currency = (int) ($currency->id); - } - } - } - - if ((int) $cookie->id_currency) { - $currency = Currency::getCurrencyInstance((int) $cookie->id_currency); - if (is_object($currency) && (int) $currency->id && (int) $currency->deleted != 1 && $currency->active) { - return $currency; - } - } - $currency = Currency::getCurrencyInstance((int) (Configuration::get('PS_CURRENCY_DEFAULT'))); - if (is_object($currency) && $currency->id) { - $cookie->id_currency = (int) ($currency->id); - } - - return $currency; - } - - /** - * Return price with currency sign for a given product. - * - * @param float $price Product price - * @param object $currency Current currency (object, id_currency, NULL => getCurrent()) - * - * @return string Price correctly formated (sign, decimal separator...) - */ - public static function displayPrice($price, $currency = null, $no_utf8 = false) - { - if ($currency === null) { - $currency = Currency::getCurrent(); - } - /* if you modified this function, don't forget to modify the Javascript function formatCurrency (in tools.js) */ - if (is_int($currency)) { - $currency = Currency::getCurrencyInstance((int) ($currency)); - } - $c_char = (is_array($currency) ? $currency['sign'] : $currency->sign); - $c_format = (is_array($currency) ? $currency['format'] : $currency->format); - $c_decimals = (is_array($currency) ? (int) ($currency['decimals']) : (int) ($currency->decimals)) * _PS_PRICE_DISPLAY_PRECISION_; - $c_blank = (is_array($currency) ? $currency['blank'] : $currency->blank); - $blank = ($c_blank ? ' ' : ''); - $ret = 0; - if (($isNegative = ($price < 0))) { - $price *= -1; - } - $price = self::ps_round($price, $c_decimals); - switch ($c_format) { - /* X 0,000.00 */ - case 1: - $ret = $c_char . $blank . number_format($price, $c_decimals, '.', ','); - break; - /* 0 000,00 X*/ - case 2: - $ret = number_format($price, $c_decimals, ',', ' ') . $blank . $c_char; - break; - /* X 0.000,00 */ - case 3: - $ret = $c_char . $blank . number_format($price, $c_decimals, ',', '.'); - break; - /* 0,000.00 X */ - case 4: - $ret = number_format($price, $c_decimals, '.', ',') . $blank . $c_char; - break; - } - if ($isNegative) { - $ret = '-' . $ret; - } - if ($no_utf8) { - return str_replace('€', chr(128), $ret); - } - - return $ret; - } - - public static function displayPriceSmarty($params, &$smarty) - { - if (array_key_exists('currency', $params)) { - $currency = Currency::getCurrencyInstance((int) ($params['currency'])); - if (Validate::isLoadedObject($currency)) { - return self::displayPrice($params['price'], $currency, false); - } - } - - return self::displayPrice($params['price']); - } - - /** - * Return price converted. - * - * @param float $price Product price - * @param object $currency Current currency object - * @param bool $to_currency convert to currency or from currency to default currency - */ - public static function convertPrice($price, $currency = null, $to_currency = true) - { - if ($currency === null) { - $currency = Currency::getCurrent(); - } elseif (is_numeric($currency)) { - $currency = Currency::getCurrencyInstance($currency); - } - - $c_id = (is_array($currency) ? $currency['id_currency'] : $currency->id); - $c_rate = (is_array($currency) ? $currency['conversion_rate'] : $currency->conversion_rate); - - if ($c_id != (int) (Configuration::get('PS_CURRENCY_DEFAULT'))) { - if ($to_currency) { - $price *= $c_rate; - } else { - $price /= $c_rate; - } - } - - return $price; - } - - /** - * Display date regarding to language preferences. - * - * @param array $params Date, format... - * @param object $smarty Smarty object for language preferences - * - * @return string Date - */ - public static function dateFormat($params, &$smarty) - { - return self::displayDate($params['date'], $smarty->ps_language->id, (isset($params['full']) ? $params['full'] : false)); - } - - /** - * Display date regarding to language preferences. - * - * @param string $date Date to display format UNIX - * @param int $id_lang Language id - * @param bool $full With time or not (optional) - * - * @return string Date - */ - public static function displayDate($date, $id_lang, $full = false, $separator = '-') - { - if (!$date || !($time = strtotime($date))) { - return $date; - } - if (!Validate::isDate($date) || !Validate::isBool($full)) { - exit(self::displayError('Invalid date')); - } - - $language = Language::getLanguage((int) $id_lang); - - return date($full ? $language['date_format_full'] : $language['date_format_lite'], $time); - } - /** * Sanitize a string. * @@ -561,20 +119,6 @@ public static function htmlentitiesUTF8($string, $type = ENT_QUOTES) return htmlentities($string, $type, 'utf-8'); } - public static function htmlentitiesDecodeUTF8($string) - { - if (is_array($string)) { - return array_map(['Tools', 'htmlentitiesDecodeUTF8'], $string); - } - - return html_entity_decode($string, ENT_QUOTES, 'utf-8'); - } - - public static function safePostVars() - { - $_POST = array_map(['Tools', 'htmlentitiesUTF8'], $_POST); - } - /** * Delete directory and subdirectories. * @@ -634,1755 +178,103 @@ public static function displayError($string = 'Fatal error', $htmlentities = tru } /** - * Display an error with detailed object. - * - * @param mixed $object - * @param bool $kill + * Check if submit has been posted. * - * @return $object if $kill = false; + * @param string $submit submit name */ - public static function dieObject($object, $kill = true) + public static function isSubmit($submit) { - echo '
'; - print_r($object); - echo '