Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

class.t3lib_svbase.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2004 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 writeable
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 
00191    function getServiceInfo() {
00192       return $this->info;
00193    }
00194 
00195 
00199    function getServiceKey() {
00200       return $this->info['serviceKey'];
00201    }
00202 
00203 
00207    function getServiceTitle() {
00208       return $this->info['title'];
00209    }
00210 
00211 
00220    function getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE) {
00221       global $TYPO3_CONF_VARS;
00222 
00223       $config = NULL;
00224 
00225       $svOptions = $TYPO3_CONF_VARS['SVCONF'][$this->info['serviceType']];
00226 
00227       if(isset($svOptions[$this->info['serviceKey']][$optionName])) {
00228          $config = $svOptions['default'][$optionName];
00229       } elseif($includeDefaultConfig AND isset($svOptions['default'][$optionName])) {
00230          $config = $svOptions['default'][$optionName];
00231       }
00232       if(!isset($config)) {
00233          $config = $defaultValue;
00234       }
00235       return $config;
00236    }
00237 
00238 
00239 
00240    /***************************************
00241     *
00242     *  Error handling
00243     *
00244     ***************************************/
00245 
00246 
00255    function devLog($msg, $severity=0, $dataVar=FALSE) {
00256       if($this->writeDevLog) {
00257          t3lib_div::devLog($msg, $this->info['serviceKey'], $severity, $dataVar);
00258       }
00259    }
00260 
00261 
00269    function errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occured') {
00270       array_push($this->error, array('nr'=>$errNum, 'msg'=>$errMsg));
00271 
00272       if (is_object($GLOBALS["TT"])) {
00273          $GLOBALS['TT']->setTSlogMessage($errMsg,2);
00274       }
00275 
00276    }
00277 
00278 
00284    function errorPull() {
00285       array_pop($this->error);
00286 
00287       // pop for $GLOBALS['TT']->setTSlogMessage is not supported
00288    }
00289 
00290 
00296    function getLastError() {
00297       if(count($this->error)) {
00298          $error = end($this->error);
00299          return $error['nr'];
00300       } else {
00301          return TRUE; // means all is ok - no error
00302       }
00303    }
00304 
00305 
00311    function getLastErrorMsg() {
00312       if(count($this->error)) {
00313          $error = end($this->error);
00314          return $error['msg'];
00315       } else {
00316          return '';
00317       }
00318    }
00319 
00320 
00326    function getErrorMsgArray() {
00327       $errArr = array();
00328 
00329       if(count($this->error)) {
00330          reset($this->error);
00331          foreach($this->error as $error) {
00332             $errArr[] = $error['msg'];
00333          }
00334       }
00335       return $errArr;
00336    }
00337 
00338 
00344    function getLastErrorArray() {
00345       return end($this->error);
00346    }
00347 
00353    function resetErrors() {
00354       $this->error=array();
00355    }
00356 
00357 
00358 
00359    /***************************************
00360     *
00361     *  General service functions
00362     *
00363     ***************************************/
00364 
00365 
00366 
00373    function checkExec($progList) {
00374       $ret = TRUE;
00375 
00376       require_once(PATH_t3lib.'class.t3lib_exec.php');
00377 
00378       $progList = t3lib_div::trimExplode(',', $progList, 1);
00379       foreach($progList as $prog) {
00380          if (!t3lib_exec::checkCommand($prog)) {
00381                // program not found
00382             $this->errorPush('External program not found: '.$prog, T3_ERR_SV_PROG_NOT_FOUND);
00383             $ret = FALSE;
00384          }
00385       }
00386       return $ret;
00387    }
00388 
00389 
00395    function deactivateService() {
00396       t3lib_extMgm::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
00397    }
00398 
00399 
00425    /***************************************
00426     *
00427     *  IO tools
00428     *
00429     ***************************************/
00430 
00431 
00432 
00439    function checkInputFile ($absFile)  {
00440       if(@is_file($absFile)) {
00441          if(@is_readable($absFile)) {
00442             return $absFile;
00443          } else {
00444             $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: '.$absFile);
00445          }
00446       } else {
00447          $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: '.$absFile);
00448       }
00449       return FALSE;
00450    }
00451 
00452 
00460    function readFile ($absFile, $length=0)   {
00461       $out = FALSE;
00462 
00463       if ($this->checkInputFile ($absFile)) {
00464          if ($fd = fopen ($absFile, 'rb')) {
00465             $length = intval($length) ? intval($length) : filesize ($absFile);
00466             if ($length > 0) {
00467                $out = fread ($fd, $length);
00468             }
00469             fclose ($fd);
00470          } else {
00471             $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: '.$absFile);
00472          }
00473       }
00474       return $out;
00475    }
00476 
00477 
00485    function writeFile ($content, $absFile='')   {
00486       $ret = TRUE;
00487 
00488       if (!$absFile) {
00489          $absFile = $this->tempFile($this->prefixId);
00490       }
00491 
00492       if($absFile) {
00493          if ($fd = @fopen($absFile,'wb')) {
00494             @fwrite($fd, $content);
00495             @fclose($fd);
00496          } else {
00497             $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: '.$absFile);
00498             $absFile = FALSE;
00499          }
00500       }
00501 
00502       return $absFile;
00503    }
00504 
00511    function tempFile ($filePrefix)  {
00512       $absFile = t3lib_div::tempnam($filePrefix);
00513       if($absFile) {
00514          $ret = TRUE;
00515          $this->registerTempFile ($absFile);
00516       } else {
00517          $ret = FALSE;
00518          $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
00519       }
00520       return ($ret ? $absFile : FALSE);
00521    }
00522 
00529    function registerTempFile ($absFile)   {
00530       $this->tempFiles[] = $absFile;
00531    }
00532 
00539    function unlinkTempFiles ()   {
00540       foreach ($this->tempFiles as $absFile) {
00541          t3lib_div::unlink_tempfile($absFile);
00542       }
00543       $this->tempFiles = array();
00544    }
00545 
00546 
00547    /***************************************
00548     *
00549     *  IO input
00550     *
00551     ***************************************/
00552 
00553 
00561    function setInput ($content, $type='') {
00562       $this->inputContent = $content;
00563       $this->inputFile = '';
00564       $this->inputType = $type;
00565    }
00566 
00567 
00575    function setInputFile ($absFile, $type='') {
00576       $this->inputContent = '';
00577       $this->inputFile = $absFile;
00578       $this->inputType = $type;
00579    }
00580 
00581 
00588    function getInput () {
00589       if ($this->inputContent=='') {
00590          $this->inputContent = $this->readFile($this->inputFile);
00591       }
00592       return $this->inputContent;
00593    }
00594 
00595 
00603    function getInputFile ($createFile='') {
00604       if($this->inputFile) {
00605          $this->inputFile = $this->checkInputFile($this->inputFile);
00606       } elseif ($this->inputContent) {
00607          $this->inputFile = $this->writeFile($this->inputContent, $createFile);
00608       }
00609       return $this->inputFile;
00610    }
00611 
00612 
00613 
00614 
00615    /***************************************
00616     *
00617     *  IO output
00618     *
00619     ***************************************/
00620 
00621 
00628    function setOutputFile ($absFile) {
00629       $this->outputFile = $absFile;
00630    }
00631 
00632 
00638    function getOutput () {
00639       if ($this->outputFile) {
00640          $this->out = $this->readFile($this->outputFile);
00641       }
00642       return $this->out;
00643    }
00644 
00645 
00652    function getOutputFile ($absFile='') {
00653       if (!$this->outputFile) {
00654          $this->outputFile = $this->writeFile($this->out, $absFile);
00655       }
00656       return $this->outputFile;
00657    }
00658 
00659 
00660 
00661 
00662    /***************************************
00663     *
00664     *  Service implementation
00665     *
00666     ***************************************/
00667 
00676    function init()   {
00677       // do not work :-(  but will not hurt
00678       register_shutdown_function(array(&$this, '__destruct'));
00679       // look in makeInstanceService()
00680 
00681       $this->reset();
00682 
00683          // check for external programs which are defined by $info['exec']
00684       if (trim($this->info['exec'])) {
00685          if (!$this->checkExec($this->info['exec'])) {
00686             // nothing todo here or?
00687          }
00688       }
00689 
00690       return $this->getLastError();
00691    }
00692 
00693 
00700    function reset()  {
00701       $this->unlinkTempFiles();
00702       $this->resetErrors();
00703       $this->out = '';
00704       $this->inputFile = '';
00705       $this->inputContent = '';
00706       $this->inputType = '';
00707       $this->outputFile = '';
00708    }
00709 
00715    function __destruct() {
00716       $this->unlinkTempFiles();
00717    }
00718 
00719 
00720    /* every service type has it's own API
00721    function process($content='', $type='', $conf=array())   {
00722    }
00723    */
00724 
00725 }
00726 
00734 ?>

Generated on Sun Oct 3 01:05:50 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913