[*] Installer: Add PHP CLI installer

This commit is contained in:
Rémi Gaillard
2013-03-29 14:28:48 +01:00
parent 052d17c30d
commit 6dcf2d53e3
4 changed files with 686 additions and 0 deletions
+169
View File
@@ -0,0 +1,169 @@
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 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/osl-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 PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
abstract class InstallControllerConsole
{
/**
* @var array List of installer steps
*/
protected static $steps = array('process');
protected static $instances = array();
/**
* @var string Current step
*/
public $step;
/**
* @var array List of errors
*/
public $errors = array();
/**
* @var InstallController
*/
public $controller;
/**
* @var InstallSession
*/
public $session;
/**
* @var InstallLanguages
*/
public $language;
/**
* @var bool If false, disable next button access
*/
public $next_button = true;
/**
* @var bool If false, disable previous button access
*/
public $previous_button = true;
/**
* @var InstallAbstractModel
*/
public $model;
/**
* @var array Magic vars
*/
protected $__vars = array();
/**
* Process form to go to next step
*/
abstract public function processNextStep();
/**
* Validate current step
*/
abstract public function validate();
final public static function execute()
{
// Include all controllers
foreach (self::$steps as $step)
{
if (!file_exists(_PS_INSTALL_CONTROLLERS_PATH_.'console/'.$step.'.php'))
throw new PrestashopInstallerException("Controller file 'console/{$step}.php' not found");
require_once _PS_INSTALL_CONTROLLERS_PATH_.'console/'.$step.'.php';
$classname = 'InstallControllerConsole'.$step;
self::$instances[$step] = new $classname($step);
}
$datas = Datas::getInstance();
/* redefine HTTP_HOST */
$_SERVER['HTTP_HOST'] = $datas->http_host;
@date_default_timezone_set($datas->timezone);
if (!$current_step = $datas->step)
return false;
self::$instances[$current_step]->process();
}
final public function __construct($step)
{
$this->step = $step;
$this->datas = Datas::getInstance();
// Set current language
$this->language = InstallLanguages::getInstance();
if (!$this->datas->language)
die('No language defined');
$this->language->setLanguage($this->datas->language);
$this->init();
}
/**
* Initialize model
*/
public function init()
{
}
public function printErrors()
{
$errors = $this->model_install->getErrors();
if (count($errors))
{
if (!is_array($errors))
$errors = array($errors);
echo 'Errors :'."\n";
foreach ($errors as $error_process)
foreach ($error_process as $error)
echo $error."\n";
die;
}
}
/**
* Get translated string
*
* @param string $str String to translate
* @param ... All other params will be used with sprintf
* @return string
*/
public function l($str)
{
$args = func_get_args();
return call_user_func_array(array($this->language, 'l'), $args);
}
public function process()
{
}
}
+188
View File
@@ -0,0 +1,188 @@
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 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/osl-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 PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class Datas
{
private static $instance = null;
protected static $available_args = array(
'step' => array(
'name' => 'step',
'default' => 'process',
'validate' => 'isGenericName',
),
'language' => array(
'default' => 'en',
'validate' => 'isLanguageIsoCode',
'alias' => 'l',
'help' => 'language iso code',
),
'timezone' => array(
'default' => 'Europe/Paris',
'alias' => 't',
),
'http_host' => array(
'name' => 'domain',
'validate' => 'isGenericName',
),
'database_server' => array(
'name' => 'db_server',
'default' => 'localhost',
'validate' => 'isGenericName',
'alias' => 'h',
),
'database_login' => array(
'name' => 'db_user',
'alias' => 'u',
'default' => 'root',
'validate' => 'isGenericName',
),
'database_password' => array(
'name' => 'db_password',
'alias' => 'p',
'default' => '',
),
'database_name' => array(
'name' => 'db_name',
'alias' => 'd',
'default' => 'prestashop',
'validate' => 'isGenericName',
),
'database_clear' => array(
'name' => 'db_clear',
'default' => '1',
'validate' => 'isInt',
'help' => 'Drop existing tables'
),
'database_prefix' => array(
'name' => 'prefix',
'default' => 'ps_',
'validate' => 'isGenericName',
),
'database_engine' => array(
'name' => 'engine',
'validate' => 'isMySQLEngine',
'default' => 'InnoDB',
'help' => 'InnoDB/MyISAM',
),
'shop_name' => array(
'name' => 'name',
'validate' => 'isGenericName',
'default' => 'PrestaShop',
),
'shop_activity' => array(
'name' => 'activity',
'default' => 1,
'validate' => 'isInt',
),
'shop_country' => array(
'name' => 'country',
'validate' => 'isLanguageIsoCode',
'default' => 'fr',
),
'admin_firstname' => array(
'name' => 'firstname',
'validate' => 'isName',
'default' => 'John',
),
'admin_lastname' => array(
'name' => 'lastname',
'validate' => 'isName',
'default' => 'Doe',
),
'admin_password' => array(
'name' => 'password',
'validate' => 'isPasswd',
'default' => '0123456789',
),
'admin_email' => array(
'name' => 'email',
'validate' => 'isEmail',
'default' => 'pub@prestashop.com'
),
);
protected $datas = array();
public function __get($key)
{
if (isset($this->datas[$key]))
return $this->datas[$key];
return false;
}
public function __set($key, $value)
{
$this->datas[$key] = $value;
}
public static function getInstance()
{
if (Datas::$instance === null)
Datas::$instance = new Datas();
return Datas::$instance;
}
public static function getArgs()
{
return Datas::$available_args;
}
public function getAndCheckArgs($argv)
{
if (!$argv)
return false;
$args_ok = array();
foreach ($argv as $arg)
{
if (!preg_match('/^--([^=\'"><|`]+)=([^=\'"><|`]+)/i', trim($arg), $res))
continue;
$args_ok[$res[1]] = $res[2];
}
$errors = array();
foreach (Datas::getArgs() as $key => $row)
{
if (isset($row['name']))
$name = $row['name'];
else
$name = $key;
if (!isset($args_ok[$name]))
{
if (!isset($row['default']))
$errors[] = 'Field '.$row['name'].' is empty';
else
$this->$key = $row['default'];
}
elseif (isset($row['validate']) && !call_user_func(array('Validate', $row['validate']), $args_ok[$name]))
$errors[] = 'Field '.$row['name'].' is not valid';
else
$this->$key = $args_ok[$name];
}
return count($errors) ? $errors : true;
}
}
+269
View File
@@ -0,0 +1,269 @@
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 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/osl-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 PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class InstallControllerConsoleProcess extends InstallControllerConsole
{
const SETTINGS_FILE = 'config/settings.inc.php';
/**
* @var InstallModelInstall
*/
protected $model_install;
public $process_steps = array();
public $previous_button = false;
public function init()
{
require_once _PS_INSTALL_MODELS_PATH_.'install.php';
require_once _PS_INSTALL_MODELS_PATH_.'database.php';
$this->model_install = new InstallModelInstall();
$this->model_database = new InstallModelDatabase();
}
/**
* @see InstallAbstractModel::processNextStep()
*/
public function processNextStep()
{
}
/**
* @see InstallAbstractModel::validate()
*/
public function validate()
{
return false;
}
public function initializeContext()
{
global $smarty;
Context::getContext()->shop = new Shop(1);
Shop::setContext(Shop::CONTEXT_SHOP, 1);
Configuration::loadConfiguration();
Context::getContext()->language = new Language(Configuration::get('PS_LANG_DEFAULT'));
Context::getContext()->country = new Country('PS_COUNTRY_DEFAULT');
Context::getContext()->cart = new Cart();
Context::getContext()->employee = new Employee(1);
if (!defined('_PS_SMARTY_FAST_LOAD_'))
define('_PS_SMARTY_FAST_LOAD_', true);
require_once _PS_ROOT_DIR_.'/config/smarty.config.inc.php';
Context::getContext()->smarty = $smarty;
}
public function process()
{
/*if (file_exists(_PS_ROOT_DIR_.'/'.self::SETTINGS_FILE))
@require_once _PS_ROOT_DIR_.'/'.self::SETTINGS_FILE;*/
if (!$this->processGenerateSettingsFile())
$this->printErrors();
if (!$this->model_database->testDatabaseSettings($this->datas->database_server, $this->datas->database_name, $this->datas->database_login, $this->datas->database_password, $this->datas->database_prefix, $this->datas->database_engine, $this->datas->database_clear))
$this->printErrors();
if (!$this->processInstallDatabase())
$this->printErrors();
if (!$this->processInstallDefaultData())
$this->printErrors();
if (!$this->processPopulateDatabase())
$this->printErrors();
if (!$this->processConfigureShop())
$this->printErrors();
if (!$this->processInstallModules())
$this->printErrors();
if (!$this->processInstallFixtures())
$this->printErrors();
if (!$this->processInstallTheme())
$this->printErrors();
if (!$this->processSendEmail())
$this->printErrors();
$params = http_build_query(array(
'email' => $this->datas->admin_email,
'method' => 'addMemberToNewsletter',
'language' => $this->datas->lang,
'visitorType' => 1,
'source' => 'installer'
));
Tools::file_get_contents('http://www.prestashop.com/ajax/controller.php?'.$params);
}
/**
* PROCESS : generateSettingsFile
*/
public function processGenerateSettingsFile()
{
return $this->model_install->generateSettingsFile(
$this->datas->database_server,
$this->datas->database_login,
$this->datas->database_password,
$this->datas->database_name,
$this->datas->database_prefix,
$this->datas->database_engine
);
}
/**
* PROCESS : installDatabase
* Create database structure
*/
public function processInstallDatabase()
{
return $this->model_install->installDatabase($this->datas->database_clear);
}
/**
* PROCESS : installDefaultData
* Create default shop and languages
*/
public function processInstallDefaultData()
{
return $this->model_install->installDefaultData($this->datas->shop_name, true);
}
/**
* PROCESS : populateDatabase
* Populate database with default data
*/
public function processPopulateDatabase()
{
$this->initializeContext();
$this->model_install->xml_loader_ids = $this->datas->xml_loader_ids;
$result = $this->model_install->populateDatabase();
$this->datas->xml_loader_ids = $this->model_install->xml_loader_ids;
return $result;
}
/**
* PROCESS : configureShop
* Set default shop configuration
*/
public function processConfigureShop()
{
$this->initializeContext();
return $this->model_install->configureShop(array(
'shop_name' => $this->datas->shop_name,
'shop_activity' => $this->datas->shop_activity,
'shop_country' => $this->datas->shop_country,
'shop_timezone' => $this->datas->timezone,
'use_smtp' => false,
'smtp_server' => null,
'smtp_login' => null,
'smtp_password' => null,
'smtp_encryption' => null,
'smtp_port' => null,
'admin_firstname' => $this->datas->admin_firstname,
'admin_lastname' => $this->datas->admin_lastname,
'admin_password' => $this->datas->admin_password,
'admin_email' => $this->datas->admin_email,
'configuration_agrement' => true,
));
}
/**
* PROCESS : installModules
* Install all modules in ~/modules/ directory
*/
public function processInstallModules()
{
$this->initializeContext();
return $this->model_install->installModules();
}
/**
* PROCESS : installFixtures
* Install fixtures (E.g. demo products)
*/
public function processInstallFixtures()
{
$this->initializeContext();
$this->model_install->xml_loader_ids = $this->datas->xml_loader_ids;
$result = $this->model_install->installFixtures();
$this->datas->xml_loader_ids = $this->model_install->xml_loader_ids;
return $result;
}
/**
* PROCESS : installTheme
* Install theme
*/
public function processInstallTheme()
{
$this->initializeContext();
return $this->model_install->installTheme();
}
/**
* PROCESS : sendEmail
* Send information e-mail
*/
public function processSendEmail()
{
require_once _PS_INSTALL_MODELS_PATH_.'mail.php';
$mail = new InstallModelMail(
$this->datas->use_smtp,
$this->datas->smtp_server,
$this->datas->smtp_login,
$this->datas->smtp_password,
$this->datas->smtp_port,
$this->datas->smtp_encryption,
$this->datas->admin_email
);
if (file_exists(_PS_INSTALL_LANGS_PATH_.$this->language->getLanguageIso().'/mail_identifiers.txt'))
$content = file_get_contents(_PS_INSTALL_LANGS_PATH_.$this->language->getLanguageIso().'/mail_identifiers.txt');
else
$content = file_get_contents(_PS_INSTALL_LANGS_PATH_.InstallLanguages::DEFAULT_ISO.'/mail_identifiers.txt');
$vars = array(
'{firstname}' => $this->datas->admin_firstname,
'{lastname}' => $this->datas->admin_lastname,
'{shop_name}' => $this->datas->shop_name,
'{passwd}' => $this->datas->admin_password,
'{email}' => $this->datas->admin_email,
'{shop_url}' => Tools::getHttpHost(true).__PS_BASE_URI__,
);
$content = str_replace(array_keys($vars), array_values($vars), $content);
$mail->send(
$this->l('%s - Login information', $this->datas->shop_name),
$content
);
return true;
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 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/osl-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 PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/* Redefine REQUEST_URI */
$_SERVER['REQUEST_URI'] = '/install/index_cli.php';
require_once dirname(__FILE__).'/init.php';
ini_set('memory_limit', '128M');
try
{
require_once _PS_INSTALL_PATH_.'classes/datas.php';
if (!($argc-1))
{
$available_arguments = Datas::getInstance()->getArgs();
echo 'Arguments available:'."\n";
foreach ($available_arguments as $key => $arg)
{
$name = isset($arg['name']) ? $arg['name'] : $key;
echo '--'.$name."\t".(isset($arg['help']) ? $arg['help'] : '').(isset($arg['default']) ? "\t".'(Default: '.$arg['default'].')' : '')."\n";
}
exit;
}
if (($errors = Datas::getInstance()->getAndCheckArgs($argv)) !== true)
{
if (count($errors))
foreach ($errors as $error)
echo $error."\n";
exit;
}
require_once _PS_INSTALL_PATH_.'classes/controllerConsole.php';
InstallControllerConsole::execute();
echo '-- Installation successfull! --'."\n";
}
catch (PrestashopInstallerException $e)
{
$e->displayMessage();
}