TYPO3  7.6
AbstractService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Service;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 abstract class AbstractService
21 {
25  public $info = array();
26 
30  public $error = array();
31 
35  public $writeDevLog = false;
36 
40  public $out = '';
41 
45  public $inputFile = '';
46 
50  public $inputContent = '';
51 
55  public $inputType = '';
56 
60  public $outputFile = '';
61 
68  public $tempFiles = array();
69 
73  protected $shutdownRegistry = array();
74 
78  protected $prefixId = '';
79 
80  /***************************************
81  *
82  * Get service meta information
83  *
84  ***************************************/
90  public function getServiceInfo()
91  {
92  return $this->info;
93  }
94 
100  public function getServiceKey()
101  {
102  return $this->info['serviceKey'];
103  }
104 
110  public function getServiceTitle()
111  {
112  return $this->info['title'];
113  }
114 
123  public function getServiceOption($optionName, $defaultValue = '', $includeDefaultConfig = true)
124  {
125  $config = null;
126  $svOptions = $GLOBALS['TYPO3_CONF_VARS']['SVCONF'][$this->info['serviceType']];
127  if (isset($svOptions[$this->info['serviceKey']][$optionName])) {
128  $config = $svOptions[$this->info['serviceKey']][$optionName];
129  } elseif ($includeDefaultConfig && isset($svOptions['default'][$optionName])) {
130  $config = $svOptions['default'][$optionName];
131  }
132  if (!isset($config)) {
133  $config = $defaultValue;
134  }
135  return $config;
136  }
137 
138  /***************************************
139  *
140  * Error handling
141  *
142  ***************************************/
151  public function devLog($msg, $severity = 0, $dataVar = false)
152  {
153  if ($this->writeDevLog) {
154  \TYPO3\CMS\Core\Utility\GeneralUtility::devLog($msg, $this->info['serviceKey'], $severity, $dataVar);
155  }
156  }
157 
165  public function errorPush($errNum = T3_ERR_SV_GENERAL, $errMsg = 'Unspecified error occurred')
166  {
167  array_push($this->error, array('nr' => $errNum, 'msg' => $errMsg));
168  if (is_object($GLOBALS['TT'])) {
169  $GLOBALS['TT']->setTSlogMessage($errMsg, 2);
170  }
171  }
172 
178  public function errorPull()
179  {
180  array_pop($this->error);
181  }
182 
188  public function getLastError()
189  {
190  // Means all is ok - no error
191  $lastError = true;
192  if (!empty($this->error)) {
193  $error = end($this->error);
194  $lastError = $error['nr'];
195  }
196  return $lastError;
197  }
198 
204  public function getLastErrorMsg()
205  {
206  $lastErrorMessage = '';
207  if (!empty($this->error)) {
208  $error = end($this->error);
209  $lastErrorMessage = $error['msg'];
210  }
211  return $lastErrorMessage;
212  }
213 
219  public function getErrorMsgArray()
220  {
221  $errArr = array();
222  if (!empty($this->error)) {
223  foreach ($this->error as $error) {
224  $errArr[] = $error['msg'];
225  }
226  }
227  return $errArr;
228  }
229 
235  public function getLastErrorArray()
236  {
237  return end($this->error);
238  }
239 
245  public function resetErrors()
246  {
247  $this->error = array();
248  }
249 
250  /***************************************
251  *
252  * General service functions
253  *
254  ***************************************/
261  public function checkExec($progList)
262  {
263  $ret = true;
264  $progList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $progList, true);
265  foreach ($progList as $prog) {
266  if (!\TYPO3\CMS\Core\Utility\CommandUtility::checkCommand($prog)) {
267  // Program not found
268  $this->errorPush(T3_ERR_SV_PROG_NOT_FOUND, 'External program not found: ' . $prog);
269  $ret = false;
270  }
271  }
272  return $ret;
273  }
274 
280  public function deactivateService()
281  {
282  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
283  }
284 
285  /***************************************
286  *
287  * IO tools
288  *
289  ***************************************/
296  public function checkInputFile($absFile)
297  {
298  $checkResult = false;
299  if (\TYPO3\CMS\Core\Utility\GeneralUtility::isAllowedAbsPath($absFile) && @is_file($absFile)) {
300  if (@is_readable($absFile)) {
301  $checkResult = $absFile;
302  } else {
303  $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: ' . $absFile);
304  }
305  } else {
306  $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: ' . $absFile);
307  }
308  return $checkResult;
309  }
310 
318  public function readFile($absFile, $length = 0)
319  {
320  $out = false;
321  if ($this->checkInputFile($absFile)) {
322  $out = file_get_contents($absFile);
323  if ($out === false) {
324  $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: ' . $absFile);
325  }
326  }
327  return $out;
328  }
329 
337  public function writeFile($content, $absFile = '')
338  {
339  if (!$absFile) {
340  $absFile = $this->tempFile($this->prefixId);
341  }
342  if ($absFile && \TYPO3\CMS\Core\Utility\GeneralUtility::isAllowedAbsPath($absFile)) {
343  if ($fd = @fopen($absFile, 'wb')) {
344  @fwrite($fd, $content);
345  @fclose($fd);
346  } else {
347  $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: ' . $absFile);
348  $absFile = false;
349  }
350  }
351  return $absFile;
352  }
353 
360  public function tempFile($filePrefix)
361  {
362  $absFile = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam($filePrefix);
363  if ($absFile) {
364  $ret = $absFile;
365  $this->registerTempFile($absFile);
366  } else {
367  $ret = false;
368  $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
369  }
370  return $ret;
371  }
372 
379  public function registerTempFile($absFile)
380  {
381  if (!isset($this->shutdownRegistry[__METHOD__])) {
382  register_shutdown_function(array($this, 'unlinkTempFiles'));
383  $this->shutdownRegistry[__METHOD__] = true;
384  }
385  $this->tempFiles[] = $absFile;
386  }
387 
393  public function unlinkTempFiles()
394  {
395  foreach ($this->tempFiles as $absFile) {
396  \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($absFile);
397  }
398  $this->tempFiles = array();
399  }
400 
401  /***************************************
402  *
403  * IO input
404  *
405  ***************************************/
413  public function setInput($content, $type = '')
414  {
415  $this->inputContent = $content;
416  $this->inputFile = '';
417  $this->inputType = $type;
418  }
419 
427  public function setInputFile($absFile, $type = '')
428  {
429  $this->inputContent = '';
430  $this->inputFile = $absFile;
431  $this->inputType = $type;
432  }
433 
440  public function getInput()
441  {
442  if ($this->inputContent == '') {
443  $this->inputContent = $this->readFile($this->inputFile);
444  }
445  return $this->inputContent;
446  }
447 
455  public function getInputFile($createFile = '')
456  {
457  if ($this->inputFile) {
458  $this->inputFile = $this->checkInputFile($this->inputFile);
459  } elseif ($this->inputContent) {
460  $this->inputFile = $this->writeFile($this->inputContent, $createFile);
461  }
462  return $this->inputFile;
463  }
464 
465  /***************************************
466  *
467  * IO output
468  *
469  ***************************************/
476  public function setOutputFile($absFile)
477  {
478  $this->outputFile = $absFile;
479  }
480 
486  public function getOutput()
487  {
488  if ($this->outputFile) {
489  $this->out = $this->readFile($this->outputFile);
490  }
491  return $this->out;
492  }
493 
500  public function getOutputFile($absFile = '')
501  {
502  if (!$this->outputFile) {
503  $this->outputFile = $this->writeFile($this->out, $absFile);
504  }
505  return $this->outputFile;
506  }
507 
508  /***************************************
509  *
510  * Service implementation
511  *
512  ***************************************/
521  public function init()
522  {
523  // look in makeInstanceService()
524  $this->reset();
525  // Check for external programs which are defined by $info['exec']
526  if (trim($this->info['exec'])) {
527  if (!$this->checkExec($this->info['exec'])) {
528  }
529  }
530  return $this->getLastError() === true;
531  }
532 
539  public function reset()
540  {
541  $this->unlinkTempFiles();
542  $this->resetErrors();
543  $this->out = '';
544  $this->inputFile = '';
545  $this->inputContent = '';
546  $this->inputType = '';
547  $this->outputFile = '';
548  }
549 
556  public function __destruct()
557  {
558  $this->unlinkTempFiles();
559  }
560 }