]> Raphaël G. Git Repositories - blogbundle/blob - Controller/DefaultController.php
Fix default bind ciphersuites and options
[blogbundle] / Controller / DefaultController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\BlogBundle\Controller;
13
14 use Symfony\Bridge\Twig\Mime\TemplatedEmail;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
18 use Symfony\Component\Mime\Address;
19
20 use Rapsys\BlogBundle\Entity\Article;
21
22 /**
23 * {@inheritdoc}
24 */
25 class DefaultController extends AbstractController {
26 /**
27 * The about page
28 *
29 * Display the about informations
30 *
31 * @param Request $request The request instance
32 * @return Response The rendered view or redirection
33 */
34 public function about(Request $request): Response {
35 //Set page
36 $this->context['title'] = $this->translator->trans('About');
37
38 //Set description
39 $this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary about page');
40
41 //Set keywords
42 $this->context['keywords'] = $this->translator->trans('about');
43
44 //Create response
45 $response = new Response();
46
47 //Set etag
48 //XXX: only for public to force revalidation by last modified
49 $response->setEtag(md5(serialize($this->context)));
50
51 //Set last modified
52 $response->setLastModified((new \DateTime)->setTimestamp(getlastmod()));
53
54 //Set as public
55 $response->setPublic();
56
57 //Without role and modification
58 if ($response->isNotModified($request)) {
59 //Return 304 response
60 return $response;
61 }
62
63 //Render template
64 return $this->render('@RapsysBlog/about.html.twig', $this->context, $response);
65 }
66
67 /**
68 * The contact page
69 *
70 * Send a contact mail to configured contact
71 *
72 * @param Request $request The request instance
73 * @return Response The rendered view or redirection
74 */
75 public function contact(Request $request): Response {
76 //Set title
77 $this->context['title'] = $this->translator->trans('Contact');
78
79 //Set description
80 $this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary contact page');
81
82 //Set keywords
83 $this->context['keywords'] = $this->translator->trans('contact');
84
85 //Set data
86 $data = [];
87
88 //With user
89 if ($user = $this->security->getUser()) {
90 //Set data
91 $data = [
92 'name' => $user->getRecipientName(),
93 'mail' => $user->getMail()
94 ];
95 }
96
97 //Create response
98 $response = new Response();
99
100 //Create the form according to the FormType created previously.
101 //And give the proper parameters
102 $form = $this->createForm('Rapsys\BlogBundle\Form\ContactType', $data, [
103 'action' => $this->generateUrl('rapsys_blog_contact'),
104 'method' => 'POST'
105 ]);
106
107 if ($request->isMethod('POST')) {
108 // Refill the fields in case the form is not valid.
109 $form->handleRequest($request);
110
111 if ($form->isSubmitted() && $form->isValid()) {
112 //Get data
113 $data = $form->getData();
114
115 //Set context
116 $context = [
117 'subject' => $data['subject'],
118 'message' => strip_tags($data['message']),
119 ]+$this->context;
120
121 //Create message
122 $message = (new TemplatedEmail())
123 //Set sender
124 ->from(new Address($data['mail'], $data['name']))
125 //Set recipient
126 ->to(new Address($this->config['contact']['address'], $this->config['contact']['name']))
127 //Set subject
128 ->subject($data['subject'])
129
130 //Set path to twig templates
131 ->htmlTemplate('@RapsysBlog/mail/contact.html.twig')
132 ->textTemplate('@RapsysBlog/mail/contact.text.twig')
133
134 //Set context
135 ->context($context);
136
137 //Try sending message
138 //XXX: mail delivery may silently fail
139 try {
140 //Send message
141 $this->mailer->send($message);
142
143 //Redirect on the same route with sent=1 to cleanup form
144 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params'));
145 //Catch obvious transport exception
146 } catch(TransportExceptionInterface $e) {
147 //Add error message mail unreachable
148 $form->get('mail')->addError(new FormError($this->translator->trans('Unable to reach account')));
149 }
150 }
151 //With logged user
152 } elseif ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
153 //Set last modified
154 $response->setLastModified(new \DateTime('-1 year'));
155
156 //Set as private
157 $response->setPrivate();
158 //Without logged user
159 } else {
160 //Set etag
161 //XXX: only for public to force revalidation by last modified
162 $response->setEtag(md5(serialize($this->context)));
163
164 //Set last modified
165 $response->setLastModified((new \DateTime)->setTimestamp(getlastmod()));
166
167 //Set as public
168 $response->setPublic();
169
170 //Without role and modification
171 if ($response->isNotModified($request)) {
172 //Return 304 response
173 return $response;
174 }
175 }
176
177 //Render template
178 return $this->render('@RapsysBlog/form/contact.html.twig', ['contact' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context, $response);
179 }
180
181 /**
182 * The index page
183 *
184 * Display articles
185 *
186 * @param Request $request The request instance
187 * @return Response The rendered view
188 */
189 public function index(Request $request): Response {
190 //With not enough articles
191 if (($this->count = $this->doctrine->getRepository(Article::class)->findCountAsInt()) < $this->page * $this->limit) {
192 //Throw 404
193 throw $this->createNotFoundException($this->translator->trans('Unable to find articles'));
194 }
195
196 //Get articles
197 if ($this->context['articles'] = $this->doctrine->getRepository(Article::class)->findAllAsArray($this->page, $this->limit)) {
198 //Set modified
199 $this->modified = max(array_map(function ($v) { return $v['modified']; }, $this->context['articles']));
200 //Without keywords
201 } else {
202 //Set empty modified
203 $this->modified = new \DateTime('-1 year');
204 }
205
206 //Create response
207 $response = new Response();
208
209 //With logged user
210 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
211 //Set last modified
212 $response->setLastModified(new \DateTime('-1 year'));
213
214 //Set as private
215 $response->setPrivate();
216 //Without logged user
217 } else {
218 //Set etag
219 //XXX: only for public to force revalidation by last modified
220 $response->setEtag(md5(serialize($this->context['articles'])));
221
222 //Set last modified
223 $response->setLastModified($this->modified);
224
225 //Set as public
226 $response->setPublic();
227
228 //Without role and modification
229 if ($response->isNotModified($request)) {
230 //Return 304 response
231 return $response;
232 }
233 }
234
235 //Set keywords
236 $this->context['head']['keywords'] = implode(
237 ', ',
238 //Use closure to extract each unique article keywords sorted
239 (function ($t) {
240 //Return array
241 $r = [];
242
243 //Iterate on articles
244 foreach($t as $a) {
245 //Non empty keywords
246 if (!empty($a['keywords'])) {
247 //Iterate on keywords
248 foreach($a['keywords'] as $k) {
249 //Set keyword
250 $r[$k['title']] = $k['title'];
251 }
252 }
253 }
254
255 //Sort array
256 sort($r);
257
258 //Return array
259 return $r;
260 })($this->context['articles'])
261 );
262
263 //Set title
264 $this->context['title'] = $this->translator->trans('Home');
265
266 //Set description
267 $this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary');
268
269 //Render the view
270 return $this->render('@RapsysBlog/index.html.twig', $this->context, $response);
271 }
272 }