Repository navigation
Expand file tree
/
Copy pathcloudforms.php
More file actions
325 lines (306 loc) · 16.9 KB
/
Copy pathcloudforms.php
File metadata and controls
325 lines (306 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
// Cloudforms extension, https://github.com/pfadfinder26/yellow-cloudforms
// Based on Datenstrom Yellow, https://datenstrom.se/yellow/
class YellowCloudforms {
const VERSION = "0.3.0";
public $yellow; // access to API
// Handle initialisation
public function onLoad($yellow) {
$this->yellow = $yellow;
$this->yellow->system->setDefault("cloudformsUrl", "");
$this->yellow->system->setDefault("cloudformsCacheTime", "3600");
$this->yellow->system->setDefault("cloudformsLabelSubmit", "Send");
$this->yellow->system->setDefault("cloudformsLabelDone", "Thank you, your answer is in.");
$this->yellow->system->setDefault("cloudformsLabelFailed", "Sorry, your answer could not be sent.");
$this->yellow->system->setDefault("cloudformsLabelRequired", "required");
$this->yellow->system->setDefault("cloudformsLabelOpen", "Open form in the cloud");
$this->yellow->system->setDefault("cloudformsLabelClosed", "This form does not take answers at the moment.");
$this->yellow->system->setDefault("cloudformsLabelFile",
"Print this sheet and bring it along, or send it to us by email.");
$this->yellow->system->setDefault("cloudformsLabelAgain", "Fill in the form again");
}
// Handle request, an answer is sent to the cloud and the page is shown again
public function onRequest($scheme, $address, $base, $location, $fileName) {
if ($this->yellow->toolbox->getServer("REQUEST_METHOD")!="POST") return 0;
$hash = trim($this->yellow->page->getRequest("cloudform"));
if (is_string_empty($hash) || !preg_match("/^[A-Za-z0-9]+$/", $hash)) return 0;
if (!$this->isRequestSameSite($scheme, $address)) return $this->yellow->sendStatus(400);
if (!is_string_empty(trim($this->yellow->page->getRequest("cloudform-website")))) {
return $this->yellow->sendStatus(400);
}
$form = $this->getForm($this->getFormUrl($hash));
$status = is_null($form) ? "failed" : $this->sendAnswers($form, $hash);
// the answer comes back on the page it was sent from, the result in a short lived cookie
setcookie("cloudform", $status, time()+60, "$base/", "", $scheme=="https", true);
$location = $this->yellow->lookup->normaliseUrl($scheme, $address, $base, $location);
return $this->yellow->sendStatus(303, $location);
}
// Handle page content element
public function onParseContentElement($page, $name, $text, $attributes, $type) {
$output = null;
if ($name=="form" && ($type=="block" || $type=="inline")) {
list($url) = $this->yellow->toolbox->getTextArguments($text);
if (is_string_empty($url)) $url = $this->yellow->system->get("cloudformsUrl");
if (is_string_empty($url)) return $this->getErrorHtml("Please add a form link!");
$hash = $this->getShareHash($url);
if (is_string_empty($hash)) return $this->getErrorHtml("Can't understand form link '$url'!");
$form = $this->getForm($this->getFormUrl($hash));
if (is_null($form)) return $this->getErrorHtml("Can't read form '$url'!");
$page->setLastModified(time());
$output = $this->getFormHtml($page, $form, $hash);
}
return $output;
}
// Return the form of a share, from the cache of this server if it is fresh enough
public function getForm($url) {
$fileName = $this->yellow->system->get("coreCacheDirectory")."cloudforms-".
substru(md5($url), 0, 8).".cache";
$cacheTime = intval($this->yellow->system->get("cloudformsCacheTime"));
if (is_file($fileName) && filemtime($fileName)+$cacheTime>time()) {
return $this->getFormData($this->yellow->toolbox->readFile($fileName));
}
$context = stream_context_create(array("http" => array("timeout" => 5,
"header" => "User-Agent: Datenstrom Yellow Cloudforms\r\n")));
$fileData = @file_get_contents($url, false, $context, 0, 2097152);
if ($fileData===false || strposu($fileData, "initial-state-forms-form")===false) {
return is_file($fileName) ? $this->getFormData($this->yellow->toolbox->readFile($fileName)) : null;
}
$this->yellow->toolbox->writeFile($fileName, $fileData, true);
return $this->getFormData($fileData);
}
// Return the questions of a form, the page of a share carries them as its initial state
public function getFormData($fileData) {
if (!preg_match("/initial-state-forms-form\"\s+value=\"([A-Za-z0-9+\/=]+)\"/", $fileData, $matches)) return null;
$form = @json_decode(base64_decode($matches[1]), true);
if (!is_array($form) || !isset($form["id"]) || !isset($form["questions"])) return null;
return $form;
}
// Return the address of a shared form
public function getFormUrl($hash) {
list($server) = $this->getShare($this->yellow->system->get("cloudformsUrl"));
return "$server/apps/forms/s/$hash";
}
// Return the hash of a form share, as a link from the app or as a hash
public function getShareHash($url) {
if (preg_match("#^(?:https?://[^/]+)?/?(?:index\.php/)?apps/forms/(?:s|embed)/([A-Za-z0-9]+)#", $url, $matches)) {
return $matches[1];
}
return preg_match("/^[A-Za-z0-9]{8,}$/", $url) ? $url : "";
}
// Return server and hash of a form link
public function getShare($url) {
if (preg_match("#^(https?://[^/]+)/(?:index\.php/)?apps/forms/(?:s|embed)/([A-Za-z0-9]+)#", $url, $matches)) {
return array($matches[1], $matches[2]);
}
if (preg_match("#^(https?://[^/]+)#", $url, $matches)) return array($matches[1], "");
return array("", "");
}
// Send the answers of this request to the cloud
public function sendAnswers($form, $hash) {
$answers = array();
foreach ($form["questions"] as $question) {
$value = $this->yellow->page->getRequest("cloudform-".$question["id"]);
if (is_array($value)) {
$value = array_values(array_filter(array_map("strval", $value), function ($text) {
return !is_string_empty($text);
}));
} else {
$value = is_string_empty(trim(strval($value))) ? array() : array(trim(strval($value)));
}
if (!is_array_empty($value)) $answers[strval($question["id"])] = $value;
}
if (is_array_empty($answers)) return "failed";
list($server) = $this->getShare($this->yellow->system->get("cloudformsUrl"));
$url = $server."/ocs/v2.php/apps/forms/api/v3/forms/".intval($form["id"])."/submissions";
$content = json_encode(array("answers" => $answers, "shareHash" => $hash));
$context = stream_context_create(array("http" => array(
"method" => "POST",
"timeout" => 15,
"ignore_errors" => true,
"header" => "OCS-APIRequest: true\r\nContent-Type: application/json\r\n".
"Accept: application/json\r\nContent-Length: ".strlenb($content)."\r\n",
"content" => $content)));
$fileData = @file_get_contents($url, false, $context);
if ($fileData===false) return "failed";
$data = @json_decode($fileData, true);
$statusCode = isset($data["ocs"]["meta"]["statuscode"]) ? intval($data["ocs"]["meta"]["statuscode"]) : 0;
return ($statusCode>=100 && $statusCode<300) ? "done" : "failed";
}
// Check if a request comes from this website
public function isRequestSameSite($scheme, $address) {
$origin = "";
if (preg_match("#^(\w+)://([^/]+)#", $this->yellow->toolbox->getServer("HTTP_REFERER"), $matches)) {
$origin = "$matches[1]://$matches[2]";
}
if ($this->yellow->toolbox->getServer("HTTP_ORIGIN")) $origin = $this->yellow->toolbox->getServer("HTTP_ORIGIN");
return $origin=="$scheme://$address";
}
// Return form HTML, the questions of the cloud as fields of this website
public function getFormHtml($page, $form, $hash) {
$status = $this->yellow->toolbox->getCookie("cloudform");
if ($status=="done" || $status=="failed") {
$page->setHeader("Set-Cookie", "cloudform=; Max-Age=0; Path=".
$this->yellow->system->get("coreServerBase")."/");
$page->setHeader("Cache-Control", "no-store");
}
$output = "<div class=\"cloudform\">\n";
if ($status=="done" || $status=="failed") {
$message = $this->yellow->system->get($status=="done" ?
"cloudformsLabelDone" : "cloudformsLabelFailed");
if ($status=="done" && !is_string_empty(strval($form["submissionMessage"]))) {
$message = strval($form["submissionMessage"]);
}
$output .= "<p class=\"cloudform-status cloudform-".htmlspecialchars($status)."\">".
nl2br(htmlspecialchars($message))."</p>\n";
}
$closed = isset($form["canSubmit"]) && !$form["canSubmit"];
foreach ($form["questions"] as $question) {
if ($question["type"]=="file" && !empty($question["isRequired"])) $closed = true;
}
if ($status=="done") {
$output .= "<p class=\"cloudform-again\"><a class=\"button\" href=\"".
htmlspecialchars($page->getLocation(true))."\">".
htmlspecialchars($this->yellow->system->get("cloudformsLabelAgain"))."</a></p>\n";
}
if ($status!="done" && $closed) {
$output .= "<p class=\"cloudform-status cloudform-closed\">".
htmlspecialchars($this->yellow->system->get("cloudformsLabelClosed"))."</p>\n";
}
if ($status!="done" && !$closed) {
$output .= "<form class=\"cloudform-fields\" method=\"post\" action=\"".
htmlspecialchars($page->getLocation(true))."\">\n";
$output .= "<input type=\"hidden\" name=\"cloudform\" value=\"".htmlspecialchars($hash)."\" />\n";
$output .= "<p class=\"cloudform-website\"><label>Website<input type=\"text\"".
" name=\"cloudform-website\" tabindex=\"-1\" autocomplete=\"off\" /></label></p>\n";
if (!is_string_empty($form["title"])) {
$output .= "<h3>".htmlspecialchars($form["title"])."</h3>\n";
}
if (!is_string_empty(strval($form["description"]))) {
$output .= "<p class=\"cloudform-intro\">".
nl2br(htmlspecialchars(strval($form["description"])))."</p>\n";
}
foreach ($form["questions"] as $question) $output .= $this->getQuestionHtml($question);
// the way to the form in the cloud stands beside the button that sends this one
$output .= "<p class=\"cloudform-submit\"><button type=\"submit\">".
htmlspecialchars($this->yellow->system->get("cloudformsLabelSubmit"))."</button>".
$this->getLinkHtml($hash)."</p>\n";
$output .= "</form>\n";
} else {
$output .= "<p class=\"cloudform-link-alone\">".$this->getLinkHtml($hash)."</p>\n";
}
$output .= "</div>\n";
return $output;
}
// Return the link that opens the form in the cloud
public function getLinkHtml($hash) {
return "<a class=\"cloudform-link\" href=\"".htmlspecialchars($this->getFormUrl($hash))."\">".
htmlspecialchars($this->yellow->system->get("cloudformsLabelOpen"))."</a>";
}
// Return one question as its fields
public function getQuestionHtml($question) {
$id = "cloudform-".intval($question["id"]);
$name = "cloudform-".intval($question["id"]);
$required = !is_array_empty($question["options"]) || $question["type"]!="multiple" ?
!empty($question["isRequired"]) : false;
$output = "<div class=\"cloudform-question\">\n";
$output .= "<span class=\"cloudform-label\" id=\"$id-label\">".htmlspecialchars($question["text"]);
if ($required) {
$output .= " <span class=\"cloudform-required\">".
htmlspecialchars($this->yellow->system->get("cloudformsLabelRequired"))."</span>";
}
$output .= "</span>\n";
if (!is_string_empty($question["description"])) {
$output .= "<span class=\"cloudform-description\">".htmlspecialchars($question["description"])."</span>\n";
}
$attributes = $required ? " required=\"required\"" : "";
switch ($question["type"]) {
case "long":
$output .= "<textarea id=\"$id\" name=\"$name\" rows=\"4\"$attributes></textarea>\n";
break;
case "date":
$output .= "<input type=\"date\" id=\"$id\" name=\"$name\"$attributes />\n";
break;
case "time":
$output .= "<input type=\"time\" id=\"$id\" name=\"$name\"$attributes />\n";
break;
case "datetime":
$output .= "<input type=\"datetime-local\" id=\"$id\" name=\"$name\"$attributes />\n";
break;
case "dropdown":
$output .= "<select id=\"$id\" name=\"$name\"$attributes>\n<option value=\"\"></option>\n";
foreach ($question["options"] as $option) {
$output .= "<option value=\"".htmlspecialchars($option["id"])."\">".
htmlspecialchars($option["text"])."</option>\n";
}
$output .= "</select>\n";
break;
case "multiple":
case "multiple_unique":
$unique = $question["type"]=="multiple_unique";
$lengthMax = 0;
foreach ($question["options"] as $option) {
$lengthMax = max($lengthMax, strlenu($option["text"]));
}
// answers as short as "yes" and "no" fit in one line, the theme decides how
$class = "cloudform-options".($lengthMax<=15 ? " cloudform-options-short" : "");
$output .= "<span class=\"".$class."\" role=\"group\" aria-labelledby=\"$id-label\">\n";
foreach ($question["options"] as $number=>$option) {
$output .= "<label><input type=\"".($unique ? "radio" : "checkbox")."\"".
" name=\"$name".($unique ? "" : "[]")."\" value=\"".htmlspecialchars($option["id"])."\"".
($unique && $required ? $attributes : "")." /> ".
htmlspecialchars($option["text"])."</label>\n";
}
$output .= "</span>\n";
break;
case "file":
// the cloud takes the file itself, this website never sees it
$output .= "<span class=\"cloudform-file\">".
htmlspecialchars($this->yellow->system->get("cloudformsLabelFile"))."</span>\n";
break;
default:
$output .= "<input type=\"".$this->getInputType($question)."\" id=\"$id\" name=\"$name\"".
$this->getInputAttributes($question).$attributes." />\n";
}
return $output."</div>\n";
}
// Return the type of a field, what the question is checked for in the cloud
public function getInputType($question) {
switch ($this->getValidation($question)) {
case "email": return "email";
case "phone": return "tel";
case "number": return "number";
default: return "text";
}
}
// Return what the browser can check and fill in by itself
public function getInputAttributes($question) {
$output = "";
// "inputmode" does not survive the filter of Yellow, the type of the field says enough
$validation = $this->getValidation($question);
if ($validation=="regex") {
$expression = $this->getExtraSetting($question, "validationRegex");
if (preg_match("#^/(.*)/[a-z]*$#s", $expression, $matches)) $expression = $matches[1];
if (!is_string_empty($expression)) {
$output .= " pattern=\"".htmlspecialchars($expression)."\"";
}
}
$autocomplete = trim(strval($question["name"]));
if (!is_string_empty($autocomplete) && preg_match("/^[\w\- ]+$/", $autocomplete)) {
$output .= " autocomplete=\"".htmlspecialchars($autocomplete)."\"";
}
return $output;
}
// Return how a question is checked, empty when it is not
public function getValidation($question) {
return strtoloweru(strval($this->getExtraSetting($question, "validationType")));
}
// Return a setting a question carries beside its type
public function getExtraSetting($question, $key) {
$extra = isset($question["extraSettings"]) ? $question["extraSettings"] : array();
return is_array($extra) && isset($extra[$key]) ? $extra[$key] : "";
}
// Return error message for authors
public function getErrorHtml($text) {
return "<p class=\"error\">Cloudforms: ".htmlspecialchars($text)."</p>\n";
}
}