03fc62d2d1ded93df36629ad376f5929eccf137a
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / class.tx_dbal_autoloader.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Xavier Perseguers <typo3@perseguers.ch>
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 /**
29 * Autoloader included from Install Tool that lets DBAL load itself
30 * if it makes sense.
31 *
32 * $Id$
33 *
34 * @author Xavier Perseguers <typo3@perseguers.ch>
35 *
36 * @package TYPO3
37 * @subpackage dbal
38 */
39 class tx_dbal_autoloader {
40
41 /**
42 * Activates DBAL if it is supported.
43 *
44 * @param tx_install $instObj
45 * @return void
46 */
47 public function execute(tx_install $instObj) {
48 if ($instObj->mode == '123') {
49 switch ($instObj->step) {
50 case 0:
51 if (!t3lib_extMgm::isLoaded('dbal') && $this->isDbalSupported()) {
52 $this->activateDbal();
53
54 // Reload page to have Install Tool actually load DBAL
55 $redirectUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
56 t3lib_utility_Http::redirect($redirectUrl);
57 }
58 break;
59 case 1:
60 case 2:
61 if (!t3lib_extMgm::isLoaded('dbal') && $this->isDbalSupported()) {
62 $this->activateDbal();
63 }
64 break;
65 case 3:
66 $driver = $instObj->INSTALL['localconf.php']['typo_db_driver'];
67 if ($driver === 'mysql') {
68 $this->deactivateDbal();
69 }
70 break;
71 }
72 }
73 }
74
75 /**
76 * Returns TRUE if PHP modules to run DBAL are loaded.
77 *
78 * @return boolean
79 */
80 protected function isDbalSupported() {
81 return extension_loaded('odbc')
82 || extension_loaded('pdo')
83 || extension_loaded('oci8');
84 }
85
86 /**
87 * Activates DBAL.
88 *
89 * @return void
90 */
91 protected function activateDbal() {
92 $extList = t3lib_div::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']);
93 if (!t3lib_div::inArray($extList, 'adodb')) {
94 $extList[] = 'adodb';
95 }
96 if (!t3lib_div::inArray($extList, 'dbal')) {
97 $extList[] = 'dbal';
98 }
99 $this->updateExtensionList(implode(',', $extList));
100 }
101
102 /**
103 * Dectivates DBAL.
104 *
105 * @return void
106 */
107 protected function deactivateDbal() {
108 $extList = t3lib_div::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']);
109 $extList = array_flip($extList);
110
111 // Remove sysext dbal and adodb
112 if (isset($extList['dbal'])) {
113 unset($extList['dbal']);
114 }
115 if (isset($extList['adodb'])) {
116 unset($extList['adodb']);
117 }
118 $extList = array_flip($extList);
119
120 $this->updateExtensionList(implode(',', $extList));
121 }
122
123 /**
124 * Updates the list of extensions.
125 *
126 * @param string $newExtList
127 * @return void
128 */
129 protected function updateExtensionList($newExtList) {
130 // Instance of install tool
131 $instObj = t3lib_div::makeInstance('t3lib_install');
132 $instObj->allowUpdateLocalConf = 1;
133 $instObj->updateIdentity = 'TYPO3 Core Update Manager';
134
135 try {
136 // Get lines from localconf file
137 $lines = $instObj->writeToLocalconf_control();
138 $instObj->setValueInLocalconfFile($lines, '$TYPO3_CONF_VARS[\'EXT\'][\'extList\']', $newExtList);
139 $result = $instObj->writeToLocalconf_control($lines);
140 if ($result === 'nochange') {
141 $message = 'DBAL was not loaded.';
142 if (!@is_writable(PATH_typo3conf)) {
143 $message .= ' ' . PATH_typo3conf . ' is not writable!';
144 }
145 throw new Exception($message);
146 }
147
148 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = $newExtList;
149 // Make sure to get cache file for backend, not frontend
150 $cacheFilePrefix = $GLOBALS['TYPO3_LOADED_EXT']['_CACHEFILE'];
151 $GLOBALS['TYPO3_LOADED_EXT']['_CACHEFILE'] = str_replace('temp_CACHED_FE', 'temp_CACHED', $cacheFilePrefix);
152 t3lib_extMgm::removeCacheFiles();
153 } catch (Exception $e) {
154 $header = 'Error';
155 $message = $e->getMessage();
156 t3lib_timeTrack::debug_typo3PrintError($header, $message, FALSE, t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
157 exit;
158 }
159 }
160
161 }
162
163
164 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.tx_dbal_autoloader.php']) {
165 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.tx_dbal_autoloader.php']);
166 }
167
168 // Make instance:
169 $SOBE = t3lib_div::makeInstance('tx_dbal_autoloader');
170 $SOBE->execute($this);
171 ?>