"TYPO3 4.0.1: typo3_src-4.0.1/t3lib/class.t3lib_svbase.php Source File", "datetime" => "Sat Dec 2 19:22:19 2006", "date" => "2 Dec 2006", "doxygenversion" => "1.4.6", "projectname" => "TYPO3 4.0.1", "projectnumber" => "4.0.1" ); get_header($doxygen_vars); ?>
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the Typo3 project. The Typo3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00095 define ('T3_ERR_SV_GENERAL', -1); // General error - something went wrong 00096 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() 00097 define ('T3_ERR_SV_WRONG_SUBTYPE', -3); // passed subtype is not possible with this service 00098 define ('T3_ERR_SV_NO_INPUT', -4); // passed subtype is not possible with this service 00099 00100 00101 define ('T3_ERR_SV_FILE_NOT_FOUND', -20); // File not found which the service should process 00102 define ('T3_ERR_SV_FILE_READ', -21); // File not readable 00103 define ('T3_ERR_SV_FILE_WRITE', -22); // File not writable 00104 00105 define ('T3_ERR_SV_PROG_NOT_FOUND', -40); // passed subtype is not possible with this service 00106 define ('T3_ERR_SV_PROG_FAILED', -41); // passed subtype is not possible with this service 00107 00108 // define ('T3_ERR_SV_serviceType_myerr, -100); // All errors with prefix T3_ERR_SV_[serviceType]_ and lower than -99 are service type dependent error 00109 00110 00111 require_once(PATH_t3lib.'class.t3lib_exec.php'); 00112 00113 00114 00115 00116 00117 00125 class t3lib_svbase { 00126 00130 var $info=array(); 00131 00135 var $error=array(); 00136 00140 var $writeDevLog = false; 00141 00142 00147 var $out = ''; 00148 00152 var $inputFile = ''; 00153 00157 var $inputContent = ''; 00158 00162 var $inputType = ''; 00163 00167 var $outputFile = ''; 00168 00169 00175 var $tempFiles = array(); 00176 00177 00178 00179 /*************************************** 00180 * 00181 * Get service meta information 00182 * 00183 ***************************************/ 00184 00185 00191 function getServiceInfo() { 00192 return $this->info; 00193 } 00194 00195 00201 function getServiceKey() { 00202 return $this->info['serviceKey']; 00203 } 00204 00205 00211 function getServiceTitle() { 00212 return $this->info['title']; 00213 } 00214 00215 00224 function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE) { 00225 global $TYPO3_CONF_VARS; 00226 00227 $config = NULL; 00228 00229 $svOptions = $TYPO3_CONF_VARS['SVCONF'][$this->info['serviceType']]; 00230 00231 if(isset($svOptions[$this->info['serviceKey']][$optionName])) { 00232 $config = $svOptions[$this->info['serviceKey']][$optionName]; 00233 } elseif($includeDefaultConfig AND isset($svOptions['default'][$optionName])) { 00234 $config = $svOptions['default'][$optionName]; 00235 } 00236 if(!isset($config)) { 00237 $config = $defaultValue; 00238 } 00239 return $config; 00240 } 00241 00242 00243 00244 /*************************************** 00245 * 00246 * Error handling 00247 * 00248 ***************************************/ 00249 00250 00259 function devLog($msg, $severity=0, $dataVar=FALSE) { 00260 if($this->writeDevLog) { 00261 t3lib_div::devLog($msg, $this->info['serviceKey'], $severity, $dataVar); 00262 } 00263 } 00264 00265 00273 function errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occured') { 00274 array_push($this->error, array('nr'=>$errNum, 'msg'=>$errMsg)); 00275 00276 if (is_object($GLOBALS['TT'])) { 00277 $GLOBALS['TT']->setTSlogMessage($errMsg,2); 00278 } 00279 00280 } 00281 00282 00288 function errorPull() { 00289 array_pop($this->error); 00290 00291 // pop for $GLOBALS['TT']->setTSlogMessage is not supported 00292 } 00293 00294 00300 function getLastError() { 00301 if(count($this->error)) { 00302 $error = end($this->error); 00303 return $error['nr']; 00304 } else { 00305 return TRUE; // means all is ok - no error 00306 } 00307 } 00308 00309 00315 function getLastErrorMsg() { 00316 if(count($this->error)) { 00317 $error = end($this->error); 00318 return $error['msg']; 00319 } else { 00320 return ''; 00321 } 00322 } 00323 00324 00330 function getErrorMsgArray() { 00331 $errArr = array(); 00332 00333 if(count($this->error)) { 00334 reset($this->error); 00335 foreach($this->error as $error) { 00336 $errArr[] = $error['msg']; 00337 } 00338 } 00339 return $errArr; 00340 } 00341 00342 00348 function getLastErrorArray() { 00349 return end($this->error); 00350 } 00351 00357 function resetErrors() { 00358 $this->error=array(); 00359 } 00360 00361 00362 00363 /*************************************** 00364 * 00365 * General service functions 00366 * 00367 ***************************************/ 00368 00369 00370 00377 function checkExec($progList) { 00378 global $T3_VAR, $TYPO3_CONF_VARS; 00379 00380 $ret = TRUE; 00381 00382 require_once(PATH_t3lib.'class.t3lib_exec.php'); 00383 00384 $progList = t3lib_div::trimExplode(',', $progList, 1); 00385 foreach($progList as $prog) { 00386 if (!t3lib_exec::checkCommand($prog)) { 00387 // program not found 00388 $this->errorPush('External program not found: '.$prog, T3_ERR_SV_PROG_NOT_FOUND); 00389 $ret = FALSE; 00390 } 00391 } 00392 return $ret; 00393 } 00394 00395 00401 function deactivateService() { 00402 t3lib_extMgm::deactivateService($this->info['serviceType'], $this->info['serviceKey']); 00403 } 00404 00405 00406 00407 00408 00409 00410 00411 00412 00413 /*************************************** 00414 * 00415 * IO tools 00416 * 00417 ***************************************/ 00418 00419 00420 00427 function checkInputFile ($absFile) { 00428 if(t3lib_div::isAllowedAbsPath($absFile) && @is_file($absFile)) { 00429 if(@is_readable($absFile)) { 00430 return $absFile; 00431 } else { 00432 $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: '.$absFile); 00433 } 00434 } else { 00435 $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: '.$absFile); 00436 } 00437 return FALSE; 00438 } 00439 00440 00448 function readFile ($absFile, $length=0) { 00449 $out = FALSE; 00450 00451 if ($this->checkInputFile ($absFile)) { 00452 if ($fd = fopen ($absFile, 'rb')) { 00453 $length = intval($length) ? intval($length) : filesize ($absFile); 00454 if ($length > 0) { 00455 $out = fread ($fd, $length); 00456 } 00457 fclose ($fd); 00458 } else { 00459 $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: '.$absFile); 00460 } 00461 } 00462 return $out; 00463 } 00464 00465 00473 function writeFile ($content, $absFile='') { 00474 $ret = TRUE; 00475 00476 if (!$absFile) { 00477 $absFile = $this->tempFile($this->prefixId); 00478 } 00479 00480 if($absFile && t3lib_div::isAllowedAbsPath($absFile)) { 00481 if ($fd = @fopen($absFile,'wb')) { 00482 @fwrite($fd, $content); 00483 @fclose($fd); 00484 } else { 00485 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: '.$absFile); 00486 $absFile = FALSE; 00487 } 00488 } 00489 00490 return $absFile; 00491 } 00492 00499 function tempFile ($filePrefix) { 00500 $absFile = t3lib_div::tempnam($filePrefix); 00501 if($absFile) { 00502 $ret = TRUE; 00503 $this->registerTempFile ($absFile); 00504 } else { 00505 $ret = FALSE; 00506 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.'); 00507 } 00508 return ($ret ? $absFile : FALSE); 00509 } 00510 00517 function registerTempFile ($absFile) { 00518 $this->tempFiles[] = $absFile; 00519 } 00520 00527 function unlinkTempFiles () { 00528 foreach ($this->tempFiles as $absFile) { 00529 t3lib_div::unlink_tempfile($absFile); 00530 } 00531 $this->tempFiles = array(); 00532 } 00533 00534 00535 /*************************************** 00536 * 00537 * IO input 00538 * 00539 ***************************************/ 00540 00541 00549 function setInput ($content, $type='') { 00550 $this->inputContent = $content; 00551 $this->inputFile = ''; 00552 $this->inputType = $type; 00553 } 00554 00555 00563 function setInputFile ($absFile, $type='') { 00564 $this->inputContent = ''; 00565 $this->inputFile = $absFile; 00566 $this->inputType = $type; 00567 } 00568 00569 00576 function getInput () { 00577 if ($this->inputContent=='') { 00578 $this->inputContent = $this->readFile($this->inputFile); 00579 } 00580 return $this->inputContent; 00581 } 00582 00583 00591 function getInputFile ($createFile='') { 00592 if($this->inputFile) { 00593 $this->inputFile = $this->checkInputFile($this->inputFile); 00594 } elseif ($this->inputContent) { 00595 $this->inputFile = $this->writeFile($this->inputContent, $createFile); 00596 } 00597 return $this->inputFile; 00598 } 00599 00600 00601 00602 00603 /*************************************** 00604 * 00605 * IO output 00606 * 00607 ***************************************/ 00608 00609 00616 function setOutputFile ($absFile) { 00617 $this->outputFile = $absFile; 00618 } 00619 00620 00626 function getOutput () { 00627 if ($this->outputFile) { 00628 $this->out = $this->readFile($this->outputFile); 00629 } 00630 return $this->out; 00631 } 00632 00633 00640 function getOutputFile ($absFile='') { 00641 if (!$this->outputFile) { 00642 $this->outputFile = $this->writeFile($this->out, $absFile); 00643 } 00644 return $this->outputFile; 00645 } 00646 00647 00648 00649 00650 /*************************************** 00651 * 00652 * Service implementation 00653 * 00654 ***************************************/ 00655 00664 function init() { 00665 // do not work :-( but will not hurt 00666 register_shutdown_function(array(&$this, '__destruct')); 00667 // look in makeInstanceService() 00668 00669 $this->reset(); 00670 00671 // check for external programs which are defined by $info['exec'] 00672 if (trim($this->info['exec'])) { 00673 if (!$this->checkExec($this->info['exec'])) { 00674 // nothing todo here or? 00675 } 00676 } 00677 00678 return $this->getLastError(); 00679 } 00680 00681 00688 function reset() { 00689 $this->unlinkTempFiles(); 00690 $this->resetErrors(); 00691 $this->out = ''; 00692 $this->inputFile = ''; 00693 $this->inputContent = ''; 00694 $this->inputType = ''; 00695 $this->outputFile = ''; 00696 } 00697 00703 function __destruct() { 00704 $this->unlinkTempFiles(); 00705 } 00706 00707 00708 /* every service type has it's own API 00709 function process($content='', $type='', $conf=array()) { // 00710 } 00711 */ 00712 00713 } 00714 00722 ?>