|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\PostModel; |
|
6
|
|
|
use App\Models\UserModel; |
|
7
|
|
|
use Core\Config; |
|
8
|
|
|
use Core\Container; |
|
9
|
|
|
use Core\Traits\StringFunctions; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Home |
|
13
|
|
|
* |
|
14
|
|
|
* The home page |
|
15
|
|
|
* |
|
16
|
|
|
* @package App\Controllers |
|
17
|
|
|
*/ |
|
18
|
|
|
class Home extends \Core\Controller |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
use StringFunctions; |
|
22
|
|
|
|
|
23
|
|
|
protected $siteConfig; |
|
24
|
|
|
protected $sendMail; |
|
25
|
|
|
|
|
26
|
|
|
private $config; |
|
27
|
|
|
private $userModel; |
|
28
|
|
|
private $postModel; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Container $container) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->loadModules[] = 'SiteConfig'; |
|
33
|
|
|
$this->loadModules[] = 'SendMail'; |
|
34
|
|
|
parent::__construct($container); |
|
35
|
|
|
|
|
36
|
|
|
$this->config = $this->siteConfig->getSiteConfig(); |
|
37
|
|
|
$this->userModel = new UserModel($this->container); |
|
38
|
|
|
$this->postModel = new PostModel($this->container); |
|
39
|
|
|
if($this->auth->isuser()) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->data["user"] = $this->userModel->getUserDetailsById($this->session->get("userId")); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* test the capcha |
|
47
|
|
|
* @param string $gCapchaResponse |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
private function testCapcha(string $gCapchaResponse):bool |
|
51
|
|
|
{ |
|
52
|
|
|
$error = false; |
|
53
|
|
|
if(Config::GOOGLE_RECAPCHA_PUBLIC_KEY !== "" && Config::GOOGLE_RECAPCHA_SECRET_KEY !== "") |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
if(empty($gCapchaResponse)) |
|
56
|
|
|
{ |
|
57
|
|
|
$error = true; |
|
58
|
|
|
$this->alertBox->setAlert('Capcha not set', 'error'); |
|
59
|
|
|
} |
|
60
|
|
|
//check the capcha |
|
61
|
|
|
$grequest = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.Config::GOOGLE_RECAPCHA_SECRET_KEY.'&response='.$gCapchaResponse); |
|
62
|
|
|
// The result is in a JSON format. Decoding.. |
|
63
|
|
|
$gresponse = json_decode($grequest); |
|
64
|
|
|
if(!$gresponse->success) |
|
65
|
|
|
{ |
|
66
|
|
|
$error = true; |
|
67
|
|
|
$this->alertBox->setAlert('Capcha Error', 'error'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
return $error; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Show the front page |
|
75
|
|
|
* @throws \ErrorException |
|
76
|
|
|
* @throws \ReflectionException |
|
77
|
|
|
* @throws \Twig_Error_Loader |
|
78
|
|
|
* @throws \Twig_Error_Runtime |
|
79
|
|
|
* @throws \Twig_Error_Syntax |
|
80
|
|
|
*/ |
|
81
|
|
|
public function index() |
|
82
|
|
|
{ |
|
83
|
|
|
$frontPosts = $this->postModel->getFrontPosts(); |
|
84
|
|
|
|
|
85
|
|
|
$this->data['configs'] = $this->config; |
|
86
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
|
87
|
|
|
$this->data['jumbotron'] = true; |
|
88
|
|
|
$this->data['front_posts'] = $frontPosts; |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
//check if have prefilled form data and error messages |
|
92
|
|
|
$this->data["contactInfo"] = $this->session->get("contactInfo"); |
|
93
|
|
|
$this->data["contactErrors"] = $this->session->get("contactErrors"); |
|
94
|
|
|
|
|
95
|
|
|
//remove the set data as it is now sent to the template |
|
96
|
|
|
$this->session->remove("contactInfo"); |
|
97
|
|
|
$this->session->remove("contactErrors"); |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$this->renderView('Home'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function contact() |
|
104
|
|
|
{ |
|
105
|
|
|
|
|
106
|
|
|
$this->data['configs'] = $this->config; |
|
107
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
|
108
|
|
|
|
|
109
|
|
|
//check if have prefilled form data and error messages |
|
110
|
|
|
$this->data["contactInfo"] = $this->session->get("contactInfo"); |
|
111
|
|
|
$this->data["contactErrors"] = $this->session->get("contactErrors"); |
|
112
|
|
|
|
|
113
|
|
|
//remove the set data as it is now sent to the template |
|
114
|
|
|
$this->session->remove("contactInfo"); |
|
115
|
|
|
$this->session->remove("contactErrors"); |
|
116
|
|
|
|
|
117
|
|
|
$this->renderView('Contact'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Send the contact form with error checking |
|
123
|
|
|
* @throws \Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
public function sendContactForm() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->onlyPost(); |
|
128
|
|
|
|
|
129
|
|
|
//verify input values (html special chars ?) |
|
130
|
|
|
$to = $this->config["admin_email_address"]; |
|
131
|
|
|
$message = $this->request->getDataFull(); |
|
132
|
|
|
|
|
133
|
|
|
//Error checking |
|
134
|
|
|
|
|
135
|
|
|
//check all the fields |
|
136
|
|
|
$error = false; |
|
137
|
|
|
$contactErrors = new \stdClass(); |
|
138
|
|
|
|
|
139
|
|
|
if ($message["contactName"] == "") { |
|
140
|
|
|
$error = true; |
|
141
|
|
|
$contactErrors->contactName = "Name must not be empty"; |
|
142
|
|
|
} |
|
143
|
|
|
if ($message["contactEmail"] == "") { |
|
144
|
|
|
$error = true; |
|
145
|
|
|
$contactErrors->contactEmail = "Email must not be empty"; |
|
146
|
|
|
} |
|
147
|
|
|
if ($message["contactSubject"] == "") { |
|
148
|
|
|
$error = true; |
|
149
|
|
|
$contactErrors->contactSubject = "Subject must not be empty"; |
|
150
|
|
|
} |
|
151
|
|
|
if ($message["contactMessage"] == "") { |
|
152
|
|
|
$error = true; |
|
153
|
|
|
$contactErrors->contactMessage = "Message must not be empty"; |
|
154
|
|
|
} |
|
155
|
|
|
if (!$this->isEmail($message["contactEmail"])) { |
|
156
|
|
|
$error = true; |
|
157
|
|
|
$contactErrors->contactEmail = "email is not valid"; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$capchaError = $this->testCapcha($message["g-recaptcha-response"]); |
|
161
|
|
|
|
|
162
|
|
|
if($capchaError === true) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
$error = true; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
//If we found an error, return data to the register form and no create |
|
168
|
|
|
if ($error) { |
|
169
|
|
|
$this->session->set("contactInfo", $message); |
|
170
|
|
|
$this->session->set("contactErrors", $contactErrors); |
|
171
|
|
|
$this->response->redirect("/home/contact"); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$config = $this->siteConfig->getSiteConfig(); |
|
175
|
|
|
|
|
176
|
|
|
//from here all is good, send mail |
|
177
|
|
|
$userName = htmlspecialchars($message["contactName"]); |
|
178
|
|
|
$subject = "Contact from ".$config["site_name"]." : "; |
|
179
|
|
|
$subject .= htmlspecialchars($message["contactSubject"]); |
|
180
|
|
|
$textMessage = "<h1>message sent by ".$userName."</h1>"; |
|
181
|
|
|
$textMessage .= "<p>from : <a href='mailto:".$message["contactEmail"]."'>".$message["contactEmail"]."</a></p>"; |
|
182
|
|
|
$textMessage .= htmlspecialchars($message["contactMessage"]); |
|
183
|
|
|
$from = $config["SMTP_from"]; |
|
184
|
|
|
|
|
185
|
|
|
$this->sendMail->send($to, $subject, $textMessage, $from); |
|
186
|
|
|
|
|
187
|
|
|
$this->alertBox->setAlert('Email sent'); |
|
188
|
|
|
$this->response->redirect(); |
|
189
|
|
|
} |
|
190
|
|
|
} |