‪TYPO3CMS  10.4
AbstractService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
25 
29 abstract class ‪AbstractService implements LoggerAwareInterface
30 {
32  use LoggerAwareTrait;
33 
34  // General error - something went wrong
35  const ‪ERROR_GENERAL = -1;
36 
37  // During execution it showed that the service is not available and
38  // should be ignored. The service itself should call $this->setNonAvailable()
40 
41  // Passed subtype is not possible with this service
43 
44  // Passed subtype is not possible with this service
45  const ‪ERROR_NO_INPUT = -4;
46 
47  // File not found which the service should process
49 
50  // File not readable
52 
53  // File not writable
54  // @todo: check writeable vs. writable
56 
57  // Passed subtype is not possible with this service
59 
60  // Passed subtype is not possible with this service
65  public ‪$info = [];
66 
70  public ‪$error = [];
71 
75  public ‪$out = '';
76 
80  public ‪$inputFile = '';
81 
85  public ‪$inputContent = '';
86 
90  public ‪$inputType = '';
91 
95  public ‪$outputFile = '';
96 
103  public ‪$tempFiles = [];
104 
108  protected ‪$shutdownRegistry = [];
109 
113  protected ‪$prefixId = '';
114 
115  /***************************************
116  *
117  * Get service meta information
118  *
119  ***************************************/
125  public function ‪getServiceInfo()
126  {
127  return ‪$this->info;
128  }
129 
135  public function ‪getServiceKey()
136  {
137  return $this->info['serviceKey'];
138  }
139 
145  public function ‪getServiceTitle()
146  {
147  return $this->info['title'];
148  }
149 
158  public function ‪getServiceOption($optionName, $defaultValue = '', $includeDefaultConfig = true)
159  {
160  $config = null;
161  $serviceType = $this->info['serviceType'] ?? '';
162  $serviceKey = $this->info['serviceKey'] ?? '';
163  $svOptions = ‪$GLOBALS['TYPO3_CONF_VARS']['SVCONF'][$serviceType] ?? [];
164  if (isset($svOptions[$serviceKey][$optionName])) {
165  $config = $svOptions[$serviceKey][$optionName];
166  } elseif ($includeDefaultConfig && isset($svOptions['default'][$optionName])) {
167  $config = $svOptions['default'][$optionName];
168  }
169  if (!isset($config)) {
170  $config = $defaultValue;
171  }
172  return $config;
173  }
174 
175  /***************************************
176  *
177  * Error handling
178  *
179  ***************************************/
180 
187  public function ‪errorPush($errNum = self::ERROR_GENERAL, $errMsg = 'Unspecified error occurred')
188  {
189  $this->error[] = ['nr' => $errNum, 'msg' => $errMsg];
191  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
192  $timeTracker->setTSlogMessage($errMsg, 2);
193  }
194 
198  public function ‪errorPull()
199  {
200  array_pop($this->error);
201  }
202 
208  public function ‪getLastError()
209  {
210  // Means all is ok - no error
211  $lastError = true;
212  if (!empty($this->error)) {
213  ‪$error = end($this->error);
214  $lastError = ‪$error['nr'];
215  }
216  return $lastError;
217  }
218 
224  public function ‪getLastErrorMsg()
225  {
226  $lastErrorMessage = '';
227  if (!empty($this->error)) {
228  ‪$error = end($this->error);
229  $lastErrorMessage = ‪$error['msg'];
230  }
231  return $lastErrorMessage;
232  }
233 
239  public function ‪getErrorMsgArray()
240  {
241  $errArr = [];
242  if (!empty($this->error)) {
243  foreach ($this->error as ‪$error) {
244  $errArr[] = ‪$error['msg'];
245  }
246  }
247  return $errArr;
248  }
249 
255  public function ‪getLastErrorArray()
256  {
257  return end($this->error);
258  }
259 
263  public function ‪resetErrors()
264  {
265  $this->error = [];
266  }
267 
268  /***************************************
269  *
270  * General service functions
271  *
272  ***************************************/
279  public function ‪checkExec($progList)
280  {
281  $ret = true;
282  $progList = ‪GeneralUtility::trimExplode(',', $progList, true);
283  foreach ($progList as $prog) {
284  if (!‪CommandUtility::checkCommand($prog)) {
285  // Program not found
286  $this->‪errorPush(self::ERROR_PROGRAM_NOT_FOUND, 'External program not found: ' . $prog);
287  $ret = false;
288  }
289  }
290  return $ret;
291  }
292 
296  public function ‪deactivateService()
297  {
298  ‪ExtensionManagementUtility::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
299  }
300 
301  /***************************************
302  *
303  * IO tools
304  *
305  ***************************************/
312  public function ‪checkInputFile($absFile)
313  {
314  $checkResult = false;
315  if (GeneralUtility::isAllowedAbsPath($absFile) && @is_file($absFile)) {
316  if (@is_readable($absFile)) {
317  $checkResult = $absFile;
318  } else {
319  $this->‪errorPush(self::ERROR_FILE_NOT_READABLE, 'File is not readable: ' . $absFile);
320  }
321  } else {
322  $this->‪errorPush(self::ERROR_FILE_NOT_FOUND, 'File not found: ' . $absFile);
323  }
324  return $checkResult;
325  }
326 
334  public function ‪readFile($absFile, $length = 0)
335  {
336  ‪$out = false;
337  if ($this->‪checkInputFile($absFile)) {
338  ‪$out = file_get_contents($absFile);
339  if (‪$out === false) {
340  $this->‪errorPush(self::ERROR_FILE_NOT_READABLE, 'Can not read from file: ' . $absFile);
341  }
342  }
343  return ‪$out;
344  }
345 
353  public function ‪writeFile($content, $absFile = '')
354  {
355  if (!$absFile) {
356  $absFile = $this->‪tempFile($this->prefixId);
357  if ($absFile === false) {
358  return false;
359  }
360  $absFile = (string)$absFile;
361  }
362  if (GeneralUtility::isAllowedAbsPath($absFile)) {
363  if ($fd = @fopen($absFile, 'wb')) {
364  @fwrite($fd, $content);
365  @fclose($fd);
366  } else {
367  $this->‪errorPush(self::ERROR_FILE_NOT_WRITEABLE, 'Can not write to file: ' . $absFile);
368  $absFile = false;
369  }
370  }
371  return $absFile;
372  }
373 
380  public function ‪tempFile($filePrefix)
381  {
382  $absFile = GeneralUtility::tempnam($filePrefix);
383  if ($absFile) {
384  $ret = $absFile;
385  $this->‪registerTempFile($absFile);
386  } else {
387  $ret = false;
388  $this->‪errorPush(self::ERROR_FILE_NOT_WRITEABLE, 'Can not create temp file.');
389  }
390  return $ret;
391  }
392 
398  public function ‪registerTempFile($absFile)
399  {
400  if (!isset($this->shutdownRegistry[__METHOD__])) {
401  register_shutdown_function([$this, 'unlinkTempFiles']);
402  $this->shutdownRegistry[__METHOD__] = true;
403  }
404  $this->tempFiles[] = $absFile;
405  }
406 
410  public function ‪unlinkTempFiles()
411  {
412  foreach ($this->tempFiles as $absFile) {
413  GeneralUtility::unlink_tempfile($absFile);
414  }
415  $this->tempFiles = [];
416  }
417 
418  /***************************************
419  *
420  * IO input
421  *
422  ***************************************/
429  public function ‪setInput($content, $type = '')
430  {
431  $this->inputContent = $content;
432  $this->inputFile = '';
433  $this->inputType = $type;
434  }
435 
442  public function ‪setInputFile($absFile, $type = '')
443  {
444  $this->inputContent = '';
445  $this->inputFile = $absFile;
446  $this->inputType = $type;
447  }
448 
455  public function ‪getInput()
456  {
457  if ($this->inputContent == '') {
458  $this->inputContent = $this->‪readFile($this->inputFile);
459  }
461  }
462 
470  public function ‪getInputFile($createFile = '')
471  {
472  if ($this->inputFile) {
473  $this->inputFile = $this->‪checkInputFile($this->inputFile);
474  } elseif ($this->inputContent) {
475  $this->inputFile = $this->‪writeFile($this->inputContent, $createFile);
476  }
477  return ‪$this->inputFile;
478  }
479 
480  /***************************************
481  *
482  * IO output
483  *
484  ***************************************/
490  public function ‪setOutputFile($absFile)
491  {
492  $this->outputFile = $absFile;
493  }
494 
500  public function ‪getOutput()
501  {
502  if ($this->outputFile) {
503  $this->out = $this->‪readFile($this->outputFile);
504  }
505  return ‪$this->out;
506  }
507 
514  public function ‪getOutputFile($absFile = '')
515  {
516  if (!$this->outputFile) {
517  $this->outputFile = $this->‪writeFile($this->out, $absFile);
518  }
519  return ‪$this->outputFile;
520  }
521 
522  /***************************************
523  *
524  * Service implementation
525  *
526  ***************************************/
535  public function ‪init()
536  {
537  // look in makeInstanceService()
538  $this->‪reset();
539  // Check for external programs which are defined by $info['exec']
540  if (trim($this->info['exec'])) {
541  $this->‪checkExec($this->info['exec']);
542  }
543  return $this->‪getLastError() === true;
544  }
545 
550  public function ‪reset()
551  {
552  $this->‪unlinkTempFiles();
553  $this->‪resetErrors();
554  $this->out = '';
555  $this->inputFile = '';
556  $this->inputContent = '';
557  $this->inputType = '';
558  $this->outputFile = '';
559  }
560 
565  public function ‪__destruct()
566  {
567  $this->‪unlinkTempFiles();
568  }
569 }
‪TYPO3\CMS\Core\Service\AbstractService\writeFile
‪string bool writeFile($content, $absFile='')
Definition: AbstractService.php:343
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_GENERAL
‪const ERROR_GENERAL
Definition: AbstractService.php:35
‪TYPO3\CMS\Core\Service\AbstractService\$tempFiles
‪array $tempFiles
Definition: AbstractService.php:95
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_NO_INPUT
‪const ERROR_NO_INPUT
Definition: AbstractService.php:45
‪TYPO3\CMS\Core\Service\AbstractService\unlinkTempFiles
‪unlinkTempFiles()
Definition: AbstractService.php:400
‪TYPO3\CMS\Core\Utility\CommandUtility\checkCommand
‪static bool checkCommand($cmd, $handler='')
Definition: CommandUtility.php:160
‪TYPO3\CMS\Core\Service\AbstractService\getServiceInfo
‪array getServiceInfo()
Definition: AbstractService.php:115
‪TYPO3\CMS\Core\Service\AbstractService\getInput
‪string bool getInput()
Definition: AbstractService.php:445
‪TYPO3\CMS\Core\Service\AbstractService\checkExec
‪bool checkExec($progList)
Definition: AbstractService.php:269
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_FOUND
‪const ERROR_FILE_NOT_FOUND
Definition: AbstractService.php:48
‪TYPO3\CMS\Core\Service\AbstractService\resetErrors
‪resetErrors()
Definition: AbstractService.php:253
‪TYPO3\CMS\Core\Service\AbstractService\getLastErrorArray
‪array getLastErrorArray()
Definition: AbstractService.php:245
‪TYPO3\CMS\Core\Service\AbstractService\getServiceTitle
‪string getServiceTitle()
Definition: AbstractService.php:135
‪TYPO3\CMS\Core\Service\AbstractService\tempFile
‪string bool tempFile($filePrefix)
Definition: AbstractService.php:370
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_WRONG_SUBTYPE
‪const ERROR_WRONG_SUBTYPE
Definition: AbstractService.php:42
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_PROGRAM_FAILED
‪const ERROR_PROGRAM_FAILED
Definition: AbstractService.php:61
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Core\Service\AbstractService\getServiceKey
‪string getServiceKey()
Definition: AbstractService.php:125
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_WRITEABLE
‪const ERROR_FILE_NOT_WRITEABLE
Definition: AbstractService.php:55
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Service\AbstractService\errorPull
‪errorPull()
Definition: AbstractService.php:188
‪TYPO3\CMS\Core\Service\AbstractService\getOutput
‪string getOutput()
Definition: AbstractService.php:490
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\deactivateService
‪static deactivateService($serviceType, $serviceKey)
Definition: ExtensionManagementUtility.php:1220
‪TYPO3\CMS\Core\Service\AbstractService\setOutputFile
‪setOutputFile($absFile)
Definition: AbstractService.php:480
‪TYPO3\CMS\Core\Service\AbstractService\errorPush
‪errorPush($errNum=self::ERROR_GENERAL, $errMsg='Unspecified error occurred')
Definition: AbstractService.php:177
‪TYPO3\CMS\Core\Service\AbstractService\getErrorMsgArray
‪array getErrorMsgArray()
Definition: AbstractService.php:229
‪TYPO3\CMS\Core\Service\AbstractService\setInput
‪setInput($content, $type='')
Definition: AbstractService.php:419
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_SERVICE_NOT_AVAILABLE
‪const ERROR_SERVICE_NOT_AVAILABLE
Definition: AbstractService.php:39
‪TYPO3\CMS\Core\Service\AbstractService\reset
‪reset()
Definition: AbstractService.php:540
‪TYPO3\CMS\Core\Service\AbstractService\$out
‪string $out
Definition: AbstractService.php:72
‪TYPO3\CMS\Core\Service\AbstractService\readFile
‪string bool readFile($absFile, $length=0)
Definition: AbstractService.php:324
‪TYPO3\CMS\Core\Service\AbstractService\checkInputFile
‪string bool checkInputFile($absFile)
Definition: AbstractService.php:302
‪TYPO3\CMS\Core\Service\AbstractService\$inputType
‪string $inputType
Definition: AbstractService.php:84
‪TYPO3\CMS\Core\Service\AbstractService\registerTempFile
‪registerTempFile($absFile)
Definition: AbstractService.php:388
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Service\AbstractService\__destruct
‪__destruct()
Definition: AbstractService.php:555
‪TYPO3\CMS\Core\Service\AbstractService\getLastError
‪int bool getLastError()
Definition: AbstractService.php:198
‪TYPO3\CMS\Core\Service\AbstractService\deactivateService
‪deactivateService()
Definition: AbstractService.php:286
‪TYPO3\CMS\Core\Service\AbstractService\$shutdownRegistry
‪array $shutdownRegistry
Definition: AbstractService.php:99
‪TYPO3\CMS\Core\Service
Definition: AbstractService.php:16
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_READABLE
‪const ERROR_FILE_NOT_READABLE
Definition: AbstractService.php:51
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Service\AbstractService\getLastErrorMsg
‪string getLastErrorMsg()
Definition: AbstractService.php:214
‪TYPO3\CMS\Core\Service\AbstractService\init
‪bool init()
Definition: AbstractService.php:525
‪TYPO3\CMS\Core\Service\AbstractService\$inputFile
‪string $inputFile
Definition: AbstractService.php:76
‪TYPO3\CMS\Core\Service\AbstractService\$prefixId
‪string $prefixId
Definition: AbstractService.php:103
‪TYPO3\CMS\Core\Service\AbstractService\getInputFile
‪string getInputFile($createFile='')
Definition: AbstractService.php:460
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Service\AbstractService
Definition: AbstractService.php:30
‪TYPO3\CMS\Core\Service\AbstractService\setInputFile
‪setInputFile($absFile, $type='')
Definition: AbstractService.php:432
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:49
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_PROGRAM_NOT_FOUND
‪const ERROR_PROGRAM_NOT_FOUND
Definition: AbstractService.php:58
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:30
‪TYPO3\CMS\Core\Service\AbstractService\$info
‪array $info
Definition: AbstractService.php:64
‪TYPO3\CMS\Core\Service\AbstractService\$outputFile
‪string $outputFile
Definition: AbstractService.php:88
‪TYPO3\CMS\Core\Service\AbstractService\$error
‪array $error
Definition: AbstractService.php:68
‪TYPO3\CMS\Core\Service\AbstractService\getOutputFile
‪mixed getOutputFile($absFile='')
Definition: AbstractService.php:504
‪TYPO3\CMS\Core\Service\AbstractService\$inputContent
‪string $inputContent
Definition: AbstractService.php:80
‪TYPO3\CMS\Core\Service\AbstractService\getServiceOption
‪mixed getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=true)
Definition: AbstractService.php:148