‪TYPO3CMS  11.5
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;
20 use Psr\Log\LogLevel;
26 
32 abstract class ‪AbstractService implements LoggerAwareInterface
33 {
35  use LoggerAwareTrait;
36 
37  // General error - something went wrong
38  public const ‪ERROR_GENERAL = -1;
39 
40  // During execution it showed that the service is not available and
41  // should be ignored. The service itself should call $this->setNonAvailable()
42  public const ‪ERROR_SERVICE_NOT_AVAILABLE = -2;
43 
44  // Passed subtype is not possible with this service
45  public const ‪ERROR_WRONG_SUBTYPE = -3;
46 
47  // Passed subtype is not possible with this service
48  public const ‪ERROR_NO_INPUT = -4;
49 
50  // File not found which the service should process
51  public const ‪ERROR_FILE_NOT_FOUND = -20;
52 
53  // File not readable
54  public const ‪ERROR_FILE_NOT_READABLE = -21;
55 
56  // File not writable
57  // @todo: check writeable vs. writable
58  public const ‪ERROR_FILE_NOT_WRITEABLE = -22;
59 
60  // Passed subtype is not possible with this service
61  public const ‪ERROR_PROGRAM_NOT_FOUND = -40;
62 
63  // Passed subtype is not possible with this service
64  public const ‪ERROR_PROGRAM_FAILED = -41;
68  public ‪$info = [];
69 
73  public ‪$error = [];
74 
78  public ‪$out = '';
79 
83  public ‪$inputFile = '';
84 
88  public ‪$inputContent = '';
89 
93  public ‪$inputType = '';
94 
98  public ‪$outputFile = '';
99 
106  public ‪$tempFiles = [];
107 
111  protected ‪$shutdownRegistry = [];
112 
116  protected ‪$prefixId = '';
117 
118  /***************************************
119  *
120  * Get service meta information
121  *
122  ***************************************/
128  public function ‪getServiceInfo()
129  {
130  return ‪$this->info;
131  }
132 
138  public function ‪getServiceKey()
139  {
140  return $this->info['serviceKey'];
141  }
142 
148  public function ‪getServiceTitle()
149  {
150  return $this->info['title'];
151  }
152 
161  public function ‪getServiceOption($optionName, $defaultValue = '', $includeDefaultConfig = true)
162  {
163  $config = null;
164  $serviceType = $this->info['serviceType'] ?? '';
165  $serviceKey = $this->info['serviceKey'] ?? '';
166  $svOptions = ‪$GLOBALS['TYPO3_CONF_VARS']['SVCONF'][$serviceType] ?? [];
167  if (isset($svOptions[$serviceKey][$optionName])) {
168  $config = $svOptions[$serviceKey][$optionName];
169  } elseif ($includeDefaultConfig && isset($svOptions['default'][$optionName])) {
170  $config = $svOptions['default'][$optionName];
171  }
172  if (!isset($config)) {
173  $config = $defaultValue;
174  }
175  return $config;
176  }
177 
178  /***************************************
179  *
180  * Error handling
181  *
182  ***************************************/
183 
190  public function ‪errorPush($errNum = self::ERROR_GENERAL, $errMsg = 'Unspecified error occurred')
191  {
192  $this->error[] = ['nr' => $errNum, 'msg' => $errMsg];
193  $timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
194  $timeTracker->setTSlogMessage($errMsg, LogLevel::WARNING);
195  }
196 
200  public function ‪errorPull()
201  {
202  array_pop($this->error);
203  }
204 
210  public function ‪getLastError()
211  {
212  // Means all is ok - no error
213  $lastError = true;
214  if (!empty($this->error)) {
215  ‪$error = end($this->error);
216  $lastError = ‪$error['nr'];
217  }
218  return $lastError;
219  }
220 
226  public function ‪getLastErrorMsg()
227  {
228  $lastErrorMessage = '';
229  if (!empty($this->error)) {
230  ‪$error = end($this->error);
231  $lastErrorMessage = ‪$error['msg'];
232  }
233  return $lastErrorMessage;
234  }
235 
241  public function ‪getErrorMsgArray()
242  {
243  $errArr = [];
244  if (!empty($this->error)) {
245  foreach ($this->error as ‪$error) {
246  $errArr[] = ‪$error['msg'];
247  }
248  }
249  return $errArr;
250  }
251 
257  public function ‪getLastErrorArray()
258  {
259  return end($this->error);
260  }
261 
265  public function ‪resetErrors()
266  {
267  $this->error = [];
268  }
269 
270  /***************************************
271  *
272  * General service functions
273  *
274  ***************************************/
281  public function ‪checkExec($progList)
282  {
283  $ret = true;
284  $progList = ‪GeneralUtility::trimExplode(',', $progList, true);
285  foreach ($progList as $prog) {
286  if (!‪CommandUtility::checkCommand($prog)) {
287  // Program not found
288  $this->‪errorPush(self::ERROR_PROGRAM_NOT_FOUND, 'External program not found: ' . $prog);
289  $ret = false;
290  }
291  }
292  return $ret;
293  }
294 
298  public function ‪deactivateService()
299  {
300  ‪ExtensionManagementUtility::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
301  }
302 
303  /***************************************
304  *
305  * IO tools
306  *
307  ***************************************/
314  public function ‪checkInputFile($absFile)
315  {
316  $checkResult = false;
317  if (GeneralUtility::isAllowedAbsPath($absFile) && @is_file($absFile)) {
318  if (@is_readable($absFile)) {
319  $checkResult = $absFile;
320  } else {
321  $this->‪errorPush(self::ERROR_FILE_NOT_READABLE, 'File is not readable: ' . $absFile);
322  }
323  } else {
324  $this->‪errorPush(self::ERROR_FILE_NOT_FOUND, 'File not found: ' . $absFile);
325  }
326  return $checkResult;
327  }
328 
336  public function ‪readFile($absFile, $length = 0)
337  {
338  ‪$out = false;
339  if ($this->‪checkInputFile($absFile)) {
340  ‪$out = file_get_contents($absFile);
341  if (‪$out === false) {
342  $this->‪errorPush(self::ERROR_FILE_NOT_READABLE, 'Can not read from file: ' . $absFile);
343  }
344  }
345  return ‪$out;
346  }
347 
355  public function ‪writeFile($content, $absFile = '')
356  {
357  if (!$absFile) {
358  $absFile = $this->‪tempFile($this->prefixId);
359  if ($absFile === false) {
360  return false;
361  }
362  $absFile = (string)$absFile;
363  }
364  if (GeneralUtility::isAllowedAbsPath($absFile)) {
365  if ($fd = @fopen($absFile, 'wb')) {
366  @fwrite($fd, $content);
367  @fclose($fd);
368  } else {
369  $this->‪errorPush(self::ERROR_FILE_NOT_WRITEABLE, 'Can not write to file: ' . $absFile);
370  $absFile = false;
371  }
372  }
373  return $absFile;
374  }
375 
382  public function ‪tempFile($filePrefix)
383  {
384  $absFile = GeneralUtility::tempnam($filePrefix);
385  if ($absFile) {
386  $ret = $absFile;
387  $this->‪registerTempFile($absFile);
388  } else {
389  $ret = false;
390  $this->‪errorPush(self::ERROR_FILE_NOT_WRITEABLE, 'Can not create temp file.');
391  }
392  return $ret;
393  }
394 
400  public function ‪registerTempFile($absFile)
401  {
402  if (!isset($this->shutdownRegistry[__METHOD__])) {
403  register_shutdown_function([$this, 'unlinkTempFiles']);
404  $this->shutdownRegistry[__METHOD__] = true;
405  }
406  $this->tempFiles[] = $absFile;
407  }
408 
412  public function ‪unlinkTempFiles()
413  {
414  foreach ($this->tempFiles as $absFile) {
415  GeneralUtility::unlink_tempfile($absFile);
416  }
417  $this->tempFiles = [];
418  }
419 
420  /***************************************
421  *
422  * IO input
423  *
424  ***************************************/
431  public function ‪setInput($content, $type = '')
432  {
433  $this->inputContent = $content;
434  $this->inputFile = '';
435  $this->inputType = $type;
436  }
437 
444  public function ‪setInputFile($absFile, $type = '')
445  {
446  $this->inputContent = '';
447  $this->inputFile = $absFile;
448  $this->inputType = $type;
449  }
450 
457  public function ‪getInput()
458  {
459  if ($this->inputContent == '') {
460  $this->inputContent = (string)$this->‪readFile($this->inputFile);
461  }
463  }
464 
472  public function ‪getInputFile($createFile = '')
473  {
474  if ($this->inputFile) {
475  $this->inputFile = (string)$this->‪checkInputFile($this->inputFile);
476  } elseif ($this->inputContent) {
477  $this->inputFile = (string)$this->‪writeFile($this->inputContent, $createFile);
478  }
479  return ‪$this->inputFile;
480  }
481 
482  /***************************************
483  *
484  * IO output
485  *
486  ***************************************/
492  public function ‪setOutputFile($absFile)
493  {
494  $this->outputFile = $absFile;
495  }
496 
502  public function ‪getOutput()
503  {
504  if ($this->outputFile) {
505  $this->out = (string)$this->‪readFile($this->outputFile);
506  }
507  return ‪$this->out;
508  }
509 
516  public function ‪getOutputFile($absFile = '')
517  {
518  if (!$this->outputFile) {
519  $this->outputFile = (string)$this->‪writeFile($this->out, $absFile);
520  }
521  return ‪$this->outputFile;
522  }
523 
524  /***************************************
525  *
526  * Service implementation
527  *
528  ***************************************/
537  public function ‪init()
538  {
539  // look in makeInstanceService()
540  $this->‪reset();
541  // Check for external programs which are defined by $info['exec']
542  if (trim($this->info['exec'])) {
543  $this->‪checkExec($this->info['exec']);
544  }
545  return $this->‪getLastError() === true;
546  }
547 
552  public function ‪reset()
553  {
554  $this->‪unlinkTempFiles();
555  $this->‪resetErrors();
556  $this->out = '';
557  $this->inputFile = '';
558  $this->inputContent = '';
559  $this->inputType = '';
560  $this->outputFile = '';
561  }
562 
567  public function ‪__destruct()
568  {
569  $this->‪unlinkTempFiles();
570  }
571 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Core\Service\AbstractService\writeFile
‪string bool writeFile($content, $absFile='')
Definition: AbstractService.php:345
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_GENERAL
‪const ERROR_GENERAL
Definition: AbstractService.php:38
‪TYPO3\CMS\Core\Service\AbstractService\$tempFiles
‪array $tempFiles
Definition: AbstractService.php:98
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_NO_INPUT
‪const ERROR_NO_INPUT
Definition: AbstractService.php:48
‪TYPO3\CMS\Core\Service\AbstractService\unlinkTempFiles
‪unlinkTempFiles()
Definition: AbstractService.php:402
‪TYPO3\CMS\Core\Utility\CommandUtility\checkCommand
‪static bool checkCommand($cmd, $handler='')
Definition: CommandUtility.php:164
‪TYPO3\CMS\Core\Service\AbstractService\getServiceInfo
‪array getServiceInfo()
Definition: AbstractService.php:118
‪TYPO3\CMS\Core\Service\AbstractService\checkExec
‪bool checkExec($progList)
Definition: AbstractService.php:271
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_FOUND
‪const ERROR_FILE_NOT_FOUND
Definition: AbstractService.php:51
‪TYPO3\CMS\Core\Service\AbstractService\resetErrors
‪resetErrors()
Definition: AbstractService.php:255
‪TYPO3\CMS\Core\Service\AbstractService\getLastErrorArray
‪array getLastErrorArray()
Definition: AbstractService.php:247
‪TYPO3\CMS\Core\Service\AbstractService\getServiceTitle
‪string getServiceTitle()
Definition: AbstractService.php:138
‪TYPO3\CMS\Core\Service\AbstractService\tempFile
‪string bool tempFile($filePrefix)
Definition: AbstractService.php:372
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_WRONG_SUBTYPE
‪const ERROR_WRONG_SUBTYPE
Definition: AbstractService.php:45
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_PROGRAM_FAILED
‪const ERROR_PROGRAM_FAILED
Definition: AbstractService.php:64
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Core\Service\AbstractService\getServiceKey
‪string getServiceKey()
Definition: AbstractService.php:128
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_FILE_NOT_WRITEABLE
‪const ERROR_FILE_NOT_WRITEABLE
Definition: AbstractService.php:58
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Service\AbstractService\errorPull
‪errorPull()
Definition: AbstractService.php:190
‪TYPO3\CMS\Core\Service\AbstractService\getOutput
‪string getOutput()
Definition: AbstractService.php:492
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\deactivateService
‪static deactivateService($serviceType, $serviceKey)
Definition: ExtensionManagementUtility.php:1177
‪TYPO3\CMS\Core\Service\AbstractService\setOutputFile
‪setOutputFile($absFile)
Definition: AbstractService.php:482
‪TYPO3\CMS\Core\Service\AbstractService\errorPush
‪errorPush($errNum=self::ERROR_GENERAL, $errMsg='Unspecified error occurred')
Definition: AbstractService.php:180
‪TYPO3\CMS\Core\Service\AbstractService\getErrorMsgArray
‪array getErrorMsgArray()
Definition: AbstractService.php:231
‪TYPO3\CMS\Core\Service\AbstractService\getInput
‪string getInput()
Definition: AbstractService.php:447
‪TYPO3\CMS\Core\Service\AbstractService\setInput
‪setInput($content, $type='')
Definition: AbstractService.php:421
‪TYPO3\CMS\Core\Service\AbstractService\ERROR_SERVICE_NOT_AVAILABLE
‪const ERROR_SERVICE_NOT_AVAILABLE
Definition: AbstractService.php:42
‪TYPO3\CMS\Core\Service\AbstractService\reset
‪reset()
Definition: AbstractService.php:542
‪TYPO3\CMS\Core\Service\AbstractService\$out
‪string $out
Definition: AbstractService.php:75
‪TYPO3\CMS\Core\Service\AbstractService\readFile
‪string bool readFile($absFile, $length=0)
Definition: AbstractService.php:326
‪TYPO3\CMS\Core\Service\AbstractService\checkInputFile
‪string bool checkInputFile($absFile)
Definition: AbstractService.php:304
‪TYPO3\CMS\Core\Service\AbstractService\$inputType
‪string $inputType
Definition: AbstractService.php:87
‪TYPO3\CMS\Core\Service\AbstractService\registerTempFile
‪registerTempFile($absFile)
Definition: AbstractService.php:390
‪TYPO3\CMS\Core\Service\AbstractService\__destruct
‪__destruct()
Definition: AbstractService.php:557
‪TYPO3\CMS\Core\Service\AbstractService\getLastError
‪int bool getLastError()
Definition: AbstractService.php:200
‪TYPO3\CMS\Core\Service\AbstractService\deactivateService
‪deactivateService()
Definition: AbstractService.php:288
‪TYPO3\CMS\Core\Service\AbstractService\$shutdownRegistry
‪array $shutdownRegistry
Definition: AbstractService.php:102
‪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:54
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Service\AbstractService\getLastErrorMsg
‪string getLastErrorMsg()
Definition: AbstractService.php:216
‪TYPO3\CMS\Core\Service\AbstractService\init
‪bool init()
Definition: AbstractService.php:527
‪TYPO3\CMS\Core\Service\AbstractService\$inputFile
‪string $inputFile
Definition: AbstractService.php:79
‪TYPO3\CMS\Core\Service\AbstractService\$prefixId
‪string $prefixId
Definition: AbstractService.php:106
‪TYPO3\CMS\Core\Service\AbstractService\getInputFile
‪string getInputFile($createFile='')
Definition: AbstractService.php:462
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Service\AbstractService
Definition: AbstractService.php:33
‪TYPO3\CMS\Core\Service\AbstractService\setInputFile
‪setInputFile($absFile, $type='')
Definition: AbstractService.php:434
‪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:61
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:31
‪TYPO3\CMS\Core\Service\AbstractService\$info
‪array $info
Definition: AbstractService.php:67
‪TYPO3\CMS\Core\Service\AbstractService\$outputFile
‪string $outputFile
Definition: AbstractService.php:91
‪TYPO3\CMS\Core\Service\AbstractService\$error
‪array $error
Definition: AbstractService.php:71
‪TYPO3\CMS\Core\Service\AbstractService\getOutputFile
‪mixed getOutputFile($absFile='')
Definition: AbstractService.php:506
‪TYPO3\CMS\Core\Service\AbstractService\$inputContent
‪string $inputContent
Definition: AbstractService.php:83
‪TYPO3\CMS\Core\Service\AbstractService\getServiceOption
‪mixed getServiceOption($optionName, $defaultValue='', $includeDefaultConfig=true)
Definition: AbstractService.php:151