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