From 2c10ba5359b7f52cdcf26de9ee1978df9ae660d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Gaillard?= Date: Wed, 15 May 2013 14:46:40 +0200 Subject: [PATCH] [*] Core: you can now pass a query string with php-cli which will merged with for cronjobs and other things --- classes/Tools.php | 21 +++++++++++++++++++++ classes/shop/Shop.php | 8 ++++++-- config/config.inc.php | 3 +++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/classes/Tools.php b/classes/Tools.php index 97b1e1a2a..d0c8208a6 100644 --- a/classes/Tools.php +++ b/classes/Tools.php @@ -2198,6 +2198,27 @@ exit; return (PHP_INT_MAX == '9223372036854775807'); } + /** + * + * @return bool true if php-cli is used + */ + public static function isPHPCLI() + { + return (defined('STDIN') || (Tools::strtolower(php_sapi_name()) == 'cli' && (!isset($_SERVER['REMOTE_ADDR']) || empty($_SERVER['REMOTE_ADDR'])))); + } + + public static function argvToGET($argc, $argv) + { + if ($argc <= 1) + return; + + // get the first argument and parse it like a query string + parse_str($argv[1], $args); + if (!is_array($args) || !count($args)) + return; + $_GET = array_merge($args, $_GET); + } + /** * Get max file upload size considering server settings and optional max value * diff --git a/classes/shop/Shop.php b/classes/shop/Shop.php index 1538bb2e2..0648118dd 100644 --- a/classes/shop/Shop.php +++ b/classes/shop/Shop.php @@ -349,12 +349,16 @@ class ShopCore extends ObjectModel } } - if (!$id_shop && defined('_PS_ADMIN_DIR_')) + if ((!$id_shop && defined('_PS_ADMIN_DIR_')) || Tools::isPHPCLI()) { // If in admin, we can access to the shop without right URL - $shop = new Shop(Configuration::get('PS_SHOP_DEFAULT')); + $shop = new Shop((int)Configuration::get('PS_SHOP_DEFAULT')); $shop->physical_uri = preg_replace('#/+#', '/', str_replace('\\', '/', dirname(dirname($_SERVER['SCRIPT_NAME']))).'/'); $shop->virtual_uri = ''; + + // Define HTTP_HOST if PHP is launched with php-cli + if (Tools::isPHPCLI() && !isset($_SERVER['HTTP_HOST']) || empty($_SERVER['HTTP_HOST'])) + $_SERVER['HTTP_HOST'] = $shop->domain; } else { diff --git a/config/config.inc.php b/config/config.inc.php index ce76ea8c6..9a8b09a03 100644 --- a/config/config.inc.php +++ b/config/config.inc.php @@ -53,6 +53,9 @@ require_once(dirname(__FILE__).'/settings.inc.php'); require_once(dirname(__FILE__).'/autoload.php'); +if (Tools::isPHPCLI()) + Tools::argvToGET($argc, $argv); + if (_PS_DEBUG_PROFILING_) { include_once(_PS_TOOL_DIR_.'profiling/Controller.php');