-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPicoContactModal.php
662 lines (606 loc) · 22.9 KB
/
PicoContactModal.php
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
<?php
/**
* @id PicoContactModal.php 30.Jan.2016
*
* a pico plugin, which provides a ready to use contact form
* only for installation with bootstrap. for other themes
* there has to made some changes to get it running.
*
*
* @author Uwe Fritz
* @link http://www.edor.de
* @copyright Copyright (c) 2016 edor.de (Uwe Fritz)
* @license GPL
*/
final class PicoContactModal extends AbstractPicoPlugin
{
/**
* This plugin is enabled by default?
*
* @see AbstractPicoPlugin::$enabled
* @var boolean
*/
protected $enabled = false;
/**
* This plugin depends on ...
*
* @see AbstractPicoPlugin::$dependsOn
* @var string[]
*/
protected $dependsOn = array();
// some text from config
private $title;
private $intro;
private $subintro;
private $name;
private $email;
private $message;
private $buttontext;
private $missingName;
private $missingEmail;
private $wrongEmail;
private $missingMessage;
private $successMessageHeading;
private $successMessage;
private $serverErrorMessageHeading;
private $serverErrorMessage;
private $myMailSubject;
private $myMailSomeSenderName;
private $myMailToAddress;
private $myMailLineDescForName;
private $myMailLineDescForEmail;
private $myMailLineDescForMEssage;
private $pluginPath;
private $pagetitle;
private $MinTimeInSecondsForHumanToWriteMessage;
/**
* Triggered after Pico has loaded all available plugins
*
* This event is triggered nevertheless the plugin is enabled or not.
* It is NOT guaranteed that plugin dependencies are fulfilled!
*
* @see Pico::getPlugin()
* @see Pico::getPlugins()
* @param object[] &$plugins loaded plugin instances
* @return void
*/
public function onPluginsLoaded(array &$plugins)
{
// your code
}
/**
* Triggered after Pico has read its configuration
*
* @see Pico::getConfig()
* @param mixed[] &$config array of config variables
* @return void
*/
public function onConfigLoaded(array &$config)
{
// your code
$this->title = $config['PicoContactModal']['TitleH4'];
$this->intro = $config['PicoContactModal']['IntroTextForLaregeScreens'];
$this->subintro = $config['PicoContactModal']['SubIntroTextForAllScreens'];
$this->name = $config['PicoContactModal']['TextForNameField'];
$this->email = $config['PicoContactModal']['TextForEmailField'];
$this->message = $config['PicoContactModal']['TextForMessageField'];
$this->buttontext = $config['PicoContactModal']['TextOnSubmitButton'];
$this->missingName = $config['PicoContactModal']['MissingName'];
$this->missingEmail = $config['PicoContactModal']['MissingEmailAddress'];
$this->wrongEmail = $config['PicoContactModal']['WrongEmailAddress'];
$this->missingMessage = $config['PicoContactModal']['MissingMessage'];
$this->successMessageHeading = $config['PicoContactModal']['MessageSendSuccessHeading'];
$this->successMessage = $config['PicoContactModal']['MessageSendSuccessText'];
$this->serverErrorMessageHeading = $config['PicoContactModal']['MessageServerErrorHeading'];
$this->serverErrorMessage = $config['PicoContactModal']['MessageServerErrorText'];
$this->myMailSubject = $config['PicoContactModal']['MyMailSubject'];
$this->myMailSomeSenderName = $config['PicoContactModal']['MyMailSomeSenderName'];
$this->myMailToAddress = $config['PicoContactModal']['MyMailToAddress'];
$this->myMailLineDescForName = $config['PicoContactModal']['MyMailLineDescForName'];
$this->myMailLineDescForEmail = $config['PicoContactModal']['MyMailLineDescForEmail'];
$this->myMailLineDescForMEssage = $config['PicoContactModal']['MyMailLineDescForMEssage'];
$this->pluginPath = $config[ 'base_url' ]."plugins/PicoContactModal/";
$this->MinTimeInSecondsForHumanToWriteMessage = $config['PicoContactModal']['MinTimeInSecondsForHumanToWriteMessage'];
}
/**
* Triggered after Pico has evaluated the request URL
*
* @see Pico::getRequestUrl()
* @param string &$url part of the URL describing the requested contents
* @return void
*/
public function onRequestUrl(&$url)
{
// your code
}
/**
* Triggered after Pico has discovered the content file to serve
*
* @see Pico::getBaseUrl()
* @see Pico::getRequestFile()
* @param string &$file absolute path to the content file to serve
* @return void
*/
public function onRequestFile(&$file)
{
// your code
}
/**
* Triggered before Pico reads the contents of the file to serve
*
* @see Pico::loadFileContent()
* @see DummyPlugin::onContentLoaded()
* @param string &$file path to the file which contents will be read
* @return void
*/
public function onContentLoading(&$file)
{
// your code
}
/**
* Triggered after Pico has read the contents of the file to serve
*
* @see Pico::getRawContent()
* @param string &$rawContent raw file contents
* @return void
*/
public function onContentLoaded(&$rawContent)
{
// your code
}
/**
* Triggered before Pico reads the contents of a 404 file
*
* @see Pico::load404Content()
* @see DummyPlugin::on404ContentLoaded()
* @param string &$file path to the file which contents were requested
* @return void
*/
public function on404ContentLoading(&$file)
{
// your code
}
/**
* Triggered after Pico has read the contents of the 404 file
*
* @see Pico::getRawContent()
* @param string &$rawContent raw file contents
* @return void
*/
public function on404ContentLoaded(&$rawContent)
{
// your code
}
/**
* Triggered when Pico reads its known meta header fields
*
* @see Pico::getMetaHeaders()
* @param string[] &$headers list of known meta header
* fields; the array value specifies the YAML key to search for, the
* array key is later used to access the found value
* @return void
*/
public function onMetaHeaders(array &$headers)
{
// your code
}
/**
* Triggered before Pico parses the meta header
*
* @see Pico::parseFileMeta()
* @see DummyPlugin::onMetaParsed()
* @param string &$rawContent raw file contents
* @param string[] &$headers known meta header fields
* @return void
*/
public function onMetaParsing(&$rawContent, array &$headers)
{
// your code
}
/**
* Triggered after Pico has parsed the meta header
*
* @see Pico::getFileMeta()
* @param string[] &$meta parsed meta data
* @return void
*/
public function onMetaParsed(array &$meta)
{
// your code
$this->pagetitle = $meta['title'];
}
/**
* Triggered before Pico parses the pages content
*
* @see Pico::prepareFileContent()
* @see DummyPlugin::prepareFileContent()
* @see DummyPlugin::onContentParsed()
* @param string &$rawContent raw file contents
* @return void
*/
public function onContentParsing(&$rawContent)
{
// your code
}
/**
* Triggered after Pico has prepared the raw file contents for parsing
*
* @see Pico::parseFileContent()
* @see DummyPlugin::onContentParsed()
* @param string &$content prepared file contents for parsing
* @return void
*/
public function onContentPrepared(&$content)
{
// generate link
$url_first = "<a href=\"#\" data-toggle=\"modal\" data-target=\"#contactModal\"><span class=\"glyphicon glyphicon-link\"></span>";
$url_last = "</a>";
$content = preg_replace('/\%contact_start%(.*?)\%contact_end%/', "{$url_first}$1{$url_last}" , $content);
}
/**
* Triggered after Pico has parsed the contents of the file to serve
*
* @see Pico::getFileContent()
* @param string &$content parsed contents
* @return void
*/
public function onContentParsed(&$content)
{
$title = $this->title;
$intro = $this->intro;
$subintro = $this->subintro;
$name = $this->name;
$email = $this->email;
$message = $this->message;
$buttontext = $this->buttontext;
// your code
$content .= "\n";
$content .= <<<EOT
<div class="modal fade" id="contactModal" tabindex="-1" role="dialog" aria-labelledby="contactModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" title="close">X</button>
<h4 class="modal-title">$title</h4>
<p class="hidden-xs">$intro
</p>
<p>$subintro</p>
</div>
<div class="modal-body">
<div id="thanks"></div>
<form id="contactForm" method="post" class="form-horizontal" action="">
<div class="form-group">
<label for="yname" class="col-md-3 control-label">$name</label>
<div class="col-md-8">
<input type="text" name="yname" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-3 control-label">$email</label>
<div class="col-md-8">
<input type="email" name="email" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-3 control-label">$message</label>
<div class="col-md-8">
<textarea name="message" class="form-control" /></textarea>
</div>
</div>
<p id="hidden"></p>
<input type="hidden" name="send" value="true">
<div class="form-group">
<div class="col-md-5 col-md-offset-3">
<button type="submit" class="btn btn-success"><i
class="glyphicon glyphicon-hand-right"></i>$buttontext</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
EOT;
$subject = $this->myMailSubject;
$senderName = $this->myMailSomeSenderName;
$emailTo = $this->myMailToAddress;
$lineName = $this->myMailLineDescForName;
$lineMail = $this->myMailLineDescForEmail;
$lineComment = $this->myMailLineDescForMEssage;
// are you a stupid bot?
if ( isset( $_POST[ 'send' ] ) && !empty( $_POST[ 'name' ] ) ) {
die();
}
//If the form is submitted
if ( isset( $_POST[ 'yname' ] ) ) {
//Check to make sure comments were entered -> last field first
if ( trim( $_POST[ 'message' ] ) == '' ) {
$hasError = TRUE;
// somebody try to cheat
die();
}
else {
if ( function_exists( 'stripslashes' ) ) {
$comments = stripslashes( trim( $_POST[ 'message' ] ) );
}
else {
$comments = trim( $_POST[ 'message' ] );
}
}
//Check to make sure sure that a valid email address is submitted -> then second fieled
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if ( trim( $_POST[ 'email' ] ) == '' ) {
$hasError = TRUE;
// somebody try to cheat
die();
}
else if ( !preg_match( $regex, trim( $_POST[ 'email' ] ) ) ) {
$hasError = TRUE;
// somebody try to cheat
die();
}
else {
$email = trim( $_POST[ 'email' ] );
}
//Check to make sure that the name field is not empty -> now the first field
if ( trim( $_POST[ 'yname' ] ) == '' ) {
$hasError = TRUE;
// somebody try to cheat
die();
}
else {
$name = trim( $_POST[ 'yname' ] );
}
// bot ckeck
$jetzt = time ();
$form_start = $_POST['reference'];
$diff = $jetzt-$form_start;
//Check to make sure that the form is opened by user action
if ( !$form_start ) {
$hasError = TRUE;
// somebody try to cheat
die();
}
//Check to make sure that the time between opening the form and sending is respected
if ( $diff < $this->MinTimeInSecondsForHumanToWriteMessage ) {
$hasError = TRUE;
// somebody try to cheat
die();
}
//If there is no error, send the email
if ( !isset( $hasError ) ) {
$body = "$lineName $name \n\n$lineMail $email \n\n\n$lineComment\n$comments";
$headers = 'From: '.$senderName.' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
if ( $this->pagetitle != "contactFormTest" ) mail($emailTo, $subject, $body, $headers);
}
}
}
/**
* Triggered before Pico reads all known pages
*
* @see Pico::readPages()
* @see DummyPlugin::onSinglePageLoaded()
* @see DummyPlugin::onPagesLoaded()
* @return void
*/
public function onPagesLoading()
{
// your code
}
/**
* Triggered when Pico reads a single page from the list of all known pages
*
* The `$pageData` parameter consists of the following values:
*
* | Array key | Type | Description |
* | -------------- | ------ | ---------------------------------------- |
* | id | string | relative path to the content file |
* | url | string | URL to the page |
* | title | string | title of the page (YAML header) |
* | description | string | description of the page (YAML header) |
* | author | string | author of the page (YAML header) |
* | time | string | timestamp derived from the Date header |
* | date | string | date of the page (YAML header) |
* | date_formatted | string | formatted date of the page |
* | raw_content | string | raw, not yet parsed contents of the page |
* | meta | string | parsed meta data of the page |
*
* @see DummyPlugin::onPagesLoaded()
* @param array &$pageData data of the loaded page
* @return void
*/
public function onSinglePageLoaded(array &$pageData)
{
// your code
}
/**
* Triggered after Pico has read all known pages
*
* See {@link DummyPlugin::onSinglePageLoaded()} for details about the
* structure of the page data.
*
* @see Pico::getPages()
* @see Pico::getCurrentPage()
* @see Pico::getPreviousPage()
* @see Pico::getNextPage()
* @param array[] &$pages data of all known pages
* @param array|null &$currentPage data of the page being served
* @param array|null &$previousPage data of the previous page
* @param array|null &$nextPage data of the next page
* @return void
*/
public function onPagesLoaded(
array &$pages,
array &$currentPage = null,
array &$previousPage = null,
array &$nextPage = null
) {
// your code
}
/**
* Triggered before Pico registers the twig template engine
*
* @return void
*/
public function onTwigRegistration()
{
// your code
}
/**
* Triggered before Pico renders the page
*
* @see Pico::getTwig()
* @see DummyPlugin::onPageRendered()
* @param Twig_Environment &$twig twig template engine
* @param mixed[] &$twigVariables template variables
* @param string &$templateName file name of the template
* @return void
*/
public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
{
// your code
}
/**
* Triggered after Pico has rendered the page
*
* @param string &$output contents which will be sent to the user
* @return void
*/
public function onPageRendered(&$output)
{
// your code
$missingName = $this->missingName;
$missingEmail = $this->missingEmail;
$wrongEmail = $this->wrongEmail;
$missingMessage = $this->missingMessage;
$successMessageHeading = $this->successMessageHeading;
$successMessage = $this->successMessage;
$serverErrorMessageHeading = $this->serverErrorMessageHeading;
$serverErrorMessage = $this->serverErrorMessage;
$pluginPath = $this->pluginPath;
// css in head
$head = "\n";
$head .= '<link href="'.$pluginPath.'assets/css/bootstrapValidator.min.css" rel="stylesheet">';
$head .= "\n";
$head .= "</head> ";
$head .= "\n";
$output = preg_replace('/(.*<\s*\/head[^>]*>)/i', $head, $output);
$alertContainer = <<<EOT
<body>
<div id="hideOnLoad" style="visibility: hidden;">
<div class="hidden-xs" style="position: fixed; left: 50%; top: 120px; margin: 10px; z-index: 2000;">
<div style="left: -50%; position: relative;">
<div id="formResultContainer" class="alert alert-danger alert-dismissible fade in" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true" title="close">X</span></button><h4 id="formResultHeadingSmall"></h4>
<p id="formResultText"></p>
</div>
</div>
</div>
<div class="visible-xs" style="position: fixed; margin: 10px; top: 80px; z-index: 2000;">
<div id="formResultContainerSmall" class="alert alert-danger alert-dismissible fade in" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">X</span></button><h4 id="formResultHeading"></h4>
<p id="formResultTextSmall"></p>
</div>
</div>
</div>
EOT;
$output .= "\n";
$output = preg_replace('/(.*<\s*body[^>]*>)|(<\s*\/\s*body\s*\>.+)/i', $alertContainer, $output);
$output .= "\n";
$js = <<<EOT
<!--old and free (MIT lisenced) version of bootstrapValidator from: https://github.com/nghuuphuoc/bootstrapvalidator -->
<script src="{$pluginPath}assets/js/bootstrapValidator.min.js"></script>
<script>
$(document).ready(function() {
$('#contactModal').on('show.bs.modal', function() {
$('#contactForm').bootstrapValidator('resetForm', true);
$("input[id='should']").remove();
insertInput(Math.round((new Date()).getTime()/1000));
});
$('#contactForm').bootstrapValidator({
message: 'This value is not valid',
excluded: [':disabled'],
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
submitHandler: function(validator, form, submitButton) {
$.ajax({
type: "POST",
url: "",
data: $('#contactForm').serialize(),
success: function () {
$("#contactModal").modal('hide');
// make success class
document.getElementById("formResultContainer").className = "alert alert-success alert-dismissible fade in";
document.getElementById("formResultContainerSmall").className = "alert alert-success alert-dismissible fade in";
// show the result div
document.getElementById("hideOnLoad").style.visibility = "visible";
formResultHeading.textContent = "$successMessageHeading";
formResultHeadingSmall.textContent = "$successMessageHeading";
formResultText.textContent = "$successMessage";
formResultTextSmall.textContent = "$successMessage";
},
error: function () {
// show the result div
document.getElementById("hideOnLoad").style.visibility = "visible";
formResultHeading.textContent = "$serverErrorMessageHeading";
formResultHeadingSmall.textContent = "$serverErrorMessageHeading";
formResultText.textContent = "$serverErrorMessage";
formResultTextSmall.textContent = "$serverErrorMessage";
}
});
},
fields: {
yname: {
validators: {
notEmpty: {
message: '$missingName'
}
}
},
email: {
validators: {
notEmpty: {
message: '$missingEmail'
},
emailAddress: {
message: '$wrongEmail'
}
}
},
message: {
validators: {
notEmpty: {
message: '$missingMessage'
}
}
}
}
});
});
</script>
<script type="text/javascript">
hname="reference";
function insertInput(hvalue){
var para, hiddenInput, br;
para = document.getElementById('hidden');
hiddenInput = document.createElement('input');
hiddenInput.type = 'hidden';
hiddenInput.name = hname;
hiddenInput.id = 'should';
hiddenInput.value = hvalue;
para.appendChild(hiddenInput);
br = document.createElement('br'); //Not sure why you needed this <br> tag but here it is
para.appendChild(br);
return true; //Have this function return true if you want to post right away after adding the hidden value, otherwise leave it to false
}
</script>
</body>
EOT;
$output .= "\n";
$output = preg_replace('/(.*<\s*\/body[^>]*>)/i', $js, $output);
$output .= "\n";
}
}