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
|
|
|
* Show the front page |
47
|
|
|
* @throws \ErrorException |
48
|
|
|
* @throws \ReflectionException |
49
|
|
|
* @throws \Twig_Error_Loader |
50
|
|
|
* @throws \Twig_Error_Runtime |
51
|
|
|
* @throws \Twig_Error_Syntax |
52
|
|
|
*/ |
53
|
|
|
public function index() |
54
|
|
|
{ |
55
|
|
|
$frontPosts = $this->postModel->getFrontPosts(); |
56
|
|
|
|
57
|
|
|
$this->data['configs'] = $this->config; |
58
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
59
|
|
|
$this->data['jumbotron'] = true; |
60
|
|
|
$this->data['front_posts'] = $frontPosts; |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
//check if have prefilled form data and error messages |
64
|
|
|
$this->data["contactInfo"] = $this->session->get("contactInfo"); |
65
|
|
|
$this->data["contactErrors"] = $this->session->get("contactErrors"); |
66
|
|
|
|
67
|
|
|
//remove the set data as it is now sent to the template |
68
|
|
|
$this->session->remove("contactInfo"); |
69
|
|
|
$this->session->remove("contactErrors"); |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->renderView('Home'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function contact() |
76
|
|
|
{ |
77
|
|
|
|
78
|
|
|
$this->data['configs'] = $this->config; |
79
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
80
|
|
|
|
81
|
|
|
//check if have prefilled form data and error messages |
82
|
|
|
$this->data["contactInfo"] = $this->session->get("contactInfo"); |
83
|
|
|
$this->data["contactErrors"] = $this->session->get("contactErrors"); |
84
|
|
|
|
85
|
|
|
//remove the set data as it is now sent to the template |
86
|
|
|
$this->session->remove("contactInfo"); |
87
|
|
|
$this->session->remove("contactErrors"); |
88
|
|
|
|
89
|
|
|
$this->renderView('Contact'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Send the contact form with error checking |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
public function sendContactForm() |
98
|
|
|
{ |
99
|
|
|
$this->onlyPost(); |
100
|
|
|
|
101
|
|
|
//verify input values (html special chars ?) |
102
|
|
|
$to = $this->config["admin_email_address"]; |
103
|
|
|
$message = $this->request->getDataFull(); |
104
|
|
|
|
105
|
|
|
//Error checking |
106
|
|
|
|
107
|
|
|
//check all the fields |
108
|
|
|
$error = false; |
109
|
|
|
$contactErrors = new \stdClass(); |
110
|
|
|
|
111
|
|
|
if ($message["contactName"] == "") { |
112
|
|
|
$error = true; |
113
|
|
|
$contactErrors->contactName = "Name must not be empty"; |
114
|
|
|
} |
115
|
|
|
if ($message["contactEmail"] == "") { |
116
|
|
|
$error = true; |
117
|
|
|
$contactErrors->contactEmail = "Email must not be empty"; |
118
|
|
|
} |
119
|
|
|
if ($message["contactSubject"] == "") { |
120
|
|
|
$error = true; |
121
|
|
|
$contactErrors->contactSubject = "Subject must not be empty"; |
122
|
|
|
} |
123
|
|
|
if ($message["contactMessage"] == "") { |
124
|
|
|
$error = true; |
125
|
|
|
$contactErrors->contactMessage = "Message must not be empty"; |
126
|
|
|
} |
127
|
|
|
if (!$this->isEmail($message["contactEmail"])) { |
128
|
|
|
$error = true; |
129
|
|
|
$contactErrors->contactEmail = "email is not valid"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
if(Config::GOOGLE_RECAPCHA_PUBLIC_KEY !== "" && Config::GOOGLE_RECAPCHA_SECRET_KEY !== "") |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
if(empty($message["g-recaptcha-response"])) |
136
|
|
|
{ |
137
|
|
|
$error = true; |
138
|
|
|
$this->alertBox->setAlert('Capcha not set', 'error'); |
139
|
|
|
} |
140
|
|
|
//check the capcha |
141
|
|
|
$grequest = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.Config::GOOGLE_RECAPCHA_SECRET_KEY.'&response='.$message["g-recaptcha-response"]); |
142
|
|
|
// The result is in a JSON format. Decoding.. |
143
|
|
|
$gresponse = json_decode($grequest); |
144
|
|
|
if(!$gresponse->success) |
145
|
|
|
{ |
146
|
|
|
$error = true; |
147
|
|
|
$this->alertBox->setAlert('Capcha Error', 'error'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
//If we found an error, return data to the register form and no create |
152
|
|
|
if ($error) { |
153
|
|
|
$this->session->set("contactInfo", $message); |
154
|
|
|
$this->session->set("contactErrors", $contactErrors); |
155
|
|
|
$this->response->redirect("/home/contact"); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$config = $this->siteConfig->getSiteConfig(); |
159
|
|
|
|
160
|
|
|
//from here all is good, send mail |
161
|
|
|
$userName = htmlspecialchars($message["contactName"]); |
162
|
|
|
$subject = "Contact from ".$config["site_name"]." : "; |
163
|
|
|
$subject .= htmlspecialchars($message["contactSubject"]); |
164
|
|
|
$textMessage = "<h1>message sent by ".$userName."</h1>"; |
165
|
|
|
$textMessage .= "<p>from : <a href='mailto:".$message["contactEmail"]."'>".$message["contactEmail"]."</a></p>"; |
166
|
|
|
$textMessage .= htmlspecialchars($message["contactMessage"]); |
167
|
|
|
$from = $config["SMTP_from"]; |
168
|
|
|
|
169
|
|
|
$this->sendMail->send($to, $subject, $textMessage, $from); |
170
|
|
|
|
171
|
|
|
$this->alertBox->setAlert('Email sent'); |
172
|
|
|
$this->response->redirect(); |
173
|
|
|
} |
174
|
|
|
} |