cbc1a68f662b64a3365108423b4a6438593e4a92
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / mod / class.tx_install.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2007 Thomas Hempel (thomas@work.de)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 require('conf.php');
29
30 require_once(PATH_site.'typo3/sysext/lang/lang.php');
31 require (PATH_site.'typo3/template.php');
32
33 require_once('class.tx_install_view.php');
34 require_once('class.tx_install_basics.php');
35 require_once(t3lib_extMgm::extPath('install').'modules/class.tx_install_module_base.php');
36
37 /**
38 * Main controller class for Installer
39 *
40 * $Id$
41 *
42 * @author Thomas Hempel <thomas@typo3-unleashed.net>
43 * @author Ingo Renner <ingo@typo3.org>
44 */
45 class tx_install {
46 private $env = NULL;
47 private $collectedResults = array();
48 private $backPath = '../../typo3/';
49
50 private $defaults = array (
51 'module' => 'setup',
52 'language' => 'default'
53 );
54
55 private $language = 'default';
56 private $LOCAL_LANG = NULL;
57
58 /**
59 * view object
60 *
61 * @var tx_install_view
62 */
63 private $viewObj = NULL;
64
65 /**
66 * basics object
67 *
68 * @var tx_install_basics
69 */
70 private $basicsObj = NULL;
71
72 /**
73 * language object
74 *
75 * @var language
76 */
77 private $langObj = NULL;
78
79 /**
80 * An index that stores which deliverable is associated to which label
81 *
82 * @var array
83 */
84 private $labelIndex = NULL;
85
86 /**
87 * The name of the cookie that is used for setup login
88 *
89 * @var string
90 */
91 private $cookieName = 'Typo3InstallTool';
92
93 /**
94 * Stores if the password entered is correct. Eventually this stores if a user if sucessfully logged in.
95 *
96 * @var boolean
97 */
98 private $passwordOK = false;
99
100 /**
101 * Array with all load modules
102 *
103 * @var array
104 */
105 private $loadedModules = array();
106
107 /**
108 * Set to true if writing data to localconf.php should be allowed
109 *
110 * @var boolean
111 */
112 private $allowUpdateLocalConf;
113
114 /**
115 * The results from the search. Current searchString can be found above the category tree
116 *
117 * @var array
118 */
119 private $filterResults = array();
120
121 /**
122 * Constructor, gets called from TYPO3 backend
123 *
124 * @return void
125 */
126 public function init() {
127 global $PAGE_TYPES, $TBE_STYLES;
128
129 if (!defined('PATH_typo3')) exit;
130
131 $this->loadedModules = array();
132 session_start();
133
134 // load the temp_CACHED files. This is necesarry to get some environment information like
135 // $GLOBALS['TBE_STYLES']. Without this information we can't use skins that might be installed.
136 if(TYPO3_MODE == 'BE') {
137 include (TYPO3_tables_script ? PATH_typo3conf.TYPO3_tables_script : PATH_t3lib.'stddb/tables.php');
138 if ($GLOBALS['TYPO3_LOADED_EXT']['_CACHEFILE']) {
139 include (PATH_typo3conf.$GLOBALS['TYPO3_LOADED_EXT']['_CACHEFILE'].'_ext_tables.php');
140 }
141 }
142
143 // Build the environment by processing incoming variables - the session is the base; POST and GET vars will overrule the data in the session while
144 // POST overrules GET
145 $this->env = (array) $_SESSION;
146 $get = t3lib_div::_GET();
147 $post = t3lib_div::_POST();
148 $this->env = t3lib_div::array_merge_recursive_overrule($this->env, $get);
149 $this->env = t3lib_div::array_merge_recursive_overrule($this->env, $post);
150
151 // instanciate the needed objects
152 $this->viewObj = t3lib_div::makeInstance('tx_install_view');
153 $this->basicsObj = t3lib_div::makeInstance('tx_install_basics');
154 $this->viewObj->init($this);
155 $this->basicsObj->init($this);
156
157 // select the language
158 // TODO: Here we have to determine which language is currently selected for the backend
159 $this->language = (isset($this->env['L'])) ? $this->env['L'] : $this->defaults['language'];
160
161 // instanciate the language module
162 $this->langObj = t3lib_div::makeInstance('language');
163 $this->langObj->init($this->language);
164
165 if (!isset($GLOBALS['LANG'])) {
166 $GLOBALS['LANG'] = $this->langObj;
167 }
168
169 // load the basic language files for the installer-module itself
170 $this->LOCAL_LANG = $this->langObj->includeLLFile(t3lib_extMgm::extPath('install').'mod/locallang_mod.xml', FALSE);
171
172 // check for a password
173 if(!isset($GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword'])) {
174 $this->viewObj->addContent('', 'Install Tool deactivated.<br />You must enable it by setting a password in typo3conf/localconf.php. If you insert the line below, the password will be "joh316":<br /><br />$TYPO3_CONF_VARS[\'BE\'][\'installToolPassword\'] = \'bacb98acf97e0b6112b1d1b650b84971\';');
175 echo $this->viewObj->getDocCode();
176 die();
177 }
178
179 if($this->env['mode'] == '123') {
180 $this->env['module'] = 'installer';
181 $this->passwordOK = true;
182 } else {
183 $this->env['module'] = $this->defaults['module'];
184 }
185
186 // load localconf file
187 $this->basicsObj->loadLocalconf();
188
189 // do the login if mode is not 123
190 if($this->env['mode'] != '123') {
191 $randomKey = $_COOKIE[$this->cookieName.'_key'];
192 if(!$randomKey) {
193 $randomKey = md5(uniqid(microtime()));
194 setcookie($this->cookieName.'_key', $randomKey, 0, '/'); // Cookie is set
195 // add a message that pops up in a JavaScript alert
196 $this->viewObj->addJSmessage($this->basicsObj->getLabel('msg_setup_firstcall'));
197 }
198
199 $combinedKey = $_COOKIE[$this->cookieName];
200 if(md5($GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword'].'|'.$randomKey) == $combinedKey || $this->checkPassword($randomKey)) {
201 $this->passwordOK = true;
202 } else {
203 $this->viewObj->addContent($this->basicsObj->getLabel('label_login'), $this->loginForm());
204 echo $this->viewObj->getDocCode();
205 }
206 }
207
208 if($this->passwordOK) {
209 // load module and execute main method
210 $method = 'main';
211 if ($this->env['method']) {
212 $method = $this->env['method'];
213 }
214
215 $moduleContent = $this->basicsObj->executeMethod(array($this->env['module'], $method));
216
217 if ($this->env['ajax'] == 1) {
218 header('X-JSON: (true)');
219 header('Content-type: text/html; charset=utf-8');
220 echo $moduleContent;
221 } else {
222 if($moduleContent === false) {
223 // if we reach this point, something went wrong with the module. Print all collected general errors
224 $this->viewObj->addContent($this->basicsObj->getLabel('msg_error_occured'), $this->viewObj->renderErrors());
225 echo $this->viewObj->getDocCode();
226 } else {
227 // depending on the mode, we print the result directly or use the doc object
228 if($this->env['mode'] == '123') {
229 echo $moduleContent['content'];
230 } else {
231 // add the module content to the document
232 $this->viewObj->addContent($moduleContent['title'], $moduleContent['content']);
233
234 // and print out the result
235 echo $this->viewObj->getDocCode();
236 }
237 }
238 }
239 }
240 }
241
242 /**
243 * Checks whether the submitted password is ok or not.
244 *
245 * @param String a random key for combination and compare
246 * @return boolean true if submitted password is ok, false otherwise
247 */
248 private function checkPassword($randomKey) {
249 $password = t3lib_div::_GP('password');
250 $warningEmail = $GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'];
251 $passwordOk = false;
252
253 if($password && md5($password) == $GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword']) {
254 $combinedKey = md5($GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword'].'|'.$randomKey);
255 setcookie($this->cookieName, $combinedKey, 0, '/');
256
257 // Sending warning email
258 if($warningEmail) {
259 $subject = sprintf($this->basicsObj->getLabel('login_emailwarning_true_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
260 $emailBody = sprintf($this->basicsObj->getLabel('login_emailwarning_true_message'),
261 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
262 t3lib_div::getIndpEnv('HTTP_HOST'),
263 t3lib_div::getIndpEnv('REMOTE_ADDR'),
264 t3lib_div::getIndpEnv('REMOTE_HOST')
265 );
266
267 mail($warningEmail,
268 $subject,
269 $emailBody,
270 'From: TYPO3 Install Tool WARNING <>'
271 );
272 }
273
274 $passwordOk = true;
275 } else {
276 // Bad password, send warning:
277 if($password) {
278 if($warningEmail) {
279 $subject = sprintf($this->basicsObj->getLabel('login_emailwarning_false_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
280 $emailBody = sprintf($this->basicsObj->getLabel('login_emailwarning_false_subject'),
281 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
282 t3lib_div::getIndpEnv('HTTP_HOST'),
283 $password,
284 t3lib_div::getIndpEnv('REMOTE_ADDR'),
285 t3lib_div::getIndpEnv('REMOTE_HOST')
286 );
287
288 mail($warningEmail,
289 $subject,
290 $emailBody,
291 'From: TYPO3 Install Tool WARNING <>'
292 );
293 }
294
295 $this->basicsObj->addError(sprintf($this->basicsObj->getLabel('msg_error_login_not_sucessful_message'), md5($password)), FATAL, 'fields', 'password');
296 }
297 }
298
299 return $passwordOk;
300 }
301
302 /**
303 * [Describe function...]
304 *
305 * @return [type] ...
306 */
307 private function loginForm() {
308 $formConfig = array(
309 'type' => 'form',
310 'value' => array(
311 'options' => array(
312 'name' => 'form_setuplogin',
313 'submit' => $this->basicsObj->getLabel('label_login'),
314 ),
315 'elements' => array(
316 array(
317 'type' => 'formelement',
318 'value' => array(
319 'label' => 'label_setup_password',
320 'elementType' => 'password',
321 'options' => array(
322 'name' => 'password'
323 )
324 )
325 ),
326 array(
327 'type' => 'plain',
328 'value' => $this->basicsObj->getLabel('msg_login_passwordhint')
329 )
330 )
331 )
332 );
333
334 return $this->viewObj->render($formConfig);
335 }
336
337 /**
338 * gets the basics object
339 *
340 * @return tx_install_basics
341 */
342 public function getBasicsObject() {
343 return $this->basicsObj;
344 }
345
346 /**
347 * gets the view object
348 *
349 * @return tx_install_view
350 */
351 public function getViewObject() {
352 return $this->viewObj;
353 }
354
355 /**
356 * gets the language object
357 *
358 * @return language
359 */
360 public function getLanguageObject() {
361 return $this->langObj;
362 }
363
364 /**
365 * gets LOCAL_LANG
366 *
367 * @return array arrayof language labels
368 */
369 public function getLocalLang() {
370 return $this->LOCAL_LANG;
371 }
372
373 /**
374 * Enter description here...
375 *
376 * TODO add check to $value
377 *
378 * @param array $value
379 */
380 public function setLocalLang($value) {
381 $this->LOCAL_LANG = $value;
382 }
383
384 /**
385 * Enter description here...
386 *
387 * @return array
388 */
389 public function getLoadedModules() {
390 return $this->loadedModules;
391 }
392
393 /**
394 * adds a module to the loaded modules
395 *
396 * TODO add checks
397 *
398 * @param string moduel name
399 * @param object module object
400 */
401 public function addLoadedModule($moduleName, $moduleObject) {
402 $this->loadedModules[$moduleName] = $moduleObject;
403 }
404
405 /**
406 * gets the environment
407 *
408 * @return array
409 */
410 public function getEnvironment() {
411 return $this->env;
412 }
413
414 /**
415 * Enter description here...
416 *
417 * @return boolean
418 */
419 public function updateLocalconfAllowed() {
420 return $this->allowUpdateLocalConf;
421 }
422
423 /**
424 * Enter description here...
425 *
426 * TODO add checks
427 *
428 * @param boolean $value
429 */
430 public function setAllowUpdateLocalConf($value) {
431 $this->allowUpdateLocalConf = $value;
432 }
433
434 /**
435 * gets the backpath
436 *
437 * @return string backpath
438 */
439 public function getBackPath() {
440 return $this->backPath;
441 }
442
443 public function getLanguage() {
444 return $this->language;
445 }
446
447 public function getLabelIndex() {
448 return $_SESSION['installer']['labelIndex'];
449 }
450
451 public function getLabelIndexItem($index) {
452 return $_SESSION['installer']['labelIndex'][$index];
453 }
454
455 public function addLabelIndex($mainCat, $subCat, $deliverableType, $deliverable, $index) {
456 $_SESSION['installer']['labelIndex'][$index] = array (
457 'mainCat' => $mainCat,
458 'subCat' => $subCat,
459 'deliverableType' => $deliverableType,
460 'deliverable' => $deliverable
461 );
462 }
463
464 public function getFilterResults() {
465 $this->filterResults = $_SESSION['installer']['filterResults'];
466 return $this->filterResults;
467 }
468
469 public function setFilterResults($filterResults) {
470 $this->filterResults = $filterResults;
471 $_SESSION['installer']['filterResults'] = $this->filterResults;
472 }
473
474 public function __set($property, $value) {
475 // needed for comaptibility with /typo3/init.php
476 // TODO fix init.php and remove this method
477 switch($property) {
478 case 'allowUpdateLocalConf':
479 $this->allowUpdateLocalConf = (bool) $value;
480 break;
481 // case 'backPath':
482 // $this->backPath = (string) $value;
483 // break;
484 default:
485 // nothing
486 }
487 }
488 }
489
490 if(defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/install/mod/class.tx_install.php']) {
491 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/install/mod/class.tx_install.php']);
492 }
493
494 ?>