00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Install_Model_Installer_Console extends Mage_Install_Model_Installer_Abstract
00035 {
00036
00037
00038
00039
00040
00041
00042 protected $_options;
00043
00044
00045
00046
00047
00048
00049 protected $_args = array();
00050
00051
00052
00053
00054
00055
00056 protected $_dataModel;
00057
00058
00059
00060
00061
00062
00063 protected $_app;
00064
00065
00066
00067
00068
00069
00070 protected function _getOptions()
00071 {
00072 if (is_null($this->_options)) {
00073 $this->_options = array(
00074 'license_agreement_accepted' => array('required' => true, 'comment' => ''),
00075 'locale' => array('required' => true, 'comment' => ''),
00076 'timezone' => array('required' => true, 'comment' => ''),
00077 'default_currency' => array('required' => true, 'comment' => ''),
00078 'db_host' => array('required' => true, 'comment' => ''),
00079 'db_name' => array('required' => true, 'comment' => ''),
00080 'db_user' => array('required' => true, 'comment' => ''),
00081 'db_pass' => array('comment' => ''),
00082 'db_prefix' => array('comment' => ''),
00083 'url' => array('required' => true, 'comment' => ''),
00084 'skip_url_validation' => array('comment' => ''),
00085 'use_rewrites' => array('required' => true, 'comment' => ''),
00086 'use_secure' => array('required' => true, 'comment' => ''),
00087 'secure_base_url' => array('required' => true, 'comment' => ''),
00088 'use_secure_admin' => array('required' => true, 'comment' => ''),
00089 'admin_lastname' => array('required' => true, 'comment' => ''),
00090 'admin_firstname' => array('required' => true, 'comment' => ''),
00091 'admin_email' => array('required' => true, 'comment' => ''),
00092 'admin_username' => array('required' => true, 'comment' => ''),
00093 'admin_password' => array('required' => true, 'comment' => ''),
00094 'encryption_key' => array('comment' => ''),
00095 'session_save' => array('comment' => ''),
00096 'admin_frontname' => array('comment' => ''),
00097 );
00098 }
00099 return $this->_options;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 public function setArgs($args = null)
00109 {
00110 if (empty($args)) {
00111
00112 $args = $_SERVER['argv'];
00113 }
00114
00115
00116
00117
00118 $currentArg = false;
00119 $match = false;
00120 foreach ($args as $arg) {
00121 if (preg_match('/^--(.*)$/', $arg, $match)) {
00122
00123 $currentArg = $match[1];
00124
00125 $args[$currentArg] = true;
00126 } else {
00127
00128 if ($currentArg) {
00129 $args[$currentArg] = $arg;
00130 }
00131 $currentArg = false;
00132 }
00133 }
00134
00135 if (isset($args['get_options'])) {
00136 $this->printOptions();
00137 return false;
00138 }
00139
00140
00141
00142
00143 foreach ($this->_getOptions() as $name => $option) {
00144 if (isset($option['required']) && $option['required'] && !isset($args[$name])) {
00145 $error = 'ERROR: ' . 'You should provide the value for --' . $name .' parameter';
00146 if (!empty($option['comment'])) {
00147 $error .= ': ' . $option['comment'];
00148 }
00149 $this->addError($error);
00150 }
00151 }
00152
00153 if ($this->hasErrors()) {
00154 return false;
00155 }
00156
00157
00158
00159
00160 if (!$this->_checkFlag($args['license_agreement_accepted'])) {
00161 $this->addError('ERROR: You have to accept Magento license agreement terms and conditions to continue installation');
00162 return false;
00163 }
00164
00165
00166
00167
00168 foreach ($this->_getOptions() as $name => $option) {
00169 $this->_args[$name] = isset($args[$name]) ? $args[$name] : '';
00170 }
00171
00172 return true;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 public function addError($error)
00182 {
00183 $this->_getDataModel()->addError($error);
00184 return $this;
00185 }
00186
00187
00188
00189
00190
00191
00192 public function hasErrors()
00193 {
00194 return (count($this->_getDataModel()->getErrors()) > 0);
00195 }
00196
00197
00198
00199
00200
00201
00202 public function getErrors()
00203 {
00204 return $this->_getDataModel()->getErrors();
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 protected function _checkFlag($value)
00217 {
00218 $res = (1 == $value)
00219 || preg_match('/^(yes|y|true)$/i', $value);
00220 return $res;
00221 }
00222
00223
00224
00225
00226
00227
00228 protected function _getDataModel()
00229 {
00230 if (is_null($this->_dataModel)) {
00231 $this->_dataModel = Mage::getModel('install/installer_data');
00232 }
00233 return $this->_dataModel;
00234 }
00235
00236
00237
00238
00239
00240
00241 public function getEncryptionKey()
00242 {
00243 return $this->_getDataModel()->getEncryptionKey();
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 public function init(Mage_Core_Model_App $app)
00253 {
00254 $this->_app = $app;
00255 $this->_getInstaller()->setDataModel($this->_getDataModel());
00256
00257
00258
00259
00260 if (Mage::isInstalled()) {
00261 $this->addError('ERROR: Magento is already installed');
00262 return false;
00263 }
00264
00265 return true;
00266 }
00267
00268
00269
00270
00271
00272
00273 protected function _prepareData()
00274 {
00275
00276
00277
00278 $this->_getDataModel()->setLocaleData(array(
00279 'locale' => $this->_args['locale'],
00280 'timezone' => $this->_args['timezone'],
00281 'currency' => $this->_args['default_currency'],
00282 ));
00283
00284
00285
00286
00287 $this->_getDataModel()->setConfigData(array(
00288 'db_host' => $this->_args['db_host'],
00289 'db_name' => $this->_args['db_name'],
00290 'db_user' => $this->_args['db_user'],
00291 'db_pass' => $this->_args['db_pass'],
00292 'db_prefix' => $this->_args['db_prefix'],
00293 'use_rewrites' => $this->_checkFlag($this->_args['use_rewrites']),
00294 'use_secure' => $this->_checkFlag($this->_args['use_secure']),
00295 'unsecure_base_url' => $this->_args['url'],
00296 'secure_base_url' => $this->_args['secure_base_url'],
00297 'use_secure_admin' => $this->_checkFlag($this->_args['use_secure_admin']),
00298 'session_save' => $this->_checkSessionSave($this->_args['session_save']),
00299 'admin_frontname' => $this->_checkAdminFrontname($this->_args['admin_frontname']),
00300 'skip_url_validation' => $this->_checkFlag($this->_args['skip_url_validation']),
00301 ));
00302
00303
00304
00305
00306 $this->_getDataModel()->setAdminData(array(
00307 'firstname' => $this->_args['admin_firstname'],
00308 'lastname' => $this->_args['admin_lastname'],
00309 'email' => $this->_args['admin_email'],
00310 'username' => $this->_args['admin_username'],
00311 'password' => $this->_args['admin_password'],
00312 ));
00313
00314 return $this;
00315 }
00316
00317
00318
00319
00320
00321
00322 public function install()
00323 {
00324 try {
00325
00326
00327
00328
00329 if (Mage::isInstalled()) {
00330 $this->addError('ERROR: Magento is already installed');
00331 return false;
00332 }
00333
00334
00335
00336
00337 $this->_getDataModel()->setSkipUrlValidation($this->_args['skip_url_validation']);
00338 $this->_getDataModel()->setSkipBaseUrlValidation($this->_args['skip_url_validation']);
00339
00340
00341
00342
00343 $this->_prepareData();
00344
00345 if ($this->hasErrors()) {
00346 return false;
00347 }
00348
00349 $installer = $this->_getInstaller();
00350
00351
00352
00353
00354 $installer->installConfig($this->_getDataModel()->getConfigData());
00355
00356 if ($this->hasErrors()) {
00357 return false;
00358 }
00359
00360
00361
00362
00363
00364 $this->_app->cleanCache();
00365 Mage::getConfig()->reinit();
00366
00367
00368
00369
00370 $installer->installDb();
00371
00372 if ($this->hasErrors()) {
00373 return false;
00374 }
00375
00376
00377
00378
00379 $installer->createAdministrator($this->_getDataModel()->getAdminData());
00380
00381 if ($this->hasErrors()) {
00382 return false;
00383 }
00384
00385
00386
00387
00388 $encryptionKey = empty($this->_args['encryption_key']) ? md5(time()) : $this->_args['encryption_key'];
00389 $this->_getDataModel()->setEncryptionKey($encryptionKey);
00390 $installer->installEnryptionKey($encryptionKey);
00391
00392 if ($this->hasErrors()) {
00393 return false;
00394 }
00395
00396
00397
00398
00399 $installer->finish();
00400
00401 if ($this->hasErrors()) {
00402 return false;
00403 }
00404
00405
00406
00407
00408 @chmod('var/cache', 0777);
00409 @chmod('var/session', 0777);
00410
00411 } catch (Exception $e) {
00412 $this->addError('ERROR: ' . $e->getMessage());
00413 return false;
00414 }
00415
00416 return true;
00417 }
00418
00419
00420
00421
00422
00423
00424 public function printOptions()
00425 {
00426 $options = array(
00427 'locale' => $this->_app->getLocale()->getOptionLocales(),
00428 'currency' => $this->_app->getLocale()->getOptionCurrencies(),
00429 'timezone' => $this->_app->getLocale()->getOptionTimezones(),
00430 );
00431 var_export($options);
00432 return $this;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441 public function checkConsole($url=null)
00442 {
00443 if (defined('STDIN') && defined('STDOUT') && (defined('STDERR'))) {
00444 return true;
00445 }
00446 if (is_null($url)) {
00447 $url = preg_replace('/install\.php/i', '', Mage::getBaseUrl());
00448 $url = preg_replace('/\/\/$/', '/', $url);
00449 }
00450 header('Location: ' . $url);
00451 return false;
00452 }
00453
00454 }