[BUGFIX] Mirror selection is not respected
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / settings / class.tx_em_settings.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Steffen Kamper (info@sk-typo3.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 * Module: Extension manager, developer module
29 *
30 * @author Steffen Kamper <info@sk-typo3.de>
31 */
32
33
34 $GLOBALS['LANG']->includeLLFile(t3lib_extMgm::extPath('em', 'language/locallang.xml'));
35
36
37 class tx_em_Settings implements t3lib_Singleton {
38
39 public $MOD_MENU = array(); // Module menu items
40
41
42 /**
43 * Settings array
44 *
45 * @var array
46 */
47 protected $settings;
48
49
50 /**
51 * Constructor
52 *
53 */
54 public function __construct() {
55 $this->readSettings();
56 }
57
58 /**
59 * Get settings
60 */
61 public function getSettings() {
62 return $this->settings;
63 }
64
65 /**
66 * Get user Settings
67 */
68 public function getUserSettings() {
69 $userSettings = t3lib_beFunc::getModTSconfig(0, 'mod.tools_em');
70 return $userSettings['properties'];
71 }
72
73 /**
74 * Save user settings
75 *
76 * @param array $settings
77 */
78 public function saveSettings($settings) {
79 $this->settings = t3lib_BEfunc::getModuleData($this->MOD_MENU, $settings, 'tools_em');
80 return $this->settings;
81 }
82
83 /**
84 * Save single value in session settings
85 *
86 * @param string $name
87 * @param string $value
88 */
89 public function saveSetting($name, $value) {
90 t3lib_BEfunc::getModuleData($this->MOD_MENU, array($name => $value), 'tools_em');
91 }
92
93 /**
94 * Initial settings for extension manager module data
95 *
96 * @return void
97 */
98 protected function readSettings() {
99 $globalSettings = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['em']);
100 if (!is_array($globalSettings)) {
101 $globalSettings = array(
102 'displayMyExtensions' => 0,
103 'selectedLanguages' => array(),
104 'inlineToWindow' => 1,
105 );
106 }
107 $this->MOD_MENU = array(
108 'function' => array(
109 'loaded_list' => $GLOBALS['LANG']->getLL('menu_loaded_extensions'),
110 'installed_list' => $GLOBALS['LANG']->getLL('menu_install_extensions'),
111 'import' => $GLOBALS['LANG']->getLL('menu_import_extensions'),
112 'translations' => $GLOBALS['LANG']->getLL('menu_translation_handling'),
113 'settings' => $GLOBALS['LANG']->getLL('menu_settings'),
114 'extensionmanager' => $GLOBALS['LANG']->getLL('header'),
115 'updates' => $GLOBALS['LANG']->getLL('menu_extension_updates'),
116 ),
117 'listOrder' => array(
118 'cat' => $GLOBALS['LANG']->getLL('list_order_category'),
119 'author_company' => $GLOBALS['LANG']->getLL('list_order_author'),
120 'state' => $GLOBALS['LANG']->getLL('list_order_state'),
121 'type' => $GLOBALS['LANG']->getLL('list_order_type'),
122 ),
123 'display_details' => array(
124 1 => $GLOBALS['LANG']->getLL('show_details'),
125 0 => $GLOBALS['LANG']->getLL('show_description'),
126 2 => $GLOBALS['LANG']->getLL('show_more_details'),
127
128 3 => $GLOBALS['LANG']->getLL('show_technical'),
129 4 => $GLOBALS['LANG']->getLL('show_validating'),
130 5 => $GLOBALS['LANG']->getLL('show_changed'),
131 ),
132 'display_shy' => '',
133 'display_own' => '',
134 'display_obsolete' => '',
135 'display_installed' => '',
136 'display_files' => '',
137 'hide_shy' => 0,
138 'hide_obsolete' => 0,
139
140
141 'singleDetails' => array(
142 'info' => $GLOBALS['LANG']->getLL('details_info'),
143 'edit' => $GLOBALS['LANG']->getLL('details_edit'),
144 'backup' => $GLOBALS['LANG']->getLL('details_backup_delete'),
145 'dump' => $GLOBALS['LANG']->getLL('details_dump_db'),
146 'upload' => $GLOBALS['LANG']->getLL('details_upload'),
147 'updateModule' => $GLOBALS['LANG']->getLL('details_update'),
148 ),
149 'fe_u' => '',
150 'fe_p' => '',
151
152 'mirrorListURL' => '',
153 'rep_url' => '',
154 'extMirrors' => '',
155
156 // returns uid of currently selected repository
157 // default and hardcoded: 1 = TYPO3.org
158 'selectedRepository' => '1',
159 'selectedMirror' => '0',
160
161 'selectedLanguages' => '',
162
163 'mainTab' => '0',
164 );
165 $this->settings = t3lib_BEfunc::getModuleData($this->MOD_MENU, t3lib_div::_GP('SET'), 'tools_em');
166 $this->settings = array_merge($this->settings, $globalSettings);
167 }
168
169 /**
170 * Gets url for mirror
171 *
172 * @return string
173 */
174 public function getMirrorURL() {
175 if (strlen($this->settings['rep_url'])) {
176 return $this->settings['rep_url'];
177 }
178
179 $mirrors = unserialize($this->settings['extMirrors']);
180
181 if(!is_array($mirrors)) {
182 if ($this->settings['selectedRepository'] < 1) {
183 $this->settings['selectedRepository'] = 1;
184 }
185 }
186 /** @var $repository tx_em_Repository */
187 $repository = t3lib_div::makeInstance('tx_em_Repository', $this->settings['selectedRepository']);
188 if ($repository->getMirrorListUrl()) {
189 $objRepositoryUtility = t3lib_div::makeInstance('tx_em_Repository_Utility', $repository);
190 $mirrors = $objRepositoryUtility->getMirrors(TRUE)->getMirrors();
191 if(!is_array($mirrors)) {
192 return FALSE;
193 } else {
194 $this->settings['extMirrors'] = serialize($mirrors);
195 $this->saveSetting('extMirrors', $this->settings['extMirrors']);
196 }
197 }
198 if (!$this->settings['selectedMirror']) {
199 $rand = array_rand($mirrors);
200 $url = 'http://' . $mirrors[$rand]['host'] . $mirrors[$rand]['path'];
201 }
202 else {
203 $selectedMirror = NULL;
204 foreach ($mirrors as $mirror) {
205 if ($mirror['host'] === $this->settings['selectedMirror']) {
206 $selectedMirror = $mirror;
207 break;
208 }
209 }
210 $url = 'http://' . $selectedMirror['host'] . $selectedMirror['path'];
211 }
212
213 return $url;
214 }
215
216 /**
217 * Method returns registered extension repositories.
218 *
219 * Registered repositories are global (per installation) settings.
220 *
221 * @access public
222 * @return array of {@link em_repository em_repository} instances
223 * @see registerDefaultRepository(), setRegisteredRepositories()
224 */
225 public function getRegisteredRepositories() {
226 $registry = t3lib_div::makeInstance('t3lib_Registry');
227 $regRepos = $registry->get('core', 'em.repositories.registered');
228
229 // create default entry if there wasn't one
230 if (empty($regRepos)) {
231 $this->registerDefaultRepository();
232 $regRepos = $registry->get('core', 'em.repositories.registered');
233 }
234
235 return $regRepos;
236 }
237
238 /**
239 * Method creates default registered repositories entry.
240 *
241 * @access protected
242 * @return void
243 * @see getRegisteredRepository(), setRegisteredRepositories()
244 */
245 protected function registerDefaultRepository() {
246 $registry = t3lib_div::makeInstance('t3lib_Registry');
247 $defaultRepo = t3lib_div::makeInstance('tx_em_Repository');
248 $registry->set('core', 'em.repositories.registered', array($defaultRepo));
249 }
250
251 /**
252 * Method sets (persists) registered repositories.
253 *
254 * Registered repositories are global (per installation) settings.
255 *
256 * @access public
257 * @param array $repositories array of {@link em_repository em_repository} instances
258 * @see registerDefaultRepository(), setRegisteredRepositories()
259 * @throws InvalidArgumentException in case argument contains no instances of {@link em_repository em_repository}
260 */
261 public function setRegisteredRepositories(array $repositories) {
262 // removing mirror instances
263 foreach ($repositories as $repository) {
264 if ($repository instanceof em_repository) {
265 $repository->removeMirrors();
266 } else {
267 throw new InvalidArgumentException(get_class($this) . ': ' . 'No valid instances of em_repository given.');
268 }
269 }
270 if (count($repositories)) {
271 $registry = t3lib_div::makeInstance('t3lib_Registry');
272 $registry->set('core', 'em.repositories.registered', $repositories);
273 }
274 }
275
276 /**
277 * Method returns currently selected repository
278 *
279 * Selected repository is local (per user) settings.
280 *
281 * @access public
282 * @return em_repository repository instance that is currently selected by a BE user
283 * @see setSelectedRepository()
284 */
285 public function getSelectedRepository() {
286 return t3lib_div::makeInstance('tx_em_Repository', $this->settings['selectedRepository']);
287 }
288
289 /**
290 * Method sets currently selected repository.
291 *
292 * Selected repository is local (per user) settings.
293 *
294 * @todo STUB, implementation missing
295 * @access public
296 * @param em_repository $repository repository instance that is currently selected by a BE user
297 * @see getSelectedRepository()
298 */
299 public function setSelectedRepository(em_repository $repository) {
300 // this method would set sth. like "REPOSITORY_TITLE:INT" in a setting field
301 // REPOSITORY_TITLE = example: main
302 // INT = 0 means randomly selected mirror, >0 selects specific mirror
303 }
304
305 }
306
307 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/settings/class.tx_em_settings.php'])) {
308 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/repository/class.tx_em_settings.php']);
309 }
310
311 ?>