TYPO3 CMS  TYPO3_7-6
RsaEncryptionDecoder.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Rsaauth;
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 
18 
23 {
27  protected $backend = null;
28 
32  protected $storage = null;
33 
37  protected $key = null;
38 
43  public function decrypt($data)
44  {
45  if ($this->getKey() === '' || !$this->isAvailable()) {
46  return $data;
47  }
48 
49  $decryptedData = is_array($data) ? $data : [$data];
50  $decryptedData = $this->decryptDataArray($decryptedData);
51  $this->getStorage()->put(null);
52 
53  return is_array($data) ? $decryptedData : $decryptedData[0];
54  }
55 
59  public function isAvailable()
60  {
61  return $this->getBackend() instanceof Backend\AbstractBackend;
62  }
63 
68  protected function decryptDataArray(array $data)
69  {
70  foreach ($data as $key => $value) {
71  if (empty($value)) {
72  continue;
73  }
74  if (is_array($value)) {
75  $data[$key] = $this->decryptDataArray($value);
76  continue;
77  }
78 
79  if (!StringUtility::beginsWith($value, 'rsa:')) {
80  continue;
81  }
82 
83  $decryptedValue = $this->getBackend()->decrypt($this->getKey(), substr($value, 4));
84  if ($decryptedValue !== null) {
85  $data[$key] = $decryptedValue;
86  }
87  }
88 
89  return $data;
90  }
91 
95  protected function getKey()
96  {
97  if ($this->key === null) {
98  $this->key = $this->getStorage()->get();
99 
100  if ($this->key === null) {
101  $this->key = '';
102  }
103  }
104 
105  return $this->key;
106  }
107 
111  protected function getBackend()
112  {
113  if ($this->backend === null) {
114  $this->backend = Backend\BackendFactory::getBackend();
115  }
116 
117  return $this->backend;
118  }
119 
123  protected function getStorage()
124  {
125  if ($this->storage === null) {
126  $this->storage = Storage\StorageFactory::getStorage();
127  }
128 
129  return $this->storage;
130  }
131 }
static beginsWith($haystack, $needle)