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