2 /***************************************************************
5 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Parent class for "Services" classes
31 * TODO: temp files are not removed
33 * @author René Fritz <r.fritz@colorcube.de>
36 * [CLASS/FUNCTION INDEX of SCRIPT]
40 * 125: class t3lib_svbase
42 * SECTION: Get service meta information
43 * 191: function getServiceInfo()
44 * 201: function getServiceKey()
45 * 211: function getServiceTitle()
46 * 224: function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE)
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()
58 * SECTION: General service functions
59 * 377: function checkExec($progList)
60 * 401: function deactivateService()
63 * 427: function checkInputFile ($absFile)
64 * 448: function readFile ($absFile, $length=0)
65 * 473: function writeFile ($content, $absFile='')
66 * 499: function tempFile ($filePrefix)
67 * 517: function registerTempFile ($absFile)
68 * 527: function unlinkTempFiles ()
71 * 549: function setInput ($content, $type='')
72 * 563: function setInputFile ($absFile, $type='')
73 * 576: function getInput ()
74 * 591: function getInputFile ($createFile='')
77 * 616: function setOutputFile ($absFile)
78 * 626: function getOutput ()
79 * 640: function getOutputFile ($absFile='')
81 * SECTION: Service implementation
82 * 664: function init()
83 * 688: function reset()
84 * 703: function __destruct()
87 * (This index is automatically created/updated by the extension "extdeveval")
95 define ('T3_ERR_SV_GENERAL', -1); // General error - something went wrong
96 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()
97 define ('T3_ERR_SV_WRONG_SUBTYPE', -3); // passed subtype is not possible with this service
98 define ('T3_ERR_SV_NO_INPUT', -4); // passed subtype is not possible with this service
101 define ('T3_ERR_SV_FILE_NOT_FOUND', -20); // File not found which the service should process
102 define ('T3_ERR_SV_FILE_READ', -21); // File not readable
103 define ('T3_ERR_SV_FILE_WRITE', -22); // File not writable
105 define ('T3_ERR_SV_PROG_NOT_FOUND', -40); // passed subtype is not possible with this service
106 define ('T3_ERR_SV_PROG_FAILED', -41); // passed subtype is not possible with this service
108 // 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 require_once(PATH_t3lib
.'class.t3lib_exec.php');
119 * Parent class for "Services" classes
121 * @author René Fritz <r.fritz@colorcube.de>
128 * service description array
138 * Defines if debug messages should be written with t3lib_div::devLog
140 var $writeDevLog = false
;
144 * The output content.
145 * That's what the services produced as result.
150 * The file that should be processed.
155 * The content that should be processed.
157 var $inputContent = '';
160 * The type of the input content (or file). Might be the same as the service subtypes.
165 * The file where the output should be written to.
167 var $outputFile = '';
171 * Temporary files which have to be deleted
175 var $tempFiles = array();
179 /***************************************
181 * Get service meta information
183 ***************************************/
187 * Returns internal information array for service
189 * @return array service description array
191 function getServiceInfo() {
197 * Returns the service key of the service
199 * @return string service key
201 function getServiceKey() {
202 return $this->info
['serviceKey'];
207 * Returns the title of the service
209 * @return string service title
211 function getServiceTitle() {
212 return $this->info
['title'];
217 * Returns service configuration values from the $TYPO3_CONF_VARS['SVCONF'] array
219 * @param string Name of the config option
220 * @param boolean If set the 'default' config will be return if no special config for this service is available (default: true)
221 * @param [type] $includeDefaultConfig: ...
222 * @return mixed configuration value for the service
224 function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE
) {
225 global $TYPO3_CONF_VARS;
229 $svOptions = $TYPO3_CONF_VARS['SVCONF'][$this->info
['serviceType']];
231 if(isset($svOptions[$this->info
['serviceKey']][$optionName])) {
232 $config = $svOptions[$this->info
['serviceKey']][$optionName];
233 } elseif($includeDefaultConfig AND isset($svOptions['default'][$optionName])) {
234 $config = $svOptions['default'][$optionName];
236 if(!isset($config)) {
237 $config = $defaultValue;
244 /***************************************
248 ***************************************/
252 * Logs debug messages to t3lib_div::devLog()
254 * @param string Debug message
255 * @param integer Severity: 0 is info, 1 is notice, 2 is warning, 3 is fatal error, -1 is "OK" message
256 * @param array Additional data you want to pass to the logger.
259 function devLog($msg, $severity=0, $dataVar=FALSE
) {
260 if($this->writeDevLog
) {
261 t3lib_div
::devLog($msg, $this->info
['serviceKey'], $severity, $dataVar);
267 * Puts an error on the error stack. Calling without parameter adds a general error.
269 * @param string error message
270 * @param string error number (see T3_ERR_SV_* constants)
273 function errorPush($errNum=T3_ERR_SV_GENERAL
, $errMsg='Unspecified error occured') {
274 array_push($this->error
, array('nr'=>$errNum, 'msg'=>$errMsg));
276 if (is_object($GLOBALS['TT'])) {
277 $GLOBALS['TT']->setTSlogMessage($errMsg,2);
284 * Removes the last error from the error stack.
288 function errorPull() {
289 array_pop($this->error
);
291 // pop for $GLOBALS['TT']->setTSlogMessage is not supported
296 * Returns the last error number from the error stack.
298 * @return string error number
300 function getLastError() {
301 if(count($this->error
)) {
302 $error = end($this->error
);
305 return TRUE
; // means all is ok - no error
311 * Returns the last message from the error stack.
313 * @return string error message
315 function getLastErrorMsg() {
316 if(count($this->error
)) {
317 $error = end($this->error
);
318 return $error['msg'];
326 * Returns all error messages as array.
328 * @return array error messages
330 function getErrorMsgArray() {
333 if(count($this->error
)) {
335 foreach($this->error
as $error) {
336 $errArr[] = $error['msg'];
344 * Returns the last array from the error stack.
346 * @return array error nr and message
348 function getLastErrorArray() {
349 return end($this->error
);
353 * Reset the error stack.
357 function resetErrors() {
358 $this->error
=array();
363 /***************************************
365 * General service functions
367 ***************************************/
372 * check the availability of external programs
374 * @param string comma list of programs 'perl,python,pdftotext'
375 * @return boolean return FALSE if one program was not found
377 function checkExec($progList) {
378 global $T3_VAR, $TYPO3_CONF_VARS;
382 require_once(PATH_t3lib
.'class.t3lib_exec.php');
384 $progList = t3lib_div
::trimExplode(',', $progList, 1);
385 foreach($progList as $prog) {
386 if (!t3lib_exec
::checkCommand($prog)) {
388 $this->errorPush('External program not found: '.$prog, T3_ERR_SV_PROG_NOT_FOUND
);
397 * Deactivate the service. Use this if the service fails at runtime and will not be available.
401 function deactivateService() {
402 t3lib_extMgm
::deactivateService($this->info
['serviceType'], $this->info
['serviceKey']);
413 /***************************************
417 ***************************************/
422 * Check if a file exists and is readable.
424 * @param string File name with absolute path.
425 * @return string File name or FALSE.
427 function checkInputFile ($absFile) {
428 if(t3lib_div
::isAllowedAbsPath($absFile) && @is_file
($absFile)) {
429 if(@is_readable
($absFile)) {
432 $this->errorPush(T3_ERR_SV_FILE_READ
, 'File is not readable: '.$absFile);
435 $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND
, 'File not found: '.$absFile);
442 * Read content from a file a file.
444 * @param string File name to read from.
445 * @param integer Maximum length to read. If empty the whole file will be read.
446 * @return string $content or FALSE
448 function readFile ($absFile, $length=0) {
451 if ($this->checkInputFile ($absFile)) {
452 if ($fd = fopen ($absFile, 'rb')) {
453 $length = intval($length) ?
intval($length) : filesize ($absFile);
455 $out = fread ($fd, $length);
459 $this->errorPush(T3_ERR_SV_FILE_READ
, 'Can not read from file: '.$absFile);
467 * Write content to a file.
469 * @param string Content to write to the file
470 * @param string File name to write into. If empty a temp file will be created.
471 * @return string File name or FALSE
473 function writeFile ($content, $absFile='') {
477 $absFile = $this->tempFile($this->prefixId
);
480 if($absFile && t3lib_div
::isAllowedAbsPath($absFile)) {
481 if ($fd = @fopen
($absFile,'wb')) {
482 @fwrite
($fd, $content);
485 $this->errorPush(T3_ERR_SV_FILE_WRITE
, 'Can not write to file: '.$absFile);
494 * Create a temporary file.
496 * @param string File prefix.
497 * @return string File name or FALSE
499 function tempFile ($filePrefix) {
500 $absFile = t3lib_div
::tempnam($filePrefix);
503 $this->registerTempFile ($absFile);
506 $this->errorPush(T3_ERR_SV_FILE_WRITE
, 'Can not create temp file.');
508 return ($ret ?
$absFile : FALSE
);
512 * Register file which should be deleted afterwards.
514 * @param string File name with absolute path.
517 function registerTempFile ($absFile) {
518 $this->tempFiles
[] = $absFile;
522 * Delete registered temporary files.
524 * @param string File name with absolute path.
527 function unlinkTempFiles () {
528 foreach ($this->tempFiles
as $absFile) {
529 t3lib_div
::unlink_tempfile($absFile);
531 $this->tempFiles
= array();
535 /***************************************
539 ***************************************/
543 * Set the input content for service processing.
545 * @param mixed Input content (going into ->inputContent)
546 * @param string The type of the input content (or file). Might be the same as the service subtypes.
549 function setInput ($content, $type='') {
550 $this->inputContent
= $content;
551 $this->inputFile
= '';
552 $this->inputType
= $type;
557 * Set the input file name for service processing.
559 * @param string file name
560 * @param string The type of the input content (or file). Might be the same as the service subtypes.
563 function setInputFile ($absFile, $type='') {
564 $this->inputContent
= '';
565 $this->inputFile
= $absFile;
566 $this->inputType
= $type;
571 * Get the input content.
572 * Will be read from input file if needed. (That is if ->inputContent is empty and ->inputFile is not)
576 function getInput () {
577 if ($this->inputContent
=='') {
578 $this->inputContent
= $this->readFile($this->inputFile
);
580 return $this->inputContent
;
585 * Get the input file name.
586 * If the content was set by setContent a file will be created.
588 * @param string File name. If empty a temp file will be created.
589 * @return string File name or FALSE if no input or file error.
591 function getInputFile ($createFile='') {
592 if($this->inputFile
) {
593 $this->inputFile
= $this->checkInputFile($this->inputFile
);
594 } elseif ($this->inputContent
) {
595 $this->inputFile
= $this->writeFile($this->inputContent
, $createFile);
597 return $this->inputFile
;
603 /***************************************
607 ***************************************/
611 * Set the output file name.
613 * @param string file name
616 function setOutputFile ($absFile) {
617 $this->outputFile
= $absFile;
622 * Get the output content.
626 function getOutput () {
627 if ($this->outputFile
) {
628 $this->out
= $this->readFile($this->outputFile
);
635 * Get the output file name. If no output file is set, the ->out buffer is written to the file given by input parameter filename
637 * @param string Absolute filename to write to
640 function getOutputFile ($absFile='') {
641 if (!$this->outputFile
) {
642 $this->outputFile
= $this->writeFile($this->out
, $absFile);
644 return $this->outputFile
;
650 /***************************************
652 * Service implementation
654 ***************************************/
657 * Initialization of the service.
659 * The class have to do a strict check if the service is available.
660 * example: check if the perl interpreter is available which is needed to run an extern perl script.
662 * @return boolean TRUE if the service is available
665 // do not work :-( but will not hurt
666 register_shutdown_function(array(&$this, '__destruct'));
667 // look in makeInstanceService()
671 // check for external programs which are defined by $info['exec']
672 if (trim($this->info
['exec'])) {
673 if (!$this->checkExec($this->info
['exec'])) {
674 // nothing todo here or?
678 return $this->getLastError();
683 * Resets the service.
684 * Will be called by init(). Should be used before every use if a service instance is used multiple times.
689 $this->unlinkTempFiles();
690 $this->resetErrors();
692 $this->inputFile
= '';
693 $this->inputContent
= '';
694 $this->inputType
= '';
695 $this->outputFile
= '';
699 * Clean up the service.
703 function __destruct() {
704 $this->unlinkTempFiles();
708 /* every service type has it's own API
709 function process($content='', $type='', $conf=array()) { //
716 // Does not make sense, because this class is always extended by the service classes..
717 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]) {
718 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]);