1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33:
34: class Mage_Install_Model_Installer_Console extends Mage_Install_Model_Installer_Abstract
35: {
36:
37: 38: 39: 40: 41:
42: protected $_options;
43:
44: 45: 46: 47: 48:
49: protected $_args = array();
50:
51: 52: 53: 54: 55:
56: protected $_dataModel;
57:
58: 59: 60: 61: 62:
63: protected $_app;
64:
65: 66: 67: 68: 69:
70: protected function _getOptions()
71: {
72: if (is_null($this->_options)) {
73: $this->_options = array(
74: 'license_agreement_accepted' => array('required' => true, 'comment' => ''),
75: 'locale' => array('required' => true, 'comment' => ''),
76: 'timezone' => array('required' => true, 'comment' => ''),
77: 'default_currency' => array('required' => true, 'comment' => ''),
78: 'db_model' => array('comment' => ''),
79: 'db_host' => array('required' => true, 'comment' => ''),
80: 'db_name' => array('required' => true, 'comment' => ''),
81: 'db_user' => array('required' => true, 'comment' => ''),
82: 'db_pass' => array('comment' => ''),
83: 'db_prefix' => array('comment' => ''),
84: 'url' => array('required' => true, 'comment' => ''),
85: 'skip_url_validation' => array('comment' => ''),
86: 'use_rewrites' => array('required' => true, 'comment' => ''),
87: 'use_secure' => array('required' => true, 'comment' => ''),
88: 'secure_base_url' => array('required' => true, 'comment' => ''),
89: 'use_secure_admin' => array('required' => true, 'comment' => ''),
90: 'admin_lastname' => array('required' => true, 'comment' => ''),
91: 'admin_firstname' => array('required' => true, 'comment' => ''),
92: 'admin_email' => array('required' => true, 'comment' => ''),
93: 'admin_username' => array('required' => true, 'comment' => ''),
94: 'admin_password' => array('required' => true, 'comment' => ''),
95: 'encryption_key' => array('comment' => ''),
96: 'session_save' => array('comment' => ''),
97: 'admin_frontname' => array('comment' => ''),
98: 'enable_charts' => array('comment' => ''),
99: );
100: }
101: return $this->_options;
102: }
103:
104: 105: 106: 107: 108: 109:
110: public function setArgs($args = null)
111: {
112: if (empty($args)) {
113:
114: $args = $_SERVER['argv'];
115: }
116:
117: 118: 119:
120: $currentArg = false;
121: $match = false;
122: foreach ($args as $arg) {
123: if (preg_match('/^--(.*)$/', $arg, $match)) {
124:
125: $currentArg = $match[1];
126:
127: $args[$currentArg] = true;
128: } else {
129:
130: if ($currentArg) {
131: $args[$currentArg] = $arg;
132: }
133: $currentArg = false;
134: }
135: }
136:
137: if (isset($args['get_options'])) {
138: $this->printOptions();
139: return false;
140: }
141:
142: 143: 144:
145: foreach ($this->_getOptions() as $name => $option) {
146: if (isset($option['required']) && $option['required'] && !isset($args[$name])) {
147: $error = 'ERROR: ' . 'You should provide the value for --' . $name . ' parameter';
148: if (!empty($option['comment'])) {
149: $error .= ': ' . $option['comment'];
150: }
151: $this->addError($error);
152: }
153: }
154:
155: if ($this->hasErrors()) {
156: return false;
157: }
158:
159: 160: 161:
162: if (!$this->_checkFlag($args['license_agreement_accepted'])) {
163: $this->addError('ERROR: You have to accept Magento license agreement terms and conditions to continue installation');
164: return false;
165: }
166:
167: 168: 169:
170: foreach ($this->_getOptions() as $name => $option) {
171: $this->_args[$name] = isset($args[$name]) ? $args[$name] : '';
172: }
173:
174: return true;
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function addError($error)
184: {
185: $this->_getDataModel()->addError($error);
186: return $this;
187: }
188:
189: 190: 191: 192: 193:
194: public function hasErrors()
195: {
196: return (count($this->_getDataModel()->getErrors()) > 0);
197: }
198:
199: 200: 201: 202: 203:
204: public function getErrors()
205: {
206: return $this->_getDataModel()->getErrors();
207: }
208:
209: 210: 211: 212: 213: 214: 215: 216: 217:
218: protected function _checkFlag($value)
219: {
220: $res = (1 == $value)
221: || preg_match('/^(yes|y|true)$/i', $value);
222: return $res;
223: }
224:
225: 226: 227: 228: 229:
230: protected function _getDataModel()
231: {
232: if (is_null($this->_dataModel)) {
233: $this->_dataModel = Mage::getModel('install/installer_data');
234: }
235: return $this->_dataModel;
236: }
237:
238: 239: 240: 241: 242:
243: public function getEncryptionKey()
244: {
245: return $this->_getDataModel()->getEncryptionKey();
246: }
247:
248: 249: 250: 251: 252: 253:
254: public function init(Mage_Core_Model_App $app)
255: {
256: $this->_app = $app;
257: $this->_getInstaller()->setDataModel($this->_getDataModel());
258:
259: 260: 261:
262: if (Mage::isInstalled()) {
263: $this->addError('ERROR: Magento is already installed');
264: return false;
265: }
266:
267: return true;
268: }
269:
270: 271: 272: 273: 274:
275: protected function _prepareData()
276: {
277: 278: 279:
280: $this->_getDataModel()->setLocaleData(array(
281: 'locale' => $this->_args['locale'],
282: 'timezone' => $this->_args['timezone'],
283: 'currency' => $this->_args['default_currency'],
284: ));
285:
286: 287: 288:
289: $this->_getDataModel()->setConfigData(array(
290: 'db_model' => $this->_args['db_model'],
291: 'db_host' => $this->_args['db_host'],
292: 'db_name' => $this->_args['db_name'],
293: 'db_user' => $this->_args['db_user'],
294: 'db_pass' => $this->_args['db_pass'],
295: 'db_prefix' => $this->_args['db_prefix'],
296: 'use_rewrites' => $this->_checkFlag($this->_args['use_rewrites']),
297: 'use_secure' => $this->_checkFlag($this->_args['use_secure']),
298: 'unsecure_base_url' => $this->_args['url'],
299: 'secure_base_url' => $this->_args['secure_base_url'],
300: 'use_secure_admin' => $this->_checkFlag($this->_args['use_secure_admin']),
301: 'session_save' => $this->_checkSessionSave($this->_args['session_save']),
302: 'admin_frontname' => $this->_checkAdminFrontname($this->_args['admin_frontname']),
303: 'skip_url_validation' => $this->_checkFlag($this->_args['skip_url_validation']),
304: 'enable_charts' => $this->_checkFlag($this->_args['enable_charts']),
305: ));
306:
307: 308: 309:
310: $this->_getDataModel()->setAdminData(array(
311: 'firstname' => $this->_args['admin_firstname'],
312: 'lastname' => $this->_args['admin_lastname'],
313: 'email' => $this->_args['admin_email'],
314: 'username' => $this->_args['admin_username'],
315: 'new_password' => $this->_args['admin_password'],
316: ));
317:
318: return $this;
319: }
320:
321: 322: 323: 324: 325:
326: public function install()
327: {
328: try {
329:
330: 331: 332:
333: if (Mage::isInstalled()) {
334: $this->addError('ERROR: Magento is already installed');
335: return false;
336: }
337:
338: 339: 340:
341: $this->_getDataModel()->setSkipUrlValidation($this->_args['skip_url_validation']);
342: $this->_getDataModel()->setSkipBaseUrlValidation($this->_args['skip_url_validation']);
343:
344: 345: 346:
347: $this->_prepareData();
348:
349: if ($this->hasErrors()) {
350: return false;
351: }
352:
353: $installer = $this->_getInstaller();
354:
355: 356: 357:
358: $installer->installConfig($this->_getDataModel()->getConfigData());
359:
360: if ($this->hasErrors()) {
361: return false;
362: }
363:
364: 365: 366:
367:
368: $this->_app->cleanCache();
369: Mage::getConfig()->reinit();
370:
371: 372: 373:
374: $installer->installDb();
375:
376: if ($this->hasErrors()) {
377: return false;
378: }
379:
380:
381: Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
382:
383: 384: 385:
386: $user = $installer->validateAndPrepareAdministrator($this->_getDataModel()->getAdminData());
387:
388: if ($this->hasErrors()) {
389: return false;
390: }
391:
392: 393: 394:
395: $encryptionKey = empty($this->_args['encryption_key'])
396: ? md5(Mage::helper('core')->getRandomString(10))
397: : $this->_args['encryption_key'];
398: $this->_getDataModel()->setEncryptionKey($encryptionKey);
399: $installer->validateEncryptionKey($encryptionKey);
400:
401: if ($this->hasErrors()) {
402: return false;
403: }
404:
405: 406: 407:
408: $installer->createAdministrator($user);
409:
410: if ($this->hasErrors()) {
411: return false;
412: }
413:
414: 415: 416:
417: $installer->installEnryptionKey($encryptionKey);
418:
419: if ($this->hasErrors()) {
420: return false;
421: }
422:
423: 424: 425:
426: $installer->finish();
427:
428: if ($this->hasErrors()) {
429: return false;
430: }
431:
432: 433: 434:
435: @chmod('var/cache', 0777);
436: @chmod('var/session', 0777);
437:
438: } catch (Exception $e) {
439: $this->addError('ERROR: ' . $e->getMessage());
440: return false;
441: }
442:
443: return true;
444: }
445:
446: 447: 448: 449: 450:
451: public function printOptions()
452: {
453: $options = array(
454: 'locale' => $this->_app->getLocale()->getOptionLocales(),
455: 'currency' => $this->_app->getLocale()->getOptionCurrencies(),
456: 'timezone' => $this->_app->getLocale()->getOptionTimezones(),
457: );
458: var_export($options);
459: return $this;
460: }
461:
462: 463: 464: 465: 466: 467:
468: public function checkConsole($url=null)
469: {
470: if (defined('STDIN') && defined('STDOUT') && (defined('STDERR'))) {
471: return true;
472: }
473: if (is_null($url)) {
474: $url = preg_replace('/install\.php/i', '', Mage::getBaseUrl());
475: $url = preg_replace('/\/\/$/', '/', $url);
476: }
477: header('Location: ' . $url);
478: return false;
479: }
480:
481: }
482: