1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Controllers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Firesphere\PartialUserforms\Models\PartialFormSubmission; |
7
|
|
|
use Page; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use Firesphere\PartialUserforms\Forms\PasswordForm; |
10
|
|
|
use SilverStripe\Control\HTTPResponse; |
11
|
|
|
use SilverStripe\Control\HTTPResponse_Exception; |
12
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
13
|
|
|
use SilverStripe\Forms\Form; |
14
|
|
|
use SilverStripe\ORM\DataObject; |
15
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
16
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
17
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
18
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class PartialUserFormController |
22
|
|
|
* |
23
|
|
|
* @package Firesphere\PartialUserforms\Controllers |
24
|
|
|
*/ |
25
|
|
|
class PartialUserFormController extends UserDefinedFormController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private static $url_handlers = [ |
|
|
|
|
31
|
|
|
'$Key/$Token' => 'partial', |
32
|
|
|
]; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private static $allowed_actions = [ |
|
|
|
|
37
|
|
|
'partial', |
38
|
|
|
]; |
39
|
|
|
/** |
40
|
|
|
* @var PartialFormSubmission |
41
|
|
|
*/ |
42
|
|
|
protected $partialFormSubmission; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Partial form |
46
|
|
|
* |
47
|
|
|
* @param HTTPRequest $request |
48
|
|
|
* @return HTTPResponse|DBHTMLText|void |
49
|
|
|
* @throws HTTPResponse_Exception |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
4 |
|
public function partial(HTTPRequest $request) |
53
|
|
|
{ |
54
|
|
|
/** @var PartialFormSubmission $partial */ |
55
|
4 |
|
$partial = $this->setData($request); |
56
|
1 |
|
if ($this->dataRecord->PasswordProtected && |
57
|
1 |
|
$request->getSession()->get(PasswordForm::PASSWORD_SESSION_KEY) !== $partial->ID |
58
|
|
|
) { |
59
|
1 |
|
return $this->redirect('verify'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var Form $form */ |
63
|
|
|
$form = $this->Form(); |
64
|
|
|
$fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
65
|
|
|
$form->loadDataFrom($fields); |
66
|
|
|
|
67
|
|
|
// Copied from {@link UserDefinedFormController} |
68
|
|
|
if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { |
|
|
|
|
69
|
|
|
$hasLocation = stristr($this->Content, '$UserDefinedForm'); |
|
|
|
|
70
|
|
|
if ($hasLocation) { |
71
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
72
|
|
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
73
|
|
|
$content = preg_replace( |
74
|
|
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
75
|
|
|
$formEscapedForRegex, |
76
|
|
|
$this->Content |
|
|
|
|
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
return $this->customise([ |
80
|
|
|
'Content' => DBField::create_field('HTMLText', $content), |
81
|
|
|
'Form' => '', |
82
|
|
|
'PartialLink' => $partial->getPartialLink() |
83
|
|
|
])->renderWith([static::class, Page::class]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->customise([ |
88
|
|
|
'Content' => DBField::create_field('HTMLText', $this->Content), |
|
|
|
|
89
|
|
|
'Form' => $form, |
90
|
|
|
'PartialLink' => $partial->getPartialLink() |
91
|
|
|
])->renderWith([static::class, Page::class]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* A little abstraction to be more readable |
96
|
|
|
* |
97
|
|
|
* @param HTTPRequest $request |
98
|
|
|
* @return PartialFormSubmission|void |
99
|
|
|
* @throws HTTPResponse_Exception |
100
|
|
|
*/ |
101
|
4 |
|
public function setData($request) |
102
|
|
|
{ |
103
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
104
|
4 |
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
105
|
|
|
|
106
|
4 |
|
$key = $request->param('Key'); |
107
|
4 |
|
$token = $request->param('Token'); |
108
|
4 |
|
if (!$key || !$token) { |
109
|
1 |
|
return $this->httpError(404); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @var PartialFormSubmission $partial */ |
113
|
3 |
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
114
|
3 |
|
if (!$token || |
115
|
3 |
|
!$partial || |
116
|
1 |
|
!$partial->UserDefinedFormID || |
117
|
3 |
|
!hash_equals($partial->generateKey($token), $key) |
118
|
|
|
) { |
119
|
2 |
|
return $this->httpError(404); |
120
|
|
|
} |
121
|
|
|
/** @var UserDefinedForm $dataRecord */ |
122
|
1 |
|
$dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
123
|
|
|
// @todo look at to solve this. Also breaks on CircleCI |
124
|
|
|
// $this->setFailover($dataRecord); |
125
|
1 |
|
$this->dataRecord = $dataRecord; |
126
|
|
|
|
127
|
|
|
// Set the session if the last session has expired |
128
|
1 |
|
if (!$request->getSession()->get(PartialSubmissionController::SESSION_KEY)) { |
129
|
1 |
|
$request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// TODO: Recognize visitor with the password |
133
|
|
|
// Set data record and load the form |
134
|
1 |
|
$this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
135
|
|
|
// $this->setFailover($this->dataRecord); |
136
|
|
|
|
137
|
1 |
|
return $partial; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return PartialFormSubmission |
142
|
|
|
*/ |
143
|
|
|
public function getPartialFormSubmission(): PartialFormSubmission |
144
|
|
|
{ |
145
|
|
|
return $this->partialFormSubmission; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param PartialFormSubmission $partialFormSubmission |
150
|
|
|
*/ |
151
|
|
|
public function setPartialFormSubmission(PartialFormSubmission $partialFormSubmission): void |
152
|
|
|
{ |
153
|
|
|
$this->partialFormSubmission = $partialFormSubmission; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|