PageRenderTime 74ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Install/Model/Installer/Console.php

https://bitbucket.org/jokusafet/magento2
PHP | 446 lines | 233 code | 51 blank | 162 comment | 21 complexity | 59fbf9508a426544b6f4bb08e55e33d6 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Install
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Magento application console installer
  28. */
  29. class Mage_Install_Model_Installer_Console extends Mage_Install_Model_Installer_Abstract
  30. {
  31. /**
  32. * Available installation options
  33. *
  34. * @var array
  35. */
  36. protected $_installOptions = array(
  37. 'license_agreement_accepted' => array('required' => 1),
  38. 'locale' => array('required' => 1),
  39. 'timezone' => array('required' => 1),
  40. 'default_currency' => array('required' => 1),
  41. 'db_model' => array('required' => 0),
  42. 'db_host' => array('required' => 1),
  43. 'db_name' => array('required' => 1),
  44. 'db_user' => array('required' => 1),
  45. 'db_pass' => array('required' => 0),
  46. 'db_prefix' => array('required' => 0),
  47. 'url' => array('required' => 1),
  48. 'skip_url_validation' => array('required' => 0),
  49. 'use_rewrites' => array('required' => 1),
  50. 'use_secure' => array('required' => 1),
  51. 'secure_base_url' => array('required' => 1),
  52. 'use_secure_admin' => array('required' => 1),
  53. 'admin_lastname' => array('required' => 1),
  54. 'admin_firstname' => array('required' => 1),
  55. 'admin_email' => array('required' => 1),
  56. 'admin_username' => array('required' => 1),
  57. 'admin_password' => array('required' => 1),
  58. 'admin_no_form_key' => array('required' => 0),
  59. 'encryption_key' => array('required' => 0),
  60. 'session_save' => array('required' => 0),
  61. 'backend_frontname' => array('required' => 0),
  62. 'enable_charts' => array('required' => 0),
  63. 'order_increment_prefix' => array('required' => 0),
  64. 'cleanup_database' => array('required' => 0),
  65. );
  66. /**
  67. * Installer data model to store data between installations steps
  68. *
  69. * @var Mage_Install_Model_Installer_Data|Mage_Install_Model_Session
  70. */
  71. protected $_dataModel;
  72. /**
  73. * Constructor
  74. */
  75. public function __construct()
  76. {
  77. Mage::app();
  78. $this->_getInstaller()->setDataModel($this->_getDataModel());
  79. }
  80. /**
  81. * Retrieve validated installation options
  82. *
  83. * @param array $options
  84. * @return array|boolean
  85. */
  86. protected function _getInstallOptions(array $options)
  87. {
  88. /**
  89. * Check required options
  90. */
  91. foreach ($this->_installOptions as $optionName => $optionInfo) {
  92. if (isset($optionInfo['required']) && $optionInfo['required'] && !isset($options[$optionName])) {
  93. $this->addError("ERROR: installation option '$optionName' is required.");
  94. }
  95. }
  96. if ($this->hasErrors()) {
  97. return false;
  98. }
  99. /**
  100. * Validate license agreement acceptance
  101. */
  102. if (!$this->_getFlagValue($options['license_agreement_accepted'])) {
  103. $this->addError(
  104. 'ERROR: You have to accept Magento license agreement terms and conditions to continue installation.'
  105. );
  106. return false;
  107. }
  108. $result = array();
  109. foreach ($this->_installOptions as $optionName => $optionInfo) {
  110. $result[$optionName] = isset($options[$optionName]) ? $options[$optionName] : '';
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Add error
  116. *
  117. * @param string $error
  118. * @return Mage_Install_Model_Installer_Console
  119. */
  120. public function addError($error)
  121. {
  122. $this->_getDataModel()->addError($error);
  123. return $this;
  124. }
  125. /**
  126. * Check if there were any errors
  127. *
  128. * @return boolean
  129. */
  130. public function hasErrors()
  131. {
  132. return (count($this->_getDataModel()->getErrors()) > 0);
  133. }
  134. /**
  135. * Get all errors
  136. *
  137. * @return array
  138. */
  139. public function getErrors()
  140. {
  141. return $this->_getDataModel()->getErrors();
  142. }
  143. /**
  144. * Return TRUE for 'yes', 1, 'true' (case insensitive) or FALSE otherwise
  145. *
  146. * @param string $value
  147. * @return boolean
  148. */
  149. protected function _getFlagValue($value)
  150. {
  151. $res = (1 == $value) || preg_match('/^(yes|y|true)$/i', $value);
  152. return $res;
  153. }
  154. /**
  155. * Get data model (used to store data between installation steps
  156. *
  157. * @return Mage_Install_Model_Installer_Data
  158. */
  159. protected function _getDataModel()
  160. {
  161. if (is_null($this->_dataModel)) {
  162. $this->_dataModel = Mage::getModel('Mage_Install_Model_Installer_Data');
  163. }
  164. return $this->_dataModel;
  165. }
  166. /**
  167. * Install Magento
  168. *
  169. * @param array $options
  170. * @return string|boolean
  171. */
  172. public function install(array $options)
  173. {
  174. try {
  175. $options = $this->_getInstallOptions($options);
  176. if (!$options) {
  177. return false;
  178. }
  179. /**
  180. * Check if already installed
  181. */
  182. if (Mage::isInstalled()) {
  183. $this->addError('ERROR: Magento is already installed.');
  184. return false;
  185. }
  186. /**
  187. * Skip URL validation, if set
  188. */
  189. $this->_getDataModel()->setSkipUrlValidation($options['skip_url_validation']);
  190. $this->_getDataModel()->setSkipBaseUrlValidation($options['skip_url_validation']);
  191. /**
  192. * Locale settings
  193. */
  194. $this->_getDataModel()->setLocaleData(array(
  195. 'locale' => $options['locale'],
  196. 'timezone' => $options['timezone'],
  197. 'currency' => $options['default_currency'],
  198. ));
  199. /**
  200. * Database and web config
  201. */
  202. $this->_getDataModel()->setConfigData(array(
  203. 'db_model' => $options['db_model'],
  204. 'db_host' => $options['db_host'],
  205. 'db_name' => $options['db_name'],
  206. 'db_user' => $options['db_user'],
  207. 'db_pass' => $options['db_pass'],
  208. 'db_prefix' => $options['db_prefix'],
  209. 'use_rewrites' => $this->_getFlagValue($options['use_rewrites']),
  210. 'use_secure' => $this->_getFlagValue($options['use_secure']),
  211. 'unsecure_base_url' => $options['url'],
  212. 'secure_base_url' => $options['secure_base_url'],
  213. 'use_secure_admin' => $this->_getFlagValue($options['use_secure_admin']),
  214. 'session_save' => $this->_checkSessionSave($options['session_save']),
  215. 'backend_frontname' => $this->_checkBackendFrontname($options['backend_frontname']),
  216. 'admin_no_form_key' => $this->_getFlagValue($options['admin_no_form_key']),
  217. 'skip_url_validation' => $this->_getFlagValue($options['skip_url_validation']),
  218. 'enable_charts' => $this->_getFlagValue($options['enable_charts']),
  219. 'order_increment_prefix' => $options['order_increment_prefix'],
  220. ));
  221. /**
  222. * Primary admin user
  223. */
  224. $this->_getDataModel()->setAdminData(array(
  225. 'firstname' => $options['admin_firstname'],
  226. 'lastname' => $options['admin_lastname'],
  227. 'email' => $options['admin_email'],
  228. 'username' => $options['admin_username'],
  229. 'new_password' => $options['admin_password'],
  230. ));
  231. $installer = $this->_getInstaller();
  232. /**
  233. * Install configuration
  234. */
  235. $installer->installConfig($this->_getDataModel()->getConfigData());
  236. if (!empty($options['cleanup_database'])) {
  237. $this->_cleanUpDatabase();
  238. }
  239. if ($this->hasErrors()) {
  240. return false;
  241. }
  242. /**
  243. * Install database
  244. */
  245. $installer->installDb();
  246. if ($this->hasErrors()) {
  247. return false;
  248. }
  249. // apply data updates
  250. Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
  251. /**
  252. * Validate entered data for administrator user
  253. */
  254. $user = $installer->validateAndPrepareAdministrator($this->_getDataModel()->getAdminData());
  255. if ($this->hasErrors()) {
  256. return false;
  257. }
  258. /**
  259. * Prepare encryption key and validate it
  260. */
  261. $encryptionKey = empty($options['encryption_key'])
  262. ? $this->generateEncryptionKey()
  263. : $options['encryption_key'];
  264. $this->_getDataModel()->setEncryptionKey($encryptionKey);
  265. $installer->validateEncryptionKey($encryptionKey);
  266. if ($this->hasErrors()) {
  267. return false;
  268. }
  269. /**
  270. * Create primary administrator user
  271. */
  272. $installer->createAdministrator($user);
  273. if ($this->hasErrors()) {
  274. return false;
  275. }
  276. /**
  277. * Save encryption key or create if empty
  278. */
  279. $installer->installEnryptionKey($encryptionKey);
  280. if ($this->hasErrors()) {
  281. return false;
  282. }
  283. /**
  284. * Installation finish
  285. */
  286. $installer->finish();
  287. if ($this->hasErrors()) {
  288. return false;
  289. }
  290. /**
  291. * Change directories mode to be writable by apache user
  292. */
  293. Varien_Io_File::chmodRecursive(Mage::getBaseDir('var'), 0777);
  294. return $encryptionKey;
  295. } catch (Exception $e) {
  296. $this->addError('ERROR: ' . $e->getMessage());
  297. return false;
  298. }
  299. }
  300. /**
  301. * Generate pseudorandom encryption key
  302. *
  303. * @param Mage_Core_Helper_Data $helper
  304. * @return string
  305. */
  306. public function generateEncryptionKey($helper = null)
  307. {
  308. if ($helper === null) {
  309. $helper = Mage::helper('Mage_Core_Helper_Data');
  310. }
  311. return md5($helper->getRandomString(10));
  312. }
  313. /**
  314. * Cleanup database use system configuration
  315. */
  316. protected function _cleanUpDatabase()
  317. {
  318. $dbConfig = Mage::getConfig()->getResourceConnectionConfig(Mage_Core_Model_Resource::DEFAULT_SETUP_RESOURCE);
  319. $modelName = 'Mage_Install_Model_Installer_Db_' . ucfirst($dbConfig->model);
  320. if (!class_exists($modelName)) {
  321. $this->addError('Database uninstall is not supported for the ' . ucfirst($dbConfig->model) . '.');
  322. return false;
  323. }
  324. /** @var $resourceModel Mage_Install_Model_Installer_Db_Abstract */
  325. $resourceModel = Mage::getModel($modelName);
  326. $resourceModel->cleanUpDatabase($dbConfig);
  327. }
  328. /**
  329. * Uninstall the application
  330. *
  331. * @return bool
  332. */
  333. public function uninstall()
  334. {
  335. if (!Mage::isInstalled()) {
  336. return false;
  337. }
  338. $this->_cleanUpDatabase();
  339. /* Remove temporary directories */
  340. $configOptions = Mage::app()->getConfig()->getOptions();
  341. $dirsToRemove = array(
  342. $configOptions->getCacheDir(),
  343. $configOptions->getSessionDir(),
  344. $configOptions->getExportDir(),
  345. $configOptions->getLogDir(),
  346. $configOptions->getVarDir() . '/report',
  347. );
  348. foreach ($dirsToRemove as $dir) {
  349. Varien_Io_File::rmdirRecursive($dir);
  350. }
  351. /* Remove local configuration */
  352. unlink($configOptions->getEtcDir() . '/local.xml');
  353. return true;
  354. }
  355. /**
  356. * Retrieve available locale codes
  357. *
  358. * @return array
  359. */
  360. public function getAvailableLocales()
  361. {
  362. return Mage::app()->getLocale()->getOptionLocales();
  363. }
  364. /**
  365. * Retrieve available currency codes
  366. *
  367. * @return array
  368. */
  369. public function getAvailableCurrencies()
  370. {
  371. return Mage::app()->getLocale()->getOptionCurrencies();
  372. }
  373. /**
  374. * Retrieve available timezone codes
  375. *
  376. * @return array
  377. */
  378. public function getAvailableTimezones()
  379. {
  380. return Mage::app()->getLocale()->getOptionTimezones();
  381. }
  382. /**
  383. * Retrieve available installation options
  384. *
  385. * @return array
  386. */
  387. public function getAvailableInstallOptions()
  388. {
  389. $result = array();
  390. foreach ($this->_installOptions as $optionName => $optionInfo) {
  391. $result[$optionName] = ($optionInfo['required'] ? 'required' : 'optional');
  392. }
  393. return $result;
  394. }
  395. }