TYPO3 CMS  TYPO3_6-2
AbstractService.php
Go to the documentation of this file.
1 <?php
3 
22 abstract class AbstractService {
23 
28  public $info = array();
29 
34  public $error = array();
35 
40  public $writeDevLog = FALSE;
41 
46  public $out = '';
47 
52  public $inputFile = '';
53 
58  public $inputContent = '';
59 
64  public $inputType = '';
65 
70  public $outputFile = '';
71 
78  public $tempFiles = array();
79 
83  protected $shutdownRegistry = array();
84 
88  protected $prefixId = '';
89 
90  /***************************************
91  *
92  * Get service meta information
93  *
94  ***************************************/
101  public function getServiceInfo() {
102  return $this->info;
103  }
104 
111  public function getServiceKey() {
112  return $this->info['serviceKey'];
113  }
114 
121  public function getServiceTitle() {
122  return $this->info['title'];
123  }
124 
134  public function getServiceOption($optionName, $defaultValue = '', $includeDefaultConfig = TRUE) {
135  $config = NULL;
136  $svOptions = $GLOBALS['TYPO3_CONF_VARS']['SVCONF'][$this->info['serviceType']];
137  if (isset($svOptions[$this->info['serviceKey']][$optionName])) {
138  $config = $svOptions[$this->info['serviceKey']][$optionName];
139  } elseif ($includeDefaultConfig && isset($svOptions['default'][$optionName])) {
140  $config = $svOptions['default'][$optionName];
141  }
142  if (!isset($config)) {
143  $config = $defaultValue;
144  }
145  return $config;
146  }
147 
148  /***************************************
149  *
150  * Error handling
151  *
152  ***************************************/
162  public function devLog($msg, $severity = 0, $dataVar = FALSE) {
163  if ($this->writeDevLog) {
164  \TYPO3\CMS\Core\Utility\GeneralUtility::devLog($msg, $this->info['serviceKey'], $severity, $dataVar);
165  }
166  }
167 
176  public function errorPush($errNum = T3_ERR_SV_GENERAL, $errMsg = 'Unspecified error occurred') {
177  array_push($this->error, array('nr' => $errNum, 'msg' => $errMsg));
178  if (is_object($GLOBALS['TT'])) {
179  $GLOBALS['TT']->setTSlogMessage($errMsg, 2);
180  }
181  }
182 
189  public function errorPull() {
190  array_pop($this->error);
191  }
192 
199  public function getLastError() {
200  // Means all is ok - no error
201  $lastError = TRUE;
202  if (count($this->error)) {
203  $error = end($this->error);
204  $lastError = $error['nr'];
205  }
206  return $lastError;
207  }
208 
215  public function getLastErrorMsg() {
216  $lastErrorMessage = '';
217  if (count($this->error)) {
218  $error = end($this->error);
219  $lastErrorMessage = $error['msg'];
220  }
221  return $lastErrorMessage;
222  }
223 
230  public function getErrorMsgArray() {
231  $errArr = array();
232  if (count($this->error)) {
233  foreach ($this->error as $error) {
234  $errArr[] = $error['msg'];
235  }
236  }
237  return $errArr;
238  }
239 
246  public function getLastErrorArray() {
247  return end($this->error);
248  }
249 
256  public function resetErrors() {
257  $this->error = array();
258  }
259 
260  /***************************************
261  *
262  * General service functions
263  *
264  ***************************************/
272  public function checkExec($progList) {
273  $ret = TRUE;
274  $progList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $progList, TRUE);
275  foreach ($progList as $prog) {
276  if (!\TYPO3\CMS\Core\Utility\CommandUtility::checkCommand($prog)) {
277  // Program not found
278  $this->errorPush(T3_ERR_SV_PROG_NOT_FOUND, 'External program not found: ' . $prog);
279  $ret = FALSE;
280  }
281  }
282  return $ret;
283  }
284 
291  public function deactivateService() {
292  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
293  }
294 
295  /***************************************
296  *
297  * IO tools
298  *
299  ***************************************/
307  public function checkInputFile($absFile) {
308  $checkResult = FALSE;
309  if (\TYPO3\CMS\Core\Utility\GeneralUtility::isAllowedAbsPath($absFile) && @is_file($absFile)) {
310  if (@is_readable($absFile)) {
311  $checkResult = $absFile;
312  } else {
313  $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: ' . $absFile);
314  }
315  } else {
316  $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: ' . $absFile);
317  }
318  return $checkResult;
319  }
320 
329  public function readFile($absFile, $length = 0) {
330  $out = FALSE;
331  if ($this->checkInputFile($absFile)) {
332  $out = file_get_contents($absFile);
333  if ($out === FALSE) {
334  $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: ' . $absFile);
335  }
336  }
337  return $out;
338  }
339 
348  public function writeFile($content, $absFile = '') {
349  if (!$absFile) {
350  $absFile = $this->tempFile($this->prefixId);
351  }
352  if ($absFile && \TYPO3\CMS\Core\Utility\GeneralUtility::isAllowedAbsPath($absFile)) {
353  if ($fd = @fopen($absFile, 'wb')) {
354  @fwrite($fd, $content);
355  @fclose($fd);
356  } else {
357  $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: ' . $absFile);
358  $absFile = FALSE;
359  }
360  }
361  return $absFile;
362  }
363 
371  public function tempFile($filePrefix) {
372  $absFile = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam($filePrefix);
373  if ($absFile) {
374  $ret = $absFile;
375  $this->registerTempFile($absFile);
376  } else {
377  $ret = FALSE;
378  $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
379  }
380  return $ret;
381  }
382 
390  public function registerTempFile($absFile) {
391  if (!isset($this->shutdownRegistry[__METHOD__])) {
392  register_shutdown_function(array($this, 'unlinkTempFiles'));
393  $this->shutdownRegistry[__METHOD__] = TRUE;
394  }
395  $this->tempFiles[] = $absFile;
396  }
397 
404  public function unlinkTempFiles() {
405  foreach ($this->tempFiles as $absFile) {
407  }
408  $this->tempFiles = array();
409  }
410 
411  /***************************************
412  *
413  * IO input
414  *
415  ***************************************/
424  public function setInput($content, $type = '') {
425  $this->inputContent = $content;
426  $this->inputFile = '';
427  $this->inputType = $type;
428  }
429 
438  public function setInputFile($absFile, $type = '') {
439  $this->inputContent = '';
440  $this->inputFile = $absFile;
441  $this->inputType = $type;
442  }
443 
451  public function getInput() {
452  if ($this->inputContent == '') {
453  $this->inputContent = $this->readFile($this->inputFile);
454  }
455  return $this->inputContent;
456  }
457 
466  public function getInputFile($createFile = '') {
467  if ($this->inputFile) {
468  $this->inputFile = $this->checkInputFile($this->inputFile);
469  } elseif ($this->inputContent) {
470  $this->inputFile = $this->writeFile($this->inputContent, $createFile);
471  }
472  return $this->inputFile;
473  }
474 
475  /***************************************
476  *
477  * IO output
478  *
479  ***************************************/
487  public function setOutputFile($absFile) {
488  $this->outputFile = $absFile;
489  }
490 
497  public function getOutput() {
498  if ($this->outputFile) {
499  $this->out = $this->readFile($this->outputFile);
500  }
501  return $this->out;
502  }
503 
511  public function getOutputFile($absFile = '') {
512  if (!$this->outputFile) {
513  $this->outputFile = $this->writeFile($this->out, $absFile);
514  }
515  return $this->outputFile;
516  }
517 
518  /***************************************
519  *
520  * Service implementation
521  *
522  ***************************************/
532  public function init() {
533  // look in makeInstanceService()
534  $this->reset();
535  // Check for external programs which are defined by $info['exec']
536  if (trim($this->info['exec'])) {
537  if (!$this->checkExec($this->info['exec'])) {
538 
539  }
540  }
541  return $this->getLastError() === TRUE;
542  }
543 
551  public function reset() {
552  $this->unlinkTempFiles();
553  $this->resetErrors();
554  $this->out = '';
555  $this->inputFile = '';
556  $this->inputContent = '';
557  $this->inputType = '';
558  $this->outputFile = '';
559  }
560 
568  public function __destruct() {
569  $this->unlinkTempFiles();
570  }
571 
572 }
static unlink_tempfile($uploadedTempFileName)
static devLog($msg, $extKey, $severity=0, $dataVar=FALSE)
getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=TRUE)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
devLog($msg, $severity=0, $dataVar=FALSE)
static tempnam($filePrefix, $fileSuffix='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occurred')