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