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