Only changes to function indexes, comments and stripping of trailing whitespace in...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_extmgm.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (kasper@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 <kasper@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 113: class t3lib_extMgm
41 *
42 * SECTION: PATHS and other evaluation
43 * 129: function isLoaded($key,$exitOnError=0)
44 * 144: function extPath($key,$script='')
45 * 158: function extRelPath($key)
46 * 172: function siteRelPath($key)
47 * 183: function getCN($key)
48 *
49 * SECTION: Adding BACKEND features
50 * 215: function addTCAcolumns($table,$columnArray,$addTofeInterface=0)
51 * 238: function addToAllTCAtypes($table,$str,$specificTypesList='',$beforeList='')
52 * 289: function allowTableOnStandardPages($table)
53 * 305: function addModule($main,$sub='',$position='',$path='')
54 * 366: function insertModuleFunction($modname,$className,$classPath,$title,$MM_key='function')
55 * 383: function addPageTSConfig($content)
56 * 396: function addUserTSConfig($content)
57 * 410: function addLLrefForTCAdescr($tca_descr_key,$file_ref)
58 *
59 * SECTION: Adding SERVICES features
60 * 452: function addService($extKey, $serviceType, $serviceKey, $info)
61 * 517: function findService($serviceType, $serviceSubType='', $excludeServiceKeys='')
62 * 584: function deactivateService($serviceType, $serviceKey)
63 *
64 * SECTION: Adding FRONTEND features
65 * 623: function addPlugin($itemArray,$type='list_type')
66 * 647: function addPiFlexFormValue($piKeyToMatch,$value)
67 * 666: function addToInsertRecords($table,$content_table='tt_content',$content_field='records')
68 * 696: function addPItoST43($key,$classFile='',$prefix='',$type='list_type',$cached=0)
69 * 770: function addStaticFile($extKey,$path,$title)
70 * 788: function addTypoScriptSetup($content)
71 * 801: function addTypoScriptConstants($content)
72 * 817: function addTypoScript($key,$type,$content,$afterStaticUid=0)
73 *
74 * SECTION: INTERNAL EXTENSION MANAGEMENT:
75 * 879: function typo3_loadExtensions()
76 * 958: function _makeIncludeHeader($key,$file)
77 * 978: function isCacheFilesAvailable($cacheFilePrefix)
78 * 990: function isLocalconfWritable()
79 * 1002: function cannotCacheFilesWritable($cacheFilePrefix)
80 * 1025: function currentCacheFiles()
81 * 1047: function writeCacheFiles($extensions,$cacheFilePrefix)
82 *
83 * TOTAL FUNCTIONS: 31
84 * (This index is automatically created/updated by the extension "extdeveval")
85 *
86 */
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 /**
104 * Extension Management functions
105 *
106 * This class is never instantiated, rather the methods inside is called as functions like
107 * t3lib_extMgm::isLoaded('my_extension');
108 *
109 * @author Kasper Skaarhoj <kasper@typo3.com>
110 * @package TYPO3
111 * @subpackage t3lib
112 */
113 class t3lib_extMgm {
114
115
116 /**************************************
117 *
118 * PATHS and other evaluation
119 *
120 ***************************************/
121
122 /**
123 * Returns true if the extension with extension key $key is loaded.
124 *
125 * @param string Extension key to test
126 * @param boolean If $exitOnError is true and the extension is not loaded the function will die with an error message
127 * @return boolean
128 */
129 function isLoaded($key,$exitOnError=0) {
130 global $TYPO3_LOADED_EXT;
131 if ($exitOnError && !isset($TYPO3_LOADED_EXT[$key])) die('Fatal Error: Extension "'.$key.'" was not loaded.');
132 return isset($TYPO3_LOADED_EXT[$key]);
133 }
134
135 /**
136 * Returns the absolute path to the extension with extension key $key
137 * If the extension is not loaded the function will die with an error message
138 * Useful for internal fileoperations
139 *
140 * @param string Extension key
141 * @param string $script is appended to the output if set.
142 * @return string
143 */
144 function extPath($key,$script='') {
145 global $TYPO3_LOADED_EXT;
146 if (!isset($TYPO3_LOADED_EXT[$key])) die('TYPO3 Fatal Error: Extension key "'.$key.'" was NOT loaded!');
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 *
155 * @param string Extension key
156 * @return string
157 */
158 function extRelPath($key) {
159 global $TYPO3_LOADED_EXT;
160 if (!isset($TYPO3_LOADED_EXT[$key])) die('TYPO3 Fatal Error: Extension key "'.$key.'" was NOT loaded!');
161 return $TYPO3_LOADED_EXT[$key]['typo3RelPath'];
162 }
163
164 /**
165 * Returns the relative path to the extension as measured from the PATH_site (frontend)
166 * If the extension is not loaded the function will die with an error message
167 * Useful for images and links from the frontend
168 *
169 * @param string Extension key
170 * @return string
171 */
172 function siteRelPath($key) {
173 return substr(t3lib_extMgm::extPath($key),strlen(PATH_site));
174 }
175
176 /**
177 * Returns the correct class name prefix for the extension key $key
178 *
179 * @param string Extension key
180 * @return string
181 * @internal
182 */
183 function getCN($key) {
184 return substr($key,0,5)=='user_' ? 'user_'.str_replace('_','',substr($key,5)) : 'tx_'.str_replace('_','',$key);
185 }
186
187
188
189
190
191
192
193
194
195
196 /**************************************
197 *
198 * Adding BACKEND features
199 * (related to core features)
200 *
201 ***************************************/
202
203 /**
204 * Adding fields to an existing table definition in $TCA
205 * Adds an array with $TCA column-configuration to the $TCA-entry for that table.
206 * 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!
207 * So to have the fields displayed you must also call fx. addToAllTCAtypes or manually add the fields to the types list.
208 * FOR USE IN ext_tables.php FILES
209 *
210 * @param string $table is the table name of a table already present in $TCA with a columns section
211 * @param array $columnArray is the array with the additional columns (typical some fields an extension wants to add)
212 * @param boolean If $addTofeInterface is true the list of fields are also added to the fe_admin_fieldList.
213 * @return void
214 */
215 function addTCAcolumns($table,$columnArray,$addTofeInterface=0) {
216 global $TCA;
217 t3lib_div::loadTCA($table);
218 if (is_array($columnArray) && is_array($TCA[$table]) && is_array($TCA[$table]['columns'])) {
219 $TCA[$table]['columns'] = array_merge($TCA[$table]['columns'],$columnArray); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
220 if ($addTofeInterface) $TCA[$table]['feInterface']['fe_admin_fieldList'].=','.implode(',',array_keys($columnArray));
221 }
222 }
223
224 /**
225 * Makes fields visible on the form, adding them to the end.
226 *
227 * Adds a string $str (comma list of field names) to all ["types"][xxx]["showitem"] entries for table $table (unless limited by $specificTypesList)
228 * This is needed to have new fields shown automatically in the TCEFORMS of a record from $table.
229 * Typically this function is called after having added new columns (database fields) with the addTCAcolumns function
230 * FOR USE IN ext_tables.php FILES
231 *
232 * @param string Table name
233 * @param string Field list to add.
234 * @param string List of specific types to add the field list to. (If empty, all type entries are affected)
235 * @param string Insert fields before one of this fields (commalist). Example: "keywords,--palette--;;4,description". Palettes must be passed like in the example no matter how the palette definition looks like in TCA.
236 * @return void
237 */
238 function addToAllTCAtypes($table,$str,$specificTypesList='',$beforeList='') {
239 global $TCA;
240
241 $beforeFields=t3lib_div::trimExplode(',',$beforeList,1);
242 $before=count($beforeFields);
243
244 t3lib_div::loadTCA($table);
245 if (trim($str) && is_array($TCA[$table]) && is_array($TCA[$table]['types'])) {
246 foreach($TCA[$table]['types'] as $k => $v) {
247 if (!$specificTypesList || t3lib_div::inList($specificTypesList,$k)) {
248
249 if ($before) {
250 $append=true;
251 $showItem = t3lib_div::trimExplode(',',$TCA[$table]['types'][$k]['showitem'],1);
252 foreach($showItem as $key => $fieldInfo) {
253
254 $parts = explode(';',$fieldInfo);
255 $theField = trim($parts[0]);
256 $palette = trim($parts[0]).';;'.trim($parts[2]);
257 // find exact field name or palette with number
258 if (in_array($theField,$beforeFields) OR in_array($palette,$beforeFields)) {
259 $showItem[$key]=trim($str).', '.$fieldInfo;
260 $append=false;
261 break;
262 }
263 }
264
265 // Not found? Then append.
266 if($append) {
267 $showItem[]=trim($str);
268 }
269
270 $TCA[$table]['types'][$k]['showitem']=implode(', ', $showItem);
271
272 } else {
273 $TCA[$table]['types'][$k]['showitem'].=', '.trim($str);
274 }
275 }
276 }
277 }
278 }
279
280
281 /**
282 * Add tablename to default list of allowed tables on pages.
283 * Will add the $table to the list of tables allowed by default on pages as setup by $PAGES_TYPES['default']['allowedTables']
284 * FOR USE IN ext_tables.php FILES
285 *
286 * @param string Table name
287 * @return void
288 */
289 function allowTableOnStandardPages($table) {
290 global $PAGES_TYPES;
291
292 $PAGES_TYPES['default']['allowedTables'].=','.$table;
293 }
294
295 /**
296 * Adds a module (main or sub) to the backend interface
297 * FOR USE IN ext_tables.php FILES
298 *
299 * @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.
300 * @param string $sub is the submodule key. If $sub is not set a blank $main module is created.
301 * @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.
302 * @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.
303 * @return void
304 */
305 function addModule($main,$sub='',$position='',$path='') {
306 global $TBE_MODULES;
307
308 if (isset($TBE_MODULES[$main]) && $sub) { // If there is already a main module by this name:
309
310 // Adding the submodule to the correct position:
311 list($place,$modRef)=t3lib_div::trimExplode(':',$position,1);
312 $mods = t3lib_div::trimExplode(',',$TBE_MODULES[$main],1);
313 if (!in_array($sub,$mods)) {
314 switch(strtolower($place)) {
315 case 'after':
316 case 'before':
317 $pointer=0;
318 reset($mods);
319 while(list($k,$m)=each($mods)) {
320 if (!strcmp($m,$modRef)) {
321 $pointer=strtolower($place)=='after'?$k+1:$k;
322 }
323 }
324 array_splice(
325 $mods, // The modules array
326 $pointer, // To insert one position from the end of the list
327 0, // Don't remove any items, just insert
328 $sub // Module to insert
329 );
330 break;
331 default:
332 if (strtolower($place)=='top') {
333 array_unshift($mods,$sub);
334 } else {
335 array_push($mods,$sub);
336 }
337 break;
338 }
339 }
340 // Re-inserting the submodule list:
341 $TBE_MODULES[$main]=implode(',',$mods);
342 } else { // Create new main modules with only one submodule, $sub (or none if $sub is blank)
343 $TBE_MODULES[$main]=$sub;
344 }
345
346 // Adding path:
347 if ($path) {
348 $TBE_MODULES['_PATHS'][$main.($sub?'_'.$sub:'')]=$path;
349 }
350 }
351
352 /**
353 * Adds a "Function menu module" ('third level module') to an existing function menu for some other backend module
354 * The arguments values are generally determined by which function menu this is supposed to interact with
355 * See Inside TYPO3 for information on how to use this function.
356 * FOR USE IN ext_tables.php FILES
357 *
358 * @param string Module name
359 * @param string Class name
360 * @param string Class path
361 * @param string Title of module
362 * @param string Menu array key - default is "function"
363 * @return void
364 * @see t3lib_SCbase::mergeExternalItems()
365 */
366 function insertModuleFunction($modname,$className,$classPath,$title,$MM_key='function') {
367 global $TBE_MODULES_EXT;
368 $TBE_MODULES_EXT[$modname]['MOD_MENU'][$MM_key][$className]=array(
369 'name' => $className,
370 'path' => $classPath,
371 'title' => $title,
372 );
373 }
374
375 /**
376 * Adds $content to the default Page TSconfig as set in $TYPO3_CONF_VARS[BE]['defaultPageTSconfig']
377 * Prefixed with a [GLOBAL] line
378 * FOR USE IN ext_tables.php/ext_locallang.php FILES
379 *
380 * @param string Page TSconfig content
381 * @return void
382 */
383 function addPageTSConfig($content) {
384 global $TYPO3_CONF_VARS;
385 $TYPO3_CONF_VARS['BE']['defaultPageTSconfig'].="\n[GLOBAL]\n".$content;
386 }
387
388 /**
389 * Adds $content to the default User TSconfig as set in $TYPO3_CONF_VARS[BE]['defaultUserTSconfig']
390 * Prefixed with a [GLOBAL] line
391 * FOR USE IN ext_tables.php/ext_locallang.php FILES
392 *
393 * @param string User TSconfig content
394 * @return void
395 */
396 function addUserTSConfig($content) {
397 global $TYPO3_CONF_VARS;
398 $TYPO3_CONF_VARS['BE']['defaultUserTSconfig'].="\n[GLOBAL]\n".$content;
399 }
400
401 /**
402 * Adds a reference to a locallang file with TCA_DESCR labels
403 * FOR USE IN ext_tables.php FILES
404 * eg. t3lib_extMgm::addLLrefForTCAdescr('pages','EXT:lang/locallang_csh_pages.php'); for the pages table or t3lib_extMgm::addLLrefForTCAdescr('_MOD_web_layout','EXT:cms/locallang_csh_weblayout.php'); for the Web > Page module.
405 *
406 * @param string Description key. Typically a database table (like "pages") but for applications can be other strings, but prefixed with "_MOD_")
407 * @param string File reference to locallang file, eg. "EXT:lang/locallang_csh_pages.php"
408 * @return void
409 */
410 function addLLrefForTCAdescr($tca_descr_key,$file_ref) {
411 global $TCA_DESCR;
412 if ($tca_descr_key) {
413 if (!is_array($TCA_DESCR[$tca_descr_key])) {
414 $TCA_DESCR[$tca_descr_key]=array();
415 }
416 if (!is_array($TCA_DESCR[$tca_descr_key]['refs'])) {
417 $TCA_DESCR[$tca_descr_key]['refs']=array();
418 }
419 $TCA_DESCR[$tca_descr_key]['refs'][]=$file_ref;
420 }
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434 /**************************************
435 *
436 * Adding SERVICES features
437 *
438 * @author René Fritz <r.fritz@colorcube.de>
439 *
440 ***************************************/
441
442 /**
443 * Adds a service to the global services array
444 *
445 * @param string Extension key
446 * @param string Service type
447 * @param string Service key
448 * @param array Service description array
449 * @return void
450 * @author René Fritz <r.fritz@colorcube.de>
451 */
452 function addService($extKey, $serviceType, $serviceKey, $info) {
453 global $T3_SERVICES,$TYPO3_CONF_VARS;
454
455 // even not available services will be included to make it possible to give the admin a feedback of non-available services.
456 // but maybe it's better to move non-available services to a different array??
457
458 if ($serviceType AND substr($serviceType,0,3)!='tx_' AND substr($serviceKey,0,3)=='tx_' AND is_array($info)) {
459
460 $info['priority'] = max(0,min(100,$info['priority']));
461
462 $T3_SERVICES[$serviceType][$serviceKey]=$info;
463
464 $T3_SERVICES[$serviceType][$serviceKey]['extKey'] = $extKey;
465 $T3_SERVICES[$serviceType][$serviceKey]['serviceKey'] = $serviceKey;
466 $T3_SERVICES[$serviceType][$serviceKey]['serviceType'] = $serviceType;
467
468
469 // mapping a service key to a service type
470 // all service keys begin with tx_ - service types don't
471 // this way a selection of a special service key as service type is easy
472 $T3_SERVICES[$serviceKey][$serviceKey] = &$T3_SERVICES[$serviceType][$serviceKey];
473
474
475 // change the priority (and other values) from TYPO3_CONF_VARS
476 // $TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey]['priority']
477 // even the activation is possible (a unix service might be possible on windows for some reasons)
478 if (is_array($TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey])) {
479
480 // no check is done here - there might be configuration values only the service type knows about, so we pass everything
481 $T3_SERVICES[$serviceType][$serviceKey] = array_merge ($T3_SERVICES[$serviceType][$serviceKey],$TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey]);
482 }
483
484
485 // OS check
486 // empty $os means 'not limited to one OS', therefore a check is not needed
487 if ($T3_SERVICES[$serviceType][$serviceKey]['available'] AND $T3_SERVICES[$serviceType][$serviceKey]['os']!='') {
488
489 // TYPO3_OS is not yet defined
490 $os_type = stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'UNIX';
491
492 $os = t3lib_div::trimExplode(',',strtoupper($T3_SERVICES[$serviceType][$serviceKey]['os']));
493
494 if (!in_array($os_type,$os)) {
495 t3lib_extMgm::deactivateService($serviceType, $serviceKey);
496 }
497 }
498
499 // convert subtype list to array for quicker access
500 $T3_SERVICES[$serviceType][$serviceKey]['serviceSubTypes'] = array();
501 $serviceSubTypes = t3lib_div::trimExplode(',',$info['subtype']);
502 foreach ($serviceSubTypes as $subtype) {
503 $T3_SERVICES[$serviceType][$serviceKey]['serviceSubTypes'][$subtype] = $subtype;
504 }
505 }
506 }
507
508 /**
509 * Find the available service with highest priority
510 *
511 * @param string Service type
512 * @param string Service sub type
513 * @param string Service key that should be excluded in the search for a service
514 * @return mixed Service info array if a service was found, FLASE otherwise
515 * @author René Fritz <r.fritz@colorcube.de>
516 */
517 function findService($serviceType, $serviceSubType='', $excludeServiceKeys='') {
518 global $T3_SERVICES;
519
520 $serviceKey = FALSE;
521 $serviceInfo = FALSE;
522 $priority = 0;
523 $quality = 0;
524
525 if (is_array($T3_SERVICES[$serviceType])) {
526 foreach($T3_SERVICES[$serviceType] as $key => $info) {
527
528 if (t3lib_div::inList($excludeServiceKeys,$key)) {
529 continue;
530 }
531
532 // select a subtype randomly
533 // usefull to start a service by service key without knowing his subtypes - for testing purposes
534 if ($serviceSubType=='*') {
535 $serviceSubType = key($info['serviceSubTypes']);
536 }
537
538 // this matches empty subtype too
539 if( $info['available'] AND ($info['subtype']==$serviceSubType XOR $info['serviceSubTypes'][$serviceSubType]) AND $info['priority']>=$priority ) {
540
541 // has a lower quality than the already found, therefore we skip this service
542 if($info['priority']==$priority AND $info['quality']<$quality) {
543 continue;
544 }
545
546 // service depends on external programs - check if they exists
547 if(trim($info['exec'])) {
548 require_once(PATH_t3lib.'class.t3lib_exec.php');
549
550 $executables = t3lib_div::trimExplode(',', $info['exec'],1);
551 foreach($executables as $executable) {
552 if(!t3lib_exec::checkCommand($executable)) {
553 t3lib_extMgm::deactivateService($serviceType, $key);
554 $info['available']=FALSE;
555 break;
556 }
557 }
558 }
559
560 // still available after exec check?
561 if($info['available']) {
562 $serviceKey = $key;
563 $priority = $info['priority'];
564 $quality = $info['quality'];
565 }
566 }
567 }
568 }
569
570 if ($serviceKey) {
571 $serviceInfo = $T3_SERVICES[$serviceType][$serviceKey];
572 }
573 return $serviceInfo;
574 }
575
576 /**
577 * Deactivate a service
578 *
579 * @param string Service type
580 * @param string Service key
581 * @return void
582 * @author René Fritz <r.fritz@colorcube.de>
583 */
584 function deactivateService($serviceType, $serviceKey) {
585 global $T3_SERVICES;
586
587 // ... maybe it's better to move non-available services to a different array??
588 $T3_SERVICES[$serviceType][$serviceKey]['available'] = FALSE;
589 }
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604 /**************************************
605 *
606 * Adding FRONTEND features
607 * (related specifically to "cms" extension)
608 *
609 ***************************************/
610
611 /**
612 * Adds an entry to the list of plugins in content elements of type "Insert plugin"
613 *
614 * 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)
615 * 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.
616 * 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
617 * FOR USE IN ext_tables.php FILES
618 *
619 * @param array Item Array
620 * @param string Type (eg. "list_type") - basically a field from "tt_content" table
621 * @return void
622 */
623 function addPlugin($itemArray,$type='list_type') {
624 global $TCA;
625 t3lib_div::loadTCA('tt_content');
626 if (is_array($TCA['tt_content']['columns']) && is_array($TCA['tt_content']['columns'][$type]['config']['items'])) {
627 reset($TCA['tt_content']['columns'][$type]['config']['items']);
628 while(list($k,$v)=each($TCA['tt_content']['columns'][$type]['config']['items'])) {
629 if (!strcmp($v[1],$itemArray[1])) {
630 $TCA['tt_content']['columns'][$type]['config']['items'][$k]=$itemArray;
631 return;
632 }
633 }
634 $TCA['tt_content']['columns'][$type]['config']['items'][]=$itemArray;
635 }
636 }
637
638 /**
639 * Adds an entry to the "ds" array of the tt_content field "pi_flexform".
640 * This is used by plugins to add a flexform XML reference / content for use when they are selected as plugin.
641 *
642 * @param string The same value as the key for the plugin
643 * @param string Either a reference to a flex-form XML file (eg. "FILE:EXT:newloginbox/flexform_ds.xml") or the XML directly.
644 * @return void
645 * @see addPlugin()
646 */
647 function addPiFlexFormValue($piKeyToMatch,$value) {
648 global $TCA;
649 t3lib_div::loadTCA('tt_content');
650
651 if (is_array($TCA['tt_content']['columns']) && is_array($TCA['tt_content']['columns']['pi_flexform']['config']['ds'])) {
652 $TCA['tt_content']['columns']['pi_flexform']['config']['ds'][$piKeyToMatch] = $value;
653 }
654 }
655
656 /**
657 * Adds the $table tablename to the list of tables allowed to be includes by content element type "Insert records"
658 * By using $content_table and $content_field you can also use the function for other tables.
659 * FOR USE IN ext_tables.php FILES
660 *
661 * @param string Table name to allow for "insert record"
662 * @param string Table name TO WHICH the $table name is applied. See $content_field as well.
663 * @param string Field name in the database $content_table in which $table is allowed to be added as a reference ("Insert Record")
664 * @return void
665 */
666 function addToInsertRecords($table,$content_table='tt_content',$content_field='records') {
667 global $TCA;
668 t3lib_div::loadTCA($content_table);
669 if (is_array($TCA[$content_table]['columns']) && isset($TCA[$content_table]['columns'][$content_field]['config']['allowed'])) {
670 $TCA[$content_table]['columns'][$content_field]['config']['allowed'].=','.$table;
671 }
672 }
673
674 /**
675 * Add PlugIn to Static Template #43
676 *
677 * 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.
678 * 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.
679 * $type determines the type of frontend plugin:
680 * "list_type" (default) - the good old "Insert plugin" entry
681 * "menu_type" - a "Menu/Sitemap" entry
682 * "splash_layout" - a "Textbox" entry
683 * "CType" - a new content element type
684 * "header_layout" - an additional header type (added to the selection of layout1-5)
685 * "includeLib" - just includes the library for manual use somewhere in TypoScript.
686 * (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)
687 * FOR USE IN ext_locallang.php FILES
688 *
689 * @param string $key is the extension key
690 * @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.
691 * @param string $prefix is used as a - yes, suffix - of the class name (fx. "_pi1")
692 * @param string $type, see description above
693 * @param boolean If $cached is set as USER content object (cObject) is created - otherwise a USER_INT object is created.
694 * @return void
695 */
696 function addPItoST43($key,$classFile='',$prefix='',$type='list_type',$cached=0) {
697 global $TYPO3_LOADED_EXT;
698 $classFile = $classFile ? $classFile : 'pi/class.tx_'.str_replace('_','',$key).$prefix.'.php';
699 $cN = t3lib_extMgm::getCN($key);
700
701 // General plugin:
702 if ($cached) {
703 $pluginContent = trim('
704 includeLibs.'.$cN.$prefix.' = '.$TYPO3_LOADED_EXT[$key]['siteRelPath'].$classFile.'
705 plugin.'.$cN.$prefix.' = USER
706 plugin.'.$cN.$prefix.' {
707 userFunc = '.$cN.$prefix.'->main
708 }');
709 } else {
710 $pluginContent = trim('
711 plugin.'.$cN.$prefix.' = USER_INT
712 plugin.'.$cN.$prefix.' {
713 includeLibs = '.$TYPO3_LOADED_EXT[$key]['siteRelPath'].$classFile.'
714 userFunc = '.$cN.$prefix.'->main
715 }');
716 }
717 t3lib_extMgm::addTypoScript($key,'setup','
718 # Setting '.$key.' plugin TypoScript
719 '.$pluginContent);
720
721 // After ST43:
722 switch($type) {
723 case 'list_type':
724 $addLine = 'tt_content.list.20.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
725 break;
726 case 'menu_type':
727 $addLine = 'tt_content.menu.20.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
728 break;
729 case 'splash_layout':
730 $addLine = 'tt_content.splash.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
731 break;
732 case 'CType':
733 $addLine = trim('
734 tt_content.'.$key.$prefix.' = COA
735 tt_content.'.$key.$prefix.' {
736 10 = < lib.stdheader
737 20 = < plugin.'.$cN.$prefix.'
738 }
739 ');
740 break;
741 case 'header_layout':
742 $addLine = 'lib.stdheader.10.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
743 break;
744 case 'includeLib':
745 $addLine = 'page.1000 = < plugin.'.$cN.$prefix;
746 break;
747 default:
748 $addLine = '';
749 break;
750 }
751 if ($addLine) {
752 t3lib_extMgm::addTypoScript($key,'setup','
753 # Setting '.$key.' plugin TypoScript
754 '.$addLine.'
755 ',43);
756 }
757 }
758
759 /**
760 * Call this method to add an entry in the static template list found in sys_templates
761 * "static template files" are the modern equalent (provided from extensions) to the traditional records in "static_templates"
762 * FOR USE IN ext_locallang.php FILES
763 *
764 * @param string $extKey is of course the extension key
765 * @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 and editorcfg.txt is found (relative to extPath, eg. 'static/')
766 * @param string $title is the title in the selector box.
767 * @return void
768 * @see addTypoScript()
769 */
770 function addStaticFile($extKey,$path,$title) {
771 global $TCA;
772 t3lib_div::loadTCA('sys_template');
773 if ($extKey && $path && is_array($TCA['sys_template']['columns'])) {
774 $value = str_replace(',','','EXT:'.$extKey.'/'.$path);
775 $itemArray=array(trim($title.' ('.$extKey.')'),$value);
776 $TCA['sys_template']['columns']['include_static_file']['config']['items'][]=$itemArray;
777 }
778 }
779
780 /**
781 * Adds $content to the default TypoScript setup code as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_setup']
782 * Prefixed with a [GLOBAL] line
783 * FOR USE IN ext_locallang.php FILES
784 *
785 * @param string TypoScript Setup string
786 * @return void
787 */
788 function addTypoScriptSetup($content) {
789 global $TYPO3_CONF_VARS;
790 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup'].="\n[GLOBAL]\n".$content;
791 }
792
793 /**
794 * Adds $content to the default TypoScript constants code as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_constants']
795 * Prefixed with a [GLOBAL] line
796 * FOR USE IN ext_locallang.php FILES
797 *
798 * @param string TypoScript Constants string
799 * @return void
800 */
801 function addTypoScriptConstants($content) {
802 global $TYPO3_CONF_VARS;
803 $TYPO3_CONF_VARS['FE']['defaultTypoScript_constants'].="\n[GLOBAL]\n".$content;
804 }
805
806 /**
807 * Adds $content to the default TypoScript code for either setup, constants or editorcfg as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_*']
808 * (Basically this function can do the same as addTypoScriptSetup and addTypoScriptConstants - just with a little more hazzle, but also with some more options!)
809 * FOR USE IN ext_locallang.php FILES
810 *
811 * @param string $key is the extension key (informative only).
812 * @param string $type is either "setup", "constants" or "editorcfg" and obviously determines which kind of TypoScript code we are adding.
813 * @param string $content is the TS content, prefixed with a [GLOBAL] line and a comment-header.
814 * @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.
815 * @return void
816 */
817 function addTypoScript($key,$type,$content,$afterStaticUid=0) {
818 global $TYPO3_CONF_VARS;
819
820 if ($type=='setup' || $type=='editorcfg' || $type=='constants') {
821 $content = '
822
823 [GLOBAL]
824 #############################################
825 ## TypoScript added by extension "'.$key.'"
826 #############################################
827
828 '.$content;
829 if ($afterStaticUid) {
830 $TYPO3_CONF_VARS['FE']['defaultTypoScript_'.$type.'.'][$afterStaticUid].=$content;
831 if ($afterStaticUid==43) { // If 'content (default)' is targeted, also add to other 'content rendering templates', eg. css_styled_content
832 $TYPO3_CONF_VARS['FE']['defaultTypoScript_'.$type.'.']['cssstyledcontent/static/'].=$content;
833 }
834 } else {
835 $TYPO3_CONF_VARS['FE']['defaultTypoScript_'.$type].=$content;
836 }
837 }
838 }
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857 /**************************************
858 *
859 * INTERNAL EXTENSION MANAGEMENT:
860 *
861 ***************************************/
862
863 /**
864 * Loading extensions configured in $TYPO3_CONF_VARS['EXT']['extList']
865 *
866 * CACHING ON: ($TYPO3_CONF_VARS['EXT']['extCache'] = 1 or 2)
867 * 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.
868 * If cachefiles are not found they will be generated
869 * CACHING OFF: ($TYPO3_CONF_VARS['EXT']['extCache'] = 0)
870 * 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.
871 * This array will later be set in the global var $TYPO3_LOADED_EXT
872 *
873 * Usages of this function can be see in config_default.php
874 * Extensions are always detected in the order local - global - system.
875 *
876 * @return array Extension Array
877 * @internal
878 */
879 function typo3_loadExtensions() {
880 global $TYPO3_CONF_VARS;
881
882 // Full list of extensions includes both required and extList:
883 $rawExtList = $TYPO3_CONF_VARS['EXT']['requiredExt'].','.$TYPO3_CONF_VARS['EXT']['extList'];
884
885 // Empty array as a start.
886 $extensions = array();
887
888 //
889 if ($rawExtList) {
890 // The cached File prefix.
891 $cacheFilePrefix = 'temp_CACHED';
892 // Setting the name for the cache files:
893 if (intval($TYPO3_CONF_VARS['EXT']['extCache'])==1) $cacheFilePrefix.= '_ps'.substr(t3lib_div::shortMD5(PATH_site.'|'.$GLOBALS['TYPO_VERSION']),0,4);
894 if (intval($TYPO3_CONF_VARS['EXT']['extCache'])==2) $cacheFilePrefix.= '_'.t3lib_div::shortMD5($rawExtList);
895
896 // If cache files available, set cache file prefix and return:
897 if ($TYPO3_CONF_VARS['EXT']['extCache'] && t3lib_extMgm::isCacheFilesAvailable($cacheFilePrefix)) {
898 // Return cache file prefix:
899 $extensions['_CACHEFILE'] = $cacheFilePrefix;
900 } else {
901 // ... but if not, configure...
902 $temp_extensions = array_unique(t3lib_div::trimExplode(',',$rawExtList,1));
903 while(list(,$temp_extKey)=each($temp_extensions)) {
904 if (@is_dir(PATH_site.'typo3conf/ext/'.$temp_extKey)) {
905 $extensions[$temp_extKey]=array('type'=>'L','siteRelPath'=>'typo3conf/ext/'.$temp_extKey.'/','typo3RelPath'=>'../typo3conf/ext/'.$temp_extKey.'/');
906 } elseif (@is_dir(PATH_site.TYPO3_mainDir.'ext/'.$temp_extKey)) {
907 $extensions[$temp_extKey]=array('type'=>'G','siteRelPath'=>TYPO3_mainDir.'ext/'.$temp_extKey.'/','typo3RelPath'=>'ext/'.$temp_extKey.'/');
908 } elseif (@is_dir(PATH_site.TYPO3_mainDir.'sysext/'.$temp_extKey)) {
909 $extensions[$temp_extKey]=array('type'=>'S','siteRelPath'=>TYPO3_mainDir.'sysext/'.$temp_extKey.'/','typo3RelPath'=>'sysext/'.$temp_extKey.'/');
910 }
911
912 $files = t3lib_div::trimExplode(',','
913 ext_localconf.php,
914 ext_tables.php,
915 ext_tables.sql,
916 ext_tables_static+adt.sql,
917 ext_typoscript_constants.txt,
918 ext_typoscript_editorcfg.txt,
919 ext_typoscript_setup.txt
920 ',1);
921 reset($files);
922 while(list(,$fName)=each($files)) {
923 $temp_filename = PATH_site.$extensions[$temp_extKey]['siteRelPath'].trim($fName);
924 if (is_array($extensions[$temp_extKey]) && @is_file($temp_filename)) {
925 $extensions[$temp_extKey][$fName]=$temp_filename;
926 }
927 }
928 }
929 unset($extensions['_CACHEFILE']);
930
931
932 // write cache?
933 if ($TYPO3_CONF_VARS['EXT']['extCache']) {
934 $wrError = t3lib_extMgm::cannotCacheFilesWritable($cacheFilePrefix);
935 if ($wrError) {
936 //debug('Cannot write cache files: '.$wrError.'. Disabling the cache...');
937 $TYPO3_CONF_VARS['EXT']['extCache']=0;
938 } else {
939 // Write cache files:
940 $extensions = t3lib_extMgm::writeCacheFiles($extensions,$cacheFilePrefix);
941 }
942 }
943 }
944 }
945
946 #debug($extensions);
947 return $extensions;
948 }
949
950 /**
951 * Returns the section headers for the compiled cache-files.
952 *
953 * @param string $key is the extension key
954 * @param string $file is the filename (only informative for comment)
955 * @return string
956 * @internal
957 */
958 function _makeIncludeHeader($key,$file) {
959 return '<?php
960 ###########################
961 ## EXTENSION: '.$key.'
962 ## FILE: '.$file.'
963 ###########################
964
965 $_EXTKEY = \''.$key.'\';
966 $_EXTCONF = $TYPO3_CONF_VARS[\'EXT\'][\'extConf\'][$_EXTKEY];
967
968 ?>';
969 }
970
971 /**
972 * Returns true if both the localconf and tables cache file exists (with $cacheFilePrefix)
973 *
974 * @param string Prefix of the cache file to check
975 * @return boolean
976 * @internal
977 */
978 function isCacheFilesAvailable($cacheFilePrefix) {
979 return
980 @is_file(PATH_typo3conf.$cacheFilePrefix.'_ext_localconf.php') &&
981 @is_file(PATH_typo3conf.$cacheFilePrefix.'_ext_tables.php');
982 }
983
984 /**
985 * Returns true if the "localconf.php" file in "typo3conf/" is writable
986 *
987 * @return boolean
988 * @internal
989 */
990 function isLocalconfWritable() {
991 return is_writeable(PATH_typo3conf) && is_writeable(PATH_typo3conf.'localconf.php');
992 }
993
994 /**
995 * Returns an error string if typo3conf/ or cache-files with $cacheFilePrefix are NOT writable
996 * Returns false if no problem.
997 *
998 * @param string Prefix of the cache file to check
999 * @return string
1000 * @internal
1001 */
1002 function cannotCacheFilesWritable($cacheFilePrefix) {
1003 $error=array();
1004 if (!@is_writeable(PATH_typo3conf)) {
1005 $error[]=PATH_typo3conf;
1006 }
1007 if (@is_file(PATH_typo3conf.$cacheFilePrefix.'_ext_localconf.php') &&
1008 !@is_writeable(PATH_typo3conf.$cacheFilePrefix.'_ext_localconf.php')) {
1009 $error[]=PATH_typo3conf.$cacheFilePrefix.'_ext_localconf.php';
1010 }
1011 if (@is_file(PATH_typo3conf.$cacheFilePrefix.'_ext_tables.php') &&
1012 !@is_writeable(PATH_typo3conf.$cacheFilePrefix.'_ext_tables.php')) {
1013 $error[]=PATH_typo3conf.$cacheFilePrefix.'_ext_tables.php';
1014 }
1015 return implode(', ',$error);
1016 }
1017
1018 /**
1019 * Returns an array with the two cache-files (0=>localconf, 1=>tables) from typo3conf/ if they (both) exist. Otherwise false.
1020 * Evaluation relies on $TYPO3_LOADED_EXT['_CACHEFILE']
1021 *
1022 * @return array
1023 * @internal
1024 */
1025 function currentCacheFiles() {
1026 global $TYPO3_LOADED_EXT;
1027
1028 if ($TYPO3_LOADED_EXT['_CACHEFILE']) {
1029 if (t3lib_extMgm::isCacheFilesAvailable($TYPO3_LOADED_EXT['_CACHEFILE'])) {
1030 return array(
1031 PATH_typo3conf.$TYPO3_LOADED_EXT['_CACHEFILE'].'_ext_localconf.php',
1032 PATH_typo3conf.$TYPO3_LOADED_EXT['_CACHEFILE'].'_ext_tables.php'
1033 );
1034 }
1035 }
1036 }
1037
1038 /**
1039 * Compiles/Creates the two cache-files in typo3conf/ based on $cacheFilePrefix
1040 * Returns a array with the key "_CACHEFILE" set to the $cacheFilePrefix value
1041 *
1042 * @param array Extension information array
1043 * @param string Prefix for the cache files
1044 * @return array
1045 * @internal
1046 */
1047 function writeCacheFiles($extensions,$cacheFilePrefix) {
1048 // Making cache files:
1049 $extensions['_CACHEFILE'] = $cacheFilePrefix;
1050 $cFiles=array();
1051 $cFiles['ext_localconf'].='<?php
1052
1053 $TYPO3_LOADED_EXT = unserialize(stripslashes(\''.addslashes(serialize($extensions)).'\'));
1054
1055 ?>';
1056
1057 reset($extensions);
1058 while(list($key,$conf)=each($extensions)) {
1059 if (is_array($conf)) {
1060 if ($conf['ext_localconf.php']) {
1061 $cFiles['ext_localconf'].=t3lib_extMgm::_makeIncludeHeader($key,$conf['ext_localconf.php']);
1062 $cFiles['ext_localconf'].=trim(t3lib_div::getUrl($conf['ext_localconf.php']));
1063 }
1064 if ($conf['ext_tables.php']) {
1065 $cFiles['ext_tables'].=t3lib_extMgm::_makeIncludeHeader($key,$conf['ext_tables.php']);
1066 $cFiles['ext_tables'].=trim(t3lib_div::getUrl($conf['ext_tables.php']));
1067 }
1068 }
1069 }
1070
1071 t3lib_div::writeFile(PATH_typo3conf.$cacheFilePrefix.'_ext_localconf.php',$cFiles['ext_localconf']);
1072 t3lib_div::writeFile(PATH_typo3conf.$cacheFilePrefix.'_ext_tables.php',$cFiles['ext_tables']);
1073
1074 $extensions=array();
1075 $extensions['_CACHEFILE'] = $cacheFilePrefix;
1076
1077 return $extensions;
1078 }
1079 }
1080
1081 ?>