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