fixed bug in getServiceOption()
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_svbase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Parent class for "Services" classes
29 *
30 * $Id$
31 * TODO: temp files are not removed
32 *
33 * @author René Fritz <r.fritz@colorcube.de>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 127: class t3lib_svbase
41 *
42 * SECTION: Get service meta information
43 * 195: function getServiceInfo()
44 * 203: function getServiceKey()
45 * 211: function getServiceTitle()
46 * 224: function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE)
47 *
48 * SECTION: Error handling
49 * 259: function devLog($msg, $severity=0, $dataVar=FALSE)
50 * 273: function errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occured')
51 * 288: function errorPull()
52 * 300: function getLastError()
53 * 315: function getLastErrorMsg()
54 * 330: function getErrorMsgArray()
55 * 348: function getLastErrorArray()
56 * 357: function resetErrors()
57 *
58 * SECTION: General service functions
59 * 377: function checkExec($progList)
60 * 401: function deactivateService()
61 * 407: function available()
62 *
63 * SECTION: IO tools
64 * 445: function checkInputFile ($absFile)
65 * 466: function readFile ($absFile, $length=0)
66 * 491: function writeFile ($content, $absFile='')
67 * 517: function tempFile ($filePrefix)
68 * 535: function registerTempFile ($absFile)
69 * 545: function unlinkTempFiles ()
70 *
71 * SECTION: IO input
72 * 567: function setInput ($content, $type='')
73 * 581: function setInputFile ($absFile, $type='')
74 * 594: function getInput ()
75 * 609: function getInputFile ($createFile='')
76 *
77 * SECTION: IO output
78 * 634: function setOutputFile ($absFile)
79 * 644: function getOutput ()
80 * 658: function getOutputFile ($absFile='')
81 *
82 * SECTION: Service implementation
83 * 682: function init()
84 * 706: function reset()
85 * 721: function __destruct()
86 * 727: function process($content='', $type='', $conf=array())
87 *
88 * TOTAL FUNCTIONS: 32
89 * (This index is automatically created/updated by the extension "extdeveval")
90 *
91 */
92
93
94
95
96
97 define ('T3_ERR_SV_GENERAL', -1); // General error - something went wrong
98 define ('T3_ERR_SV_NOT_AVAIL', -2); // During execution it showed that the service is not available and should be ignored. The service itself should call $this->setNonAvailable()
99 define ('T3_ERR_SV_WRONG_SUBTYPE', -3); // passed subtype is not possible with this service
100 define ('T3_ERR_SV_NO_INPUT', -4); // passed subtype is not possible with this service
101
102
103 define ('T3_ERR_SV_FILE_NOT_FOUND', -20); // File not found which the service should process
104 define ('T3_ERR_SV_FILE_READ', -21); // File not readable
105 define ('T3_ERR_SV_FILE_WRITE', -22); // File not writable
106
107 define ('T3_ERR_SV_PROG_NOT_FOUND', -40); // passed subtype is not possible with this service
108 define ('T3_ERR_SV_PROG_FAILED', -41); // passed subtype is not possible with this service
109
110 // define ('T3_ERR_SV_serviceType_myerr, -100); // All errors with prefix T3_ERR_SV_[serviceType]_ and lower than -99 are service type dependent error
111
112
113 require_once(PATH_t3lib.'class.t3lib_exec.php');
114
115
116
117
118
119
120 /**
121 * Parent class for "Services" classes
122 *
123 * @author René Fritz <r.fritz@colorcube.de>
124 * @package TYPO3
125 * @subpackage t3lib
126 */
127 class t3lib_svbase {
128
129 /**
130 * service description array
131 */
132 var $info=array();
133
134 /**
135 * error stack
136 */
137 var $error=array();
138
139 /**
140 * Defines if debug messages should be written with t3lib_div::devLog
141 */
142 var $writeDevLog = false;
143
144
145 /**
146 * The output content.
147 * That's what the services produced as result.
148 */
149 var $out = '';
150
151 /**
152 * The file that should be processed.
153 */
154 var $inputFile = '';
155
156 /**
157 * The content that should be processed.
158 */
159 var $inputContent = '';
160
161 /**
162 * The type of the input content (or file). Might be the same as the service subtypes.
163 */
164 var $inputType = '';
165
166 /**
167 * The file where the output should be written to.
168 */
169 var $outputFile = '';
170
171
172 /**
173 * Temporary files which have to be deleted
174 *
175 * @private
176 */
177 var $tempFiles = array();
178
179
180
181 /***************************************
182 *
183 * Get service meta information
184 *
185 ***************************************/
186
187
188 /**
189 * Returns internal information array for service
190 *
191 * @return array service description array
192 */
193 function getServiceInfo() {
194 return $this->info;
195 }
196
197
198 /**
199 * Returns the service key of the service
200 *
201 * @return string service key
202 */
203 function getServiceKey() {
204 return $this->info['serviceKey'];
205 }
206
207
208 /**
209 * Returns the title of the service
210 *
211 * @return string service title
212 */
213 function getServiceTitle() {
214 return $this->info['title'];
215 }
216
217
218 /**
219 * Returns service configuration values from the $TYPO3_CONF_VARS['SVCONF'] array
220 *
221 * @param string Name of the config option
222 * @return mixed Default configuration value for the service. Will be returned if no value found.
223 * @param boolean If set the 'default' config will be return if no special config for this service is available (default: true)
224 * @return mixed configuration value for the service
225 */
226 function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE) {
227 global $TYPO3_CONF_VARS;
228
229 $config = NULL;
230
231 $svOptions = $TYPO3_CONF_VARS['SVCONF'][$this->info['serviceType']];
232
233 if(isset($svOptions[$this->info['serviceKey']][$optionName])) {
234 $config = $svOptions[$this->info['serviceKey']][$optionName];
235 } elseif($includeDefaultConfig AND isset($svOptions['default'][$optionName])) {
236 $config = $svOptions['default'][$optionName];
237 }
238 if(!isset($config)) {
239 $config = $defaultValue;
240 }
241 return $config;
242 }
243
244
245
246 /***************************************
247 *
248 * Error handling
249 *
250 ***************************************/
251
252
253 /**
254 * Logs debug messages to t3lib_div::devLog()
255 *
256 * @param string Debug message
257 * @param integer Severity: 0 is info, 1 is notice, 2 is warning, 3 is fatal error, -1 is "OK" message
258 * @param array Additional data you want to pass to the logger.
259 * @return void
260 */
261 function devLog($msg, $severity=0, $dataVar=FALSE) {
262 if($this->writeDevLog) {
263 t3lib_div::devLog($msg, $this->info['serviceKey'], $severity, $dataVar);
264 }
265 }
266
267
268 /**
269 * Puts an error on the error stack. Calling without parameter adds a general error.
270 *
271 * @param string error message
272 * @param string error number (see T3_ERR_SV_* constants)
273 * @return void
274 */
275 function errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occured') {
276 array_push($this->error, array('nr'=>$errNum, 'msg'=>$errMsg));
277
278 if (is_object($GLOBALS['TT'])) {
279 $GLOBALS['TT']->setTSlogMessage($errMsg,2);
280 }
281
282 }
283
284
285 /**
286 * Removes the last error from the error stack.
287 *
288 * @return void
289 */
290 function errorPull() {
291 array_pop($this->error);
292
293 // pop for $GLOBALS['TT']->setTSlogMessage is not supported
294 }
295
296
297 /**
298 * Returns the last error number from the error stack.
299 *
300 * @return string error number
301 */
302 function getLastError() {
303 if(count($this->error)) {
304 $error = end($this->error);
305 return $error['nr'];
306 } else {
307 return TRUE; // means all is ok - no error
308 }
309 }
310
311
312 /**
313 * Returns the last message from the error stack.
314 *
315 * @return string error message
316 */
317 function getLastErrorMsg() {
318 if(count($this->error)) {
319 $error = end($this->error);
320 return $error['msg'];
321 } else {
322 return '';
323 }
324 }
325
326
327 /**
328 * Returns all error messages as array.
329 *
330 * @return array error messages
331 */
332 function getErrorMsgArray() {
333 $errArr = array();
334
335 if(count($this->error)) {
336 reset($this->error);
337 foreach($this->error as $error) {
338 $errArr[] = $error['msg'];
339 }
340 }
341 return $errArr;
342 }
343
344
345 /**
346 * Returns the last array from the error stack.
347 *
348 * @return array error nr and message
349 */
350 function getLastErrorArray() {
351 return end($this->error);
352 }
353
354 /**
355 * Reset the error stack.
356 *
357 * @return void
358 */
359 function resetErrors() {
360 $this->error=array();
361 }
362
363
364
365 /***************************************
366 *
367 * General service functions
368 *
369 ***************************************/
370
371
372
373 /**
374 * check the availability of external programs
375 *
376 * @param string comma list of programs 'perl,python,pdftotext'
377 * @return boolean return FALSE if one program was not found
378 */
379 function checkExec($progList) {
380 global $T3_VAR, $TYPO3_CONF_VARS;
381
382 $ret = TRUE;
383
384 require_once(PATH_t3lib.'class.t3lib_exec.php');
385
386 $progList = t3lib_div::trimExplode(',', $progList, 1);
387 foreach($progList as $prog) {
388 if (!t3lib_exec::checkCommand($prog)) {
389 // program not found
390 $this->errorPush('External program not found: '.$prog, T3_ERR_SV_PROG_NOT_FOUND);
391 $ret = FALSE;
392 }
393 }
394 return $ret;
395 }
396
397
398 /**
399 * Deactivate the service. Use this if the service fails at runtime and will not be available.
400 *
401 * @return void
402 */
403 function deactivateService() {
404 t3lib_extMgm::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
405 }
406
407
408
409
410
411
412
413
414
415 /***************************************
416 *
417 * IO tools
418 *
419 ***************************************/
420
421
422
423 /**
424 * Check if a file exists and is readable.
425 *
426 * @param string File name with absolute path.
427 * @return string File name or FALSE.
428 */
429 function checkInputFile ($absFile) {
430 if(t3lib_div::isAllowedAbsPath($absFile) && @is_file($absFile)) {
431 if(@is_readable($absFile)) {
432 return $absFile;
433 } else {
434 $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: '.$absFile);
435 }
436 } else {
437 $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: '.$absFile);
438 }
439 return FALSE;
440 }
441
442
443 /**
444 * Read content from a file a file.
445 *
446 * @param string File name to read from.
447 * @param integer Maximum length to read. If empty the whole file will be read.
448 * @return string $content or FALSE
449 */
450 function readFile ($absFile, $length=0) {
451 $out = FALSE;
452
453 if ($this->checkInputFile ($absFile)) {
454 if ($fd = fopen ($absFile, 'rb')) {
455 $length = intval($length) ? intval($length) : filesize ($absFile);
456 if ($length > 0) {
457 $out = fread ($fd, $length);
458 }
459 fclose ($fd);
460 } else {
461 $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: '.$absFile);
462 }
463 }
464 return $out;
465 }
466
467
468 /**
469 * Write content to a file.
470 *
471 * @param string Content to write to the file
472 * @param string File name to write into. If empty a temp file will be created.
473 * @return string File name or FALSE
474 */
475 function writeFile ($content, $absFile='') {
476 $ret = TRUE;
477
478 if (!$absFile) {
479 $absFile = $this->tempFile($this->prefixId);
480 }
481
482 if($absFile && t3lib_div::isAllowedAbsPath($absFile)) {
483 if ($fd = @fopen($absFile,'wb')) {
484 @fwrite($fd, $content);
485 @fclose($fd);
486 } else {
487 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: '.$absFile);
488 $absFile = FALSE;
489 }
490 }
491
492 return $absFile;
493 }
494
495 /**
496 * Create a temporary file.
497 *
498 * @param string File prefix.
499 * @return string File name or FALSE
500 */
501 function tempFile ($filePrefix) {
502 $absFile = t3lib_div::tempnam($filePrefix);
503 if($absFile) {
504 $ret = TRUE;
505 $this->registerTempFile ($absFile);
506 } else {
507 $ret = FALSE;
508 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
509 }
510 return ($ret ? $absFile : FALSE);
511 }
512
513 /**
514 * Register file which should be deleted afterwards.
515 *
516 * @param string File name with absolute path.
517 * @return void
518 */
519 function registerTempFile ($absFile) {
520 $this->tempFiles[] = $absFile;
521 }
522
523 /**
524 * Delete registered temporary files.
525 *
526 * @param string File name with absolute path.
527 * @return void
528 */
529 function unlinkTempFiles () {
530 foreach ($this->tempFiles as $absFile) {
531 t3lib_div::unlink_tempfile($absFile);
532 }
533 $this->tempFiles = array();
534 }
535
536
537 /***************************************
538 *
539 * IO input
540 *
541 ***************************************/
542
543
544 /**
545 * Set the input content for service processing.
546 *
547 * @param mixed Input content (going into ->inputContent)
548 * @param string The type of the input content (or file). Might be the same as the service subtypes.
549 * @return void
550 */
551 function setInput ($content, $type='') {
552 $this->inputContent = $content;
553 $this->inputFile = '';
554 $this->inputType = $type;
555 }
556
557
558 /**
559 * Set the input file name for service processing.
560 *
561 * @param string file name
562 * @param string The type of the input content (or file). Might be the same as the service subtypes.
563 * @return void
564 */
565 function setInputFile ($absFile, $type='') {
566 $this->inputContent = '';
567 $this->inputFile = $absFile;
568 $this->inputType = $type;
569 }
570
571
572 /**
573 * Get the input content.
574 * Will be read from input file if needed. (That is if ->inputContent is empty and ->inputFile is not)
575 *
576 * @return mixed
577 */
578 function getInput () {
579 if ($this->inputContent=='') {
580 $this->inputContent = $this->readFile($this->inputFile);
581 }
582 return $this->inputContent;
583 }
584
585
586 /**
587 * Get the input file name.
588 * If the content was set by setContent a file will be created.
589 *
590 * @param string File name. If empty a temp file will be created.
591 * @return string File name or FALSE if no input or file error.
592 */
593 function getInputFile ($createFile='') {
594 if($this->inputFile) {
595 $this->inputFile = $this->checkInputFile($this->inputFile);
596 } elseif ($this->inputContent) {
597 $this->inputFile = $this->writeFile($this->inputContent, $createFile);
598 }
599 return $this->inputFile;
600 }
601
602
603
604
605 /***************************************
606 *
607 * IO output
608 *
609 ***************************************/
610
611
612 /**
613 * Set the output file name.
614 *
615 * @param string file name
616 * @return void
617 */
618 function setOutputFile ($absFile) {
619 $this->outputFile = $absFile;
620 }
621
622
623 /**
624 * Get the output content.
625 *
626 * @return mixed
627 */
628 function getOutput () {
629 if ($this->outputFile) {
630 $this->out = $this->readFile($this->outputFile);
631 }
632 return $this->out;
633 }
634
635
636 /**
637 * Get the output file name. If no output file is set, the ->out buffer is written to the file given by input parameter filename
638 *
639 * @param string Absolute filename to write to
640 * @return mixed
641 */
642 function getOutputFile ($absFile='') {
643 if (!$this->outputFile) {
644 $this->outputFile = $this->writeFile($this->out, $absFile);
645 }
646 return $this->outputFile;
647 }
648
649
650
651
652 /***************************************
653 *
654 * Service implementation
655 *
656 ***************************************/
657
658 /**
659 * Initialization of the service.
660 *
661 * The class have to do a strict check if the service is available.
662 * example: check if the perl interpreter is available which is needed to run an extern perl script.
663 *
664 * @return boolean TRUE if the service is available
665 */
666 function init() {
667 // do not work :-( but will not hurt
668 register_shutdown_function(array(&$this, '__destruct'));
669 // look in makeInstanceService()
670
671 $this->reset();
672
673 // check for external programs which are defined by $info['exec']
674 if (trim($this->info['exec'])) {
675 if (!$this->checkExec($this->info['exec'])) {
676 // nothing todo here or?
677 }
678 }
679
680 return $this->getLastError();
681 }
682
683
684 /**
685 * Resets the service.
686 * Will be called by init(). Should be used before every use if a service instance is used multiple times.
687 *
688 * @return void
689 */
690 function reset() {
691 $this->unlinkTempFiles();
692 $this->resetErrors();
693 $this->out = '';
694 $this->inputFile = '';
695 $this->inputContent = '';
696 $this->inputType = '';
697 $this->outputFile = '';
698 }
699
700 /**
701 * Clean up the service.
702 *
703 * @return void
704 */
705 function __destruct() {
706 $this->unlinkTempFiles();
707 }
708
709
710 /* every service type has it's own API
711 function process($content='', $type='', $conf=array()) { //
712 }
713 */
714
715 }
716
717 /**
718 // Does not make sense, because this class is always extended by the service classes..
719 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]) {
720 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]);
721 }
722 */
723
724 ?>