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: class Mage_Install_Model_Installer_Config extends Mage_Install_Model_Installer_Abstract
34: {
35: const TMP_INSTALL_DATE_VALUE= 'd-d-d-d-d';
36: const TMP_ENCRYPT_KEY_VALUE = 'k-k-k-k-k';
37:
38: 39: 40: 41: 42:
43: protected $_localConfigFile;
44:
45: protected $_configData = array();
46:
47: public function __construct()
48: {
49: $this->_localConfigFile = Mage::getBaseDir('etc') . DS . 'local.xml';
50: }
51:
52: public function setConfigData($data)
53: {
54: if (is_array($data)) {
55: $this->_configData = $data;
56: }
57: return $this;
58: }
59:
60: public function getConfigData()
61: {
62: return $this->_configData;
63: }
64:
65: public function install()
66: {
67: $data = $this->getConfigData();
68: foreach (Mage::getModel('core/config')->getDistroServerVars() as $index=>$value) {
69: if (!isset($data[$index])) {
70: $data[$index] = $value;
71: }
72: }
73:
74: if (isset($data['unsecure_base_url'])) {
75: $data['unsecure_base_url'] .= substr($data['unsecure_base_url'], -1) != '/' ? '/' : '';
76: if (strpos($data['unsecure_base_url'], 'http') !== 0) {
77: $data['unsecure_base_url'] = 'http://' . $data['unsecure_base_url'];
78: }
79: if (!$this->_getInstaller()->getDataModel()->getSkipBaseUrlValidation()) {
80: $this->_checkUrl($data['unsecure_base_url']);
81: }
82: }
83: if (isset($data['secure_base_url'])) {
84: $data['secure_base_url'] .= substr($data['secure_base_url'], -1) != '/' ? '/' : '';
85: if (strpos($data['secure_base_url'], 'http') !== 0) {
86: $data['secure_base_url'] = 'https://' . $data['secure_base_url'];
87: }
88:
89: if (!empty($data['use_secure'])
90: && !$this->_getInstaller()->getDataModel()->getSkipUrlValidation()) {
91: $this->_checkUrl($data['secure_base_url']);
92: }
93: }
94:
95: $data['date'] = self::TMP_INSTALL_DATE_VALUE;
96: $data['key'] = self::TMP_ENCRYPT_KEY_VALUE;
97: $data['var_dir'] = $data['root_dir'] . '/var';
98:
99: $data['use_script_name'] = isset($data['use_script_name']) ? 'true' : 'false';
100:
101: $this->_getInstaller()->getDataModel()->setConfigData($data);
102:
103: $template = file_get_contents(Mage::getBaseDir('etc') . DS . 'local.xml.template');
104: foreach ($data as $index => $value) {
105: $template = str_replace('{{' . $index . '}}', '<![CDATA[' . $value . ']]>', $template);
106: }
107: file_put_contents($this->_localConfigFile, $template);
108: chmod($this->_localConfigFile, 0777);
109: }
110:
111: public function getFormData()
112: {
113: $uri = Zend_Uri::factory(Mage::getBaseUrl('web'));
114:
115: $baseUrl = $uri->getUri();
116: if ($uri->getScheme() !== 'https') {
117: $uri->setPort(null);
118: $baseSecureUrl = str_replace('http://', 'https://', $uri->getUri());
119: } else {
120: $baseSecureUrl = $uri->getUri();
121: }
122:
123: $connectDefault = Mage::getConfig()
124: ->getResourceConnectionConfig(Mage_Core_Model_Resource::DEFAULT_SETUP_RESOURCE);
125:
126: $data = Mage::getModel('varien/object')
127: ->setDbHost($connectDefault->host)
128: ->setDbName($connectDefault->dbname)
129: ->setDbUser($connectDefault->username)
130: ->setDbModel($connectDefault->model)
131: ->setDbPass('')
132: ->setSecureBaseUrl($baseSecureUrl)
133: ->setUnsecureBaseUrl($baseUrl)
134: ->setAdminFrontname('admin')
135: ->setEnableCharts('1')
136: ;
137: return $data;
138: }
139:
140: protected function _checkHostsInfo($data)
141: {
142: $url = $data['protocol'] . '://' . $data['host'] . ':' . $data['port'] . $data['base_path'];
143: $surl = $data['secure_protocol'] . '://' . $data['secure_host'] . ':' . $data['secure_port']
144: . $data['secure_base_path'];
145:
146: $this->_checkUrl($url);
147: $this->_checkUrl($surl, true);
148:
149: return $this;
150: }
151:
152: protected function _checkUrl($url, $secure = false)
153: {
154: $prefix = $secure ? 'install/wizard/checkSecureHost/' : 'install/wizard/checkHost/';
155: try {
156: $client = new Varien_Http_Client($url . 'index.php/' . $prefix);
157: $response = $client->request('GET');
158:
159: $body = $response->getBody();
160: }
161: catch (Exception $e){
162: $this->_getInstaller()->getDataModel()
163: ->addError(Mage::helper('install')->__('The URL "%s" is not accessible.', $url));
164: throw $e;
165: }
166:
167: if ($body != Mage_Install_Model_Installer::INSTALLER_HOST_RESPONSE) {
168: $this->_getInstaller()->getDataModel()
169: ->addError(Mage::helper('install')->__('The URL "%s" is invalid.', $url));
170: Mage::throwException(Mage::helper('install')->__('Response from server isn\'t valid.'));
171: }
172: return $this;
173: }
174:
175: public function replaceTmpInstallDate($date = null)
176: {
177: $stamp = strtotime((string) $date);
178: $localXml = file_get_contents($this->_localConfigFile);
179: $localXml = str_replace(self::TMP_INSTALL_DATE_VALUE, date('r', $stamp ? $stamp : time()), $localXml);
180: file_put_contents($this->_localConfigFile, $localXml);
181:
182: return $this;
183: }
184:
185: public function replaceTmpEncryptKey($key = null)
186: {
187: if (!$key) {
188: $key = md5(Mage::helper('core')->getRandomString(10));
189: }
190: $localXml = file_get_contents($this->_localConfigFile);
191: $localXml = str_replace(self::TMP_ENCRYPT_KEY_VALUE, $key, $localXml);
192: file_put_contents($this->_localConfigFile, $localXml);
193:
194: return $this;
195: }
196: }
197: