Fixed bug #15686: Call the list module through the dispatcher instead directly (Thank...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_extmgm.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skårhøj (kasperYYYY@typo3.com)
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 * Contains a class with Extension Management functions
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 114: class t3lib_extMgm
41 *
42 * SECTION: PATHS and other evaluation
43 * 131: function isLoaded($key,$exitOnError = 0)
44 * 147: function extPath($key,$script='')
45 * 165: function extRelPath($key)
46 * 182: function siteRelPath($key)
47 * 194: function getCN($key)
48 *
49 * SECTION: Adding BACKEND features
50 * 227: function addTCAcolumns($table,$columnArray,$addTofeInterface=0)
51 * 251: function addToAllTCAtypes($table,$str,$specificTypesList='',$position='')
52 * 309: function allowTableOnStandardPages($table)
53 * 326: function addModule($main,$sub='',$position='',$path='')
54 * 389: function insertModuleFunction($modname,$className,$classPath,$title,$MM_key='function',$WS='')
55 * 408: function addPageTSConfig($content)
56 * 422: function addUserTSConfig($content)
57 * 437: function addLLrefForTCAdescr($tca_descr_key,$file_ref)
58 *
59 * SECTION: Adding SERVICES features
60 * 479: function addService($extKey, $serviceType, $serviceKey, $info)
61 * 547: function findService($serviceType, $serviceSubType='', $excludeServiceKeys=array())
62 * 618: function deactivateService($serviceType, $serviceKey)
63 *
64 * SECTION: Adding FRONTEND features
65 * 657: function addPlugin($itemArray,$type='list_type')
66 * 682: function addPiFlexFormValue($piKeyToMatch,$value)
67 * 702: function addToInsertRecords($table,$content_table='tt_content',$content_field='records')
68 * 733: function addPItoST43($key,$classFile='',$prefix='',$type='list_type',$cached=0)
69 * 808: function addStaticFile($extKey,$path,$title)
70 * 827: function addTypoScriptSetup($content)
71 * 841: function addTypoScriptConstants($content)
72 * 858: function addTypoScript($key,$type,$content,$afterStaticUid=0)
73 *
74 * SECTION: INTERNAL EXTENSION MANAGEMENT:
75 * 921: function typo3_loadExtensions()
76 * 998: function _makeIncludeHeader($key,$file)
77 * 1019: function isCacheFilesAvailable($cacheFilePrefix)
78 * 1032: function isLocalconfWritable()
79 * 1045: function cannotCacheFilesWritable($cacheFilePrefix)
80 * 1069: function currentCacheFiles()
81 * 1092: function writeCacheFiles($extensions,$cacheFilePrefix)
82 * 1130: function removeCacheFiles()
83 *
84 * TOTAL FUNCTIONS: 32
85 * (This index is automatically created/updated by the extension "extdeveval")
86 *
87 */
88
89
90 /**
91 * Extension Management functions
92 *
93 * This class is never instantiated, rather the methods inside is called as functions like
94 * t3lib_extMgm::isLoaded('my_extension');
95 *
96 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
97 * @package TYPO3
98 * @subpackage t3lib
99 */
100 final class t3lib_extMgm {
101 protected static $extensionKeyMap;
102
103
104 /**************************************
105 *
106 * PATHS and other evaluation
107 *
108 ***************************************/
109
110 /**
111 * Returns true if the extension with extension key $key is loaded.
112 * Usage: 109
113 *
114 * @param string Extension key to test
115 * @param boolean If $exitOnError is true and the extension is not loaded the function will die with an error message
116 * @return boolean
117 */
118 public static function isLoaded($key, $exitOnError = 0) {
119 global $TYPO3_LOADED_EXT;
120 if ($exitOnError && !isset($TYPO3_LOADED_EXT[$key])) {
121 throw new BadFunctionCallException(
122 'TYPO3 Fatal Error: Extension "' . $key . '" was not loaded!',
123 1270853910
124 );
125 }
126 return isset($TYPO3_LOADED_EXT[$key]);
127 }
128
129 /**
130 * Returns the absolute path to the extension with extension key $key
131 * If the extension is not loaded the function will die with an error message
132 * Useful for internal fileoperations
133 * Usage: 136
134 *
135 * @param string Extension key
136 * @param string $script is appended to the output if set.
137 * @return string
138 */
139 public static function extPath($key, $script = '') {
140 global $TYPO3_LOADED_EXT;
141 if (!isset($TYPO3_LOADED_EXT[$key])) {
142 throw new BadFunctionCallException(
143 'TYPO3 Fatal Error: Extension key "' . $key . '" was NOT loaded!',
144 1270853878
145 );
146 }
147 return PATH_site . $TYPO3_LOADED_EXT[$key]['siteRelPath'] . $script;
148 }
149
150 /**
151 * Returns the relative path to the extension as measured from from the TYPO3_mainDir
152 * If the extension is not loaded the function will die with an error message
153 * Useful for images and links from backend
154 * Usage: 54
155 *
156 * @param string Extension key
157 * @return string
158 */
159 public static function extRelPath($key) {
160 global $TYPO3_LOADED_EXT;
161 if (!isset($TYPO3_LOADED_EXT[$key])) {
162 throw new BadFunctionCallException(
163 'TYPO3 Fatal Error: Extension key "' . $key . '" was NOT loaded!',
164 1270853879
165 );
166 }
167 return $TYPO3_LOADED_EXT[$key]['typo3RelPath'];
168 }
169
170 /**
171 * Returns the relative path to the extension as measured from the PATH_site (frontend)
172 * If the extension is not loaded the function will die with an error message
173 * Useful for images and links from the frontend
174 * Usage: 6
175 *
176 * @param string Extension key
177 * @return string
178 */
179 public static function siteRelPath($key) {
180 return substr(self::extPath($key), strlen(PATH_site));
181 }
182
183 /**
184 * Returns the correct class name prefix for the extension key $key
185 * Usage: 3
186 *
187 * @param string Extension key
188 * @return string
189 * @internal
190 */
191 public static function getCN($key) {
192 return substr($key, 0, 5) == 'user_' ? 'user_' . str_replace('_', '', substr($key, 5)) : 'tx_' . str_replace('_', '', $key);
193 }
194
195 /**
196 * Returns the real extension key like 'tt_news' from an extension prefix like 'tx_ttnews'.
197 *
198 * @param string $prefix: The extension prefix (e.g. 'tx_ttnews')
199 * @return mixed Real extension key (string) or false (boolean) if something went wrong
200 */
201 public static function getExtensionKeyByPrefix($prefix) {
202 $result = FALSE;
203 // Build map of short keys referencing to real keys:
204 if (!isset(self::$extensionKeyMap)) {
205 self::$extensionKeyMap = array();
206 foreach (array_keys($GLOBALS['TYPO3_LOADED_EXT']) as $extensionKey) {
207 $shortKey = str_replace('_', '', $extensionKey);
208 self::$extensionKeyMap[$shortKey] = $extensionKey;
209 }
210 }
211 // Lookup by the given short key:
212 $parts = explode('_', $prefix);
213 if (isset(self::$extensionKeyMap[$parts[1]])) {
214 $result = self::$extensionKeyMap[$parts[1]];
215 }
216 return $result;
217 }
218
219 /**
220 * Clears the extension key map.
221 *
222 * @return void
223 */
224 public static function clearExtensionKeyMap() {
225 self::$extensionKeyMap = NULL;
226 }
227
228 /**
229 * Retrieves the version of an installed extension.
230 * If the extension is not installed, this function returns an empty string.
231 *
232 * @param string $key the key of the extension to look up, must not be empty
233 * @return string the extension version as a string in the format "x.y.z",
234 * will be an empty string if the extension is not loaded
235 */
236 public static function getExtensionVersion($key) {
237 if (!is_string($key) || empty($key)) {
238 throw new InvalidArgumentException('Extension key must be a non-empty string.');
239 }
240 if (!self::isLoaded($key)) {
241 return '';
242 }
243
244 $EM_CONF = array();
245 $_EXTKEY = $key;
246 include(self::extPath($key) . 'ext_emconf.php');
247
248 return $EM_CONF[$key]['version'];
249 }
250
251
252 /**************************************
253 *
254 * Adding BACKEND features
255 * (related to core features)
256 *
257 ***************************************/
258
259 /**
260 * Adding fields to an existing table definition in $TCA
261 * Adds an array with $TCA column-configuration to the $TCA-entry for that table.
262 * This function adds the configuration needed for rendering of the field in TCEFORMS - but it does NOT add the field names to the types lists!
263 * So to have the fields displayed you must also call fx. addToAllTCAtypes or manually add the fields to the types list.
264 * FOR USE IN ext_tables.php FILES
265 * Usage: 4
266 *
267 * @param string $table is the table name of a table already present in $TCA with a columns section
268 * @param array $columnArray is the array with the additional columns (typical some fields an extension wants to add)
269 * @param boolean If $addTofeInterface is true the list of fields are also added to the fe_admin_fieldList.
270 * @return void
271 */
272 public static function addTCAcolumns($table, $columnArray, $addTofeInterface = 0) {
273 global $TCA;
274 t3lib_div::loadTCA($table);
275 if (is_array($columnArray) && is_array($TCA[$table]) && is_array($TCA[$table]['columns'])) {
276 $TCA[$table]['columns'] = array_merge($TCA[$table]['columns'], $columnArray); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
277 if ($addTofeInterface) {
278 $TCA[$table]['feInterface']['fe_admin_fieldList'] .= ',' . implode(',', array_keys($columnArray));
279 }
280 }
281 }
282
283 /**
284 * Makes fields visible in the TCEforms, adding them to the end of (all) "types"-configurations
285 *
286 * Adds a string $str (comma list of field names) to all ["types"][xxx]["showitem"] entries for table $table (unless limited by $specificTypesList)
287 * This is needed to have new fields shown automatically in the TCEFORMS of a record from $table.
288 * Typically this function is called after having added new columns (database fields) with the addTCAcolumns function
289 * FOR USE IN ext_tables.php FILES
290 * Usage: 1
291 *
292 * @param string Table name
293 * @param string Field list to add.
294 * @param string List of specific types to add the field list to. (If empty, all type entries are affected)
295 * @param string Insert fields before (default) or after one of this fields (commalist with "before:" or "after:" commands). Example: "before:keywords,--palette--;;4,after:description". Palettes must be passed like in the example no matter how the palette definition looks like in TCA.
296 * @return void
297 */
298 public static function addToAllTCAtypes($table, $str, $specificTypesList = '', $position = '') {
299 t3lib_div::loadTCA($table);
300 $str = trim($str);
301
302 if ($str && is_array($GLOBALS['TCA'][$table]) && is_array($GLOBALS['TCA'][$table]['types'])) {
303 foreach ($GLOBALS['TCA'][$table]['types'] as $type => &$typeDetails) {
304 if ($specificTypesList === '' || t3lib_div::inList($specificTypesList, $type)) {
305 $typeDetails['showitem'] = self::executePositionedStringInsertion(
306 $typeDetails['showitem'],
307 $str,
308 $position
309 );
310 }
311 }
312 }
313 }
314
315 /**
316 * Adds new fields to all palettes of an existing field.
317 * If the field does not have a palette yet, it's created automatically and
318 * gets called "generatedFor-$field".
319 *
320 * @param string $table: Name of the table
321 * @param string $field: Name of the field that has the palette to be extended
322 * @param string $addFields: List of fields to be added to the palette
323 * @param string $insertionPosition: Insert fields before (default) or after one
324 * of this fields (commalist with "before:" or "after:" commands).
325 * Example: "before:keywords,--palette--;;4,after:description".
326 * Palettes must be passed like in the example no matter how the
327 * palette definition looks like in TCA.
328 * @return void
329 */
330 public static function addFieldsToAllPalettesOfField($table, $field, $addFields, $insertionPosition = '') {
331 $generatedPalette = '';
332 $processedPalettes = array();
333 t3lib_div::loadTCA($table);
334
335 if (isset($GLOBALS['TCA'][$table]['columns'][$field])) {
336 $types =& $GLOBALS['TCA'][$table]['types'];
337 if (is_array($types)) {
338 // Iterate through all types and search for the field that defines the palette to be extended:
339 foreach (array_keys($types) as $type) {
340 $items = self::explodeItemList($types[$type]['showitem']);
341 if (isset($items[$field])) {
342 // If the field already has a palette, extend it:
343 if ($items[$field]['details']['palette']) {
344 $palette = $items[$field]['details']['palette'];
345 if (!isset($processedPalettes[$palette])) {
346 self::addFieldsToPalette($table, $palette, $addFields, $insertionPosition);
347 $processedPalettes[$palette] = TRUE;
348 }
349 // If there's not palette yet, create one:
350 } else {
351 if ($generatedPalette) {
352 $palette = $generatedPalette;
353 } else {
354 $palette = $generatedPalette = 'generatedFor-' . $field;
355 self::addFieldsToPalette($table, $palette, $addFields, $insertionPosition);
356 }
357 $items[$field]['details']['palette'] = $palette;
358 $types[$type]['showitem'] = self::generateItemList($items);
359 }
360 }
361 }
362 }
363 }
364 }
365
366 /**
367 * Adds new fields to a palette.
368 * If the palette does not exist yet, it's created automatically.
369 *
370 * @param string $table: Name of the table
371 * @param string $palette: Name of the palette to be extended
372 * @param string $addFields: List of fields to be added to the palette
373 * @param string $insertionPosition: Insert fields before (default) or after one
374 * of this fields (commalist with "before:" or "after:" commands).
375 * Example: "before:keywords,--palette--;;4,after:description".
376 * Palettes must be passed like in the example no matter how the
377 * palette definition looks like in TCA.
378 * @return void
379 */
380 public static function addFieldsToPalette($table, $palette, $addFields, $insertionPosition = '') {
381 t3lib_div::loadTCA($table);
382
383 if (isset($GLOBALS['TCA'][$table])) {
384 $paletteData =& $GLOBALS['TCA'][$table]['palettes'][$palette];
385 // If palette already exists, merge the data:
386 if (is_array($paletteData)) {
387 $paletteData['showitem'] = self::executePositionedStringInsertion(
388 $paletteData['showitem'],
389 $addFields,
390 $insertionPosition
391 );
392 // If it's a new palette, just set the data:
393 } else {
394 $paletteData['showitem'] = self::removeDuplicatesForInsertion($addFields);
395 }
396 }
397 }
398
399 /**
400 * Adds a list of new fields to the TYPO3 USER SETTINGS configuration "showitem" list, the array with
401 * the new fields itself needs to be added additionally to show up in the user setup, like
402 * $GLOBALS['TYPO3_USER_SETTINGS']['columns'] += $tempColumns
403 *
404 * @param string $addFields: List of fields to be added to the user settings
405 * @param string $insertionPosition: Insert fields before (default) or after one
406 * of this fields (commalist with "before:" or "after:" commands).
407 * Example: "before:password,after:email".
408 * @return void
409 */
410 public function addFieldsToUserSettings($addFields, $insertionPosition = '') {
411 $GLOBALS['TYPO3_USER_SETTINGS']['showitem'] = self::executePositionedStringInsertion(
412 $GLOBALS['TYPO3_USER_SETTINGS']['showitem'],
413 $addFields,
414 $insertionPosition
415 );
416 }
417
418 /**
419 * Inserts as list of data into an existing list.
420 * The insertion position can be defined accordant before of after existing list items.
421 *
422 * @param string $list: The list of items to be extended
423 * @param string $insertionList: The list of items to inserted
424 * @param string $insertionPosition: Insert fields before (default) or after one
425 * of this fields (commalist with "before:" or "after:" commands).
426 * Example: "before:keywords,--palette--;;4,after:description".
427 * Palettes must be passed like in the example no matter how the
428 * palette definition looks like in TCA.
429 * @return string The extended list
430 */
431 protected static function executePositionedStringInsertion($list, $insertionList, $insertionPosition = '') {
432 $list = trim($list);
433 $insertionList = self::removeDuplicatesForInsertion($insertionList, $list);
434
435 if ($insertionList) {
436 // Append data to the end (default):
437 if ($insertionPosition === '') {
438 $list .= ($list ? ', ' : '') . $insertionList;
439 // Insert data before or after insertion points:
440 } else {
441 $positions = t3lib_div::trimExplode(',', $insertionPosition, TRUE);
442 $items = self::explodeItemList($list);
443 $isInserted = FALSE;
444 // Iterate through all fields an check whether it's possible to inserte there:
445 foreach ($items as $item => &$itemDetails) {
446 $needles = self::getInsertionNeedles($item, $itemDetails['details']);
447 // Insert data before:
448 foreach ($needles['before'] as $needle) {
449 if (in_array($needle, $positions)) {
450 $itemDetails['rawData'] = $insertionList . ', ' . $itemDetails['rawData'];
451 $isInserted = TRUE;
452 break;
453 }
454 }
455 // Insert data after:
456 foreach ($needles['after'] as $needle) {
457 if (in_array($needle, $positions)) {
458 $itemDetails['rawData'] .= ', ' . $insertionList;
459 $isInserted = TRUE;
460 break;
461 }
462 }
463 // Break if insertion was already done:
464 if ($isInserted) {
465 break;
466 }
467 }
468 // If insertion point could not be determined, append the data:
469 if (!$isInserted) {
470 $list .= ($list ? ', ' : '') . $insertionList;
471 // If data was correctly inserted before or after existing items, recreate the list:
472 } else {
473 $list = self::generateItemList($items, TRUE);
474 }
475 }
476 }
477
478 return $list;
479 }
480
481 /**
482 * Compares an existing list of items and a list of items to be inserted
483 * and returns a duplicate-free variant of that insertion list.
484 *
485 * Example:
486 * + list: 'field_a, field_b;;;;2-2-2, field_c;;;;3-3-3'
487 * + insertion: 'field_b, field_d, field_c;;;4-4-4'
488 * -> new insertion: 'field_d'
489 *
490 * @param string $insertionList: The list of items to inserted
491 * @param string $list: The list of items to be extended (default: '')
492 * @return string Duplicate-free list of items to be inserted
493 */
494 protected static function removeDuplicatesForInsertion($insertionList, $list = '') {
495 $pattern = '/(^|,)\s*\b([^;,]+)\b[^,]*/';
496 $listItems = array();
497
498 if ($list && preg_match_all($pattern, $list, $listMatches)) {
499 $listItems = $listMatches[2];
500 }
501
502 if ($insertionList && preg_match_all($pattern, $insertionList, $insertionListMatches)) {
503 $insertionItems = array();
504 $insertionDuplicates = FALSE;
505
506 foreach ($insertionListMatches[2] as $insertionIndex => $insertionItem) {
507 if (!isset($insertionItems[$insertionItem]) && !in_array($insertionItem, $listItems)) {
508 $insertionItems[$insertionItem] = TRUE;
509 } else {
510 unset($insertionListMatches[0][$insertionIndex]);
511 $insertionDuplicates = TRUE;
512 }
513 }
514
515 if ($insertionDuplicates) {
516 $insertionList = implode('', $insertionListMatches[0]);
517 }
518 }
519
520 return $insertionList;
521 }
522
523 /**
524 * Generates search needles that are used for inserting fields/items into an existing list.
525 *
526 * @see executePositionedStringInsertion
527 * @param string $item: The name of the field/item
528 * @param array $itemDetails: Additional details of the field/item like e.g. palette information
529 * (this array gets created by the function explodeItemList())
530 * @return array The needled to be used for inserting content before or after existing fields/items
531 */
532 protected static function getInsertionNeedles($item, array $itemDetails) {
533 if (strstr($item, '--')) {
534 // If $item is a separator (--div--) or palette (--palette--) then it may have been appended by a unique number. This must be stripped away here.
535 $item = preg_replace('/[0-9]+$/', '', $item);
536 }
537
538 $needles = array(
539 'before' => array($item, 'before:' . $item),
540 'after' => array('after:' . $item),
541 );
542
543 if ($itemDetails['palette']) {
544 $palette = $item . ';;' . $itemDetails['palette'];
545 $needles['before'][] = $palette;
546 $needles['before'][] = 'before:' . $palette;
547 $needles['after'][] = 'after:' . $palette;
548 }
549
550 return $needles;
551 }
552
553 /**
554 * Generates an array of fields/items with additional information such as e.g. the name of the palette.
555 *
556 * @param string $itemList: List of fields/items to be splitted up
557 * (this mostly reflects the data in $TCA[<table>]['types'][<type>]['showitem'])
558 * @return array An array with the names of the fields/items as keys and additional information
559 */
560 protected static function explodeItemList($itemList) {
561 $items = array();
562 $itemParts = t3lib_div::trimExplode(',', $itemList, TRUE);
563
564 foreach ($itemParts as $itemPart) {
565 $itemDetails = t3lib_div::trimExplode(';', $itemPart, FALSE, 5);
566 $key = $itemDetails[0];
567 if (strstr($key, '--')) {
568 // If $key is a separator (--div--) or palette (--palette--) then it will be appended by a unique number. This must be removed again when using this value!
569 $key .= count($items);
570 }
571
572 if (!isset($items[$key])) {
573 $items[$key] = array(
574 'rawData' => $itemPart,
575 'details' => array(
576 'field' => $itemDetails[0],
577 'label' => $itemDetails[1],
578 'palette' => $itemDetails[2],
579 'special' => $itemDetails[3],
580 'styles' => $itemDetails[4],
581 ),
582 );
583 }
584 }
585
586 return $items;
587 }
588
589 /**
590 * Generates a list of fields/items out of an array provided by the function getFieldsOfFieldList().
591 *
592 * @see explodeItemList
593 * @param array $items: The array of fields/items with optional additional information
594 * @param boolean $useRawData: Use raw data instead of building by using the details (default: false)
595 * @return string The list of fields/items which gets used for $TCA[<table>]['types'][<type>]['showitem']
596 * or $TCA[<table>]['palettes'][<palette>]['showitem'] in most cases
597 */
598 protected static function generateItemList(array $items, $useRawData = FALSE) {
599 $itemParts = array();
600
601 foreach ($items as $item => $itemDetails) {
602 if (strstr($item, '--')) {
603 // If $item is a separator (--div--) or palette (--palette--) then it may have been appended by a unique number. This must be stripped away here.
604 $item = preg_replace('/[0-9]+$/', '', $item);
605 }
606
607 if ($useRawData) {
608 $itemParts[] = $itemDetails['rawData'];
609 } else {
610 $itemParts[] = (count($itemDetails['details']) > 1 ? implode(';', $itemDetails['details']) : $item);
611 }
612 }
613
614 return implode(', ', $itemParts);
615 }
616
617 /**
618 * Add tablename to default list of allowed tables on pages (in $PAGES_TYPES)
619 * Will add the $table to the list of tables allowed by default on pages as setup by $PAGES_TYPES['default']['allowedTables']
620 * FOR USE IN ext_tables.php FILES
621 * Usage: 11
622 *
623 * @param string Table name
624 * @return void
625 */
626 public static function allowTableOnStandardPages($table) {
627 global $PAGES_TYPES;
628
629 $PAGES_TYPES['default']['allowedTables'] .= ',' . $table;
630 }
631
632 /**
633 * Adds a module (main or sub) to the backend interface
634 * FOR USE IN ext_tables.php FILES
635 * Usage: 18
636 *
637 * @param string $main is the main module key, $sub is the submodule key. So $main would be an index in the $TBE_MODULES array and $sub could be an element in the lists there.
638 * @param string $sub is the submodule key. If $sub is not set a blank $main module is created.
639 * @param string $position can be used to set the position of the $sub module within the list of existing submodules for the main module. $position has this syntax: [cmd]:[submodule-key]. cmd can be "after", "before" or "top" (or blank which is default). If "after"/"before" then submodule will be inserted after/before the existing submodule with [submodule-key] if found. If not found, the bottom of list. If "top" the module is inserted in the top of the submodule list.
640 * @param string $path is the absolute path to the module. If this value is defined the path is added as an entry in $TBE_MODULES['_PATHS'][ main_sub ] = $path; and thereby tells the backend where the newly added modules is found in the system.
641 * @return void
642 */
643 public static function addModule($main, $sub = '', $position = '', $path = '') {
644 global $TBE_MODULES;
645
646 if (isset($TBE_MODULES[$main]) && $sub) { // If there is already a main module by this name:
647
648 // Adding the submodule to the correct position:
649 list($place, $modRef) = t3lib_div::trimExplode(':', $position, 1);
650 $mods = t3lib_div::trimExplode(',', $TBE_MODULES[$main], 1);
651 if (!in_array($sub, $mods)) {
652 switch (strtolower($place)) {
653 case 'after':
654 case 'before':
655 $pointer = 0;
656 foreach ($mods as $k => $m) {
657 if (!strcmp($m, $modRef)) {
658 $pointer = strtolower($place) == 'after' ? $k + 1 : $k;
659 }
660 }
661 array_splice(
662 $mods, // The modules array
663 $pointer, // To insert one position from the end of the list
664 0, // Don't remove any items, just insert
665 $sub // Module to insert
666 );
667 break;
668 default:
669 if (strtolower($place) == 'top') {
670 array_unshift($mods, $sub);
671 } else {
672 array_push($mods, $sub);
673 }
674 break;
675 }
676 }
677 // Re-inserting the submodule list:
678 $TBE_MODULES[$main] = implode(',', $mods);
679 } else { // Create new main modules with only one submodule, $sub (or none if $sub is blank)
680 $TBE_MODULES[$main] = $sub;
681 }
682
683 // Adding path:
684 if ($path) {
685 $TBE_MODULES['_PATHS'][$main . ($sub ? '_' . $sub : '')] = $path;
686 }
687 }
688
689 /**
690 * Adds a module path to TBE_MODULES for used with the module dispatcher, mod.php
691 * Used only for modules that are not placed in the main/sub menu hierarchy by the traditional mechanism of addModule()
692 * Examples for this is context menu functionality (like import/export) which runs as an independent module through mod.php
693 * FOR USE IN ext_tables.php FILES
694 * Example: t3lib_extMgm::addModulePath('xMOD_tximpexp', t3lib_extMgm::extPath($_EXTKEY).'app/');
695 *
696 * @param string $name is the name of the module, refer to conf.php of the module.
697 * @param string $path is the absolute path to the module directory inside of which "index.php" and "conf.php" is found.
698 * @return void
699 */
700 public static function addModulePath($name, $path) {
701 global $TBE_MODULES;
702
703 $TBE_MODULES['_PATHS'][$name] = $path;
704 }
705
706 /**
707 * Adds a "Function menu module" ('third level module') to an existing function menu for some other backend module
708 * The arguments values are generally determined by which function menu this is supposed to interact with
709 * See Inside TYPO3 for information on how to use this function.
710 * FOR USE IN ext_tables.php FILES
711 * Usage: 26
712 *
713 * @param string Module name
714 * @param string Class name
715 * @param string Class path
716 * @param string Title of module
717 * @param string Menu array key - default is "function"
718 * @param string Workspace conditions. Blank means all workspaces, any other string can be a comma list of "online", "offline" and "custom"
719 * @return void
720 * @see t3lib_SCbase::mergeExternalItems()
721 */
722 public static function insertModuleFunction($modname, $className, $classPath, $title, $MM_key = 'function', $WS = '') {
723 global $TBE_MODULES_EXT;
724 $TBE_MODULES_EXT[$modname]['MOD_MENU'][$MM_key][$className] = array(
725 'name' => $className,
726 'path' => $classPath,
727 'title' => $title,
728 'ws' => $WS
729 );
730 }
731
732 /**
733 * Adds $content to the default Page TSconfig as set in $TYPO3_CONF_VARS[BE]['defaultPageTSconfig']
734 * Prefixed with a [GLOBAL] line
735 * FOR USE IN ext_tables.php/ext_localconf.php FILES
736 * Usage: 5
737 *
738 * @param string Page TSconfig content
739 * @return void
740 */
741 public static function addPageTSConfig($content) {
742 global $TYPO3_CONF_VARS;
743 $TYPO3_CONF_VARS['BE']['defaultPageTSconfig'] .= "\n[GLOBAL]\n" . $content;
744 }
745
746 /**
747 * Adds $content to the default User TSconfig as set in $TYPO3_CONF_VARS[BE]['defaultUserTSconfig']
748 * Prefixed with a [GLOBAL] line
749 * FOR USE IN ext_tables.php/ext_localconf.php FILES
750 * Usage: 3
751 *
752 * @param string User TSconfig content
753 * @return void
754 */
755 public static function addUserTSConfig($content) {
756 global $TYPO3_CONF_VARS;
757 $TYPO3_CONF_VARS['BE']['defaultUserTSconfig'] .= "\n[GLOBAL]\n" . $content;
758 }
759
760 /**
761 * Adds a reference to a locallang file with TCA_DESCR labels
762 * FOR USE IN ext_tables.php FILES
763 * eg. t3lib_extMgm::addLLrefForTCAdescr('pages', 'EXT:lang/locallang_csh_pages.xml'); for the pages table or t3lib_extMgm::addLLrefForTCAdescr('_MOD_web_layout', 'EXT:cms/locallang_csh_weblayout.php'); for the Web > Page module.
764 * Usage: 31
765 *
766 * @param string Description key. Typically a database table (like "pages") but for applications can be other strings, but prefixed with "_MOD_")
767 * @param string File reference to locallang file, eg. "EXT:lang/locallang_csh_pages.php" (or ".xml")
768 * @return void
769 */
770 public static function addLLrefForTCAdescr($tca_descr_key, $file_ref) {
771 global $TCA_DESCR;
772 if ($tca_descr_key) {
773 if (!is_array($TCA_DESCR[$tca_descr_key])) {
774 $TCA_DESCR[$tca_descr_key] = array();
775 }
776 if (!is_array($TCA_DESCR[$tca_descr_key]['refs'])) {
777 $TCA_DESCR[$tca_descr_key]['refs'] = array();
778 }
779 $TCA_DESCR[$tca_descr_key]['refs'][] = $file_ref;
780 }
781 }
782
783 /**
784 * Registers a navigation component
785 *
786 * @param string $module
787 * @param string $componentId
788 * @param array $extDirectNamespaces
789 * @return void
790 */
791 public static function addNavigationComponent($module, $componentId, $extDirectNamespaces = array()) {
792 $GLOBALS['TBE_MODULES']['_navigationComponents'][$module] = array(
793 'componentId' => $componentId,
794 'extKey' => $GLOBALS['_EXTKEY'],
795 'extDirectNamespaces' => $extDirectNamespaces
796 );
797 }
798
799
800 /**************************************
801 *
802 * Adding SERVICES features
803 *
804 * @author René Fritz <r.fritz@colorcube.de>
805 *
806 ***************************************/
807
808 /**
809 * Adds a service to the global services array
810 *
811 * @param string Extension key
812 * @param string Service type, cannot be prefixed "tx_"
813 * @param string Service key, must be prefixed "tx_" or "user_"
814 * @param array Service description array
815 * @return void
816 * @author René Fritz <r.fritz@colorcube.de>
817 */
818 public static function addService($extKey, $serviceType, $serviceKey, $info) {
819 global $T3_SERVICES, $TYPO3_CONF_VARS;
820
821 // even not available services will be included to make it possible to give the admin a feedback of non-available services.
822 // but maybe it's better to move non-available services to a different array??
823
824 if ($serviceType &&
825 !t3lib_div::isFirstPartOfStr($serviceType, 'tx_') &&
826 (t3lib_div::isFirstPartOfStr($serviceKey, 'tx_') || t3lib_div::isFirstPartOfStr($serviceKey, 'user_')) &&
827 is_array($info)) {
828
829 $info['priority'] = max(0, min(100, $info['priority']));
830
831 $T3_SERVICES[$serviceType][$serviceKey] = $info;
832
833 $T3_SERVICES[$serviceType][$serviceKey]['extKey'] = $extKey;
834 $T3_SERVICES[$serviceType][$serviceKey]['serviceKey'] = $serviceKey;
835 $T3_SERVICES[$serviceType][$serviceKey]['serviceType'] = $serviceType;
836
837
838 // mapping a service key to a service type
839 // all service keys begin with tx_ - service types don't
840 // this way a selection of a special service key as service type is easy
841 $T3_SERVICES[$serviceKey][$serviceKey] = &$T3_SERVICES[$serviceType][$serviceKey];
842
843
844 // change the priority (and other values) from TYPO3_CONF_VARS
845 // $TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey]['priority']
846 // even the activation is possible (a unix service might be possible on windows for some reasons)
847 if (is_array($TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey])) {
848
849 // no check is done here - there might be configuration values only the service type knows about, so we pass everything
850 $T3_SERVICES[$serviceType][$serviceKey] = array_merge($T3_SERVICES[$serviceType][$serviceKey], $TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey]);
851 }
852
853
854 // OS check
855 // empty $os means 'not limited to one OS', therefore a check is not needed
856 if ($T3_SERVICES[$serviceType][$serviceKey]['available'] && $T3_SERVICES[$serviceType][$serviceKey]['os'] != '') {
857
858 // TYPO3_OS is not yet defined
859 $os_type = stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin') ? 'WIN' : 'UNIX';
860
861 $os = t3lib_div::trimExplode(',', strtoupper($T3_SERVICES[$serviceType][$serviceKey]['os']));
862
863 if (!in_array($os_type, $os)) {
864 self::deactivateService($serviceType, $serviceKey);
865 }
866 }
867
868 // convert subtype list to array for quicker access
869 $T3_SERVICES[$serviceType][$serviceKey]['serviceSubTypes'] = array();
870 $serviceSubTypes = t3lib_div::trimExplode(',', $info['subtype']);
871 foreach ($serviceSubTypes as $subtype) {
872 $T3_SERVICES[$serviceType][$serviceKey]['serviceSubTypes'][$subtype] = $subtype;
873 }
874 }
875 }
876
877 /**
878 * Find the available service with highest priority
879 *
880 * @param string Service type
881 * @param string Service sub type
882 * @param mixed Service keys that should be excluded in the search for a service. Array or comma list.
883 * @return mixed Service info array if a service was found, FLASE otherwise
884 * @author René Fritz <r.fritz@colorcube.de>
885 */
886 public static function findService($serviceType, $serviceSubType = '', $excludeServiceKeys = array()) {
887 global $T3_SERVICES, $TYPO3_CONF_VARS;
888
889 $serviceKey = FALSE;
890 $serviceInfo = FALSE;
891 $priority = 0;
892 $quality = 0;
893
894 if (!is_array($excludeServiceKeys)) {
895 $excludeServiceKeys = t3lib_div::trimExplode(',', $excludeServiceKeys, 1);
896 }
897
898 if (is_array($T3_SERVICES[$serviceType])) {
899 foreach ($T3_SERVICES[$serviceType] as $key => $info) {
900
901 if (in_array($key, $excludeServiceKeys)) {
902 continue;
903 }
904
905 // select a subtype randomly
906 // usefull to start a service by service key without knowing his subtypes - for testing purposes
907 if ($serviceSubType == '*') {
908 $serviceSubType = key($info['serviceSubTypes']);
909 }
910
911 // this matches empty subtype too
912 if ($info['available'] && ($info['subtype'] == $serviceSubType || $info['serviceSubTypes'][$serviceSubType]) && $info['priority'] >= $priority) {
913
914 // has a lower quality than the already found, therefore we skip this service
915 if ($info['priority'] == $priority && $info['quality'] < $quality) {
916 continue;
917 }
918
919 // service depends on external programs - check if they exists
920 if (trim($info['exec'])) {
921 $executables = t3lib_div::trimExplode(',', $info['exec'], 1);
922 foreach ($executables as $executable) {
923 if (!t3lib_exec::checkCommand($executable)) {
924 self::deactivateService($serviceType, $key);
925 $info['available'] = FALSE;
926 break;
927 }
928 }
929 }
930
931 // still available after exec check?
932 if ($info['available']) {
933 $serviceKey = $key;
934 $priority = $info['priority'];
935 $quality = $info['quality'];
936 }
937 }
938 }
939 }
940
941 if ($serviceKey) {
942 $serviceInfo = $T3_SERVICES[$serviceType][$serviceKey];
943 }
944 return $serviceInfo;
945 }
946
947 /**
948 * Deactivate a service
949 *
950 * @param string Service type
951 * @param string Service key
952 * @return void
953 * @author René Fritz <r.fritz@colorcube.de>
954 */
955 public static function deactivateService($serviceType, $serviceKey) {
956 global $T3_SERVICES;
957
958 // ... maybe it's better to move non-available services to a different array??
959 $T3_SERVICES[$serviceType][$serviceKey]['available'] = FALSE;
960 }
961
962
963 /**************************************
964 *
965 * Adding FRONTEND features
966 * (related specifically to "cms" extension)
967 *
968 ***************************************/
969
970 /**
971 * Adds an entry to the list of plugins in content elements of type "Insert plugin"
972 * Takes the $itemArray (label, value[,icon]) and adds to the items-array of $TCA[tt_content] elements with CType "listtype" (or another field if $type points to another fieldname)
973 * If the value (array pos. 1) is already found in that items-array, the entry is substituted, otherwise the input array is added to the bottom.
974 * Use this function to add a frontend plugin to this list of plugin-types - or more generally use this function to add an entry to any selectorbox/radio-button set in the TCEFORMS
975 * FOR USE IN ext_tables.php FILES
976 * Usage: 13
977 *
978 * @param array Item Array
979 * @param string Type (eg. "list_type") - basically a field from "tt_content" table
980 * @return void
981 */
982 public static function addPlugin($itemArray, $type = 'list_type') {
983 global $TCA;
984
985 $_EXTKEY = $GLOBALS['_EXTKEY'];
986 if ($_EXTKEY && !$itemArray[2]) {
987 $itemArray[2] = self::extRelPath($_EXTKEY) . 'ext_icon.gif';
988 }
989
990 t3lib_div::loadTCA('tt_content');
991 if (is_array($TCA['tt_content']['columns']) && is_array($TCA['tt_content']['columns'][$type]['config']['items'])) {
992 foreach ($TCA['tt_content']['columns'][$type]['config']['items'] as $k => $v) {
993 if (!strcmp($v[1], $itemArray[1])) {
994 $TCA['tt_content']['columns'][$type]['config']['items'][$k] = $itemArray;
995 return;
996 }
997 }
998 $TCA['tt_content']['columns'][$type]['config']['items'][] = $itemArray;
999 }
1000 }
1001
1002 /**
1003 * Adds an entry to the "ds" array of the tt_content field "pi_flexform".
1004 * This is used by plugins to add a flexform XML reference / content for use when they are selected as plugin or content element.
1005 * Usage: 0
1006 *
1007 * @param string Plugin key as used in the list_type field. Use the asterisk * to match all list_type values.
1008 * @param string Either a reference to a flex-form XML file (eg. "FILE:EXT:newloginbox/flexform_ds.xml") or the XML directly.
1009 * @param string Value of tt_content.CType (Content Type) to match. The default is "list" which corresponds to the "Insert Plugin" content element. Use the asterisk * to match all CType values.
1010 * @return void
1011 * @see addPlugin()
1012 */
1013 public static function addPiFlexFormValue($piKeyToMatch, $value, $CTypeToMatch = 'list') {
1014 global $TCA;
1015 t3lib_div::loadTCA('tt_content');
1016
1017 if (is_array($TCA['tt_content']['columns']) && is_array($TCA['tt_content']['columns']['pi_flexform']['config']['ds'])) {
1018 $TCA['tt_content']['columns']['pi_flexform']['config']['ds'][$piKeyToMatch . ',' . $CTypeToMatch] = $value;
1019 }
1020 }
1021
1022 /**
1023 * Adds the $table tablename to the list of tables allowed to be includes by content element type "Insert records"
1024 * By using $content_table and $content_field you can also use the function for other tables.
1025 * FOR USE IN ext_tables.php FILES
1026 * Usage: 9
1027 *
1028 * @param string Table name to allow for "insert record"
1029 * @param string Table name TO WHICH the $table name is applied. See $content_field as well.
1030 * @param string Field name in the database $content_table in which $table is allowed to be added as a reference ("Insert Record")
1031 * @return void
1032 */
1033 public static function addToInsertRecords($table, $content_table = 'tt_content', $content_field = 'records') {
1034 global $TCA;
1035 t3lib_div::loadTCA($content_table);
1036 if (is_array($TCA[$content_table]['columns']) && isset($TCA[$content_table]['columns'][$content_field]['config']['allowed'])) {
1037 $TCA[$content_table]['columns'][$content_field]['config']['allowed'] .= ',' . $table;
1038 }
1039 }
1040
1041 /**
1042 * Add PlugIn to Static Template #43
1043 *
1044 * When adding a frontend plugin you will have to add both an entry to the TCA definition of tt_content table AND to the TypoScript template which must initiate the rendering.
1045 * Since the static template with uid 43 is the "content.default" and practically always used for rendering the content elements it's very useful to have this function automatically adding the necessary TypoScript for calling your plugin. It will also work for the extension "css_styled_content"
1046 * $type determines the type of frontend plugin:
1047 * "list_type" (default) - the good old "Insert plugin" entry
1048 * "menu_type" - a "Menu/Sitemap" entry
1049 * "splash_layout" - a "Textbox" entry
1050 * "CType" - a new content element type
1051 * "header_layout" - an additional header type (added to the selection of layout1-5)
1052 * "includeLib" - just includes the library for manual use somewhere in TypoScript.
1053 * (Remember that your $type definition should correspond to the column/items array in $TCA[tt_content] where you added the selector item for the element! See addPlugin() function)
1054 * FOR USE IN ext_localconf.php FILES
1055 * Usage: 2
1056 *
1057 * @param string $key is the extension key
1058 * @param string $classFile is the PHP-class filename relative to the extension root directory. If set to blank a default value is chosen according to convensions.
1059 * @param string $prefix is used as a - yes, suffix - of the class name (fx. "_pi1")
1060 * @param string $type, see description above
1061 * @param boolean If $cached is set as USER content object (cObject) is created - otherwise a USER_INT object is created.
1062 * @return void
1063 */
1064 public static function addPItoST43($key, $classFile = '', $prefix = '', $type = 'list_type', $cached = 0) {
1065 global $TYPO3_LOADED_EXT;
1066 $classFile = $classFile ? $classFile : 'pi/class.tx_' . str_replace('_', '', $key) . $prefix . '.php';
1067 $cN = self::getCN($key);
1068
1069 // General plugin:
1070 $pluginContent = trim('
1071 plugin.' . $cN . $prefix . ' = USER' . ($cached ? '' : '_INT') . '
1072 plugin.' . $cN . $prefix . ' {
1073 includeLibs = ' . $TYPO3_LOADED_EXT[$key]['siteRelPath'] . $classFile . '
1074 userFunc = ' . $cN . $prefix . '->main
1075 }');
1076 self::addTypoScript($key, 'setup', '
1077 # Setting ' . $key . ' plugin TypoScript
1078 ' . $pluginContent);
1079
1080 // After ST43:
1081 switch ($type) {
1082 case 'list_type':
1083 $addLine = 'tt_content.list.20.' . $key . $prefix . ' = < plugin.' . $cN . $prefix;
1084 break;
1085 case 'menu_type':
1086 $addLine = 'tt_content.menu.20.' . $key . $prefix . ' = < plugin.' . $cN . $prefix;
1087 break;
1088 case 'splash_layout':
1089 $addLine = 'tt_content.splash.' . $key . $prefix . ' = < plugin.' . $cN . $prefix;
1090 break;
1091 case 'CType':
1092 $addLine = trim('
1093 tt_content.' . $key . $prefix . ' = COA
1094 tt_content.' . $key . $prefix . ' {
1095 10 = < lib.stdheader
1096 20 = < plugin.' . $cN . $prefix . '
1097 }
1098 ');
1099 break;
1100 case 'header_layout':
1101 $addLine = 'lib.stdheader.10.' . $key . $prefix . ' = < plugin.' . $cN . $prefix;
1102 break;
1103 case 'includeLib':
1104 $addLine = 'page.1000 = < plugin.' . $cN . $prefix;
1105 break;
1106 default:
1107 $addLine = '';
1108 break;
1109 }
1110 if ($addLine) {
1111 self::addTypoScript($key, 'setup', '
1112 # Setting ' . $key . ' plugin TypoScript
1113 ' . $addLine . '
1114 ', 43);
1115 }
1116 }
1117
1118 /**
1119 * Call this method to add an entry in the static template list found in sys_templates
1120 * "static template files" are the modern equalent (provided from extensions) to the traditional records in "static_templates"
1121 * FOR USE IN ext_localconf.php FILES
1122 * Usage: 3
1123 *
1124 * @param string $extKey is of course the extension key
1125 * @param string $path is the path where the template files (fixed names) include_static.txt (integer list of uids from the table "static_templates"), constants.txt, setup.txt, editorcfg.txt, and include_static_file.txt is found (relative to extPath, eg. 'static/'). The file include_static_file.txt, allows you to include other static templates defined in files, from your static template, and thus corresponds to the field 'include_static_file' in the sys_template table. The syntax for this is a commaseperated list of static templates to include, like: EXT:css_styled_content/static/,EXT:da_newsletter_subscription/static/,EXT:cc_random_image/pi2/static/
1126 * @param string $title is the title in the selector box.
1127 * @return void
1128 * @see addTypoScript()
1129 */
1130 public static function addStaticFile($extKey, $path, $title) {
1131 global $TCA;
1132 t3lib_div::loadTCA('sys_template');
1133 if ($extKey && $path && is_array($TCA['sys_template']['columns'])) {
1134 $value = str_replace(',', '', 'EXT:' . $extKey . '/' . $path);
1135 $itemArray = array(trim($title . ' (' . $extKey . ')'), $value);
1136 $TCA['sys_template']['columns']['include_static_file']['config']['items'][] = $itemArray;
1137 }
1138 }
1139
1140 /**
1141 * Adds $content to the default TypoScript setup code as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_setup']
1142 * Prefixed with a [GLOBAL] line
1143 * FOR USE IN ext_localconf.php FILES
1144 * Usage: 6
1145 *
1146 * @param string TypoScript Setup string
1147 * @return void
1148 */
1149 public static function addTypoScriptSetup($content) {
1150 global $TYPO3_CONF_VARS;
1151 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup'] .= "\n[GLOBAL]\n" . $content;
1152 }
1153
1154 /**
1155 * Adds $content to the default TypoScript constants code as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_constants']
1156 * Prefixed with a [GLOBAL] line
1157 * FOR USE IN ext_localconf.php FILES
1158 * Usage: 0
1159 *
1160 * @param string TypoScript Constants string
1161 * @return void
1162 */
1163 public static function addTypoScriptConstants($content) {
1164 global $TYPO3_CONF_VARS;
1165 $TYPO3_CONF_VARS['FE']['defaultTypoScript_constants'] .= "\n[GLOBAL]\n" . $content;
1166 }
1167
1168 /**
1169 * Adds $content to the default TypoScript code for either setup, constants or editorcfg as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_*']
1170 * (Basically this function can do the same as addTypoScriptSetup and addTypoScriptConstants - just with a little more hazzle, but also with some more options!)
1171 * FOR USE IN ext_localconf.php FILES
1172 * Usage: 7
1173 *
1174 * @param string $key is the extension key (informative only).
1175 * @param string $type is either "setup", "constants" or "editorcfg" and obviously determines which kind of TypoScript code we are adding.
1176 * @param string $content is the TS content, prefixed with a [GLOBAL] line and a comment-header.
1177 * @param string $afterStaticUid is either an integer pointing to a uid of a static_template or a string pointing to the "key" of a static_file template ([reduced extension_key]/[local path]). The points is that the TypoScript you add is included only IF that static template is included (and in that case, right after). So effectively the TypoScript you set can specifically overrule settings from those static templates.
1178 * @return void
1179 */
1180 public static function addTypoScript($key, $type, $content, $afterStaticUid = 0) {
1181 global $TYPO3_CONF_VARS;
1182
1183 if ($type == 'setup' || $type == 'editorcfg' || $type == 'constants') {
1184 $content = '
1185
1186 [GLOBAL]
1187 #############################################
1188 ## TypoScript added by extension "' . $key . '"
1189 #############################################
1190
1191 ' . $content;
1192 if ($afterStaticUid) {
1193 $TYPO3_CONF_VARS['FE']['defaultTypoScript_' . $type . '.'][$afterStaticUid] .= $content;
1194 // If 'content (default)' is targeted, also add to other 'content rendering templates', eg. css_styled_content
1195 if ($afterStaticUid == 43 && is_array($TYPO3_CONF_VARS['FE']['contentRenderingTemplates'])) {
1196 foreach ($TYPO3_CONF_VARS['FE']['contentRenderingTemplates'] as $templateName) {
1197 $TYPO3_CONF_VARS['FE']['defaultTypoScript_' . $type . '.'][$templateName] .= $content;
1198 }
1199 }
1200 } else {
1201 $TYPO3_CONF_VARS['FE']['defaultTypoScript_' . $type] .= $content;
1202 }
1203 }
1204 }
1205
1206
1207 /**************************************
1208 *
1209 * INTERNAL EXTENSION MANAGEMENT:
1210 *
1211 ***************************************/
1212
1213 /**
1214 * Loading extensions configured in $TYPO3_CONF_VARS['EXT']['extList']
1215 *
1216 * CACHING ON: ($TYPO3_CONF_VARS['EXT']['extCache'] = 1 or 2)
1217 * If caching is enabled (and possible), the output will be $extensions['_CACHEFILE'] set to the cacheFilePrefix. Subsequently the cache files must be included then since those will eventually set up the extensions.
1218 * If cachefiles are not found they will be generated
1219 * CACHING OFF: ($TYPO3_CONF_VARS['EXT']['extCache'] = 0)
1220 * The returned value will be an array where each key is an extension key and the value is an array with filepaths for the extension.
1221 * This array will later be set in the global var $TYPO3_LOADED_EXT
1222 *
1223 * Usages of this function can be seen in config_default.php
1224 * Extensions are always detected in the order local - global - system.
1225 * Usage: 1
1226 *
1227 * @return array Extension Array
1228 * @internal
1229 */
1230 public static function typo3_loadExtensions() {
1231 global $TYPO3_CONF_VARS;
1232
1233 // Caching behaviour of ext_tables.php and ext_localconf.php files:
1234 $extensionCacheBehaviour = self::getExtensionCacheBehaviour();
1235 // Full list of extensions includes both required and extList:
1236 $rawExtList = self::getEnabledExtensionList();
1237
1238 // Empty array as a start.
1239 $extensions = array();
1240
1241 //
1242 if ($rawExtList) {
1243 // The cached File prefix.
1244 $cacheFilePrefix = self::getCacheFilePrefix();
1245
1246 // If cache files available, set cache file prefix and return:
1247 if ($extensionCacheBehaviour && self::isCacheFilesAvailable($cacheFilePrefix)) {
1248 // Return cache file prefix:
1249 $extensions['_CACHEFILE'] = $cacheFilePrefix;
1250 } else { // ... but if not, configure...
1251
1252 // Prepare reserved filenames:
1253 $files = array('ext_localconf.php', 'ext_tables.php', 'ext_tables.sql', 'ext_tables_static+adt.sql', 'ext_typoscript_constants.txt', 'ext_typoscript_editorcfg.txt', 'ext_typoscript_setup.txt');
1254 // Traverse extensions and check their existence:
1255 clearstatcache(); // Clear file state cache to make sure we get good results from is_dir()
1256 $temp_extensions = array_unique(t3lib_div::trimExplode(',', $rawExtList, 1));
1257 foreach ($temp_extensions as $temp_extKey) {
1258 // Check local, global and system locations:
1259 if (@is_dir(PATH_typo3conf . 'ext/' . $temp_extKey . '/')) {
1260 $extensions[$temp_extKey] = array('type' => 'L', 'siteRelPath' => 'typo3conf/ext/' . $temp_extKey . '/', 'typo3RelPath' => '../typo3conf/ext/' . $temp_extKey . '/');
1261 } elseif (@is_dir(PATH_typo3 . 'ext/' . $temp_extKey . '/')) {
1262 $extensions[$temp_extKey] = array('type' => 'G', 'siteRelPath' => TYPO3_mainDir . 'ext/' . $temp_extKey . '/', 'typo3RelPath' => 'ext/' . $temp_extKey . '/');
1263 } elseif (@is_dir(PATH_typo3 . 'sysext/' . $temp_extKey . '/')) {
1264 $extensions[$temp_extKey] = array('type' => 'S', 'siteRelPath' => TYPO3_mainDir . 'sysext/' . $temp_extKey . '/', 'typo3RelPath' => 'sysext/' . $temp_extKey . '/');
1265 }
1266
1267 // If extension was found, check for reserved filenames:
1268 if (isset($extensions[$temp_extKey])) {
1269 foreach ($files as $fName) {
1270 $temp_filename = PATH_site . $extensions[$temp_extKey]['siteRelPath'] . trim($fName);
1271 if (is_array($extensions[$temp_extKey]) && @is_file($temp_filename)) {
1272 $extensions[$temp_extKey][$fName] = $temp_filename;
1273 }
1274 }
1275 }
1276 }
1277 unset($extensions['_CACHEFILE']);
1278
1279 // write cache?
1280 if ($extensionCacheBehaviour &&
1281 @is_dir(PATH_typo3 . 'sysext/') &&
1282 @is_dir(PATH_typo3 . 'ext/')) { // Must also find global and system extension directories to exist, otherwise caching cannot be allowed (since it is most likely a temporary server problem). This might fix a rare, unrepeatable bug where global/system extensions are not loaded resulting in fatal errors if that is cached!
1283 $wrError = self::cannotCacheFilesWritable($cacheFilePrefix);
1284 if ($wrError) {
1285 $TYPO3_CONF_VARS['EXT']['extCache'] = 0;
1286 } else {
1287 // Write cache files:
1288 $extensions = self::writeCacheFiles($extensions, $cacheFilePrefix);
1289 }
1290 }
1291 }
1292 }
1293
1294 return $extensions;
1295 }
1296
1297 /**
1298 * Returns the section headers for the compiled cache-files.
1299 *
1300 * @param string $key is the extension key
1301 * @param string $file is the filename (only informative for comment)
1302 * @return string
1303 * @internal
1304 */
1305 public static function _makeIncludeHeader($key, $file) {
1306 return '<?php
1307 ###########################
1308 ## EXTENSION: ' . $key . '
1309 ## FILE: ' . $file . '
1310 ###########################
1311
1312 $_EXTKEY = \'' . $key . '\';
1313 $_EXTCONF = $TYPO3_CONF_VARS[\'EXT\'][\'extConf\'][$_EXTKEY];
1314
1315 ?>';
1316 }
1317
1318 /**
1319 * Returns true if both the localconf and tables cache file exists (with $cacheFilePrefix)
1320 * Usage: 2
1321 *
1322 * @param string Prefix of the cache file to check
1323 * @return boolean
1324 * @internal
1325 */
1326 public static function isCacheFilesAvailable($cacheFilePrefix) {
1327 return
1328 @is_file(PATH_typo3conf . $cacheFilePrefix . '_ext_localconf.php') &&
1329 @is_file(PATH_typo3conf . $cacheFilePrefix . '_ext_tables.php');
1330 }
1331
1332 /**
1333 * Returns true if the "localconf.php" file in "typo3conf/" is writable
1334 * Usage: 1
1335 *
1336 * @return boolean
1337 * @internal
1338 */
1339 public static function isLocalconfWritable() {
1340 return @is_writable(PATH_typo3conf) && @is_writable(PATH_typo3conf . 'localconf.php');
1341 }
1342
1343 /**
1344 * Returns an error string if typo3conf/ or cache-files with $cacheFilePrefix are NOT writable
1345 * Returns false if no problem.
1346 * Usage: 1
1347 *
1348 * @param string Prefix of the cache file to check
1349 * @return string
1350 * @internal
1351 */
1352 public static function cannotCacheFilesWritable($cacheFilePrefix) {
1353 $error = array();
1354 if (!@is_writable(PATH_typo3conf)) {
1355 $error[] = PATH_typo3conf;
1356 }
1357 if (@is_file(PATH_typo3conf . $cacheFilePrefix . '_ext_localconf.php') &&
1358 !@is_writable(PATH_typo3conf . $cacheFilePrefix . '_ext_localconf.php')) {
1359 $error[] = PATH_typo3conf . $cacheFilePrefix . '_ext_localconf.php';
1360 }
1361 if (@is_file(PATH_typo3conf . $cacheFilePrefix . '_ext_tables.php') &&
1362 !@is_writable(PATH_typo3conf . $cacheFilePrefix . '_ext_tables.php')) {
1363 $error[] = PATH_typo3conf . $cacheFilePrefix . '_ext_tables.php';
1364 }
1365 return implode(', ', $error);
1366 }
1367
1368 /**
1369 * Returns an array with the two cache-files (0=>localconf, 1=>tables) from typo3conf/ if they (both) exist. Otherwise false.
1370 * Evaluation relies on $TYPO3_LOADED_EXT['_CACHEFILE']
1371 * Usage: 2
1372 *
1373 * @param string $cacheFilePrefix Cache file prefix to be used (optional)
1374 * @return array
1375 * @internal
1376 */
1377 public static function currentCacheFiles($cacheFilePrefix = NULL) {
1378 if (is_null($cacheFilePrefix)) {
1379 $cacheFilePrefix = $GLOBALS['TYPO3_LOADED_EXT']['_CACHEFILE'];
1380 }
1381
1382 if ($cacheFilePrefix) {
1383 $cacheFilePrefixFE = str_replace('temp_CACHED', 'temp_CACHED_FE', $cacheFilePrefix);
1384 $files = array();
1385 if (self::isCacheFilesAvailable($cacheFilePrefix)) {
1386 $files[] = PATH_typo3conf . $cacheFilePrefix . '_ext_localconf.php';
1387 $files[] = PATH_typo3conf . $cacheFilePrefix . '_ext_tables.php';
1388 }
1389 if (self::isCacheFilesAvailable($cacheFilePrefixFE)) {
1390 $files[] = PATH_typo3conf . $cacheFilePrefixFE . '_ext_localconf.php';
1391 $files[] = PATH_typo3conf . $cacheFilePrefixFE . '_ext_tables.php';
1392 }
1393 if (!empty($files)) {
1394 return $files;
1395 }
1396 }
1397 }
1398
1399 /**
1400 * Compiles/Creates the two cache-files in typo3conf/ based on $cacheFilePrefix
1401 * Returns a array with the key "_CACHEFILE" set to the $cacheFilePrefix value
1402 * Usage: 1
1403 *
1404 * @param array Extension information array
1405 * @param string Prefix for the cache files
1406 * @return array
1407 * @internal
1408 */
1409 public static function writeCacheFiles($extensions, $cacheFilePrefix) {
1410 // Making cache files:
1411 $extensions['_CACHEFILE'] = $cacheFilePrefix;
1412 $cFiles = array();
1413 $cFiles['ext_localconf'] .= '<?php
1414
1415 $TYPO3_LOADED_EXT = unserialize(stripslashes(\'' . addslashes(serialize($extensions)) . '\'));
1416
1417 ?>';
1418
1419 foreach ($extensions as $key => $conf) {
1420 if (is_array($conf)) {
1421 if ($conf['ext_localconf.php']) {
1422 $cFiles['ext_localconf'] .= self::_makeIncludeHeader($key, $conf['ext_localconf.php']);
1423 $cFiles['ext_localconf'] .= trim(t3lib_div::getUrl($conf['ext_localconf.php']));
1424 }
1425 if ($conf['ext_tables.php']) {
1426 $cFiles['ext_tables'] .= self::_makeIncludeHeader($key, $conf['ext_tables.php']);
1427 $cFiles['ext_tables'] .= trim(t3lib_div::getUrl($conf['ext_tables.php']));
1428 }
1429 }
1430 }
1431
1432 $cFiles['ext_localconf'] = "<?php\n" . preg_replace('/<\?php|\?>/is', '', $cFiles['ext_localconf']) . "?>\n";
1433 $cFiles['ext_tables'] = "<?php\n" . preg_replace('/<\?php|\?>/is', '', $cFiles['ext_tables']) . "?>\n";
1434
1435 t3lib_div::writeFile(PATH_typo3conf . $cacheFilePrefix . '_ext_localconf.php', $cFiles['ext_localconf']);
1436 t3lib_div::writeFile(PATH_typo3conf . $cacheFilePrefix . '_ext_tables.php', $cFiles['ext_tables']);
1437
1438 $extensions = array();
1439 $extensions['_CACHEFILE'] = $cacheFilePrefix;
1440
1441 return $extensions;
1442 }
1443
1444 /**
1445 * Unlink (delete) cache files
1446 *
1447 * @param string $cacheFilePrefix Cache file prefix to be used (optional)
1448 * @return integer Number of deleted files.
1449 */
1450 public static function removeCacheFiles($cacheFilePrefix = NULL) {
1451 $cacheFiles = self::currentCacheFiles($cacheFilePrefix);
1452
1453 $out = 0;
1454 if (is_array($cacheFiles)) {
1455 reset($cacheFiles);
1456 foreach ($cacheFiles as $cfile) {
1457 @unlink($cfile);
1458 clearstatcache();
1459 $out++;
1460 }
1461 }
1462 return $out;
1463 }
1464
1465 /**
1466 * Gets the behaviour for caching ext_tables.php and ext_localconf.php files
1467 * (see $TYPO3_CONF_VARS['EXT']['extCache'] setting in the install tool).
1468 *
1469 * @param boolean $usePlainValue Whether to use the value as it is without modifications
1470 * @return integer
1471 */
1472 public static function getExtensionCacheBehaviour($usePlainValue = FALSE) {
1473 $extensionCacheBehaviour = intval($GLOBALS['TYPO3_CONF_VARS']['EXT']['extCache']);
1474
1475 // Caching of extensions is disabled when install tool is used:
1476 if (!$usePlainValue && defined('TYPO3_enterInstallScript') && TYPO3_enterInstallScript) {
1477 $extensionCacheBehaviour = 0;
1478 }
1479
1480 return $extensionCacheBehaviour;
1481 }
1482
1483 /**
1484 * Gets the prefix used for the ext_tables.php and ext_localconf.php cached files.
1485 *
1486 * @return string
1487 */
1488 public static function getCacheFilePrefix() {
1489 $extensionCacheBehaviour = self::getExtensionCacheBehaviour(TRUE);
1490
1491 $cacheFileSuffix = (TYPO3_MODE == 'FE' ? '_FE' : '');
1492 $cacheFilePrefix = 'temp_CACHED' . $cacheFileSuffix;
1493
1494 if ($extensionCacheBehaviour == 1) {
1495 $cacheFilePrefix .= '_ps' . substr(t3lib_div::shortMD5(PATH_site . '|' . $GLOBALS['TYPO_VERSION']), 0, 4);
1496 } elseif ($extensionCacheBehaviour == 2) {
1497 $cacheFilePrefix .= '_' . t3lib_div::shortMD5(self::getEnabledExtensionList());
1498 }
1499
1500 return $cacheFilePrefix;
1501 }
1502
1503 /**
1504 * Gets the list of enabled extensions for the accordant context (frontend or backend).
1505 *
1506 * @return string
1507 */
1508 public static function getEnabledExtensionList() {
1509 // Select mode how to load extensions in order to speed up the FE
1510 if (TYPO3_MODE == 'FE') {
1511 if (!($extLoadInContext = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList_FE'])) {
1512 // fall back to standard 'extList' if 'extList_FE' is not (yet) set
1513 $extLoadInContext = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'];
1514 }
1515 } else {
1516 $extLoadInContext = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'];
1517 }
1518
1519 $extensionList = $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] . ',' . $extLoadInContext;
1520
1521 return $extensionList;
1522 }
1523 }
1524
1525 ?>