1 | <?php |
||
24 | class PartialUserFormController extends UserDefinedFormController |
||
25 | { |
||
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 Exception |
||
50 | */ |
||
51 | public function partial(HTTPRequest $request) |
||
52 | { |
||
53 | /** @var PartialFormSubmission $partial */ |
||
54 | $partial = $this->setData($request); |
||
55 | if ($this->dataRecord->PasswordProtected && |
||
56 | $request->getSession()->get(PasswordForm::PASSWORD_SESSION_KEY) !== $partial->ID |
||
57 | ) { |
||
58 | return $this->redirect('verify'); |
||
59 | } |
||
60 | |||
61 | // $this->setFailover($this->dataRecord); |
||
62 | |||
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 | public function setData($request) |
||
102 | { |
||
103 | // Ensure this URL doesn't get picked up by HTTP caches |
||
104 | HTTPCacheControlMiddleware::singleton()->disableCache(); |
||
105 | |||
106 | $key = $request->param('Key'); |
||
107 | $token = $request->param('Token'); |
||
108 | |||
109 | /** @var PartialFormSubmission $partial */ |
||
110 | $partial = PartialFormSubmission::get()->find('Token', $token); |
||
111 | if (!$token || |
||
112 | !$partial || |
||
113 | !$partial->UserDefinedFormID || |
||
114 | !hash_equals($partial->generateKey($token), $key) |
||
115 | ) { |
||
116 | return $this->httpError(404); |
||
117 | } |
||
118 | |||
119 | $session = $this->getRequest()->getSession(); |
||
120 | // Set the session if the last session has expired |
||
121 | if (!$session->get(PartialSubmissionController::SESSION_KEY)) { |
||
122 | $session->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
||
123 | } |
||
124 | |||
125 | $this->setPartialFormSubmission($partial); |
||
126 | // Set data record and load the form |
||
127 | /** @var UserDefinedForm dataRecord */ |
||
128 | $this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
||
129 | |||
130 | return $partial; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return PartialFormSubmission |
||
135 | */ |
||
136 | public function getPartialFormSubmission(): PartialFormSubmission |
||
140 | |||
141 | /** |
||
142 | * @param PartialFormSubmission $partialFormSubmission |
||
143 | */ |
||
144 | public function setPartialFormSubmission(PartialFormSubmission $partialFormSubmission): void |
||
148 | } |
||
149 |