Browse code

Rewrite ajax functionality for Contao 4 compatibility

Benjamin Roth authored on22/03/2019 20:54:43
Showing1 changed files
... ...
@@ -30,11 +30,16 @@
30 30
 
31 31
 namespace legalAgeCheck;
32 32
 
33
+use Contao\Environment;
34
+use Contao\Input;
33 35
 use Contao\InsertTags;
34 36
 use Contao\PageModel;
35 37
 use Contao\LayoutModel;
36 38
 use Contao\PageRegular;
37
-use Contao\System;
39
+use Contao\FrontendTemplate;
40
+use Contao\Frontend;
41
+use Haste\Http\Response\JsonResponse;
42
+use Haste\Http\Response\Response;
38 43
 
39 44
 /**
40 45
  * Class LegalAgeCheck 
... ...
@@ -43,7 +48,7 @@ use Contao\System;
43 48
  * @author     Benjamin Roth <www.esales-media.de> 
44 49
  * @package    Controller
45 50
  */
46
-class LegalAgeCheck extends \Frontend
51
+class LegalAgeCheck extends Frontend
47 52
 {
48 53
 
49 54
 	/**
... ...
@@ -58,6 +63,16 @@ class LegalAgeCheck extends \Frontend
58 63
 	 */
59 64
 	public function performAgeCheck(PageModel $objPage, LayoutModel $objLayout, PageRegular $objPageRegular)
60 65
 	{
66
+	  // Trigger ajax cookie authentication
67
+    if (Environment::get('isAjaxRequest') && Input::get('do') == 'legalage_authentication') {
68
+      if (!is_null(Input::get('legalage_commit')))
69
+      {
70
+        static::sendResponse(array('status'=>(static::ajaxUnlockPage() ? 'OK' : 'FAILED')));
71
+      } else {
72
+        static::preconditionFailed();
73
+      }
74
+    }
75
+
61 76
 		// Skip age check if passed before
62 77
 		//if ($this->Session->get('legalAgeCheck_passed'))
63 78
 		if ($this->Input->cookie('legalAgeCheck_passed') || $objPage->es_ext_agecheck_ignorePage)
... ...
@@ -67,19 +82,19 @@ class LegalAgeCheck extends \Frontend
67 82
 		}
68 83
 
69 84
 		// Get root page
70
-		$objRootPage = \PageModel::findByPk($objPage->rootId);
85
+		$objRootPage = PageModel::findByPk($objPage->rootId);
71 86
 		
72 87
 		// Cancel verification if age check is disabled					
73 88
 		if (is_null($objRootPage) || !$objRootPage->es_ext_agecheck || $objRootPage->es_ext_agecheck_exitPage == $objPage->id)
74 89
 			return;
75 90
 		
76 91
 		// Get exit page
77
-		$objExitPage = \PageModel::findWithDetails($objRootPage->es_ext_agecheck_exitPage);
92
+		$objExitPage = PageModel::findWithDetails($objRootPage->es_ext_agecheck_exitPage);
78 93
 		if (is_null($objExitPage) || !$objExitPage->published)
79 94
 			return;
80 95
 		
81 96
 		// Load dialog template
82
-		$objTemplate = new \FrontendTemplate($this->strTemplate);
97
+		$objTemplate = new FrontendTemplate($this->strTemplate);
83 98
 		
84 99
 		// Load modal box template
85 100
 		//$objModalBoxTemplate = new FrontendTemplate('agecheck_dialog_template');
... ...
@@ -89,8 +104,10 @@ class LegalAgeCheck extends \Frontend
89 104
 		$objTemplate->text = str_replace(array("\r", "\n"),array("\\r","\\n"),$objRootPage->es_ext_agecheck_text);
90 105
 		$objTemplate->btn_over_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['over_18'];
91 106
 		$objTemplate->btn_under_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['under_18'];
92
-		$objTemplate->exitPageURL = $this->generateFrontendUrl($objExitPage->row(), null, null, true);
93
-		$objTemplate->commitURL = 'system/modules/legalAgeCheck/ajax/Ajax.php?do=legalage_authentication&legalage_commit='.$this->createToken();
107
+//		$objTemplate->exitPageURL = $this->generateFrontendUrl($objExitPage->row(), null, null, true);
108
+		$objTemplate->exitPageURL = $objExitPage->getAbsoluteUrl();
109
+//		$objTemplate->commitURL = 'system/modules/legalAgeCheck/ajax/Ajax.php?do=legalage_authentication&legalage_commit='.$this->createToken();
110
+		$objTemplate->commitURL = Frontend::addToUrl('do=legalage_authentication&legalage_commit='.$this->createToken());
94 111
 		//$objTemplate->modalBoxTemplate = addcslashes($objModalBoxTemplate->parse(), "\\'\"&\n\r");
95 112
 		
96 113
 		// Add dialog code to page
... ...
@@ -138,4 +155,16 @@ class LegalAgeCheck extends \Frontend
138 155
 		}
139 156
 		return $_SESSION['legalAgeCheck_token'];
140 157
 	}
158
+
159
+  private static function sendResponse($content)
160
+  {
161
+    $objResponse = new JsonResponse($content);
162
+    $objResponse->send();
163
+  }
164
+
165
+  private static function preconditionFailed()
166
+  {
167
+    $objResponse = new Response('Precondition failed',412);
168
+    $objResponse->send();
169
+  }
141 170
 }
142 171
\ No newline at end of file
Browse code

Allow prod environment

Benjamin Roth authored on22/03/2019 17:08:52
Showing1 changed files
... ...
@@ -58,10 +58,6 @@ class LegalAgeCheck extends \Frontend
58 58
 	 */
59 59
 	public function performAgeCheck(PageModel $objPage, LayoutModel $objLayout, PageRegular $objPageRegular)
60 60
 	{
61
-    if (System::getContainer()->get('kernel')->getEnvironment() != 'dev')
62
-    {
63
-      return;
64
-    }
65 61
 		// Skip age check if passed before
66 62
 		//if ($this->Session->get('legalAgeCheck_passed'))
67 63
 		if ($this->Input->cookie('legalAgeCheck_passed') || $objPage->es_ext_agecheck_ignorePage)
Browse code

Add customization fields for the displayed text.

Benjamin Roth authored on22/03/2019 16:56:28
Showing1 changed files
... ...
@@ -30,6 +30,11 @@
30 30
 
31 31
 namespace legalAgeCheck;
32 32
 
33
+use Contao\InsertTags;
34
+use Contao\PageModel;
35
+use Contao\LayoutModel;
36
+use Contao\PageRegular;
37
+use Contao\System;
33 38
 
34 39
 /**
35 40
  * Class LegalAgeCheck 
... ...
@@ -51,8 +56,12 @@ class LegalAgeCheck extends \Frontend
51 56
 	/**
52 57
 	 * Perform age check
53 58
 	 */
54
-	public function performAgeCheck(\PageModel $objPage, \LayoutModel $objLayout, \PageRegular $objPageRegular)
59
+	public function performAgeCheck(PageModel $objPage, LayoutModel $objLayout, PageRegular $objPageRegular)
55 60
 	{
61
+    if (System::getContainer()->get('kernel')->getEnvironment() != 'dev')
62
+    {
63
+      return;
64
+    }
56 65
 		// Skip age check if passed before
57 66
 		//if ($this->Session->get('legalAgeCheck_passed'))
58 67
 		if ($this->Input->cookie('legalAgeCheck_passed') || $objPage->es_ext_agecheck_ignorePage)
... ...
@@ -80,8 +89,8 @@ class LegalAgeCheck extends \Frontend
80 89
 		//$objModalBoxTemplate = new FrontendTemplate('agecheck_dialog_template');
81 90
 		
82 91
 		// Set template vars
83
-		$objTemplate->title = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['title'];
84
-		$objTemplate->text = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['text'];
92
+		$objTemplate->title = $objRootPage->es_ext_agecheck_title;
93
+		$objTemplate->text = str_replace(array("\r", "\n"),array("\\r","\\n"),$objRootPage->es_ext_agecheck_text);
85 94
 		$objTemplate->btn_over_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['over_18'];
86 95
 		$objTemplate->btn_under_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['under_18'];
87 96
 		$objTemplate->exitPageURL = $this->generateFrontendUrl($objExitPage->row(), null, null, true);
... ...
@@ -89,7 +98,7 @@ class LegalAgeCheck extends \Frontend
89 98
 		//$objTemplate->modalBoxTemplate = addcslashes($objModalBoxTemplate->parse(), "\\'\"&\n\r");
90 99
 		
91 100
 		// Add dialog code to page
92
-		$GLOBALS['TL_BODY'][] = $objTemplate->parse();
101
+		$GLOBALS['TL_BODY'][] = InsertTags::replaceInsertTags($objTemplate->parse());
93 102
 
94 103
 
95 104
 	}
Browse code

Add template code to TL_BODY

Benjamin Roth authored on21/03/2019 13:06:35
Showing1 changed files
... ...
@@ -89,7 +89,7 @@ class LegalAgeCheck extends \Frontend
89 89
 		//$objTemplate->modalBoxTemplate = addcslashes($objModalBoxTemplate->parse(), "\\'\"&\n\r");
90 90
 		
91 91
 		// Add dialog code to page
92
-		$GLOBALS['TL_JQUERY'][] = $objTemplate->parse();
92
+		$GLOBALS['TL_BODY'][] = $objTemplate->parse();
93 93
 
94 94
 
95 95
 	}
Browse code

Initial commit

Benjamin Roth authored on21/03/2019 09:05:45
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,136 @@
1
+<?php
2
+
3
+/**
4
+ * Contao Open Source CMS
5
+ * Copyright (C) 2005-2011 Leo Feyer
6
+ *
7
+ * Formerly known as TYPOlight Open Source CMS.
8
+ *
9
+ * This program is free software: you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation, either
12
+ * version 3 of the License, or (at your option) any later version.
13
+ * 
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ * Lesser General Public License for more details.
18
+ * 
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with this program. If not, please visit the Free
21
+ * Software Foundation website at <http://www.gnu.org/licenses/>.
22
+ *
23
+ * PHP version 5
24
+ * @copyright  eSales Media 2012 
25
+ * @author     Benjamin Roth <www.esales-media.de> 
26
+ * @package    legalAgeCheck 
27
+ * @license    GNU/LGPL 
28
+ * @filesource
29
+ */
30
+
31
+namespace legalAgeCheck;
32
+
33
+
34
+/**
35
+ * Class LegalAgeCheck 
36
+ *
37
+ * @copyright  eSales Media 2012 
38
+ * @author     Benjamin Roth <www.esales-media.de> 
39
+ * @package    Controller
40
+ */
41
+class LegalAgeCheck extends \Frontend
42
+{
43
+
44
+	/**
45
+	 * Template
46
+	 * @var string
47
+	 */
48
+	protected $strTemplate = 'agecheck_dialog';
49
+
50
+
51
+	/**
52
+	 * Perform age check
53
+	 */
54
+	public function performAgeCheck(\PageModel $objPage, \LayoutModel $objLayout, \PageRegular $objPageRegular)
55
+	{
56
+		// Skip age check if passed before
57
+		//if ($this->Session->get('legalAgeCheck_passed'))
58
+		if ($this->Input->cookie('legalAgeCheck_passed') || $objPage->es_ext_agecheck_ignorePage)
59
+		{
60
+			// $this->Session->set('legalAgeCheck_passed', null);
61
+			return;
62
+		}
63
+
64
+		// Get root page
65
+		$objRootPage = \PageModel::findByPk($objPage->rootId);
66
+		
67
+		// Cancel verification if age check is disabled					
68
+		if (is_null($objRootPage) || !$objRootPage->es_ext_agecheck || $objRootPage->es_ext_agecheck_exitPage == $objPage->id)
69
+			return;
70
+		
71
+		// Get exit page
72
+		$objExitPage = \PageModel::findWithDetails($objRootPage->es_ext_agecheck_exitPage);
73
+		if (is_null($objExitPage) || !$objExitPage->published)
74
+			return;
75
+		
76
+		// Load dialog template
77
+		$objTemplate = new \FrontendTemplate($this->strTemplate);
78
+		
79
+		// Load modal box template
80
+		//$objModalBoxTemplate = new FrontendTemplate('agecheck_dialog_template');
81
+		
82
+		// Set template vars
83
+		$objTemplate->title = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['title'];
84
+		$objTemplate->text = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['text'];
85
+		$objTemplate->btn_over_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['over_18'];
86
+		$objTemplate->btn_under_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['under_18'];
87
+		$objTemplate->exitPageURL = $this->generateFrontendUrl($objExitPage->row(), null, null, true);
88
+		$objTemplate->commitURL = 'system/modules/legalAgeCheck/ajax/Ajax.php?do=legalage_authentication&legalage_commit='.$this->createToken();
89
+		//$objTemplate->modalBoxTemplate = addcslashes($objModalBoxTemplate->parse(), "\\'\"&\n\r");
90
+		
91
+		// Add dialog code to page
92
+		$GLOBALS['TL_JQUERY'][] = $objTemplate->parse();
93
+
94
+
95
+	}
96
+
97
+	public static function ajaxUnlockPage()
98
+	{
99
+		if (!\Input::get('legalage_commit'))
100
+			return false;
101
+			
102
+		/*if ($this->Input->get('legalage_commit') == $this->Session->get('legalAgeCheck_token'))
103
+		{
104
+			$this->Session->set('legalAgeCheck_passed', true);
105
+			$this->Session->set('legalAgeCheck_token', null);
106
+		}*/
107
+
108
+		if (\Input::get('legalage_commit') == $_SESSION['legalAgeCheck_token'])
109
+		{
110
+			//preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', \Environment::get('host'), $regs);
111
+      preg_match('/(?P<subdomain>[a-z0-9][a-z0-9\-]{1,63}|)\.(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', \Environment::get('host'), $regs);
112
+			
113
+			setcookie('legalAgeCheck_passed', true, 0, '/',$regs['subdomain'].'.'.$regs['domain']);
114
+			unset($_SESSION['legalAgeCheck_token']);
115
+
116
+			return true;
117
+		}
118
+
119
+		return false;
120
+	}
121
+	
122
+	protected function createToken()
123
+	{
124
+		if (!$_SESSION['legalAgeCheck_token'])
125
+		{
126
+			$length = 32;
127
+			$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
128
+			$random_string = "";
129
+			for ($p = 0; $p < $length; $p++) {
130
+				$random_string .= $characters[mt_rand(0, strlen($characters))];
131
+			}
132
+			$_SESSION['legalAgeCheck_token'] = $random_string;
133
+		}
134
+		return $_SESSION['legalAgeCheck_token'];
135
+	}
136
+}
0 137
\ No newline at end of file