* Extended a comment of a function in t3lib_extMgm
[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
475 * @param string Service key
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 substr($serviceType,0,3)!='tx_' AND substr($serviceKey,0,3)=='tx_' AND is_array($info)) {
487
488 $info['priority'] = max(0,min(100,$info['priority']));
489
490 $T3_SERVICES[$serviceType][$serviceKey]=$info;
491
492 $T3_SERVICES[$serviceType][$serviceKey]['extKey'] = $extKey;
493 $T3_SERVICES[$serviceType][$serviceKey]['serviceKey'] = $serviceKey;
494 $T3_SERVICES[$serviceType][$serviceKey]['serviceType'] = $serviceType;
495
496
497 // mapping a service key to a service type
498 // all service keys begin with tx_ - service types don't
499 // this way a selection of a special service key as service type is easy
500 $T3_SERVICES[$serviceKey][$serviceKey] = &$T3_SERVICES[$serviceType][$serviceKey];
501
502
503 // change the priority (and other values) from TYPO3_CONF_VARS
504 // $TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey]['priority']
505 // even the activation is possible (a unix service might be possible on windows for some reasons)
506 if (is_array($TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey])) {
507
508 // no check is done here - there might be configuration values only the service type knows about, so we pass everything
509 $T3_SERVICES[$serviceType][$serviceKey] = array_merge ($T3_SERVICES[$serviceType][$serviceKey],$TYPO3_CONF_VARS['T3_SERVICES'][$serviceType][$serviceKey]);
510 }
511
512
513 // OS check
514 // empty $os means 'not limited to one OS', therefore a check is not needed
515 if ($T3_SERVICES[$serviceType][$serviceKey]['available'] AND $T3_SERVICES[$serviceType][$serviceKey]['os']!='') {
516
517 // TYPO3_OS is not yet defined
518 $os_type = stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'UNIX';
519
520 $os = t3lib_div::trimExplode(',',strtoupper($T3_SERVICES[$serviceType][$serviceKey]['os']));
521
522 if (!in_array($os_type,$os)) {
523 t3lib_extMgm::deactivateService($serviceType, $serviceKey);
524 }
525 }
526
527 // convert subtype list to array for quicker access
528 $T3_SERVICES[$serviceType][$serviceKey]['serviceSubTypes'] = array();
529 $serviceSubTypes = t3lib_div::trimExplode(',',$info['subtype']);
530 foreach ($serviceSubTypes as $subtype) {
531 $T3_SERVICES[$serviceType][$serviceKey]['serviceSubTypes'][$subtype] = $subtype;
532 }
533 }
534 }
535
536 /**
537 * Find the available service with highest priority
538 *
539 * @param string Service type
540 * @param string Service sub type
541 * @param mixed Service keys that should be excluded in the search for a service. Array or comma list.
542 * @return mixed Service info array if a service was found, FLASE otherwise
543 * @author René Fritz <r.fritz@colorcube.de>
544 */
545 function findService($serviceType, $serviceSubType='', $excludeServiceKeys=array()) {
546 global $T3_SERVICES;
547
548 $serviceKey = FALSE;
549 $serviceInfo = FALSE;
550 $priority = 0;
551 $quality = 0;
552
553 if (!is_array($excludeServiceKeys) ) {
554 $excludeServiceKeys = t3lib_div::trimExplode(',', $excludeServiceKeys, 1);
555 }
556
557 if (is_array($T3_SERVICES[$serviceType])) {
558 foreach($T3_SERVICES[$serviceType] as $key => $info) {
559
560 if (in_array($key, $excludeServiceKeys)) {
561 continue;
562 }
563
564 // select a subtype randomly
565 // usefull to start a service by service key without knowing his subtypes - for testing purposes
566 if ($serviceSubType=='*') {
567 $serviceSubType = key($info['serviceSubTypes']);
568 }
569
570 // this matches empty subtype too
571 if( $info['available'] AND ($info['subtype']==$serviceSubType OR $info['serviceSubTypes'][$serviceSubType]) AND $info['priority']>=$priority ) {
572
573 // has a lower quality than the already found, therefore we skip this service
574 if($info['priority']==$priority AND $info['quality']<$quality) {
575 continue;
576 }
577
578 // service depends on external programs - check if they exists
579 if(trim($info['exec'])) {
580 require_once(PATH_t3lib.'class.t3lib_exec.php');
581
582 $executables = t3lib_div::trimExplode(',', $info['exec'],1);
583 foreach($executables as $executable) {
584 if(!t3lib_exec::checkCommand($executable)) {
585 t3lib_extMgm::deactivateService($serviceType, $key);
586 $info['available']=FALSE;
587 break;
588 }
589 }
590 }
591
592 // still available after exec check?
593 if($info['available']) {
594 $serviceKey = $key;
595 $priority = $info['priority'];
596 $quality = $info['quality'];
597 }
598 }
599 }
600 }
601
602 if ($serviceKey) {
603 $serviceInfo = $T3_SERVICES[$serviceType][$serviceKey];
604 }
605 return $serviceInfo;
606 }
607
608 /**
609 * Deactivate a service
610 *
611 * @param string Service type
612 * @param string Service key
613 * @return void
614 * @author René Fritz <r.fritz@colorcube.de>
615 */
616 function deactivateService($serviceType, $serviceKey) {
617 global $T3_SERVICES;
618
619 // ... maybe it's better to move non-available services to a different array??
620 $T3_SERVICES[$serviceType][$serviceKey]['available'] = FALSE;
621 }
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636 /**************************************
637 *
638 * Adding FRONTEND features
639 * (related specifically to "cms" extension)
640 *
641 ***************************************/
642
643 /**
644 * Adds an entry to the list of plugins in content elements of type "Insert plugin"
645 * 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)
646 * 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.
647 * 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
648 * FOR USE IN ext_tables.php FILES
649 * Usage: 13
650 *
651 * @param array Item Array
652 * @param string Type (eg. "list_type") - basically a field from "tt_content" table
653 * @return void
654 */
655 function addPlugin($itemArray,$type='list_type') {
656 global $TCA;
657 t3lib_div::loadTCA('tt_content');
658 if (is_array($TCA['tt_content']['columns']) && is_array($TCA['tt_content']['columns'][$type]['config']['items'])) {
659 reset($TCA['tt_content']['columns'][$type]['config']['items']);
660 while(list($k,$v)=each($TCA['tt_content']['columns'][$type]['config']['items'])) {
661 if (!strcmp($v[1],$itemArray[1])) {
662 $TCA['tt_content']['columns'][$type]['config']['items'][$k]=$itemArray;
663 return;
664 }
665 }
666 $TCA['tt_content']['columns'][$type]['config']['items'][]=$itemArray;
667 }
668 }
669
670 /**
671 * Adds an entry to the "ds" array of the tt_content field "pi_flexform".
672 * This is used by plugins to add a flexform XML reference / content for use when they are selected as plugin.
673 * Usage: 0
674 *
675 * @param string The same value as the key for the plugin
676 * @param string Either a reference to a flex-form XML file (eg. "FILE:EXT:newloginbox/flexform_ds.xml") or the XML directly.
677 * @return void
678 * @see addPlugin()
679 */
680 function addPiFlexFormValue($piKeyToMatch,$value) {
681 global $TCA;
682 t3lib_div::loadTCA('tt_content');
683
684 if (is_array($TCA['tt_content']['columns']) && is_array($TCA['tt_content']['columns']['pi_flexform']['config']['ds'])) {
685 $TCA['tt_content']['columns']['pi_flexform']['config']['ds'][$piKeyToMatch] = $value;
686 }
687 }
688
689 /**
690 * Adds the $table tablename to the list of tables allowed to be includes by content element type "Insert records"
691 * By using $content_table and $content_field you can also use the function for other tables.
692 * FOR USE IN ext_tables.php FILES
693 * Usage: 9
694 *
695 * @param string Table name to allow for "insert record"
696 * @param string Table name TO WHICH the $table name is applied. See $content_field as well.
697 * @param string Field name in the database $content_table in which $table is allowed to be added as a reference ("Insert Record")
698 * @return void
699 */
700 function addToInsertRecords($table,$content_table='tt_content',$content_field='records') {
701 global $TCA;
702 t3lib_div::loadTCA($content_table);
703 if (is_array($TCA[$content_table]['columns']) && isset($TCA[$content_table]['columns'][$content_field]['config']['allowed'])) {
704 $TCA[$content_table]['columns'][$content_field]['config']['allowed'].=','.$table;
705 }
706 }
707
708 /**
709 * Add PlugIn to Static Template #43
710 *
711 * 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.
712 * 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"
713 * $type determines the type of frontend plugin:
714 * "list_type" (default) - the good old "Insert plugin" entry
715 * "menu_type" - a "Menu/Sitemap" entry
716 * "splash_layout" - a "Textbox" entry
717 * "CType" - a new content element type
718 * "header_layout" - an additional header type (added to the selection of layout1-5)
719 * "includeLib" - just includes the library for manual use somewhere in TypoScript.
720 * (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)
721 * FOR USE IN ext_locallang.php FILES
722 * Usage: 2
723 *
724 * @param string $key is the extension key
725 * @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.
726 * @param string $prefix is used as a - yes, suffix - of the class name (fx. "_pi1")
727 * @param string $type, see description above
728 * @param boolean If $cached is set as USER content object (cObject) is created - otherwise a USER_INT object is created.
729 * @return void
730 */
731 function addPItoST43($key,$classFile='',$prefix='',$type='list_type',$cached=0) {
732 global $TYPO3_LOADED_EXT;
733 $classFile = $classFile ? $classFile : 'pi/class.tx_'.str_replace('_','',$key).$prefix.'.php';
734 $cN = t3lib_extMgm::getCN($key);
735
736 // General plugin:
737 if ($cached) {
738 $pluginContent = trim('
739 includeLibs.'.$cN.$prefix.' = '.$TYPO3_LOADED_EXT[$key]['siteRelPath'].$classFile.'
740 plugin.'.$cN.$prefix.' = USER
741 plugin.'.$cN.$prefix.' {
742 userFunc = '.$cN.$prefix.'->main
743 }');
744 } else {
745 $pluginContent = trim('
746 plugin.'.$cN.$prefix.' = USER_INT
747 plugin.'.$cN.$prefix.' {
748 includeLibs = '.$TYPO3_LOADED_EXT[$key]['siteRelPath'].$classFile.'
749 userFunc = '.$cN.$prefix.'->main
750 }');
751 }
752 t3lib_extMgm::addTypoScript($key,'setup','
753 # Setting '.$key.' plugin TypoScript
754 '.$pluginContent);
755
756 // After ST43:
757 switch($type) {
758 case 'list_type':
759 $addLine = 'tt_content.list.20.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
760 break;
761 case 'menu_type':
762 $addLine = 'tt_content.menu.20.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
763 break;
764 case 'splash_layout':
765 $addLine = 'tt_content.splash.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
766 break;
767 case 'CType':
768 $addLine = trim('
769 tt_content.'.$key.$prefix.' = COA
770 tt_content.'.$key.$prefix.' {
771 10 = < lib.stdheader
772 20 = < plugin.'.$cN.$prefix.'
773 }
774 ');
775 break;
776 case 'header_layout':
777 $addLine = 'lib.stdheader.10.'.$key.$prefix.' = < plugin.'.$cN.$prefix;
778 break;
779 case 'includeLib':
780 $addLine = 'page.1000 = < plugin.'.$cN.$prefix;
781 break;
782 default:
783 $addLine = '';
784 break;
785 }
786 if ($addLine) {
787 t3lib_extMgm::addTypoScript($key,'setup','
788 # Setting '.$key.' plugin TypoScript
789 '.$addLine.'
790 ',43);
791 }
792 }
793
794 /**
795 * Call this method to add an entry in the static template list found in sys_templates
796 * "static template files" are the modern equalent (provided from extensions) to the traditional records in "static_templates"
797 * FOR USE IN ext_locallang.php FILES
798 * Usage: 3
799 *
800 * @param string $extKey is of course the extension key
801 * @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/
802 * @param string $title is the title in the selector box.
803 * @return void
804 * @see addTypoScript()
805 */
806 function addStaticFile($extKey,$path,$title) {
807 global $TCA;
808 t3lib_div::loadTCA('sys_template');
809 if ($extKey && $path && is_array($TCA['sys_template']['columns'])) {
810 $value = str_replace(',','','EXT:'.$extKey.'/'.$path);
811 $itemArray=array(trim($title.' ('.$extKey.')'),$value);
812 $TCA['sys_template']['columns']['include_static_file']['config']['items'][]=$itemArray;
813 }
814 }
815
816 /**
817 * Adds $content to the default TypoScript setup code as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_setup']
818 * Prefixed with a [GLOBAL] line
819 * FOR USE IN ext_locallang.php FILES
820 * Usage: 6
821 *
822 * @param string TypoScript Setup string
823 * @return void
824 */
825 function addTypoScriptSetup($content) {
826 global $TYPO3_CONF_VARS;
827 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup'].="\n[GLOBAL]\n".$content;
828 }
829
830 /**
831 * Adds $content to the default TypoScript constants code as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_constants']
832 * Prefixed with a [GLOBAL] line
833 * FOR USE IN ext_locallang.php FILES
834 * Usage: 0
835 *
836 * @param string TypoScript Constants string
837 * @return void
838 */
839 function addTypoScriptConstants($content) {
840 global $TYPO3_CONF_VARS;
841 $TYPO3_CONF_VARS['FE']['defaultTypoScript_constants'].="\n[GLOBAL]\n".$content;
842 }
843
844 /**
845 * Adds $content to the default TypoScript code for either setup, constants or editorcfg as set in $TYPO3_CONF_VARS[FE]['defaultTypoScript_*']
846 * (Basically this function can do the same as addTypoScriptSetup and addTypoScriptConstants - just with a little more hazzle, but also with some more options!)
847 * FOR USE IN ext_locallang.php FILES
848 * Usage: 7
849 *
850 * @param string $key is the extension key (informative only).
851 * @param string $type is either "setup", "constants" or "editorcfg" and obviously determines which kind of TypoScript code we are adding.
852 * @param string $content is the TS content, prefixed with a [GLOBAL] line and a comment-header.
853 * @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.
854 * @return void
855 */
856 function addTypoScript($key,$type,$content,$afterStaticUid=0) {
857 global $TYPO3_CONF_VARS;
858
859 if ($type=='setup' || $type=='editorcfg' || $type=='constants') {
860 $content = '
861
862 [GLOBAL]
863 #############################################
864 ## TypoScript added by extension "'.$key.'"
865 #############################################
866
867 '.$content;
868 if ($afterStaticUid) {
869 $TYPO3_CONF_VARS['FE']['defaultTypoScript_'.$type.'.'][$afterStaticUid].=$content;
870 if ($afterStaticUid==43) { // If 'content (default)' is targeted, also add to other 'content rendering templates', eg. css_styled_content
871 $TYPO3_CONF_VARS['FE']['defaultTypoScript_'.$type.'.']['cssstyledcontent/static/'].=$content;
872 }
873 } else {
874 $TYPO3_CONF_VARS['FE']['defaultTypoScript_'.$type].=$content;
875 }
876 }
877 }
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896 /**************************************
897 *
898 * INTERNAL EXTENSION MANAGEMENT:
899 *
900 ***************************************/
901
902 /**
903 * Loading extensions configured in $TYPO3_CONF_VARS['EXT']['extList']
904 *
905 * CACHING ON: ($TYPO3_CONF_VARS['EXT']['extCache'] = 1 or 2)
906 * 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.
907 * If cachefiles are not found they will be generated
908 * CACHING OFF: ($TYPO3_CONF_VARS['EXT']['extCache'] = 0)
909 * 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.
910 * This array will later be set in the global var $TYPO3_LOADED_EXT
911 *
912 * Usages of this function can be seen in config_default.php
913 * Extensions are always detected in the order local - global - system.
914 * Usage: 1
915 *
916 * @return array Extension Array
917 * @internal
918 */
919 function typo3_loadExtensions() {
920 global $TYPO3_CONF_VARS;
921
922 // Full list of extensions includes both required and extList:
923 $rawExtList = $TYPO3_CONF_VARS['EXT']['requiredExt'].','.$TYPO3_CONF_VARS['EXT']['extList'];
924
925 // Empty array as a start.
926 $extensions = array();
927
928 //
929 if ($rawExtList) {
930 // The cached File prefix.
931 $cacheFilePrefix = 'temp_CACHED';
932 // Setting the name for the cache files:
933 if (intval($TYPO3_CONF_VARS['EXT']['extCache'])==1) $cacheFilePrefix.= '_ps'.substr(t3lib_div::shortMD5(PATH_site.'|'.$GLOBALS['TYPO_VERSION']),0,4);
934 if (intval($TYPO3_CONF_VARS['EXT']['extCache'])==2) $cacheFilePrefix.= '_'.t3lib_div::shortMD5($rawExtList);
935
936 // If cache files available, set cache file prefix and return:
937 if ($TYPO3_CONF_VARS['EXT']['extCache'] && t3lib_extMgm::isCacheFilesAvailable($cacheFilePrefix)) {
938 // Return cache file prefix:
939 $extensions['_CACHEFILE'] = $cacheFilePrefix;
940 } else {
941 // ... but if not, configure...
942 $temp_extensions = array_unique(t3lib_div::trimExplode(',',$rawExtList,1));
943 while(list(,$temp_extKey)=each($temp_extensions)) {
944 if (@is_dir(PATH_site.'typo3conf/ext/'.$temp_extKey)) {
945 $extensions[$temp_extKey]=array('type'=>'L','siteRelPath'=>'typo3conf/ext/'.$temp_extKey.'/','typo3RelPath'=>'../typo3conf/ext/'.$temp_extKey.'/');
946 } elseif (@is_dir(PATH_site.TYPO3_mainDir.'ext/'.$temp_extKey)) {
947 $extensions[$temp_extKey]=array('type'=>'G','siteRelPath'=>TYPO3_mainDir.'ext/'.$temp_extKey.'/','typo3RelPath'=>'ext/'.$temp_extKey.'/');
948 } elseif (@is_dir(PATH_site.TYPO3_mainDir.'sysext/'.$temp_extKey)) {
949 $extensions[$temp_extKey]=array('type'=>'S','siteRelPath'=>TYPO3_mainDir.'sysext/'.$temp_extKey.'/','typo3RelPath'=>'sysext/'.$temp_extKey.'/');
950 }
951
952 $files = t3lib_div::trimExplode(',','
953 ext_localconf.php,
954 ext_tables.php,
955 ext_tables.sql,
956 ext_tables_static+adt.sql,
957 ext_typoscript_constants.txt,
958 ext_typoscript_editorcfg.txt,
959 ext_typoscript_setup.txt
960 ',1);
961 reset($files);
962 while(list(,$fName)=each($files)) {
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 unset($extensions['_CACHEFILE']);
970
971
972 // write cache?
973 if ($TYPO3_CONF_VARS['EXT']['extCache']) {
974 $wrError = t3lib_extMgm::cannotCacheFilesWritable($cacheFilePrefix);
975 if ($wrError) {
976 //debug('Cannot write cache files: '.$wrError.'. Disabling the cache...');
977 $TYPO3_CONF_VARS['EXT']['extCache']=0;
978 } else {
979 // Write cache files:
980 $extensions = t3lib_extMgm::writeCacheFiles($extensions,$cacheFilePrefix);
981 }
982 }
983 }
984 }
985
986 #debug($extensions);
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 ?>