2 /***************************************************************
5 * (c) 1999-2004 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 * 127: class t3lib_svbase
42 * SECTION: Get service meta information
43 * 191: function getServiceInfo()
44 * 199: function getServiceKey()
45 * 207: function getServiceTitle()
46 * 220: function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE)
48 * SECTION: Error handling
49 * 255: function devLog($msg, $severity=0, $dataVar=FALSE)
50 * 269: function errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occured')
51 * 284: function errorPull()
52 * 296: function getLastError()
53 * 311: function getLastErrorMsg()
54 * 326: function getErrorMsgArray()
55 * 344: function getLastErrorArray()
56 * 353: function resetErrors()
58 * SECTION: General service functions
59 * 373: function checkExec($progList)
60 * 395: function deactivateService()
61 * 401: function available()
64 * 439: function checkInputFile ($absFile)
65 * 460: function readFile ($absFile, $length=0)
66 * 485: function writeFile ($content, $absFile='')
67 * 511: function tempFile ($filePrefix)
68 * 529: function registerTempFile ($absFile)
69 * 539: function unlinkTempFiles ()
72 * 561: function setInput ($content, $type='')
73 * 575: function setInputFile ($absFile, $type='')
74 * 588: function getInput ()
75 * 603: function getInputFile ($createFile='')
78 * 628: function setOutputFile ($absFile)
79 * 638: function getOutput ()
80 * 652: function getOutputFile ($absFile='')
82 * SECTION: Service implementation
83 * 676: function init()
84 * 700: function reset()
85 * 715: function __destruct()
86 * 721: function process($content='', $type='', $conf=array())
89 * (This index is automatically created/updated by the extension "extdeveval")
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
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
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
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
113 require_once(PATH_t3lib
.'class.t3lib_exec.php');
121 * Parent class for "Services" classes
123 * @author René Fritz <r.fritz@colorcube.de>
130 * service description array
140 * Defines if debug messages should be written with t3lib_div::devLog
142 var $writeDevLog = false;
146 * The output content.
147 * That's what the services produced as result.
152 * The file that should be processed.
157 * The content that should be processed.
159 var $inputContent = '';
162 * The type of the input content (or file). Might be the same as the service subtypes.
167 * The file where the output should be written to.
169 var $outputFile = '';
173 * Temporary files which have to be deleted
177 var $tempFiles = array();
181 /***************************************
183 * Get service meta information
185 ***************************************/
189 * @return array service description array
191 function getServiceInfo() {
197 * @return string service key
199 function getServiceKey() {
200 return $this->info
['serviceKey'];
205 * @return string service title
207 function getServiceTitle() {
208 return $this->info
['title'];
213 * Returns service configuration values from the $TYPO3_CONF_VARS['SVCONF'] array
215 * @param string Name of the config option
216 * @param boolean If set the 'default' config will be return if no special config for this service is available (default: true)
217 * @param [type] $includeDefaultConfig: ...
218 * @return mixed configuration value for the service
220 function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE) {
221 global $TYPO3_CONF_VARS;
225 $svOptions = $TYPO3_CONF_VARS['SVCONF'][$this->info
['serviceType']];
227 if(isset($svOptions[$this->info
['serviceKey']][$optionName])) {
228 $config = $svOptions['default'][$optionName];
229 } elseif($includeDefaultConfig AND isset($svOptions['default'][$optionName])) {
230 $config = $svOptions['default'][$optionName];
232 if(!isset($config)) {
233 $config = $defaultValue;
240 /***************************************
244 ***************************************/
248 * Logs debug messages to t3lib_div::devLog()
250 * @param string Debug message
251 * @param integer Severity: 0 is info, 1 is notice, 2 is warning, 3 is fatal error, -1 is "OK" message
252 * @param array Additional data you want to pass to the logger.
255 function devLog($msg, $severity=0, $dataVar=FALSE) {
256 if($this->writeDevLog
) {
257 t3lib_div
::devLog($msg, $this->info
['serviceKey'], $severity, $dataVar);
263 * Puts an error on the error stack. Calling without parameter adds a general error.
265 * @param string error message
266 * @param string error number (see T3_ERR_SV_* constants)
269 function errorPush($errNum=T3_ERR_SV_GENERAL
, $errMsg='Unspecified error occured') {
270 array_push($this->error
, array('nr'=>$errNum, 'msg'=>$errMsg));
272 if (is_object($GLOBALS["TT"])) {
273 $GLOBALS['TT']->setTSlogMessage($errMsg,2);
280 * Removes the last error from the error stack.
284 function errorPull() {
285 array_pop($this->error
);
287 // pop for $GLOBALS['TT']->setTSlogMessage is not supported
292 * Returns the last error number from the error stack.
294 * @return string error number
296 function getLastError() {
297 if(count($this->error
)) {
298 $error = end($this->error
);
301 return TRUE; // means all is ok - no error
307 * Returns the last message from the error stack.
309 * @return string error message
311 function getLastErrorMsg() {
312 if(count($this->error
)) {
313 $error = end($this->error
);
314 return $error['msg'];
322 * Returns all error messages as array.
324 * @return array error messages
326 function getErrorMsgArray() {
329 if(count($this->error
)) {
331 foreach($this->error
as $error) {
332 $errArr[] = $error['msg'];
340 * Returns the last array from the error stack.
342 * @return array error nr and message
344 function getLastErrorArray() {
345 return end($this->error
);
349 * Reset the error stack.
353 function resetErrors() {
354 $this->error
=array();
359 /***************************************
361 * General service functions
363 ***************************************/
368 * check the availability of external programs
370 * @param string comma list of programs 'perl,python,pdftotext'
371 * @return boolean return FALSE if one program was not found
373 function checkExec($progList) {
376 require_once(PATH_t3lib
.'class.t3lib_exec.php');
378 $progList = t3lib_div
::trimExplode(',', $progList, 1);
379 foreach($progList as $prog) {
380 if (!t3lib_exec
::checkCommand($prog)) {
382 $this->errorPush('External program not found: '.$prog, T3_ERR_SV_PROG_NOT_FOUND
);
391 * Deactivate the service. Use this if the service fails at runtime and will not be available.
395 function deactivateService() {
396 t3lib_extMgm
::deactivateService($this->info
['serviceType'], $this->info
['serviceKey']);
401 function available() {
402 global $AB,$BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;
404 // check if the service is available at runtime
405 // the sense of this method is that the service might need some more information to check the availablity
409 $excludeServiceKeys='';
410 while (is_object($serviceObj = t3lib_div::makeInstanceService('anyService','', $excludeServiceKeys))) {
411 if ($serviceObj->available('some special parm to check availablity')) {
414 $excludeServiceKeys .= ','.$serviceObj->getServiceKey;
425 /***************************************
429 ***************************************/
434 * Check if a file exists and is readable.
436 * @param string File name with absolute path.
437 * @return string File name or FALSE.
439 function checkInputFile ($absFile) {
440 if(@is_file
($absFile)) {
441 if(@is_readable
($absFile)) {
444 $this->errorPush(T3_ERR_SV_FILE_READ
, 'File is not readable: '.$absFile);
447 $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND
, 'File not found: '.$absFile);
454 * Read content from a file a file.
456 * @param string File name to read from.
457 * @param integer Maximum length to read. If empty the whole file will be read.
458 * @return string $content or FALSE
460 function readFile ($absFile, $length=0) {
463 if ($this->checkInputFile ($absFile)) {
464 if ($fd = fopen ($absFile, 'rb')) {
465 $length = intval($length) ?
intval($length) : filesize ($absFile);
467 $out = fread ($fd, $length);
471 $this->errorPush(T3_ERR_SV_FILE_READ
, 'Can not read from file: '.$absFile);
479 * Write content to a file.
481 * @param string Content to write to the file
482 * @param string File name to write into. If empty a temp file will be created.
483 * @return string File name or FALSE
485 function writeFile ($content, $absFile='') {
489 $absFile = $this->tempFile($this->prefixId
);
493 if ($fd = @fopen
($absFile,'wb')) {
494 @fwrite
($fd, $content);
497 $this->errorPush(T3_ERR_SV_FILE_WRITE
, 'Can not write to file: '.$absFile);
506 * Create a temporary file.
508 * @param string File prefix.
509 * @return string File name or FALSE
511 function tempFile ($filePrefix) {
512 $absFile = t3lib_div
::tempnam($filePrefix);
515 $this->registerTempFile ($absFile);
518 $this->errorPush(T3_ERR_SV_FILE_WRITE
, 'Can not create temp file.');
520 return ($ret ?
$absFile : FALSE);
524 * Register file which should be deleted afterwards.
526 * @param string File name with absolute path.
529 function registerTempFile ($absFile) {
530 $this->tempFiles
[] = $absFile;
534 * Delete registered temporary files.
536 * @param string File name with absolute path.
539 function unlinkTempFiles () {
540 foreach ($this->tempFiles
as $absFile) {
541 t3lib_div
::unlink_tempfile($absFile);
543 $this->tempFiles
= array();
547 /***************************************
551 ***************************************/
555 * Set the input content for service processing.
558 * @param [type] $type: ...
561 function setInput ($content, $type='') {
562 $this->inputContent
= $content;
563 $this->inputFile
= '';
564 $this->inputType
= $type;
569 * Set the input file name for service processing.
571 * @param string file name
572 * @param [type] $type: ...
575 function setInputFile ($absFile, $type='') {
576 $this->inputContent
= '';
577 $this->inputFile
= $absFile;
578 $this->inputType
= $type;
583 * Get the input content.
584 * Will be read from input file if needed.
588 function getInput () {
589 if ($this->inputContent
=='') {
590 $this->inputContent
= $this->readFile($this->inputFile
);
592 return $this->inputContent
;
597 * Get the input file name.
598 * If the content was set by setContent a file will be created.
600 * @param string File name. If empty a temp file will be created.
601 * @return string File name or FALSE if no input or file error.
603 function getInputFile ($createFile='') {
604 if($this->inputFile
) {
605 $this->inputFile
= $this->checkInputFile($this->inputFile
);
606 } elseif ($this->inputContent
) {
607 $this->inputFile
= $this->writeFile($this->inputContent
, $createFile);
609 return $this->inputFile
;
615 /***************************************
619 ***************************************/
623 * Set the output file name.
625 * @param string file name
628 function setOutputFile ($absFile) {
629 $this->outputFile
= $absFile;
634 * Get the output content.
638 function getOutput () {
639 if ($this->outputFile
) {
640 $this->out
= $this->readFile($this->outputFile
);
647 * Get the output file name.
649 * @param [type] $absFile: ...
652 function getOutputFile ($absFile='') {
653 if (!$this->outputFile
) {
654 $this->outputFile
= $this->writeFile($this->out
, $absFile);
656 return $this->outputFile
;
662 /***************************************
664 * Service implementation
666 ***************************************/
669 * Initialization of the service.
671 * The class have to do a strict check if the service is available.
672 * example: check if the perl interpreter is available which is needed to run an extern perl script.
674 * @return boolean TRUE if the service is available
677 // do not work :-( but will not hurt
678 register_shutdown_function(array(&$this, '__destruct'));
679 // look in makeInstanceService()
683 // check for external programs which are defined by $info['exec']
684 if (trim($this->info
['exec'])) {
685 if (!$this->checkExec($this->info
['exec'])) {
686 // nothing todo here or?
690 return $this->getLastError();
695 * Resets the service.
696 * Will be called by init(). Should be used before every use if a service instance is used multiple times.
701 $this->unlinkTempFiles();
702 $this->resetErrors();
704 $this->inputFile
= '';
705 $this->inputContent
= '';
706 $this->inputType
= '';
707 $this->outputFile
= '';
711 * Clean up the service.
715 function __destruct() {
716 $this->unlinkTempFiles();
720 /* every service type has it's own API
721 function process($content='', $type='', $conf=array()) {
728 // Does not make sense, because this class is always extended by the service classes..
729 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]) {
730 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]);