* Added support for listing and calling methods in new installer
[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 // set some default module if nothing was set
180 if (empty($this->env['module'])) {
181 if($this->env['mode'] == '123') {
182 $this->env['module'] = 'installer';
183 $this->passwordOK = true;
184 } else {
185 $this->env['module'] = $this->defaults['module'];
186 }
187 }
188
189 // load localconf file
190 $this->basicsObj->loadLocalconf();
191
192 // do the login if mode is not 123
193 if($this->env['mode'] != '123') {
194 $this->passwordOK = false;
195
196 $randomKey = $_COOKIE[$this->cookieName.'_key'];
197 if(!$randomKey) {
198 $randomKey = md5(uniqid(microtime()));
199 setcookie($this->cookieName.'_key', $randomKey, 0, '/'); // Cookie is set
200
201 // add a message that pops up in a JavaScript alert
202 $this->viewObj->addJSmessage($this->basicsObj->getLabel('msg_setup_firstcall'));
203 }
204
205 $combinedKey = $_COOKIE[$this->cookieName];
206 if(md5($GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword'].'|'.$randomKey) == $combinedKey || $this->checkPassword($randomKey)) {
207 $this->passwordOK = true;
208 } else {
209 $this->viewObj->addContent($this->basicsObj->getLabel('label_login'), $this->loginForm());
210 echo $this->viewObj->getDocCode();
211 }
212 }
213
214 if($this->passwordOK) {
215 // load module and execute main method
216 $method = 'main';
217 if ($this->env['method']) {
218 $method = $this->env['method'];
219 }
220
221 // execute given method and save the result in a local variable
222 $moduleContent = $this->basicsObj->executeMethod(array($this->env['module'], $method));
223
224 // check if we have to handle the module content with AJAX
225 if ($this->env['ajax'] == 1) {
226 header('X-JSON: (true)');
227 header('Content-type: text/html; charset=utf-8');
228
229 // render errors if module returned FALSE
230 if ($moduleContent == false) {
231 $moduleContent = $this->viewObj->renderErrors();
232 }
233
234 echo $moduleContent;
235 } else {
236 if($moduleContent === false) {
237 // if we reach this point, something went wrong with the module. Print all collected general errors
238 $this->viewObj->addContent($this->basicsObj->getLabel('msg_error_occured'), $this->viewObj->renderErrors());
239 echo $this->viewObj->getDocCode();
240 } else {
241 // depending on the mode, we print the result directly or use the doc object
242 if($this->env['mode'] == '123') {
243 echo $moduleContent['content'];
244 } else {
245 // add the module content to the document
246 $this->viewObj->addContent($moduleContent['title'], $moduleContent['content']);
247
248 // and print out the result
249 echo $this->viewObj->getDocCode();
250 }
251 }
252 }
253 }
254 }
255
256 /**
257 * Checks whether the submitted password is ok or not.
258 *
259 * @param String a random key for combination and compare
260 * @return boolean true if submitted password is ok, false otherwise
261 */
262 private function checkPassword($randomKey) {
263 $password = t3lib_div::_GP('typo3_install_password');
264 $warningEmail = $GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'];
265 $passwordOk = false;
266
267 if($password && md5($password) == $GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword']) {
268 $combinedKey = md5($GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword'].'|'.$randomKey);
269 setcookie($this->cookieName, $combinedKey, 0, '/');
270
271 // Sending warning email
272 if($warningEmail) {
273 $subject = sprintf($this->basicsObj->getLabel('login_emailwarning_true_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
274 $emailBody = sprintf($this->basicsObj->getLabel('login_emailwarning_true_message'),
275 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
276 t3lib_div::getIndpEnv('HTTP_HOST'),
277 t3lib_div::getIndpEnv('REMOTE_ADDR'),
278 t3lib_div::getIndpEnv('REMOTE_HOST')
279 );
280
281 mail($warningEmail,
282 $subject,
283 $emailBody,
284 'From: TYPO3 Install Tool WARNING <>'
285 );
286 }
287
288 $passwordOk = true;
289 } else {
290 // Bad password, send warning:
291 if($password) {
292 if($warningEmail) {
293 $subject = sprintf($this->basicsObj->getLabel('login_emailwarning_false_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
294 $emailBody = sprintf($this->basicsObj->getLabel('login_emailwarning_false_subject'),
295 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
296 t3lib_div::getIndpEnv('HTTP_HOST'),
297 $password,
298 t3lib_div::getIndpEnv('REMOTE_ADDR'),
299 t3lib_div::getIndpEnv('REMOTE_HOST')
300 );
301
302 mail($warningEmail,
303 $subject,
304 $emailBody,
305 'From: TYPO3 Install Tool WARNING <>'
306 );
307 }
308
309 $this->basicsObj->addError(sprintf($this->basicsObj->getLabel('msg_error_login_not_sucessful_message'), md5($password)), FATAL, 'fields', 'password');
310 }
311 }
312
313 return $passwordOk;
314 }
315
316 /**
317 * [Describe function...]
318 *
319 * @return [type] ...
320 */
321 private function loginForm() {
322 $formConfig = array(
323 'type' => 'form',
324 'value' => array(
325 'options' => array(
326 'name' => 'form_setuplogin',
327 'submit' => $this->basicsObj->getLabel('label_login'),
328 ),
329 'elements' => array(
330 array(
331 'type' => 'formelement',
332 'value' => array(
333 'label' => 'label_setup_password',
334 'elementType' => 'password',
335 'options' => array(
336 'name' => 'typo3_install_password'
337 )
338 )
339 ),
340 array(
341 'type' => 'plain',
342 'value' => $this->basicsObj->getLabel('msg_login_passwordhint')
343 )
344 )
345 )
346 );
347
348 return $this->viewObj->render($formConfig);
349 }
350
351 /**
352 * gets the basics object
353 *
354 * @return tx_install_basics
355 */
356 public function getBasicsObject() {
357 return $this->basicsObj;
358 }
359
360 /**
361 * gets the view object
362 *
363 * @return tx_install_view
364 */
365 public function getViewObject() {
366 return $this->viewObj;
367 }
368
369 /**
370 * gets the language object
371 *
372 * @return language
373 */
374 public function getLanguageObject() {
375 return $this->langObj;
376 }
377
378 /**
379 * gets LOCAL_LANG
380 *
381 * @return array arrayof language labels
382 */
383 public function getLocalLang() {
384 return $this->LOCAL_LANG;
385 }
386
387 /**
388 * Enter description here...
389 *
390 * TODO add check to $value
391 *
392 * @param array $value
393 */
394 public function setLocalLang($value) {
395 $this->LOCAL_LANG = $value;
396 }
397
398 /**
399 * Enter description here...
400 *
401 * @return array
402 */
403 public function getLoadedModules() {
404 return $this->loadedModules;
405 }
406
407 /**
408 * adds a module to the loaded modules
409 *
410 * TODO add checks
411 *
412 * @param string moduel name
413 * @param object module object
414 */
415 public function addLoadedModule($moduleName, $moduleObject) {
416 $this->loadedModules[$moduleName] = $moduleObject;
417 }
418
419 /**
420 * gets the environment
421 *
422 * @return array
423 */
424 public function getEnvironment() {
425 return $this->env;
426 }
427
428 /**
429 * Enter description here...
430 *
431 * @return boolean
432 */
433 public function updateLocalconfAllowed() {
434 return $this->allowUpdateLocalConf;
435 }
436
437 /**
438 * Enter description here...
439 *
440 * TODO add checks
441 *
442 * @param boolean $value
443 */
444 public function setAllowUpdateLocalConf($value) {
445 $this->allowUpdateLocalConf = $value;
446 }
447
448 /**
449 * gets the backpath
450 *
451 * @return string backpath
452 */
453 public function getBackPath() {
454 return $this->backPath;
455 }
456
457 public function getLanguage() {
458 return $this->language;
459 }
460
461 public function getLabelIndex() {
462 return $_SESSION['installer']['labelIndex'];
463 }
464
465 public function getLabelIndexItem($index) {
466 return $_SESSION['installer']['labelIndex'][$index];
467 }
468
469 public function addLabelIndex($mainCat, $subCat, $deliverableType, $deliverable, $index) {
470 $_SESSION['installer']['labelIndex'][$index] = array (
471 'mainCat' => $mainCat,
472 'subCat' => $subCat,
473 'deliverableType' => $deliverableType,
474 'deliverable' => $deliverable
475 );
476 }
477
478 public function getFilterResults() {
479 $this->filterResults = $_SESSION['installer']['filterResults'];
480 return $this->filterResults;
481 }
482
483 public function setFilterResults($filterResults) {
484 $this->filterResults = $filterResults;
485 $_SESSION['installer']['filterResults'] = $this->filterResults;
486 }
487
488 public function __set($property, $value) {
489 // needed for comaptibility with /typo3/init.php
490 // TODO fix init.php and remove this method
491 switch($property) {
492 case 'allowUpdateLocalConf':
493 $this->allowUpdateLocalConf = (bool) $value;
494 break;
495 // case 'backPath':
496 // $this->backPath = (string) $value;
497 // break;
498 default:
499 // nothing
500 }
501 }
502 }
503
504 if(defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/install/mod/class.tx_install.php']) {
505 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/install/mod/class.tx_install.php']);
506 }
507
508 ?>