‪TYPO3CMS  9.5
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  */
16 
17 use Psr\Log\LoggerAwareInterface;
18 use Psr\Log\LoggerAwareTrait;
24 
28 abstract class ‪AbstractService implements LoggerAwareInterface
29 {
31  use LoggerAwareTrait;
32 
33  // General error - something went wrong
34  const ‪ERROR_GENERAL = -1;
35 
36  // During execution it showed that the service is not available and
37  // should be ignored. The service itself should call $this->setNonAvailable()
39 
40  // Passed subtype is not possible with this service
42 
43  // Passed subtype is not possible with this service
44  const ‪ERROR_NO_INPUT = -4;
45 
46  // File not found which the service should process
48 
49  // File not readable
51 
52  // File not writable
53  // @todo: check writeable vs. writable
55 
56  // Passed subtype is not possible with this service
58 
59  // Passed subtype is not possible with this service
64  public ‪$info = [];
65 
69  public ‪$error = [];
70 
74  public ‪$out = '';
75 
79  public ‪$inputFile = '';
80 
84  public ‪$inputContent = '';
85 
89  public ‪$inputType = '';
90 
94  public ‪$outputFile = '';
95 
102  public ‪$tempFiles = [];
103 
107  protected ‪$shutdownRegistry = [];
108 
112  protected ‪$prefixId = '';
113 
114  /***************************************
115  *
116  * Get service meta information
117  *
118  ***************************************/
124  public function ‪getServiceInfo()
125  {
126  return ‪$this->info;
127  }
128 
134  public function ‪getServiceKey()
135  {
136  return $this->info['serviceKey'];
137  }
138 
144  public function ‪getServiceTitle()
145  {
146  return $this->info['title'];
147  }
148 
157  public function ‪getServiceOption($optionName, $defaultValue = '', $includeDefaultConfig = true)
158  {
159  $config = null;
160  $serviceType = $this->info['serviceType'] ?? '';
161  $serviceKey = $this->info['serviceKey'] ?? '';
162  $svOptions = ‪$GLOBALS['TYPO3_CONF_VARS']['SVCONF'][$serviceType] ?? [];
163  if (isset($svOptions[$serviceKey][$optionName])) {
164  $config = $svOptions[$serviceKey][$optionName];
165  } elseif ($includeDefaultConfig && isset($svOptions['default'][$optionName])) {
166  $config = $svOptions['default'][$optionName];
167  }
168  if (!isset($config)) {
169  $config = $defaultValue;
170  }
171  return $config;
172  }
173 
174  /***************************************
175  *
176  * Error handling
177  *
178  ***************************************/
187  public function ‪devLog($msg, $severity = 0, $dataVar = false)
188  {
189  trigger_error('AbstractService->devLog() will be removed with TYPO3 v10.0.', E_USER_DEPRECATED);
190  $this->logger->debug($this->info['serviceKey'] . ': ' . $msg, (array)$dataVar);
191  }
192 
199  public function ‪errorPush($errNum = self::ERROR_GENERAL, $errMsg = 'Unspecified error occurred')
200  {
201  $this->error[] = ['nr' => $errNum, 'msg' => $errMsg];
203  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
204  $timeTracker->setTSlogMessage($errMsg, 2);
205  }
206 
210  public function ‪errorPull()
211  {
212  array_pop($this->error);
213  }
214 
220  public function ‪getLastError()
221  {
222  // Means all is ok - no error
223  $lastError = true;
224  if (!empty($this->error)) {
225  ‪$error = end($this->error);
226  $lastError = ‪$error['nr'];
227  }
228  return $lastError;
229  }
230 
236  public function ‪getLastErrorMsg()
237  {
238  $lastErrorMessage = '';
239  if (!empty($this->error)) {
240  ‪$error = end($this->error);
241  $lastErrorMessage = ‪$error['msg'];
242  }
243  return $lastErrorMessage;
244  }
245 
251  public function ‪getErrorMsgArray()
252  {
253  $errArr = [];
254  if (!empty($this->error)) {
255  foreach ($this->error as ‪$error) {
256  $errArr[] = ‪$error['msg'];
257  }
258  }
259  return $errArr;
260  }
261 
267  public function ‪getLastErrorArray()
268  {
269  return end($this->error);
270  }
271 
275  public function ‪resetErrors()
276  {
277  $this->error = [];
278  }
279 
280  /***************************************
281  *
282  * General service functions
283  *
284  ***************************************/
291  public function ‪checkExec($progList)
292  {
293  $ret = true;
294  $progList = GeneralUtility::trimExplode(',', $progList, true);
295  foreach ($progList as $prog) {
296  if (!‪CommandUtility::checkCommand($prog)) {
297  // Program not found
298  $this->‪errorPush(self::ERROR_PROGRAM_NOT_FOUND, 'External program not found: ' . $prog);
299  $ret = false;
300  }
301  }
302  return $ret;
303  }
304 
308  public function ‪deactivateService()
309  {
310  ‪ExtensionManagementUtility::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
311  }
312 
313  /***************************************
314  *
315  * IO tools
316  *
317  ***************************************/
324  public function ‪checkInputFile($absFile)
325  {
326  $checkResult = false;
327  if (GeneralUtility::isAllowedAbsPath($absFile) && @is_file($absFile)) {
328  if (@is_readable($absFile)) {
329  $checkResult = $absFile;
330  } else {
331  $this->‪errorPush(self::ERROR_FILE_NOT_READABLE, 'File is not readable: ' . $absFile);
332  }
333  } else {
334  $this->‪errorPush(self::ERROR_FILE_NOT_FOUND, 'File not found: ' . $absFile);
335  }
336  return $checkResult;
337  }
338 
346  public function ‪readFile($absFile, $length = 0)
347  {
348  ‪$out = false;
349  if ($this->‪checkInputFile($absFile)) {
350  ‪$out = file_get_contents($absFile);
351  if (‪$out === false) {
352  $this->‪errorPush(self::ERROR_FILE_NOT_READABLE, 'Can not read from file: ' . $absFile);
353  }
354  }
355  return ‪$out;
356  }
357 
365  public function ‪writeFile($content, $absFile = '')
366  {
367  if (!$absFile) {
368  $absFile = $this->‪tempFile($this->prefixId);
369  }
370  if ($absFile && GeneralUtility::isAllowedAbsPath($absFile)) {
371  if ($fd = @fopen($absFile, 'wb')) {
372  @fwrite($fd, $content);
373  @fclose($fd);
374  } else {
375  $this->‪errorPush(self::ERROR_FILE_NOT_WRITEABLE, 'Can not write to file: ' . $absFile);
376  $absFile = false;
377  }
378  }
379  return $absFile;
380  }
381 
388  public function ‪tempFile($filePrefix)
389  {
390  $absFile = GeneralUtility::tempnam($filePrefix);
391  if ($absFile) {
392  $ret = $absFile;
393  $this->‪registerTempFile($absFile);
394  } else {
395  $ret = false;
396  $this->‪errorPush(self::ERROR_FILE_NOT_WRITEABLE, 'Can not create temp file.');
397  }
398  return $ret;
399  }
400 
406  public function ‪registerTempFile($absFile)
407  {
408  if (!isset($this->shutdownRegistry[__METHOD__])) {
409  register_shutdown_function([$this, 'unlinkTempFiles']);
410  $this->shutdownRegistry[__METHOD__] = true;
411  }
412  $this->tempFiles[] = $absFile;
413  }
414 
418  public function ‪unlinkTempFiles()
419  {
420  foreach ($this->tempFiles as $absFile) {
421  GeneralUtility::unlink_tempfile($absFile);
422  }
423  $this->tempFiles = [];
424  }
425 
426  /***************************************
427  *
428  * IO input
429  *
430  ***************************************/
437  public function ‪setInput($content, $type = '')
438  {
439  $this->inputContent = $content;
440  $this->inputFile = '';
441  $this->inputType = $type;
442  }
443 
450  public function ‪setInputFile($absFile, $type = '')
451  {
452  $this->inputContent = '';
453  $this->inputFile = $absFile;
454  $this->inputType = $type;
455  }
456 
463  public function ‪getInput()
464  {
465  if ($this->inputContent == '') {
466  $this->inputContent = $this->‪readFile($this->inputFile);
467  }
469  }
470 
478  public function ‪getInputFile($createFile = '')
479  {
480  if ($this->inputFile) {
481  $this->inputFile = $this->‪checkInputFile($this->inputFile);
482  } elseif ($this->inputContent) {
483  $this->inputFile = $this->‪writeFile($this->inputContent, $createFile);
484  }
485  return ‪$this->inputFile;
486  }
487 
488  /***************************************
489  *
490  * IO output
491  *
492  ***************************************/
498  public function ‪setOutputFile($absFile)
499  {
500  $this->outputFile = $absFile;
501  }
502 
508  public function ‪getOutput()
509  {
510  if ($this->outputFile) {
511  $this->out = $this->‪readFile($this->outputFile);
512  }
513  return ‪$this->out;
514  }
515 
522  public function ‪getOutputFile($absFile = '')
523  {
524  if (!$this->outputFile) {
525  $this->outputFile = $this->‪writeFile($this->out, $absFile);
526  }
527  return ‪$this->outputFile;
528  }
529 
530  /***************************************
531  *
532  * Service implementation
533  *
534  ***************************************/
543  public function ‪init()
544  {
545  // look in makeInstanceService()
546  $this->‪reset();
547  // Check for external programs which are defined by $info['exec']
548  if (trim($this->info['exec'])) {
549  $this->‪checkExec($this->info['exec']);
550  }
551  return $this->‪getLastError() === true;
552  }
553 
558  public function ‪reset()
559  {
560  $this->‪unlinkTempFiles();
561  $this->‪resetErrors();
562  $this->out = '';
563  $this->inputFile = '';
564  $this->inputContent = '';
565  $this->inputType = '';
566  $this->outputFile = '';
567  }
568 
573  public function ‪__destruct()
574  {
575  $this->‪unlinkTempFiles();
576  }
577 }
‪TYPO3\CMS\Core\Service\AbstractService\writeFile
‪string bool writeFile($content, $absFile='')
Definition: AbstractService.php:355
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_GENERAL
‪const ERROR_GENERAL
Definition: AbstractService.php:34
‪TYPO3\CMS\Core\Service\AbstractService\devLog
‪devLog($msg, $severity=0, $dataVar=false)
Definition: AbstractService.php:177
‪TYPO3\CMS\Core\Service\AbstractService\$tempFiles
‪array $tempFiles
Definition: AbstractService.php:94
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_NO_INPUT
‪const ERROR_NO_INPUT
Definition: AbstractService.php:44
‪TYPO3\CMS\Core\Service\AbstractService\unlinkTempFiles
‪unlinkTempFiles()
Definition: AbstractService.php:408
‪TYPO3\CMS\Core\Utility\CommandUtility\checkCommand
‪static bool checkCommand($cmd, $handler='')
Definition: CommandUtility.php:159
‪TYPO3\CMS\Core\Service\AbstractService\getServiceInfo
‪array getServiceInfo()
Definition: AbstractService.php:114
‪TYPO3\CMS\Core\Service\AbstractService\getInput
‪mixed getInput()
Definition: AbstractService.php:453
‪TYPO3\CMS\Core\Service\AbstractService\checkExec
‪bool checkExec($progList)
Definition: AbstractService.php:281
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_FOUND
‪const ERROR_FILE_NOT_FOUND
Definition: AbstractService.php:47
‪TYPO3\CMS\Core\Service\AbstractService\resetErrors
‪resetErrors()
Definition: AbstractService.php:265
‪TYPO3\CMS\Core\Service\AbstractService\getLastErrorArray
‪array getLastErrorArray()
Definition: AbstractService.php:257
‪TYPO3\CMS\Core\Service\AbstractService\getServiceTitle
‪string getServiceTitle()
Definition: AbstractService.php:134
‪TYPO3\CMS\Core\Service\AbstractService\tempFile
‪string bool tempFile($filePrefix)
Definition: AbstractService.php:378
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_WRONG_SUBTYPE
‪const ERROR_WRONG_SUBTYPE
Definition: AbstractService.php:41
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_PROGRAM_FAILED
‪const ERROR_PROGRAM_FAILED
Definition: AbstractService.php:60
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:36
‪TYPO3\CMS\Core\Service\AbstractService\getServiceKey
‪string getServiceKey()
Definition: AbstractService.php:124
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_WRITEABLE
‪const ERROR_FILE_NOT_WRITEABLE
Definition: AbstractService.php:54
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Service\AbstractService\errorPull
‪errorPull()
Definition: AbstractService.php:200
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\deactivateService
‪static deactivateService($serviceType, $serviceKey)
Definition: ExtensionManagementUtility.php:1212
‪TYPO3\CMS\Core\Service\AbstractService\setOutputFile
‪setOutputFile($absFile)
Definition: AbstractService.php:488
‪TYPO3\CMS\Core\Service\AbstractService\errorPush
‪errorPush($errNum=self::ERROR_GENERAL, $errMsg='Unspecified error occurred')
Definition: AbstractService.php:189
‪TYPO3\CMS\Core\Service\AbstractService\getErrorMsgArray
‪array getErrorMsgArray()
Definition: AbstractService.php:241
‪TYPO3\CMS\Core\Service\AbstractService\setInput
‪setInput($content, $type='')
Definition: AbstractService.php:427
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_SERVICE_NOT_AVAILABLE
‪const ERROR_SERVICE_NOT_AVAILABLE
Definition: AbstractService.php:38
‪TYPO3\CMS\Core\Service\AbstractService\reset
‪reset()
Definition: AbstractService.php:548
‪TYPO3\CMS\Core\Service\AbstractService\$out
‪string $out
Definition: AbstractService.php:71
‪TYPO3\CMS\Core\Service\AbstractService\readFile
‪string bool readFile($absFile, $length=0)
Definition: AbstractService.php:336
‪TYPO3\CMS\Core\Service\AbstractService\checkInputFile
‪string bool checkInputFile($absFile)
Definition: AbstractService.php:314
‪TYPO3\CMS\Core\Service\AbstractService\$inputType
‪string $inputType
Definition: AbstractService.php:83
‪TYPO3\CMS\Core\Service\AbstractService\registerTempFile
‪registerTempFile($absFile)
Definition: AbstractService.php:396
‪TYPO3\CMS\Core\Service\AbstractService\__destruct
‪__destruct()
Definition: AbstractService.php:563
‪TYPO3\CMS\Core\Service\AbstractService\getLastError
‪int bool getLastError()
Definition: AbstractService.php:210
‪TYPO3\CMS\Core\Service\AbstractService\deactivateService
‪deactivateService()
Definition: AbstractService.php:298
‪TYPO3\CMS\Core\Service\AbstractService\$shutdownRegistry
‪array $shutdownRegistry
Definition: AbstractService.php:98
‪TYPO3\CMS\Core\Service
Definition: AbstractService.php:2
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_READABLE
‪const ERROR_FILE_NOT_READABLE
Definition: AbstractService.php:50
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Service\AbstractService\getLastErrorMsg
‪string getLastErrorMsg()
Definition: AbstractService.php:226
‪TYPO3\CMS\Core\Service\AbstractService\init
‪bool init()
Definition: AbstractService.php:533
‪TYPO3\CMS\Core\Service\AbstractService\$inputFile
‪string $inputFile
Definition: AbstractService.php:75
‪TYPO3\CMS\Core\Service\AbstractService\$prefixId
‪string $prefixId
Definition: AbstractService.php:102
‪TYPO3\CMS\Core\Service\AbstractService\getOutput
‪mixed getOutput()
Definition: AbstractService.php:498
‪TYPO3\CMS\Core\Service\AbstractService\getInputFile
‪string getInputFile($createFile='')
Definition: AbstractService.php:468
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Service\AbstractService
Definition: AbstractService.php:29
‪TYPO3\CMS\Core\Service\AbstractService\setInputFile
‪setInputFile($absFile, $type='')
Definition: AbstractService.php:440
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:48
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_PROGRAM_NOT_FOUND
‪const ERROR_PROGRAM_NOT_FOUND
Definition: AbstractService.php:57
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:27
‪TYPO3\CMS\Core\Service\AbstractService\$info
‪array $info
Definition: AbstractService.php:63
‪TYPO3\CMS\Core\Service\AbstractService\$outputFile
‪string $outputFile
Definition: AbstractService.php:87
‪TYPO3\CMS\Core\Service\AbstractService\$error
‪array $error
Definition: AbstractService.php:67
‪TYPO3\CMS\Core\Service\AbstractService\getOutputFile
‪mixed getOutputFile($absFile='')
Definition: AbstractService.php:512
‪TYPO3\CMS\Core\Service\AbstractService\$inputContent
‪string $inputContent
Definition: AbstractService.php:79
‪TYPO3\CMS\Core\Service\AbstractService\getServiceOption
‪mixed getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=true)
Definition: AbstractService.php:147