0feed8e54f4fd58f4cc07433a5e0068014b8d215
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / tools / class.tx_em_tools.php
1 <?php
2 /* **************************************************************
3 * Copyright notice
4 *
5 * (c) webservices.nl
6 * (c) 2006-2010 Karsten Dambekalns <karsten@typo3.org>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * class.tx_em_tools.php
30 *
31 * $Id: class.tx_em_tools.php 2084 2010-03-22 01:46:37Z steffenk $
32 */
33
34 /**
35 * Static tools for extension manager
36 * Some of them should be moved later to t3lib static libraries
37 *
38 */
39 final class tx_em_Tools {
40
41 /**
42 * Keeps default categories.
43 *
44 * @var array
45 */
46 protected static $defaultCategories = array(
47 'be' => 0,
48 'module' => 1,
49 'fe' => 2,
50 'plugin' => 3,
51 'misc' => 4,
52 'services' => 5,
53 'templates' => 6,
54 'doc' => 8,
55 'example' => 9,
56 );
57 /**
58 * Keeps default states.
59 *
60 * @var array
61 */
62 protected static $defaultStates = array(
63 'alpha' => 0,
64 'beta' => 1,
65 'stable' => 2,
66 'experimental' => 3,
67 'test' => 4,
68 'obsolete' => 5,
69 'excludeFromUpdates' => 6,
70 'n/a' => 999,
71 );
72
73 /**
74 * Colors for states
75 *
76 * @var array
77 */
78 protected static $stateColors = array(
79 'alpha' => '#d12438',
80 'beta' => '#97b17e',
81 'stable' => '#3bb65c',
82 'experimental' => '#007eba',
83 'test' => '#979797',
84 'obsolete' => '#000000',
85 'excludeFromUpdates' => '#cf7307'
86 );
87
88 /**
89 * Gets the stateColor array
90 *
91 * @static
92 * @return array
93 */
94 public static function getStateColors() {
95 return self::$stateColors;
96 }
97
98 /**
99 * Unzips a zip file in the given path.
100 *
101 * Uses unzip binary if available, otherwise a pure PHP unzip is used.
102 *
103 * @param string $file Full path to zip file
104 * @param string $path Path to change to before extracting
105 * @return boolean True on success, false in failure
106 */
107 public static function unzip($file, $path) {
108 if (strlen($GLOBALS['TYPO3_CONF_VARS']['BE']['unzip_path'])) {
109 chdir($path);
110 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['unzip_path'] . ' -o ' . escapeshellarg($file);
111 t3lib_utility_Command::exec($cmd, $list, $ret);
112 return ($ret === 0);
113 } else {
114 // we use a pure PHP unzip
115 $unzip = t3lib_div::makeInstance('tx_em_Tools_Unzip', $file);
116 $ret = $unzip->extract(array('add_path' => $path));
117 return (is_array($ret));
118 }
119 }
120
121
122 /**
123 * Refreshes the global extension list
124 *
125 * @return void
126 */
127 public static function refreshGlobalExtList() {
128 global $TYPO3_LOADED_EXT;
129
130 $TYPO3_LOADED_EXT = t3lib_extMgm::typo3_loadExtensions();
131 if ($TYPO3_LOADED_EXT['_CACHEFILE']) {
132 require(PATH_typo3conf . $TYPO3_LOADED_EXT['_CACHEFILE'] . '_ext_localconf.php');
133 }
134 return;
135
136 $GLOBALS['TYPO3_LOADED_EXT'] = t3lib_extMgm::typo3_loadExtensions();
137 if ($TYPO3_LOADED_EXT['_CACHEFILE']) {
138 require(PATH_typo3conf . $TYPO3_LOADED_EXT['_CACHEFILE'] . '_ext_localconf.php');
139 } else {
140 $temp_TYPO3_LOADED_EXT = $TYPO3_LOADED_EXT;
141 foreach ($temp_TYPO3_LOADED_EXT as $_EXTKEY => $temp_lEDat) {
142 if (is_array($temp_lEDat) && $temp_lEDat['ext_localconf.php']) {
143 $_EXTCONF = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY];
144 require($temp_lEDat['ext_localconf.php']);
145 }
146 }
147 }
148 }
149
150 /**
151 * Set category array entries for extension
152 *
153 * @param array Category index array
154 * @param array Part of list array for extension.
155 * @param string Extension key
156 * @return array Modified category index array
157 */
158 public static function setCat(&$cat, $listArrayPart, $extKey) {
159
160 // Getting extension title:
161 $extTitle = $listArrayPart['EM_CONF']['title'];
162
163 // Category index:
164 $index = $listArrayPart['EM_CONF']['category'];
165 $cat['cat'][$index][$extKey] = $extTitle;
166
167 // Author index:
168 $index = $listArrayPart['EM_CONF']['author'] . ($listArrayPart['EM_CONF']['author_company'] ? ', ' . $listArrayPart['EM_CONF']['author_company'] : '');
169 $cat['author_company'][$index][$extKey] = $extTitle;
170
171 // State index:
172 $index = $listArrayPart['EM_CONF']['state'];
173 $cat['state'][$index][$extKey] = $extTitle;
174
175 // Type index:
176 $index = $listArrayPart['type'];
177 $cat['type'][$index][$extKey] = $extTitle;
178
179 // Return categories:
180 return $cat;
181 }
182
183 /**
184 * Returns upload folder for extension
185 *
186 * @param string Extension key
187 * @return string Upload folder for extension
188 */
189 public static function uploadFolder($extKey) {
190 return 'uploads/tx_' . str_replace('_', '', $extKey) . '/';
191 }
192
193
194 /**
195 * Returns image tag for "uninstall"
196 *
197 * @return string <img> tag
198 */
199 public static function removeButton() {
200 return t3lib_iconWorks::getSpriteIcon('actions-system-extension-uninstall', array('title' => $GLOBALS['LANG']->getLL('ext_details_remove_ext')));
201 }
202
203 /**
204 * Returns image for "install"
205 *
206 * @return string <img> tag
207 */
208 public static function installButton() {
209 return t3lib_iconWorks::getSpriteIcon('actions-system-extension-install', array('title' => $GLOBALS['LANG']->getLL('helperFunction_install_extension')));
210 }
211
212 /**
213 * Warning (<img> + text string) message about the impossibility to import extensions (both local and global locations are disabled...)
214 *
215 * @return string <img> + text string.
216 */
217 public static function noImportMsg() {
218 return t3lib_iconWorks::getSpriteIcon('status-dialog-warning') .
219 '<strong>' . $GLOBALS['LANG']->getLL('helperFunction_import_not_possible') . '</strong>';
220 }
221
222
223 /**
224 * Fixes an old style ext_emconf.php array by adding constraints if needed and removing deprecated keys
225 *
226 * @param array $emConf
227 * @return array
228 */
229 public static function fixEMCONF($emConf) {
230 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['depends']) || !isset($emConf['constraints']['conflicts']) || !isset($emConf['constraints']['suggests'])) {
231 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['depends'])) {
232 $emConf['constraints']['depends'] = self::stringToDep($emConf['dependencies']);
233 if (strlen($emConf['PHP_version'])) {
234 $versionRange = self::splitVersionRange($emConf['PHP_version']);
235 if (version_compare($versionRange[0], '3.0.0', '<')) {
236 $versionRange[0] = '3.0.0';
237 }
238 if (version_compare($versionRange[1], '3.0.0', '<')) {
239 $versionRange[1] = '0.0.0';
240 }
241 $emConf['constraints']['depends']['php'] = implode('-', $versionRange);
242 }
243 if (strlen($emConf['TYPO3_version'])) {
244 $versionRange = self::splitVersionRange($emConf['TYPO3_version']);
245 if (version_compare($versionRange[0], '3.5.0', '<')) {
246 $versionRange[0] = '3.5.0';
247 }
248 if (version_compare($versionRange[1], '3.5.0', '<')) {
249 $versionRange[1] = '0.0.0';
250 }
251 $emConf['constraints']['depends']['typo3'] = implode('-', $versionRange);
252 }
253 }
254 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['conflicts'])) {
255 $emConf['constraints']['conflicts'] = self::stringToDep($emConf['conflicts']);
256 }
257 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['suggests'])) {
258 $emConf['constraints']['suggests'] = array();
259 }
260 } elseif (isset($emConf['constraints']) && isset($emConf['dependencies'])) {
261 $emConf['suggests'] = isset($emConf['suggests']) ? $emConf['suggests'] : array();
262 $emConf['dependencies'] = self::depToString($emConf['constraints']);
263 $emConf['conflicts'] = self::depToString($emConf['constraints'], 'conflicts');
264 }
265
266 // sanity check for version numbers, intentionally only checks php and typo3
267 if (isset($emConf['constraints']['depends']) && isset($emConf['constraints']['depends']['php'])) {
268 $versionRange = self::splitVersionRange($emConf['constraints']['depends']['php']);
269 if (version_compare($versionRange[0], '3.0.0', '<')) {
270 $versionRange[0] = '3.0.0';
271 }
272 if (version_compare($versionRange[1], '3.0.0', '<')) {
273 $versionRange[1] = '0.0.0';
274 }
275 $emConf['constraints']['depends']['php'] = implode('-', $versionRange);
276 }
277 if (isset($emConf['constraints']['depends']) && isset($emConf['constraints']['depends']['typo3'])) {
278 $versionRange = self::splitVersionRange($emConf['constraints']['depends']['typo3']);
279 if (version_compare($versionRange[0], '3.5.0', '<')) {
280 $versionRange[0] = '3.5.0';
281 }
282 if (version_compare($versionRange[1], '3.5.0', '<')) {
283 $versionRange[1] = '0.0.0';
284 }
285 $emConf['constraints']['depends']['typo3'] = implode('-', $versionRange);
286 }
287
288 unset($emConf['private']);
289 unset($emConf['download_password']);
290 unset($emConf['TYPO3_version']);
291 unset($emConf['PHP_version']);
292
293 return $emConf;
294 }
295
296
297 /**
298 * Returns the $EM_CONF array from an extensions ext_emconf.php file
299 *
300 * @param string Absolute path to EMCONF file.
301 * @param string Extension key.
302 * @return array EMconf array values.
303 */
304 public static function includeEMCONF($path, $_EXTKEY) {
305 $EM_CONF = NULL;
306 @include($path);
307 if (is_array($EM_CONF[$_EXTKEY])) {
308 return self::fixEMCONF($EM_CONF[$_EXTKEY]);
309 }
310 return FALSE;
311 }
312
313
314 /**
315 * Extracts the directories in the $files array
316 *
317 * @param array Array of files / directories
318 * @return array Array of directories from the input array.
319 */
320 public static function extractDirsFromFileList($files) {
321 $dirs = array();
322
323 if (is_array($files)) {
324 // Traverse files / directories array:
325 foreach ($files as $file) {
326 if (substr($file, -1) == '/') {
327 $dirs[$file] = $file;
328 } else {
329 $pI = pathinfo($file);
330 if (strcmp($pI['dirname'], '') && strcmp($pI['dirname'], '.')) {
331 $dirs[$pI['dirname'] . '/'] = $pI['dirname'] . '/';
332 }
333 }
334 }
335 }
336 return $dirs;
337 }
338
339 /**
340 * Splits a version range into an array.
341 *
342 * If a single version number is given, it is considered a minimum value.
343 * If a dash is found, the numbers left and right are considered as minimum and maximum. Empty values are allowed.
344 *
345 * @param string $ver A string with a version range.
346 * @return array
347 */
348 public static function splitVersionRange($ver) {
349 $versionRange = array();
350 if (strstr($ver, '-')) {
351 $versionRange = explode('-', $ver, 2);
352 } else {
353 $versionRange[0] = $ver;
354 $versionRange[1] = '';
355 }
356
357 if (!$versionRange[0]) {
358 $versionRange[0] = '0.0.0';
359 }
360 if (!$versionRange[1]) {
361 $versionRange[1] = '0.0.0';
362 }
363
364 return $versionRange;
365 }
366
367 /**
368 * Checks whether the passed dependency is TER2-style (array) and returns a single string for displaying the dependencies.
369 *
370 * It leaves out all version numbers and the "php" and "typo3" dependencies, as they are implicit and of no interest without the version number.
371 *
372 * @param mixed $dep Either a string or an array listing dependencies.
373 * @param string $type The dependency type to list if $dep is an array
374 * @return string A simple dependency list for display
375 */
376 public static function depToString($dep, $type = 'depends') {
377 if (is_array($dep)) {
378 unset($dep[$type]['php']);
379 unset($dep[$type]['typo3']);
380 $s = (count($dep[$type])) ? implode(',', array_keys($dep[$type])) : '';
381 return $s;
382 }
383 return '';
384 }
385
386 /**
387 * Checks whether the passed dependency is TER-style (string) or TER2-style (array) and returns a single string for displaying the dependencies.
388 *
389 * It leaves out all version numbers and the "php" and "typo3" dependencies, as they are implicit and of no interest without the version number.
390 *
391 * @param mixed $dep Either a string or an array listing dependencies.
392 * @param string $type The dependency type to list if $dep is an array
393 * @return string A simple dependency list for display
394 */
395 public static function stringToDep($dep) {
396 $constraint = array();
397 if (is_string($dep) && strlen($dep)) {
398 $dep = explode(',', $dep);
399 foreach ($dep as $v) {
400 $constraint[$v] = '';
401 }
402 }
403 return $constraint;
404 }
405
406
407 /**
408 * Returns version information
409 *
410 * @param string Version code, x.x.x
411 * @param string part: "", "int", "main", "sub", "dev"
412 * @return string
413 * @see renderVersion()
414 */
415 public static function makeVersion($v, $mode) {
416 $vDat = self::renderVersion($v);
417 return $vDat['version_' . $mode];
418 }
419
420 /**
421 * Parses the version number x.x.x and returns an array with the various parts.
422 *
423 * @param string Version code, x.x.x
424 * @param string Increase version part: "main", "sub", "dev"
425 * @return string
426 */
427 public static function renderVersion($v, $raise = '') {
428 $parts = t3lib_div::intExplode('.', $v . '..');
429 $parts[0] = t3lib_div::intInRange($parts[0], 0, 999);
430 $parts[1] = t3lib_div::intInRange($parts[1], 0, 999);
431 $parts[2] = t3lib_div::intInRange($parts[2], 0, 999);
432
433 switch ((string) $raise) {
434 case 'main':
435 $parts[0]++;
436 $parts[1] = 0;
437 $parts[2] = 0;
438 break;
439 case 'sub':
440 $parts[1]++;
441 $parts[2] = 0;
442 break;
443 case 'dev':
444 $parts[2]++;
445 break;
446 }
447
448 $res = array();
449 $res['version'] = $parts[0] . '.' . $parts[1] . '.' . $parts[2];
450 $res['version_int'] = intval($parts[0] * 1000000 + $parts[1] * 1000 + $parts[2]);
451 $res['version_main'] = $parts[0];
452 $res['version_sub'] = $parts[1];
453 $res['version_dev'] = $parts[2];
454
455 return $res;
456 }
457
458 /**
459 * Render version from intVersion
460 *
461 * @static
462 * @param int $intVersion
463 * @return string version
464 */
465 public static function versionFromInt($intVersion) {
466 $versionString = str_pad($intVersion, 9, '0', STR_PAD_LEFT);
467 $parts = array(
468 substr($versionString, 0, 3),
469 substr($versionString, 3, 3),
470 substr($versionString, 6, 3)
471 );
472 return intval($parts[0]) . '.' . intval($parts[1]) . '.' . intval($parts[2]);
473 }
474
475 /**
476 * Evaluates differences in version numbers with three parts, x.x.x. Returns true if $v1 is greater than $v2
477 *
478 * @param string Version number 1
479 * @param string Version number 2
480 * @param integer Tolerance factor. For instance, set to 1000 to ignore difference in dev-version (third part)
481 * @return boolean True if version 1 is greater than version 2
482 */
483 public static function versionDifference($v1, $v2, $div = 1) {
484 return floor(self::makeVersion($v1, 'int') / $div) > floor(self::makeVersion($v2, 'int') / $div);
485 }
486
487
488 /**
489 * Returns true if the $str is found as the first part of a string in $array
490 *
491 * @param string String to test with.
492 * @param array Input array
493 * @param boolean If set, the test is case insensitive
494 * @return boolean True if found.
495 */
496 public static function first_in_array($str, $array, $caseInsensitive = FALSE) {
497 if ($caseInsensitive) {
498 $str = strtolower($str);
499 }
500 if (is_array($array)) {
501 foreach ($array as $cl) {
502 if ($caseInsensitive) {
503 $cl = strtolower($cl);
504 }
505 if (t3lib_div::isFirstPartOfStr($cl, $str)) {
506 return TRUE;
507 }
508 }
509 }
510 return FALSE;
511 }
512
513 /**
514 * Compares two arrays with MD5-hash values for analysis of which files has changed.
515 *
516 * @param array Current values
517 * @param array Past values
518 * @return array Affected files
519 */
520 public static function findMD5ArrayDiff($current, $past) {
521 if (!is_array($current)) {
522 $current = array();
523 }
524 if (!is_array($past)) {
525 $past = array();
526 }
527 $filesInCommon = array_intersect($current, $past);
528 $diff1 = array_keys(array_diff($past, $filesInCommon));
529 $diff2 = array_keys(array_diff($current, $filesInCommon));
530 $affectedFiles = array_unique(array_merge($diff1, $diff2));
531 return $affectedFiles;
532 }
533
534 /**
535 * Returns title and style attribute for mouseover help text.
536 *
537 * @param string Help text.
538 * @return string title="" attribute prepended with a single space
539 */
540 public static function labelInfo($str) {
541 return ' title="' . htmlspecialchars($str) . '" style="cursor:help;"';
542 }
543
544
545 /**
546 * Returns the absolute path where the extension $extKey is installed (based on 'type' (SGL))
547 *
548 * @param string Extension key
549 * @param string Install scope type: L, G, S
550 * @return string Returns the absolute path to the install scope given by input $type variable. It is checked if the path is a directory. Slash is appended.
551 */
552 public static function getExtPath($extKey, $type, $returnWithoutExtKey = FALSE) {
553 $typePath = self::typePath($type);
554
555 if ($typePath) {
556 $path = $typePath . ($returnWithoutExtKey ? '' : $extKey . '/');
557 return $path; # @is_dir($path) ? $path : '';
558 } else {
559 return '';
560 }
561 }
562
563 /**
564 * Get type of extension (G,S,L) from extension path
565 *
566 * @param string $path
567 */
568 public static function getExtTypeFromPath($path) {
569 if (strpos($path, TYPO3_mainDir . 'sysext/') !== FALSE) {
570 return 'S';
571 } elseif (strpos($path, TYPO3_mainDir . 'ext/') !== FALSE) {
572 return 'G';
573 } elseif (strpos($path, 'typo3conf/ext/') !== FALSE) {
574 return 'L';
575 }
576 }
577
578 /**
579 * Get path from type
580 *
581 * @param string $type S/G/L
582 */
583 public static function typePath($type) {
584 if ($type === 'S') {
585 return PATH_typo3 . 'sysext/';
586 } elseif ($type === 'G') {
587 return PATH_typo3 . 'ext/';
588 } elseif ($type === 'L') {
589 return PATH_typo3conf . 'ext/';
590 }
591 }
592
593 /**
594 * Get relative path from type
595 *
596 * @param string $type S/G/L
597 */
598 public static function typeRelPath($type) {
599 if ($type === 'S') {
600 return 'sysext/';
601 } elseif ($type === 'G') {
602 return 'ext/';
603 } elseif ($type === 'L') {
604 return '../typo3conf/ext/';
605 }
606 }
607
608 /**
609 * Get backpath from type
610 *
611 * @param string $type S/G/L
612 */
613 public static function typeBackPath($type) {
614 if ($type === 'L') {
615 return '../../../../' . TYPO3_mainDir;
616 } else {
617 return '../../../';
618 }
619 }
620
621 /**
622 * Reads locallang file into array (for possible include in header)
623 *
624 * @param $file
625 */
626 public static function getArrayFromLocallang($file, $key = 'default') {
627 $content = t3lib_div::getURL($file);
628 $array = t3lib_div::xml2array($content);
629 return $array['data'][$key];
630
631 }
632
633 /**
634 * Include a locallang file and return the $LOCAL_LANG array serialized.
635 *
636 * @param string Absolute path to locallang file to include.
637 * @param string Old content of a locallang file (keeping the header content)
638 * @return array Array with header/content as key 0/1
639 * @see makeUploadarray()
640 */
641 public static function getSerializedLocalLang($file, $content) {
642 $LOCAL_LANG = NULL;
643 $returnParts = explode('$LOCAL_LANG', $content, 2);
644
645 include($file);
646 if (is_array($LOCAL_LANG)) {
647 $returnParts[1] = serialize($LOCAL_LANG);
648 return $returnParts;
649 } else {
650 return array();
651 }
652 }
653
654
655 /**
656 * Enter description here...
657 *
658 * @param unknown_type $array
659 * @param unknown_type $lines
660 * @param unknown_type $level
661 * @return unknown
662 */
663 public static function arrayToCode($array, $level = 0) {
664 $lines = 'array(' . LF;
665 $level++;
666 foreach ($array as $k => $v) {
667 if (strlen($k) && is_array($v)) {
668 $lines .= str_repeat(TAB, $level) . "'" . $k . "' => " . self::arrayToCode($v, $level);
669 } elseif (strlen($k)) {
670 $lines .= str_repeat(TAB, $level) . "'" . $k . "' => " . (t3lib_div::testInt($v) ? intval($v) : "'" . t3lib_div::slashJS(trim($v), 1) . "'") . ',' . LF;
671 }
672 }
673
674 $lines .= str_repeat(TAB, $level - 1) . ')' . ($level - 1 == 0 ? '' : ',' . LF);
675 return $lines;
676 }
677
678
679 /**
680 * Traverse the array of installed extensions keys and arranges extensions in the priority order they should be in
681 *
682 * @param array Array of extension keys as values
683 * @param array Extension information array
684 * @return array Modified array of extention keys as values
685 * @see addExtToList()
686 */
687 public static function managesPriorities($listArr, $instExtInfo) {
688
689 // Initialize:
690 $levels = array(
691 'top' => array(),
692 'middle' => array(),
693 'bottom' => array(),
694 );
695
696 // Traverse list of extensions:
697 foreach ($listArr as $ext) {
698 $prio = trim($instExtInfo[$ext]['EM_CONF']['priority']);
699 switch ((string) $prio) {
700 case 'top':
701 case 'bottom':
702 $levels[$prio][] = $ext;
703 break;
704 default:
705 $levels['middle'][] = $ext;
706 break;
707 }
708 }
709 return array_merge(
710 $levels['top'],
711 $levels['middle'],
712 $levels['bottom']
713 );
714 }
715
716
717 /**
718 * Returns either array with all default categories or index/title
719 * of a category entry.
720 *
721 * @access public
722 * @param mixed $cat category title or category index
723 * @return mixed
724 */
725 public static function getDefaultCategory($cat = NULL) {
726 if (is_null($cat)) {
727 return self::$defaultCategories;
728 } else {
729 if (is_string($cat)) {
730 // default category
731 $catIndex = 4;
732 if (array_key_exists(strtolower($cat), self::$defaultCategories)) {
733 $catIndex = self::$defaultCategories[strtolower($cat)];
734 }
735 return $catIndex;
736 } else {
737 if (is_int($cat) && $cat >= 0) {
738 $catTitle = array_search($cat, self::$defaultCategories);
739 // default category
740 if (!$catTitle) {
741 $catTitle = 'misc';
742 }
743 return $catTitle;
744 }
745 }
746 }
747 }
748
749 /**
750 * Returns either array with all default states or index/title
751 * of a state entry.
752 *
753 * @access public
754 * @param mixed $state state title or state index
755 * @return mixed
756 */
757 public static function getDefaultState($state = NULL) {
758 if (is_null($state)) {
759 return self::$defaultStates;
760 } else {
761 if (is_string($state)) {
762 // default state
763 $stateIndex = 999;
764 if (array_key_exists(strtolower($state), self::$defaultStates)) {
765 $stateIndex = self::$defaultStates[strtolower($state)];
766 }
767 return $stateIndex;
768 } else {
769 if (is_int($state) && $state >= 0) {
770 $stateTitle = array_search($state, self::$defaultStates);
771 // default state
772 if (!$stateTitle) {
773 $stateTitle = 'n/a';
774 }
775 return $stateTitle;
776 }
777 }
778 }
779 }
780
781 /**
782 * Extension States
783 * Content must be redundant with the same internal variable as in class.tx_extrep.php!
784 *
785 * @static
786 * @return array
787 */
788 public static function getStates() {
789 return array(
790 'alpha' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:state_alpha'),
791 'beta' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:state_beta'),
792 'stable' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:state_stable'),
793 'experimental' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:state_experimental'),
794 'test' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:state_test'),
795 'obsolete' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:state_obsolete'),
796 'excludeFromUpdates' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:state_exclude_from_updates')
797 );
798 }
799
800 /**
801 * Reports back if installation in a certain scope is possible.
802 *
803 * @param string Scope: G, L, S
804 * @param string Extension lock-type (eg. "L" or "G")
805 * @return boolean True if installation is allowed.
806 */
807 public static function importAsType($type, $lockType = '') {
808 switch ($type) {
809 case 'G':
810 return $GLOBALS['TYPO3_CONF_VARS']['EXT']['allowGlobalInstall'] && (!$lockType || !strcmp($lockType, $type));
811 break;
812 case 'L':
813 return $GLOBALS['TYPO3_CONF_VARS']['EXT']['allowLocalInstall'] && (!$lockType || !strcmp($lockType, $type));
814 break;
815 case 'S':
816 return isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['allowSystemInstall']) && $GLOBALS['TYPO3_CONF_VARS']['EXT']['allowSystemInstall'];
817 break;
818 default:
819 return FALSE;
820 }
821 }
822
823 /**
824 * Returns true if extensions in scope, $type, can be deleted (or installed for that sake)
825 *
826 * @param string Scope: "G" or "L"
827 * @return boolean True if possible.
828 */
829 public static function deleteAsType($type) {
830 switch ($type) {
831 case 'G':
832 return $GLOBALS['TYPO3_CONF_VARS']['EXT']['allowGlobalInstall'];
833 break;
834 case 'L':
835 return $GLOBALS['TYPO3_CONF_VARS']['EXT']['allowLocalInstall'];
836 break;
837 default:
838 return FALSE;
839 }
840 }
841
842
843 /**
844 * Creates directories in $extDirPath
845 *
846 * @param array Array of directories to create relative to extDirPath, eg. "blabla", "blabla/blabla" etc...
847 * @param string Absolute path to directory.
848 * @return mixed Returns false on success or an error string
849 */
850 public static function createDirsInPath($dirs, $extDirPath) {
851 if (is_array($dirs)) {
852 foreach ($dirs as $dir) {
853 $error = t3lib_div::mkdir_deep($extDirPath, $dir);
854 if ($error) {
855 return $error;
856 }
857 }
858 }
859
860 return FALSE;
861 }
862
863 /**
864 * Analyses the php-scripts of an available extension on server
865 *
866 * @param string Absolute path to extension
867 * @param string Prefix for tables/classes.
868 * @param string Extension key
869 * @return array Information array.
870 * @see makeDetailedExtensionAnalysis()
871 */
872 public static function getClassIndexLocallangFiles($absPath, $table_class_prefix, $extKey) {
873 $excludeForPackaging = $GLOBALS['TYPO3_CONF_VARS']['EXT']['excludeForPackaging'];
874 $filesInside = t3lib_div::removePrefixPathFromList(t3lib_div::getAllFilesAndFoldersInPath(array(), $absPath, 'php,inc', 0, 99, $excludeForPackaging), $absPath);
875 $out = array();
876 $reg = array();
877
878 foreach ($filesInside as $fileName) {
879 if (substr($fileName, 0, 4) != 'ext_' && substr($fileName, 0, 6) != 'tests/') { // ignore supposed-to-be unit tests as well
880 $baseName = basename($fileName);
881 if (substr($baseName, 0, 9) == 'locallang' && substr($baseName, -4) == '.php') {
882 $out['locallang'][] = $fileName;
883 } elseif ($baseName != 'conf.php') {
884 if (filesize($absPath . $fileName) < 500 * 1024) {
885 $fContent = t3lib_div::getUrl($absPath . $fileName);
886 unset($reg);
887 if (preg_match('/\n[[:space:]]*class[[:space:]]*([[:alnum:]_]+)([[:alnum:][:space:]_]*)/', $fContent, $reg)) {
888
889 // Find classes:
890 $lines = explode(LF, $fContent);
891 foreach ($lines as $l) {
892 $line = trim($l);
893 unset($reg);
894 if (preg_match('/^class[[:space:]]*([[:alnum:]_]+)([[:alnum:][:space:]_]*)/', $line, $reg)) {
895 $out['classes'][] = $reg[1];
896 $out['files'][$fileName]['classes'][] = $reg[1];
897 if ($reg[1] !== 'ext_update' && substr($reg[1], 0, 3) != 'ux_' && !t3lib_div::isFirstPartOfStr($reg[1], $table_class_prefix) && strcmp(substr($table_class_prefix, 0, -1), $reg[1])) {
898 $out['NSerrors']['classname'][] = $reg[1];
899 } else {
900 $out['NSok']['classname'][] = $reg[1];
901 }
902 }
903 }
904 // If class file prefixed 'class.'....
905 if (substr($baseName, 0, 6) == 'class.') {
906 $fI = pathinfo($baseName);
907 $testName = substr($baseName, 6, -(1 + strlen($fI['extension'])));
908 if ($testName !== 'ext_update' && substr($testName, 0, 3) != 'ux_' && !t3lib_div::isFirstPartOfStr($testName, $table_class_prefix) && strcmp(substr($table_class_prefix, 0, -1), $testName)) {
909 $out['NSerrors']['classfilename'][] = $baseName;
910 } else {
911 $out['NSok']['classfilename'][] = $baseName;
912 if (is_array($out['files'][$fileName]['classes']) && tx_em_Tools::first_in_array($testName, $out['files'][$fileName]['classes'], 1)) {
913 $out['msg'][] = sprintf($GLOBALS['LANG']->getLL('detailedExtAnalysis_class_ok'),
914 $fileName, $testName
915 );
916 } else {
917 $out['errors'][] = sprintf($GLOBALS['LANG']->getLL('detailedExtAnalysis_class_not_ok'),
918 $fileName, $testName
919 );
920 }
921 }
922 }
923 // Check for proper XCLASS definition
924 // Match $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS'] with single or doublequotes
925 $XclassSearch = '\$TYPO3_CONF_VARS\[TYPO3_MODE\]\[[\'"]XCLASS[\'"]\]';
926 $XclassParts = preg_split('/if \(defined\([\'"]TYPO3_MODE[\'"]\)(.*)' . $XclassSearch . '/', $fContent, 2);
927 if (count($XclassParts) !== 2) {
928 // Match $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS'] with single or doublequotes
929 $XclassSearch = '\$GLOBALS\[[\'"]TYPO3_CONF_VARS[\'"]\]\[TYPO3_MODE\]\[[\'"]XCLASS[\'"]\]';
930 $XclassParts = preg_split('/if \(defined\([\'"]TYPO3_MODE[\'"]\)(.*)' . $XclassSearch . '/', $fContent, 2);
931 }
932
933 if (count($XclassParts) == 2) {
934 unset($reg);
935 preg_match('/^\[[\'"]([[:alnum:]_\/\.]*)[\'"]\]/', $XclassParts[1], $reg);
936 if ($reg[1]) {
937 $cmpF = 'ext/' . $extKey . '/' . $fileName;
938 if (!strcmp($reg[1], $cmpF)) {
939 if (preg_match('/_once[[:space:]]*\(' . $XclassSearch . '\[[\'"]' . preg_quote($cmpF, '/') . '[\'"]\]\);/', $XclassParts[1])) {
940 $out['msg'][] = sprintf($GLOBALS['LANG']->getLL('detailedExtAnalysis_xclass_ok'), $fileName);
941 } else {
942 $out['errors'][] = $GLOBALS['LANG']->getLL('detailedExtAnalysis_xclass_no_include');
943 }
944 } else {
945 $out['errors'][] = sprintf($GLOBALS['LANG']->getLL('detailedExtAnalysis_xclass_incorrect'),
946 $reg[1], $cmpF
947 );
948 }
949 } else {
950 $out['errors'][] = sprintf($GLOBALS['LANG']->getLL('detailedExtAnalysis_no_xclass_filename'), $fileName);
951 }
952 } elseif (!tx_em_Tools::first_in_array('ux_', $out['files'][$fileName]['classes'])) {
953 // No Xclass definition required if classname starts with 'ux_'
954 $out['errors'][] = sprintf($GLOBALS['LANG']->getLL('detailedExtAnalysis_no_xclass_found'), $fileName);
955 }
956 }
957 }
958 }
959 }
960 }
961 return $out;
962 }
963
964 /**
965 * Write new TYPO3_MOD_PATH to "conf.php" file.
966 *
967 * @param string Absolute path to a "conf.php" file of the backend module which we want to write back to.
968 * @param string Install scope type: L, G, S
969 * @param string Relative path for the module folder in extension
970 * @return string Returns message about the status.
971 * @see modConfFileAnalysis()
972 */
973 public static function writeTYPO3_MOD_PATH($confFilePath, $type, $mP) {
974 $lines = explode(LF, t3lib_div::getUrl($confFilePath));
975 $confFileInfo = array();
976 $confFileInfo['lines'] = $lines;
977 $reg = array();
978
979 $flag_M = 0;
980 $flag_B = 0;
981 $flag_Dispatch = 0;
982
983 foreach ($lines as $k => $l) {
984 $line = trim($l);
985
986 unset($reg);
987 if (preg_match('/^define[[:space:]]*\([[:space:]]*["\']TYPO3_MOD_PATH["\'][[:space:]]*,[[:space:]]*["\']([[:alnum:]_\/\.]+)["\'][[:space:]]*\)[[:space:]]*;/', $line, $reg)) {
988 $lines[$k] = str_replace($reg[0], 'define(\'TYPO3_MOD_PATH\', \'' . self::typeRelPath($type) . $mP . '\');', $lines[$k]);
989 $flag_M = $k + 1;
990 }
991
992 unset($reg);
993 if (preg_match('/^\$BACK_PATH[[:space:]]*=[[:space:]]*["\']([[:alnum:]_\/\.]+)["\'][[:space:]]*;/', $line, $reg)) {
994 $lines[$k] = str_replace($reg[0], '$BACK_PATH=\'' . self::typeBackPath($type) . '\';', $lines[$k]);
995 $flag_B = $k + 1;
996 }
997
998 // Check if this module uses new API (see http://bugs.typo3.org/view.php?id=5278)
999 // where TYPO3_MOD_PATH and BACK_PATH are not required
1000 unset($reg);
1001 if (preg_match('/^\$MCONF\[["\']script["\']\][[:space:]]*=[[:space:]]*["\']_DISPATCH["\'][[:space:]]*;/', $line, $reg)) {
1002 $flag_Dispatch = $k + 1;
1003 }
1004
1005 }
1006
1007 if ($flag_B && $flag_M) {
1008 t3lib_div::writeFile($confFilePath, implode(LF, $lines));
1009 return sprintf($GLOBALS['LANG']->getLL('writeModPath_ok'),
1010 substr($confFilePath, strlen(PATH_site)));
1011 } elseif ($flag_Dispatch) {
1012 return sprintf(
1013 $GLOBALS['LANG']->getLL('writeModPath_notRequired'),
1014 substr($confFilePath, strlen(PATH_site))
1015 );
1016 } else {
1017 return self::rfw(
1018 sprintf($GLOBALS['LANG']->getLL('writeModPath_error'),
1019 $confFilePath)
1020 );
1021 }
1022 }
1023
1024 /**
1025 * Wrapping input string in a link tag with link to email address
1026 *
1027 * @param string Input string, being wrapped in <a> tags
1028 * @param string Email address for use in link.
1029 * @return string Output
1030 */
1031 public static function wrapEmail($str, $email) {
1032 if ($email) {
1033 $str = '<a href="mailto:' . htmlspecialchars($email) . '">' . htmlspecialchars($str) . '</a>';
1034 }
1035 return $str;
1036 }
1037
1038 /**
1039 * red-fontwrap. Returns the string wrapped in a <span>-tag defining the color to be red
1040 *
1041 * @param string Input string
1042 * @return string Output string
1043 */
1044 public static function rfw($string) {
1045 return '<span class="typo3-red">' . $string . '</span>';
1046 }
1047
1048 /**
1049 * dimmed-fontwrap. Returns the string wrapped in a <span>-tag defining the color to be gray/dimmed
1050 *
1051 * @param string Input string
1052 * @return string Output string
1053 */
1054 public static function dfw($string) {
1055 return '<span class="typo3-dimmed">' . $string . '</span>';
1056 }
1057 }
1058
1059 ?>