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->validateToken($request); |
56
|
1 |
|
$record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
57
|
|
|
/** @var self $controller */ |
58
|
1 |
|
$controller = self::create($record); |
59
|
1 |
|
$controller->doInit(); |
60
|
1 |
|
$controller->dataRecord = $record; |
|
|
|
|
61
|
1 |
|
if ($controller->data()->PasswordProtected && |
62
|
1 |
|
$request->getSession()->get(PasswordForm::PASSWORD_SESSION_KEY) !== $partial->ID |
63
|
|
|
) { |
64
|
1 |
|
return $this->redirect('verify'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @var Form $form */ |
68
|
|
|
$form = $controller->Form(); |
69
|
|
|
$fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
70
|
|
|
$form->loadDataFrom($fields); |
71
|
|
|
|
72
|
|
|
// Copied from {@link UserDefinedFormController} |
73
|
|
|
if ($controller->Content && $form && !$controller->config()->disable_form_content_shortcode) { |
|
|
|
|
74
|
|
|
$hasLocation = stristr($controller->Content, '$UserDefinedForm'); |
|
|
|
|
75
|
|
|
if ($hasLocation) { |
76
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
77
|
|
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
78
|
|
|
$content = preg_replace( |
79
|
|
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
80
|
|
|
$formEscapedForRegex, |
81
|
|
|
$controller->Content |
|
|
|
|
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $controller->customise([ |
85
|
|
|
'Content' => DBField::create_field('HTMLText', $content), |
86
|
|
|
'Form' => '', |
87
|
|
|
'PartialLink' => $partial->getPartialLink() |
88
|
|
|
])->renderWith([static::class, Page::class]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $controller->customise([ |
93
|
|
|
'Content' => DBField::create_field('HTMLText', $controller->Content), |
|
|
|
|
94
|
|
|
'Form' => $form, |
95
|
|
|
'PartialLink' => $partial->getPartialLink() |
96
|
|
|
])->renderWith([static::class, Page::class]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* A little abstraction to be more readable |
101
|
|
|
* |
102
|
|
|
* @param HTTPRequest $request |
103
|
|
|
* @return PartialFormSubmission|void |
104
|
|
|
* @throws HTTPResponse_Exception |
105
|
|
|
*/ |
106
|
4 |
|
public function validateToken($request) |
107
|
|
|
{ |
108
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
109
|
4 |
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
110
|
|
|
|
111
|
4 |
|
$key = $request->param('Key'); |
112
|
4 |
|
$token = $request->param('Token'); |
113
|
4 |
|
if (!$key || !$token) { |
114
|
1 |
|
return $this->httpError(404); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** @var PartialFormSubmission $partial */ |
118
|
3 |
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
119
|
3 |
|
if (!$token || |
120
|
3 |
|
!$partial || |
121
|
1 |
|
!$partial->UserDefinedFormID || |
122
|
3 |
|
!hash_equals($partial->generateKey($token), $key) |
123
|
|
|
) { |
124
|
2 |
|
return $this->httpError(404); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$sessionKey = PartialSubmissionController::SESSION_KEY; |
128
|
|
|
// Set the session if the last session has expired |
129
|
1 |
|
if (!$request->getSession()->get($sessionKey)) { |
130
|
1 |
|
$request->getSession()->set($sessionKey, $partial->ID); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return $partial; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return PartialFormSubmission |
138
|
|
|
*/ |
139
|
|
|
public function getPartialFormSubmission(): PartialFormSubmission |
140
|
|
|
{ |
141
|
|
|
return $this->partialFormSubmission; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param PartialFormSubmission $partialFormSubmission |
146
|
|
|
*/ |
147
|
|
|
public function setPartialFormSubmission(PartialFormSubmission $partialFormSubmission): void |
148
|
|
|
{ |
149
|
|
|
$this->partialFormSubmission = $partialFormSubmission; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|