|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Form\Service\DataObject; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\Form\FormObject; |
|
17
|
|
|
use Romm\Formz\Form\IdentifiableFormInterface; |
|
18
|
|
|
use Romm\Formz\Service\HashService; |
|
19
|
|
|
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This class is used to handle a form identifiers, that can be passed from one |
|
23
|
|
|
* request to another via a hashed string parameter. |
|
24
|
|
|
* |
|
25
|
|
|
* It allows retrieving the form hash, as well as an identifier if the form was |
|
26
|
|
|
* saved in a database (for instance a uid). |
|
27
|
|
|
*/ |
|
28
|
|
|
class FormIdentifierObject |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* This is the full hash used to retrieve this form identifier data. It can |
|
32
|
|
|
* be passed to another request. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $identifierHash; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Contains the class name of the form. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $className; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* This is the form identifier, in most cases it will be the uid of the form |
|
47
|
|
|
* if it is an instance of `DomainObjectInterface`. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $identifier; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $uniqueHash; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var FormObject |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $formObject; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param FormObject $formObject |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(FormObject $formObject) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->formObject = $formObject; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Will decrypt and parse the given hash. The data should be an array |
|
73
|
|
|
* containing the following information: |
|
74
|
|
|
* |
|
75
|
|
|
* - `c` - the class name of the form (required) |
|
76
|
|
|
* - `h` - the unique hash of the form (required) |
|
77
|
|
|
* - `i` - the identifier of the form |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $hash |
|
80
|
|
|
* @throws WrongFormIdentifierObjectDataException |
|
81
|
|
|
* @internal |
|
82
|
|
|
*/ |
|
83
|
|
|
public function analyzeIdentifierHash($hash) |
|
84
|
|
|
{ |
|
85
|
|
|
$decodedHash = base64_decode($hash); |
|
86
|
|
|
$data = HashService::get()->decrypt($decodedHash); |
|
87
|
|
|
|
|
88
|
|
|
if (false === $data) { |
|
89
|
|
|
throw WrongFormIdentifierObjectDataException::wrongHash($hash); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (false === is_array($data)) { |
|
93
|
|
|
throw WrongFormIdentifierObjectDataException::wrongType($data); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (false === isset($data['c'])) { |
|
97
|
|
|
throw WrongFormIdentifierObjectDataException::classNameNotFound(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (false === isset($data['h'])) { |
|
101
|
|
|
throw WrongFormIdentifierObjectDataException::uniqueHashNotFound(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($data['c'] !== $this->formObject->getClassName()) { |
|
105
|
|
|
throw WrongFormIdentifierObjectDataException::wrongClassName($data['c'], $this->formObject->getClassName()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->identifierHash = $hash; |
|
109
|
|
|
$this->className = $data['c']; |
|
110
|
|
|
$this->uniqueHash = $data['h']; |
|
111
|
|
|
$this->identifier = (isset($data['i'])) |
|
112
|
|
|
? $data['i'] |
|
113
|
|
|
: null; |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getIdentifierHash() |
|
121
|
|
|
{ |
|
122
|
|
|
if (null === $this->identifierHash) { |
|
123
|
|
|
$data = $this->getFormIdentifierData(); |
|
124
|
|
|
$data = HashService::get()->encrypt($data); |
|
125
|
|
|
$this->identifierHash = base64_encode($data); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $this->identifierHash; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getClassName() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->className; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getIdentifier() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->identifier; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
|
public function hasIdentifier() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->identifier !== null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getUniqueHash() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->uniqueHash; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function getFormIdentifierData() |
|
167
|
|
|
{ |
|
168
|
|
|
$data = ['c' => $this->formObject->getClassName()]; |
|
169
|
|
|
|
|
170
|
|
|
if ($this->formObject->hasForm()) { |
|
171
|
|
|
$form = $this->formObject->getForm(); |
|
172
|
|
|
|
|
173
|
|
|
if ($form instanceof IdentifiableFormInterface) { |
|
174
|
|
|
$data['i'] = $form->getFormIdentifier(); |
|
175
|
|
|
} elseif ($form instanceof DomainObjectInterface |
|
176
|
|
|
&& null !== $form->getUid() |
|
177
|
|
|
) { |
|
178
|
|
|
$data['i'] = $form->getUid(); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (false === isset($data['i'])) { |
|
183
|
|
|
$data['h'] = uniqid(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $data; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|