TYPO3 CMS  TYPO3_8-7
AbstractService.php
Go to the documentation of this file.
1 <?php
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  */
18 
22 abstract class AbstractService
23 {
27  public $info = [];
28 
32  public $error = [];
33 
37  public $writeDevLog = false;
38 
42  public $out = '';
43 
47  public $inputFile = '';
48 
52  public $inputContent = '';
53 
57  public $inputType = '';
58 
62  public $outputFile = '';
63 
70  public $tempFiles = [];
71 
75  protected $shutdownRegistry = [];
76 
80  protected $prefixId = '';
81 
82  /***************************************
83  *
84  * Get service meta information
85  *
86  ***************************************/
92  public function getServiceInfo()
93  {
94  return $this->info;
95  }
96 
102  public function getServiceKey()
103  {
104  return $this->info['serviceKey'];
105  }
106 
112  public function getServiceTitle()
113  {
114  return $this->info['title'];
115  }
116 
125  public function getServiceOption($optionName, $defaultValue = '', $includeDefaultConfig = true)
126  {
127  $config = null;
128  $svOptions = $GLOBALS['TYPO3_CONF_VARS']['SVCONF'][$this->info['serviceType']];
129  if (isset($svOptions[$this->info['serviceKey']][$optionName])) {
130  $config = $svOptions[$this->info['serviceKey']][$optionName];
131  } elseif ($includeDefaultConfig && isset($svOptions['default'][$optionName])) {
132  $config = $svOptions['default'][$optionName];
133  }
134  if (!isset($config)) {
135  $config = $defaultValue;
136  }
137  return $config;
138  }
139 
140  /***************************************
141  *
142  * Error handling
143  *
144  ***************************************/
152  public function devLog($msg, $severity = 0, $dataVar = false)
153  {
154  if ($this->writeDevLog) {
155  GeneralUtility::devLog($msg, $this->info['serviceKey'], $severity, $dataVar);
156  }
157  }
158 
165  public function errorPush($errNum = T3_ERR_SV_GENERAL, $errMsg = 'Unspecified error occurred')
166  {
167  $this->error[] = ['nr' => $errNum, 'msg' => $errMsg];
169  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
170  $timeTracker->setTSlogMessage($errMsg, 2);
171  }
172 
176  public function errorPull()
177  {
178  array_pop($this->error);
179  }
180 
186  public function getLastError()
187  {
188  // Means all is ok - no error
189  $lastError = true;
190  if (!empty($this->error)) {
191  $error = end($this->error);
192  $lastError = $error['nr'];
193  }
194  return $lastError;
195  }
196 
202  public function getLastErrorMsg()
203  {
204  $lastErrorMessage = '';
205  if (!empty($this->error)) {
206  $error = end($this->error);
207  $lastErrorMessage = $error['msg'];
208  }
209  return $lastErrorMessage;
210  }
211 
217  public function getErrorMsgArray()
218  {
219  $errArr = [];
220  if (!empty($this->error)) {
221  foreach ($this->error as $error) {
222  $errArr[] = $error['msg'];
223  }
224  }
225  return $errArr;
226  }
227 
233  public function getLastErrorArray()
234  {
235  return end($this->error);
236  }
237 
241  public function resetErrors()
242  {
243  $this->error = [];
244  }
245 
246  /***************************************
247  *
248  * General service functions
249  *
250  ***************************************/
257  public function checkExec($progList)
258  {
259  $ret = true;
260  $progList = GeneralUtility::trimExplode(',', $progList, true);
261  foreach ($progList as $prog) {
262  if (!\TYPO3\CMS\Core\Utility\CommandUtility::checkCommand($prog)) {
263  // Program not found
264  $this->errorPush(T3_ERR_SV_PROG_NOT_FOUND, 'External program not found: ' . $prog);
265  $ret = false;
266  }
267  }
268  return $ret;
269  }
270 
274  public function deactivateService()
275  {
276  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
277  }
278 
279  /***************************************
280  *
281  * IO tools
282  *
283  ***************************************/
290  public function checkInputFile($absFile)
291  {
292  $checkResult = false;
293  if (GeneralUtility::isAllowedAbsPath($absFile) && @is_file($absFile)) {
294  if (@is_readable($absFile)) {
295  $checkResult = $absFile;
296  } else {
297  $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: ' . $absFile);
298  }
299  } else {
300  $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: ' . $absFile);
301  }
302  return $checkResult;
303  }
304 
312  public function readFile($absFile, $length = 0)
313  {
314  $out = false;
315  if ($this->checkInputFile($absFile)) {
316  $out = file_get_contents($absFile);
317  if ($out === false) {
318  $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: ' . $absFile);
319  }
320  }
321  return $out;
322  }
323 
331  public function writeFile($content, $absFile = '')
332  {
333  if (!$absFile) {
334  $absFile = $this->tempFile($this->prefixId);
335  }
336  if ($absFile && GeneralUtility::isAllowedAbsPath($absFile)) {
337  if ($fd = @fopen($absFile, 'wb')) {
338  @fwrite($fd, $content);
339  @fclose($fd);
340  } else {
341  $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: ' . $absFile);
342  $absFile = false;
343  }
344  }
345  return $absFile;
346  }
347 
354  public function tempFile($filePrefix)
355  {
356  $absFile = GeneralUtility::tempnam($filePrefix);
357  if ($absFile) {
358  $ret = $absFile;
359  $this->registerTempFile($absFile);
360  } else {
361  $ret = false;
362  $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
363  }
364  return $ret;
365  }
366 
372  public function registerTempFile($absFile)
373  {
374  if (!isset($this->shutdownRegistry[__METHOD__])) {
375  register_shutdown_function([$this, 'unlinkTempFiles']);
376  $this->shutdownRegistry[__METHOD__] = true;
377  }
378  $this->tempFiles[] = $absFile;
379  }
380 
384  public function unlinkTempFiles()
385  {
386  foreach ($this->tempFiles as $absFile) {
388  }
389  $this->tempFiles = [];
390  }
391 
392  /***************************************
393  *
394  * IO input
395  *
396  ***************************************/
403  public function setInput($content, $type = '')
404  {
405  $this->inputContent = $content;
406  $this->inputFile = '';
407  $this->inputType = $type;
408  }
409 
416  public function setInputFile($absFile, $type = '')
417  {
418  $this->inputContent = '';
419  $this->inputFile = $absFile;
420  $this->inputType = $type;
421  }
422 
429  public function getInput()
430  {
431  if ($this->inputContent == '') {
432  $this->inputContent = $this->readFile($this->inputFile);
433  }
434  return $this->inputContent;
435  }
436 
444  public function getInputFile($createFile = '')
445  {
446  if ($this->inputFile) {
447  $this->inputFile = $this->checkInputFile($this->inputFile);
448  } elseif ($this->inputContent) {
449  $this->inputFile = $this->writeFile($this->inputContent, $createFile);
450  }
451  return $this->inputFile;
452  }
453 
454  /***************************************
455  *
456  * IO output
457  *
458  ***************************************/
464  public function setOutputFile($absFile)
465  {
466  $this->outputFile = $absFile;
467  }
468 
474  public function getOutput()
475  {
476  if ($this->outputFile) {
477  $this->out = $this->readFile($this->outputFile);
478  }
479  return $this->out;
480  }
481 
488  public function getOutputFile($absFile = '')
489  {
490  if (!$this->outputFile) {
491  $this->outputFile = $this->writeFile($this->out, $absFile);
492  }
493  return $this->outputFile;
494  }
495 
496  /***************************************
497  *
498  * Service implementation
499  *
500  ***************************************/
509  public function init()
510  {
511  // look in makeInstanceService()
512  $this->reset();
513  // Check for external programs which are defined by $info['exec']
514  if (trim($this->info['exec'])) {
515  if (!$this->checkExec($this->info['exec'])) {
516  }
517  }
518  return $this->getLastError() === true;
519  }
520 
525  public function reset()
526  {
527  $this->unlinkTempFiles();
528  $this->resetErrors();
529  $this->out = '';
530  $this->inputFile = '';
531  $this->inputContent = '';
532  $this->inputType = '';
533  $this->outputFile = '';
534  }
535 
540  public function __destruct()
541  {
542  $this->unlinkTempFiles();
543  }
544 }
static devLog($msg, $extKey, $severity=0, $dataVar=false)
static unlink_tempfile($uploadedTempFileName)
getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=true)
devLog($msg, $severity=0, $dataVar=false)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
static tempnam($filePrefix, $fileSuffix='')
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']