00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00095 define ('T3_ERR_SV_GENERAL', -1);
00096 define ('T3_ERR_SV_NOT_AVAIL', -2);
00097 define ('T3_ERR_SV_WRONG_SUBTYPE', -3);
00098 define ('T3_ERR_SV_NO_INPUT', -4);
00099
00100
00101 define ('T3_ERR_SV_FILE_NOT_FOUND', -20);
00102 define ('T3_ERR_SV_FILE_READ', -21);
00103 define ('T3_ERR_SV_FILE_WRITE', -22);
00104
00105 define ('T3_ERR_SV_PROG_NOT_FOUND', -40);
00106 define ('T3_ERR_SV_PROG_FAILED', -41);
00107
00108
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
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
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
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;
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
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
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
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
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
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
00648
00649
00650
00659 function init() {
00660
00661 register_shutdown_function(array(&$this, '__destruct'));
00662
00663
00664 $this->reset();
00665
00666
00667 if (trim($this->info['exec'])) {
00668 if (!$this->checkExec($this->info['exec'])) {
00669
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
00704
00705
00706
00707
00708 }
00709
00717 ?>