[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Classes / RsaEncryptionDecoder.php
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
17 /**
18 * This class decodes rsa protected data
19 */
20 class RsaEncryptionDecoder implements \TYPO3\CMS\Core\SingletonInterface
21 {
22 /**
23 * @var Backend\AbstractBackend
24 */
25 protected $backend = null;
26
27 /**
28 * @var Storage\AbstractStorage
29 */
30 protected $storage = null;
31
32 /**
33 * @var string
34 */
35 protected $key = null;
36
37 /**
38 * @param string|array $data
39 * @return string|array
40 */
41 public function decrypt($data)
42 {
43 if ($this->getKey() === '' || !$this->isAvailable()) {
44 return $data;
45 }
46
47 $decryptedData = is_array($data) ? $data : [$data];
48 $decryptedData = $this->decryptDataArray($decryptedData);
49 $this->getStorage()->put(null);
50
51 return is_array($data) ? $decryptedData : $decryptedData[0];
52 }
53
54 /**
55 * @return bool
56 */
57 public function isAvailable()
58 {
59 return $this->getBackend() instanceof Backend\AbstractBackend;
60 }
61
62 /**
63 * @param array $data
64 * @return array
65 */
66 protected function decryptDataArray(array $data)
67 {
68 foreach ($data as $key => $value) {
69 if (empty($value)) {
70 continue;
71 }
72 if (is_array($value)) {
73 $data[$key] = $this->decryptDataArray($value);
74 continue;
75 }
76
77 if (strpos($value, 'rsa:') !== 0) {
78 continue;
79 }
80
81 $decryptedValue = $this->getBackend()->decrypt($this->getKey(), substr($value, 4));
82 if ($decryptedValue !== null) {
83 $data[$key] = $decryptedValue;
84 }
85 }
86
87 return $data;
88 }
89
90 /**
91 * @return string
92 */
93 protected function getKey()
94 {
95 if ($this->key === null) {
96 $this->key = $this->getStorage()->get();
97
98 if ($this->key === null) {
99 $this->key = '';
100 }
101 }
102
103 return $this->key;
104 }
105
106 /**
107 * @return Backend\AbstractBackend|null
108 */
109 protected function getBackend()
110 {
111 if ($this->backend === null) {
112 $this->backend = Backend\BackendFactory::getBackend();
113 }
114
115 return $this->backend;
116 }
117
118 /**
119 * @return Storage\AbstractStorage
120 */
121 protected function getStorage()
122 {
123 if ($this->storage === null) {
124 $this->storage = Storage\StorageFactory::getStorage();
125 }
126
127 return $this->storage;
128 }
129 }