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