Fixed bug #16528: EM: remove dependency to $TBE_TEMPLATE
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / connection / class.tx_em_connection_extdirectserver.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 * This class handles all Ajax calls coming from ExtJS
31 *
32 * $Id: class.tx_em_Connection_ExtDirectServer.php 2083 2010-03-22 00:48:31Z steffenk $
33 *
34 * @author Steffen Kamper <info@sk-typo3.de>
35 */
36
37
38 class tx_em_Connection_ExtDirectServer {
39 /**
40 * @var tx_em_Tools_XmlHandler
41 */
42 var $xmlhandler;
43
44 /**
45 * Class for printing extension lists
46 *
47 * @var tx_em_Extensions_List
48 */
49 public $extensionList;
50
51 /**
52 * Class for extension details
53 *
54 * @var tx_em_Extensions_Details
55 */
56 public $extensionDetails;
57
58 /**
59 * Keeps instance of settings class.
60 *
61 * @var tx_em_Settings
62 */
63 static protected $objSettings;
64
65
66 /*********************************************************************/
67 /* General */
68 /*********************************************************************/
69
70 /**
71 * Constructor
72 *
73 * @return void
74 */
75 public function __construct() {
76
77 }
78
79
80 /**
81 * Method returns instance of settings class.
82 *
83 * @access protected
84 * @return em_settings instance of settings class
85 */
86 protected function getSettingsObject() {
87 if (!is_object(self::$objSettings) && !(self::$objSettings instanceof tx_em_Settings)) {
88 self::$objSettings = t3lib_div::makeInstance('tx_em_Settings');
89 }
90 return self::$objSettings;
91 }
92
93
94 /*********************************************************************/
95 /* Local Extension List */
96 /*********************************************************************/
97
98
99 /**
100 * Render local extension list
101 *
102 * @return string $content
103 */
104 public function getExtensionList() {
105 $list = t3lib_div::makeInstance('tx_em_Extensions_List');
106 $extList = $list->getInstalledExtensions(TRUE);
107
108
109 return array(
110 'length' => count($extList),
111 'data' => $extList
112 );
113
114 }
115
116 /**
117 * Enter description here...
118 *
119 * @return unknown
120 */
121 public function getInstalledExtkeys() {
122 $list = t3lib_div::makeInstance('tx_em_Extensions_List');
123 $extList = $list->getInstalledExtensions(TRUE);
124
125
126 $temp = $this->getSettings();
127 $selectedLanguage = unserialize($temp['selectedLanguages']);
128
129
130 $keys = array();
131 $i = 0;
132 foreach ($extList as $ext) {
133 if ($ext['installed']) {
134 $keys[$i] = array(
135 'extkey' => $ext['extkey'],
136 'icon' => $ext['icon'],
137 'stype' => $ext['typeShort'],
138 );
139 foreach ($selectedLanguage as $language) {
140 $keys[$i]['lang'][] = $GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xml:lang_' . $language);
141 }
142 $i++;
143 }
144 }
145
146 return array(
147 'length' => count($keys),
148 'data' => $keys,
149 );
150 }
151
152 /**
153 * Render module content
154 *
155 * @return string $content
156 */
157 public function getExtensionDetails() {
158 $list = t3lib_div::makeInstance('tx_em_Extensions_List');
159 $extList = $list->getInstalledExtensions(TRUE);
160
161
162 return array(
163 'length' => count($extList),
164 'data' => $extList
165 );
166
167 }
168
169 /**
170 * Render extension update
171 *
172 * @var string $extKey
173 * @return string $content
174 */
175 public function getExtensionUpdate($extKey) {
176 if (isset($GLOBALS['TYPO3_LOADED_EXT'][$extKey])) {
177 $path = t3lib_extMgm::extPath($extKey);
178 $ext = array();
179
180 $install = t3lib_div::makeInstance('tx_em_Install');
181 $extension = t3lib_div::makeInstance('tx_em_Extensions_List');
182 $extension->singleExtInfo($extKey, $path, $ext);
183 $ext = $ext[0];
184 $update = $install->checkDBupdates($extKey, $ext);
185 return $update ? $update : 'Extension is up to date.';
186 } else {
187 return 'Extension "' . htmlspecialchars($extKey) . '" is not installed.';
188 }
189 }
190
191
192 /**
193 * Render extension configuration
194 *
195 * @var string $extKey
196 * @return string $content
197 */
198 public function getExtensionConfiguration($extKey) {
199 $extensionList = t3lib_div::makeInstance('tx_em_Extensions_List', $this);
200 list($list,) = $extensionList->getInstalledExtensions();
201 $install = t3lib_div::makeInstance('tx_em_Install');
202 $form = $install->updatesForm($extKey, $list[$extKey], 1);
203 if (!$form) {
204 return '<p>' . 'This extension has no configuration.' . '</p>';
205 } else {
206 return $form;
207 }
208 }
209
210 /**
211 * Save extension configuration
212 *
213 * @formHandler
214 * @param array $parameter
215 * @return array
216 */
217 public function saveExtensionConfiguration($parameter) {
218 return array(
219 'success' => true,
220 'data' => $parameter
221 );
222 }
223
224 /**
225 * genereates a file tree
226 *
227 * @param object $parameter
228 * @return array
229 */
230 public function getExtFileTree($parameter) {
231 $ext = array();
232 $extKey = $parameter->extkey;
233 $type = $parameter->typeShort;
234 $node = strpos($parameter->node, '/') !== FALSE ? $parameter->node : $parameter->baseNode;
235
236 $path = PATH_site . $node;
237 $fileArray = array();
238
239 $dirs = t3lib_div::get_dirs($path);
240 $files = t3lib_div::getFilesInDir($path, '', FALSE, '', '');
241
242 $editTypes = explode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext']);
243 $imageTypes = array('gif', 'jpg', 'png');
244
245 if (!is_array($files) || !count($files)) {
246 return array();
247 }
248
249 foreach ($dirs as $dir) {
250 if ($dir{0} !== '.') {
251 $fileArray[] = array(
252 'id' => $node . '/' . $dir,
253 'text' => htmlspecialchars($dir),
254 'leaf' => false,
255 'qtip' => ''
256 );
257 }
258 }
259
260
261 foreach ($files as $key => $file) {
262 $fileExt = strtolower(substr($file, strrpos($file, '.') + 1));
263 if (in_array($fileExt, $imageTypes)) {
264 $cls = 'tree-image';
265 } elseif (in_array($fileExt, $editTypes)) {
266 $cls = 'tree-edit';
267 } else {
268 $cls = 'tree-unknown';
269 }
270 $fileArray[] = array(
271 'id' => $node . '/' . $file,
272 'text' => htmlspecialchars($file),
273 'leaf' => true,
274 'qtip' => $fileExt . ' - file',
275 'iconCls' => $cls
276 );
277 }
278 return $fileArray;
279 }
280
281 /**
282 * Read extension file and send content
283 *
284 * @param string $path
285 * @return string file content
286 */
287 public function readExtFile($path) {
288 $path = PATH_site . $path;
289 if (@file_exists($path)) {
290 //TODO: charset conversion
291 return t3lib_div::getURL($path);
292 }
293 return '';
294 }
295
296 /**
297 * Save extension file
298 *
299 * @param string $file
300 * @param string $content
301 * @return boolean success
302 */
303 public function saveExtFile($file, $content) {
304 $path = PATH_site . $path;
305 if (@file_exists($path)) {
306 //TODO: save only if saving was enabled
307 return t3lib_div::writeFile($path, $content);
308 }
309 return FALSE;
310 }
311
312
313 /**
314 * Load upload form for extension upload to TER
315 *
316 * @formcHandler
317 * @return array
318 */
319 public function loadUploadExtToTer() {
320 $settings = $this->getSettings();
321 return array(
322 'success' => TRUE,
323 'data' => array(
324 'fe_u' => $settings['fe_u'],
325 'fe_p' => $settings['fe_p']
326 )
327 );
328 }
329
330 /**
331 * Upload extension to TER
332 *
333 * @formHandler
334 *
335 * @param string $parameter
336 * @return array
337 */
338 public function uploadExtToTer($parameter) {
339 return array(
340 'success' => TRUE,
341 'fe_p' => $parameter['fe_p'],
342 'fe_u' => $parameter['fe_u'],
343 'newversion' => $parameter['newversion'],
344 'uploadcomment' => $parameter['uploadcomment']
345 );
346 }
347
348 /**
349 * Prints developer information
350 *
351 * @param string $parameter
352 * @return array
353 */
354 public function getExtensionDevelopInfo($extKey) {
355 $extensionList = t3lib_div::makeInstance('tx_em_Extensions_List', $this);
356 list($list,) = $extensionList->getInstalledExtensions();
357 $extensionDetails = t3lib_div::makeInstance('tx_em_Extensions_Details', $this);
358 return $extensionDetails->extInformationarray($extKey, $list[$extKey]);
359 }
360
361 /*********************************************************************/
362 /* Remote Extension List */
363 /*********************************************************************/
364
365
366 /**
367 * Render remote extension list
368 *
369 * @param object $parameters
370 * @return string $content
371 */
372 public function getRemoteExtensionList($parameters) {
373 $repositoryId = $this->getSettingsObject()->getSelectedRepository();
374
375 $search = $parameters->query;
376 $limit = $parameters->start . ', ' . $parameters->limit;
377 $orderBy = $parameters->sort;
378 $orderDir = $parameters->dir;
379
380 if ($search == '*') {
381 $where = '';
382 } else {
383 $quotedSearch = $GLOBALS['TYPO3_DB']->escapeStrForLike(
384 $GLOBALS['TYPO3_DB']->quoteStr($search, 'cache_extensions'),
385 'cache_extensions'
386 );
387 $where = ' AND (extkey LIKE \'%' . $quotedSearch . '%\' OR title LIKE \'%' . $quotedSearch . '%\')';
388 }
389
390 $list = tx_em_Database::getExtensionListFromRepository(
391 $repositoryId,
392 $where,
393 $orderBy,
394 $orderDir,
395 $limit
396 );
397
398
399 return array(
400 'length' => $list['count'],
401 'data' => $list['results']
402 );
403
404 }
405
406
407 /**
408 * Loads repositories
409 *
410 * @return array
411 */
412 public function repositoryUpdateLoad() {
413 $settings = $this->getSettings();
414 $repositories = tx_em_Database::getRepositories();
415 foreach ($repositories as $uid => $repository) {
416 $data[] = array(
417 'title' => $repository['title'],
418 'value' => $repository['uid'],
419 'description' => $repository['description'],
420 'wsdl_url' => $repository['wsdl_url'],
421 'mirror_url' => $repository['mirror_url'],
422 'count' => $repository['extCount'],
423 'updated' => $repository['lastUpdated'] ? date('d/m/Y H:i', $repository['lastUpdated']) : 'never',
424 'selected' => $repository['uid'] === $settings['selectedRepository'],
425 );
426 }
427
428 return array(
429 'length' => count($data),
430 'data' => $data,
431 );
432 }
433
434 /**
435 * Edit / Create repository
436 *
437 * @formHandler
438 * @param array $parameter
439 * @return array
440 */
441 public function repositoryEditFormSubmit($parameter) {
442 return array(
443 'success' => TRUE,
444 'data' => array(
445 'params' => $parameter
446 ),
447 );
448 }
449
450 /**
451 * Update repository
452 *
453 * @formHandler
454 *
455 * @param array $parameter
456 * @return array
457 */
458 public function repositoryUpdate($parameter) {
459 //debug($parameter);
460 if (!intval($parameter['rep'])) {
461 return array(
462 'success' => FALSE,
463 'errors' => 'no repository choosen'
464 );
465 }
466
467 $objRepository = t3lib_div::makeInstance('tx_em_Repository', intval($parameter['rep']));
468 $objRepositoryUtility = t3lib_div::makeInstance('tx_em_Repository_Utility', $objRepository);
469 $count = $objRepositoryUtility->updateExtList();
470
471
472 if ($count) {
473 $objRepository->setExtensionCount($count);
474 $objRepository->setLastUpdate(time());
475 tx_em_Database::updateRepository($objRepository);
476 return array(
477 'success' => TRUE,
478 'data' => array(
479 'count' => $count
480 )
481 );
482 } else {
483 return array(
484 'success' => FALSE,
485 'errormsg' => 'Your repository is up to date.'
486 );
487 }
488 }
489
490
491 /*********************************************************************/
492 /* Translation Handling */
493 /*********************************************************************/
494
495
496 /**
497 * Enter description here...
498 *
499 * @return unknown
500 */
501 public function getLanguages() {
502
503 $temp = $this->getSettings();
504 $selected = unserialize($temp['selectedLanguages']);
505 $theLanguages = t3lib_div::trimExplode('|', TYPO3_languages);
506 //drop default
507 array_shift($theLanguages);
508 $lang = array();
509 foreach ($theLanguages as $language) {
510 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xml:lang_' . $language));
511 $flag = @is_file(PATH_typo3 . 'gfx/flags/' . $language . '.gif') ? 'gfx/flags/' . $language . '.gif' : 'gfx/flags/unknown.gif';
512 $lang[] = array(
513 'label' => $label,
514 'lang' => $language,
515 'flag' => $flag,
516 'selected' => in_array($language, $selected) ? 1 : 0
517 );
518 }
519 return array(
520 'length' => count($lang),
521 'data' => $lang,
522 );
523
524 }
525
526 /**
527 * Saves language selection
528 *
529 * @param array $parameter
530 * @return string
531 */
532 public function saveLanguageSelection($parameter) {
533 $this->saveSetting('selectedLanguages', serialize($parameter));
534 //TODO: use language label
535 return 'Saved languages: ' . implode(', ', $parameter);
536 }
537
538
539 /**
540 * Fetches translation from server
541 *
542 * @param string $extkey
543 * @param string $type
544 * @param array $selection
545 * @return array
546 */
547 public function fetchTranslations($extkey, $type, $selection) {
548 $result = array();
549 if (is_array($selection) && count($selection)) {
550 $terConnection = t3lib_div::makeInstance('tx_em_Connection_Ter', $this);
551 $this->xmlhandler = t3lib_div::makeInstance('tx_em_Tools_XmlHandler');
552 $this->xmlhandler->emObj = $this;
553 $mirrorURL = $this->getSettingsObject()->getMirrorURL();
554
555 foreach ($selection as $lang) {
556 $fetch = $terConnection->fetchTranslationStatus($extkey, $mirrorURL);
557 $localmd5 = '';
558 if (!isset($fetch[$lang])) {
559 //no translation available
560 $result[0][$lang] = 'N/A';
561 } else {
562 $localmd5 = '';
563 if (is_file(PATH_site . 'typo3temp/' . $extkey . '-l10n-' . $lang . '.zip')) {
564 $localmd5 = md5_file(PATH_site . 'typo3temp/' . $extkey . '-l10n-' . $lang . '.zip');
565 }
566 if ($localmd5 !== $fetch[$lang]['md5']) {
567 if ($type) {
568 //fetch translation
569 $ret = $terConnection->updateTranslation($extkey, $lang, $mirrorURL);
570 $result[0][$lang] = $ret ? 'updated' : 'failed';
571 } else {
572 //translation status
573 $result[0][$lang] = $localmd5 !== '' ? 'update' : 'new';
574 }
575 } else {
576 //translation is up to date
577 $result[0][$lang] = 'ok';
578 }
579 }
580
581
582 }
583 }
584 return $result;
585 }
586
587
588 /*********************************************************************/
589 /* Settings */
590 /*********************************************************************/
591
592 /**
593 * Returns settings object.
594 *
595 * @access public
596 * @return tx_em_Settings instance of settings object
597 */
598 public function getSettings() {
599 return $this->getSettingsObject()->getSettings();
600 }
601
602 /**
603 * Enter description here...
604 *
605 * @param string $name
606 * @param mixed $value
607 * @return boolean
608 */
609 public function saveSetting($name, $value) {
610 $this->getSettingsObject()->saveSetting($name, $value);
611 return TRUE;
612 }
613
614 /**
615 * Load form values for settings form
616 *
617 * @return array FormValues
618 */
619 public function settingsFormLoad() {
620 $settings = $this->getSettings();
621
622 return array(
623 'success' => TRUE,
624 'data' => array(
625 'display_unchecked' => $settings['display_unchecked'],
626 'fe_u' => $settings['fe_u'],
627 'fe_p' => $settings['fe_p'],
628 'selectedMirror' => $settings['selectedMirror'],
629 'selectedRepository' => $settings['selectedRepository'],
630 )
631 );
632 }
633
634 /**
635 * Save settings from form submit
636 *
637 * @formHandler
638 * @param array $parameter
639 * @return array
640 */
641 public function settingsFormSubmit($parameter) {
642 $settings = $this->getSettingsObject()->saveSettings(array(
643 'display_unchecked' => isset($parameter['display_unchecked']),
644 'fe_u' => $parameter['fe_u'],
645 'fe_p' => $parameter['fe_p'],
646 'selectedMirror' => $parameter['selectedMirror'],
647 'selectedRepository' => $parameter['selectedRepository'],
648 ));
649 return array(
650 'success' => TRUE,
651 'data' => $parameter,
652 'settings' => $settings
653 );
654 }
655
656
657 /*********************************************************************/
658 /* EM Tools */
659 /*********************************************************************/
660
661 /**
662 * Upload an extension
663 *
664 * @formHandler
665 *
666 * @access public
667 * @param $parameter composed parameter from $POST and $_FILES
668 * @return array status
669 */
670 public function uploadExtension($parameter) {
671 $uploadedTempFile = isset($parameter['extfile']) ? $parameter['extfile'] : t3lib_div::upload_to_tempfile($parameter['extupload-path']['tmp_name']);
672 $location = ($parameter['loc'] === 'G' || $parameter['loc'] === 'S') ? $parameter['loc'] : 'L';
673 $uploadOverwrite = $parameter['uploadOverwrite'] ? TRUE : FALSE;
674
675 $install = t3lib_div::makeInstance('tx_em_Install', $this);
676 $this->extensionList = t3lib_div::makeInstance('tx_em_Extensions_List', $this);
677 $this->extensionDetails = t3lib_div::makeInstance('tx_em_Extensions_Details', $this);
678
679 $upload = $install->uploadExtensionFile($uploadedTempFile, $location, $uploadOverwrite);
680
681 if ($upload[0] === FALSE) {
682 return array(
683 'success' => FALSE,
684 'error' => $upload[1]
685 );
686 }
687
688 $extKey = $upload[1][0]['extKey'];
689 $version = '';
690 $dontDelete = TRUE;
691 $result = $install->installExtension($upload[1], $location, $version, $uploadedTempFile, $dontDelete);
692 return array(
693 'success' => TRUE,
694 'data' => $result,
695 'extKey' => $extKey
696 );
697
698 }
699
700 /**
701 * Enables an extension
702 *
703 * @param $extensionKey
704 * @return void
705 */
706 public function enableExtension($extensionKey) {
707 $this->extensionList = t3lib_div::makeInstance('tx_em_Extensions_List', $this);
708 $install = t3lib_div::makeInstance('tx_em_Install', $this);
709
710 list($installedList,) = $this->extensionList->getInstalledExtensions();
711 $newExtensionList = $this->extensionList->addExtToList($extensionKey, $installedList);
712
713 $install->writeNewExtensionList($newExtensionList);
714 tx_em_Tools::refreshGlobalExtList();
715 $install->forceDBupdates($extensionKey, $newExtensionList[$extensionKey]);
716 }
717 }
718
719 ?>