Documentation TYPO3 par Ameos

class.t3lib_svbase.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2007 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                         $out = file_get_contents($absFile);
00453                         if ($out===FALSE)       {
00454                                 $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: '.$absFile);
00455                         }
00456                 }
00457                 return $out;
00458         }
00459 
00460 
00468         function writeFile ($content, $absFile='')      {
00469                 $ret = TRUE;
00470 
00471                 if (!$absFile) {
00472                         $absFile = $this->tempFile($this->prefixId);
00473                 }
00474 
00475                 if($absFile && t3lib_div::isAllowedAbsPath($absFile)) {
00476                         if ($fd = @fopen($absFile,'wb')) {
00477                                 @fwrite($fd, $content);
00478                                 @fclose($fd);
00479                         } else {
00480                                 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: '.$absFile);
00481                                 $absFile = FALSE;
00482                         }
00483                 }
00484 
00485                 return $absFile;
00486         }
00487 
00494         function tempFile ($filePrefix) {
00495                 $absFile = t3lib_div::tempnam($filePrefix);
00496                 if($absFile) {
00497                         $ret = TRUE;
00498                         $this->registerTempFile ($absFile);
00499                 } else {
00500                         $ret = FALSE;
00501                         $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
00502                 }
00503                 return ($ret ? $absFile : FALSE);
00504         }
00505 
00512         function registerTempFile ($absFile)    {
00513                 $this->tempFiles[] = $absFile;
00514         }
00515 
00522         function unlinkTempFiles ()     {
00523                 foreach ($this->tempFiles as $absFile) {
00524                         t3lib_div::unlink_tempfile($absFile);
00525                 }
00526                 $this->tempFiles = array();
00527         }
00528 
00529 
00530         /***************************************
00531          *
00532          *       IO input
00533          *
00534          ***************************************/
00535 
00536 
00544         function setInput ($content, $type='') {
00545                 $this->inputContent = $content;
00546                 $this->inputFile = '';
00547                 $this->inputType = $type;
00548         }
00549 
00550 
00558         function setInputFile ($absFile, $type='') {
00559                 $this->inputContent = '';
00560                 $this->inputFile = $absFile;
00561                 $this->inputType = $type;
00562         }
00563 
00564 
00571         function getInput () {
00572                 if ($this->inputContent=='') {
00573                         $this->inputContent = $this->readFile($this->inputFile);
00574                 }
00575                 return $this->inputContent;
00576         }
00577 
00578 
00586         function getInputFile ($createFile='') {
00587                 if($this->inputFile) {
00588                         $this->inputFile = $this->checkInputFile($this->inputFile);
00589                 } elseif ($this->inputContent) {
00590                         $this->inputFile = $this->writeFile($this->inputContent, $createFile);
00591                 }
00592                 return $this->inputFile;
00593         }
00594 
00595 
00596 
00597 
00598         /***************************************
00599          *
00600          *       IO output
00601          *
00602          ***************************************/
00603 
00604 
00611         function setOutputFile ($absFile) {
00612                 $this->outputFile = $absFile;
00613         }
00614 
00615 
00621         function getOutput () {
00622                 if ($this->outputFile) {
00623                         $this->out = $this->readFile($this->outputFile);
00624                 }
00625                 return $this->out;
00626         }
00627 
00628 
00635         function getOutputFile ($absFile='') {
00636                 if (!$this->outputFile) {
00637                         $this->outputFile = $this->writeFile($this->out, $absFile);
00638                 }
00639                 return $this->outputFile;
00640         }
00641 
00642 
00643 
00644 
00645         /***************************************
00646          *
00647          *       Service implementation
00648          *
00649          ***************************************/
00650 
00659         function init() {
00660                 // do not work :-(  but will not hurt
00661                 register_shutdown_function(array(&$this, '__destruct'));
00662                 // look in makeInstanceService()
00663 
00664                 $this->reset();
00665 
00666                         // check for external programs which are defined by $info['exec']
00667                 if (trim($this->info['exec'])) {
00668                         if (!$this->checkExec($this->info['exec'])) {
00669                                 // nothing todo here or?
00670                         }
00671                 }
00672 
00673                 return $this->getLastError();
00674         }
00675 
00676 
00683         function reset()        {
00684                 $this->unlinkTempFiles();
00685                 $this->resetErrors();
00686                 $this->out = '';
00687                 $this->inputFile = '';
00688                 $this->inputContent = '';
00689                 $this->inputType = '';
00690                 $this->outputFile = '';
00691         }
00692 
00698         function __destruct() {
00699                 $this->unlinkTempFiles();
00700         }
00701 
00702 
00703         /* every service type has it's own API
00704         function process($content='', $type='', $conf=array())  {       //
00705         }
00706         */
00707 
00708 }
00709 
00717 ?>


Généré par Les spécialistes TYPO3 avec  doxygen 1.4.6