Documentation TYPO3 par Ameos

class.t3lib_svbase.php

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 ***************************************************************/
00097 define ('T3_ERR_SV_GENERAL', -1); // General error - something went wrong
00098 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()
00099 define ('T3_ERR_SV_WRONG_SUBTYPE', -3); // passed subtype is not possible with this service
00100 define ('T3_ERR_SV_NO_INPUT', -4); // passed subtype is not possible with this service
00101 
00102 
00103 define ('T3_ERR_SV_FILE_NOT_FOUND', -20); // File not found which the service should process
00104 define ('T3_ERR_SV_FILE_READ', -21); // File not readable
00105 define ('T3_ERR_SV_FILE_WRITE', -22); // File not writable
00106 
00107 define ('T3_ERR_SV_PROG_NOT_FOUND', -40); // passed subtype is not possible with this service
00108 define ('T3_ERR_SV_PROG_FAILED', -41); // passed subtype is not possible with this service
00109 
00110 // define ('T3_ERR_SV_serviceType_myerr, -100); // All errors with prefix T3_ERR_SV_[serviceType]_ and lower than -99 are service type dependent error
00111 
00112 
00113 require_once(PATH_t3lib.'class.t3lib_exec.php');
00114 
00115 
00116 
00117 
00118 
00119 
00127 class t3lib_svbase {
00128 
00132         var $info=array();
00133 
00137         var $error=array();
00138 
00142         var $writeDevLog = false;
00143 
00144 
00149         var $out = '';
00150 
00154         var $inputFile = '';
00155 
00159         var $inputContent = '';
00160 
00164         var $inputType = '';
00165 
00169         var $outputFile = '';
00170 
00171 
00177         var $tempFiles = array();
00178 
00179 
00180 
00181         /***************************************
00182          *
00183          *       Get service meta information
00184          *
00185          ***************************************/
00186 
00187 
00193         function getServiceInfo() {
00194                 return $this->info;
00195         }
00196 
00197 
00203         function getServiceKey() {
00204                 return $this->info['serviceKey'];
00205         }
00206 
00207 
00213         function getServiceTitle() {
00214                 return $this->info['title'];
00215         }
00216 
00217 
00226         function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE) {
00227                 global $TYPO3_CONF_VARS;
00228 
00229                 $config = NULL;
00230 
00231                 $svOptions = $TYPO3_CONF_VARS['SVCONF'][$this->info['serviceType']];
00232 
00233                 if(isset($svOptions[$this->info['serviceKey']][$optionName])) {
00234                         $config = $svOptions[$this->info['serviceKey']][$optionName];
00235                 } elseif($includeDefaultConfig AND isset($svOptions['default'][$optionName])) {
00236                         $config = $svOptions['default'][$optionName];
00237                 }
00238                 if(!isset($config)) {
00239                         $config = $defaultValue;
00240                 }
00241                 return $config;
00242         }
00243 
00244 
00245 
00246         /***************************************
00247          *
00248          *       Error handling
00249          *
00250          ***************************************/
00251 
00252 
00261         function devLog($msg, $severity=0, $dataVar=FALSE) {
00262                 if($this->writeDevLog) {
00263                         t3lib_div::devLog($msg, $this->info['serviceKey'], $severity, $dataVar);
00264                 }
00265         }
00266 
00267 
00275         function errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occured') {
00276                 array_push($this->error, array('nr'=>$errNum, 'msg'=>$errMsg));
00277 
00278                 if (is_object($GLOBALS['TT'])) {
00279                         $GLOBALS['TT']->setTSlogMessage($errMsg,2);
00280                 }
00281 
00282         }
00283 
00284 
00290         function errorPull() {
00291                 array_pop($this->error);
00292 
00293                 // pop for $GLOBALS['TT']->setTSlogMessage is not supported
00294         }
00295 
00296 
00302         function getLastError() {
00303                 if(count($this->error)) {
00304                         $error = end($this->error);
00305                         return $error['nr'];
00306                 } else {
00307                         return TRUE; // means all is ok - no error
00308                 }
00309         }
00310 
00311 
00317         function getLastErrorMsg() {
00318                 if(count($this->error)) {
00319                         $error = end($this->error);
00320                         return $error['msg'];
00321                 } else {
00322                         return '';
00323                 }
00324         }
00325 
00326 
00332         function getErrorMsgArray() {
00333                 $errArr = array();
00334 
00335                 if(count($this->error)) {
00336                         reset($this->error);
00337                         foreach($this->error as $error) {
00338                                 $errArr[] = $error['msg'];
00339                         }
00340                 }
00341                 return $errArr;
00342         }
00343 
00344 
00350         function getLastErrorArray() {
00351                 return end($this->error);
00352         }
00353 
00359         function resetErrors() {
00360                 $this->error=array();
00361         }
00362 
00363 
00364 
00365         /***************************************
00366          *
00367          *       General service functions
00368          *
00369          ***************************************/
00370 
00371 
00372 
00379         function checkExec($progList) {
00380                 global $T3_VAR, $TYPO3_CONF_VARS;
00381 
00382                 $ret = TRUE;
00383 
00384                 require_once(PATH_t3lib.'class.t3lib_exec.php');
00385 
00386                 $progList = t3lib_div::trimExplode(',', $progList, 1);
00387                 foreach($progList as $prog) {
00388                         if (!t3lib_exec::checkCommand($prog)) {
00389                                         // program not found
00390                                 $this->errorPush('External program not found: '.$prog, T3_ERR_SV_PROG_NOT_FOUND);
00391                                 $ret = FALSE;
00392                         }
00393                 }
00394                 return $ret;
00395         }
00396 
00397 
00403         function deactivateService() {
00404                 t3lib_extMgm::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
00405         }
00406 
00407 
00408 
00409 
00410 
00411 
00412 
00413 
00414 
00415         /***************************************
00416          *
00417          *       IO tools
00418          *
00419          ***************************************/
00420 
00421 
00422 
00429         function checkInputFile ($absFile)      {
00430                 if(t3lib_div::isAllowedAbsPath($absFile) && @is_file($absFile)) {
00431                         if(@is_readable($absFile)) {
00432                                 return $absFile;
00433                         } else {
00434                                 $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: '.$absFile);
00435                         }
00436                 } else {
00437                         $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: '.$absFile);
00438                 }
00439                 return FALSE;
00440         }
00441 
00442 
00450         function readFile ($absFile, $length=0) {
00451                 $out = FALSE;
00452 
00453                 if ($this->checkInputFile ($absFile)) {
00454                         if ($fd = fopen ($absFile, 'rb')) {
00455                                 $length = intval($length) ? intval($length) : filesize ($absFile);
00456                                 if ($length > 0) {
00457                                         $out = fread ($fd, $length);
00458                                 }
00459                                 fclose ($fd);
00460                         } else {
00461                                 $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: '.$absFile);
00462                         }
00463                 }
00464                 return $out;
00465         }
00466 
00467 
00475         function writeFile ($content, $absFile='')      {
00476                 $ret = TRUE;
00477 
00478                 if (!$absFile) {
00479                         $absFile = $this->tempFile($this->prefixId);
00480                 }
00481 
00482                 if($absFile && t3lib_div::isAllowedAbsPath($absFile)) {
00483                         if ($fd = @fopen($absFile,'wb')) {
00484                                 @fwrite($fd, $content);
00485                                 @fclose($fd);
00486                         } else {
00487                                 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: '.$absFile);
00488                                 $absFile = FALSE;
00489                         }
00490                 }
00491 
00492                 return $absFile;
00493         }
00494 
00501         function tempFile ($filePrefix) {
00502                 $absFile = t3lib_div::tempnam($filePrefix);
00503                 if($absFile) {
00504                         $ret = TRUE;
00505                         $this->registerTempFile ($absFile);
00506                 } else {
00507                         $ret = FALSE;
00508                         $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
00509                 }
00510                 return ($ret ? $absFile : FALSE);
00511         }
00512 
00519         function registerTempFile ($absFile)    {
00520                 $this->tempFiles[] = $absFile;
00521         }
00522 
00529         function unlinkTempFiles ()     {
00530                 foreach ($this->tempFiles as $absFile) {
00531                         t3lib_div::unlink_tempfile($absFile);
00532                 }
00533                 $this->tempFiles = array();
00534         }
00535 
00536 
00537         /***************************************
00538          *
00539          *       IO input
00540          *
00541          ***************************************/
00542 
00543 
00551         function setInput ($content, $type='') {
00552                 $this->inputContent = $content;
00553                 $this->inputFile = '';
00554                 $this->inputType = $type;
00555         }
00556 
00557 
00565         function setInputFile ($absFile, $type='') {
00566                 $this->inputContent = '';
00567                 $this->inputFile = $absFile;
00568                 $this->inputType = $type;
00569         }
00570 
00571 
00578         function getInput () {
00579                 if ($this->inputContent=='') {
00580                         $this->inputContent = $this->readFile($this->inputFile);
00581                 }
00582                 return $this->inputContent;
00583         }
00584 
00585 
00593         function getInputFile ($createFile='') {
00594                 if($this->inputFile) {
00595                         $this->inputFile = $this->checkInputFile($this->inputFile);
00596                 } elseif ($this->inputContent) {
00597                         $this->inputFile = $this->writeFile($this->inputContent, $createFile);
00598                 }
00599                 return $this->inputFile;
00600         }
00601 
00602 
00603 
00604 
00605         /***************************************
00606          *
00607          *       IO output
00608          *
00609          ***************************************/
00610 
00611 
00618         function setOutputFile ($absFile) {
00619                 $this->outputFile = $absFile;
00620         }
00621 
00622 
00628         function getOutput () {
00629                 if ($this->outputFile) {
00630                         $this->out = $this->readFile($this->outputFile);
00631                 }
00632                 return $this->out;
00633         }
00634 
00635 
00642         function getOutputFile ($absFile='') {
00643                 if (!$this->outputFile) {
00644                         $this->outputFile = $this->writeFile($this->out, $absFile);
00645                 }
00646                 return $this->outputFile;
00647         }
00648 
00649 
00650 
00651 
00652         /***************************************
00653          *
00654          *       Service implementation
00655          *
00656          ***************************************/
00657 
00666         function init() {
00667                 // do not work :-(  but will not hurt
00668                 register_shutdown_function(array(&$this, '__destruct'));
00669                 // look in makeInstanceService()
00670 
00671                 $this->reset();
00672 
00673                         // check for external programs which are defined by $info['exec']
00674                 if (trim($this->info['exec'])) {
00675                         if (!$this->checkExec($this->info['exec'])) {
00676                                 // nothing todo here or?
00677                         }
00678                 }
00679 
00680                 return $this->getLastError();
00681         }
00682 
00683 
00690         function reset()        {
00691                 $this->unlinkTempFiles();
00692                 $this->resetErrors();
00693                 $this->out = '';
00694                 $this->inputFile = '';
00695                 $this->inputContent = '';
00696                 $this->inputType = '';
00697                 $this->outputFile = '';
00698         }
00699 
00705         function __destruct() {
00706                 $this->unlinkTempFiles();
00707         }
00708 
00709 
00710         /* every service type has it's own API
00711         function process($content='', $type='', $conf=array())  {       //
00712         }
00713         */
00714 
00715 }
00716 
00724 ?>


Généré par L'expert TYPO3 avec  doxygen 1.4.6