]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/ImagesController.php
eb6286eb13a6e78f90f6e029b7e42b7bf9083f6a
[galette.git] / galette / lib / Galette / Controllers / ImagesController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette images controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-02
36 */
37
38 namespace Galette\Controllers;
39
40 use Slim\Http\Request;
41 use Slim\Http\Response;
42 use Galette\Core\Picture;
43 use Galette\Entity\Adherent;
44 use Analog\Analog;
45
46 /**
47 * Galette images controller
48 *
49 * @category Controllers
50 * @name ImageController
51 * @package Galette
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2020 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.9.4dev - 2020-05-02
57 */
58
59 class ImagesController extends AbstractController
60 {
61
62 /**
63 * Send response
64 *
65 * @param Response $response PSR Response
66 * @param Picture $picture Picture to output
67 *
68 * @return Response
69 */
70 protected function sendResponse(Response $response, Picture $picture) :Response
71 {
72 $response = $response->withHeader('Content-Type', $picture->getMime())
73 ->withHeader('Content-Transfer-Encoding', 'binary')
74 ->withHeader('Expires', '0')
75 ->withHeader('Cache-Control', 'must-revalidate')
76 ->withHeader('Pragma', 'public');
77
78 $stream = fopen('php://memory', 'r+');
79 fwrite($stream, file_get_contents($picture->getPath()));
80 rewind($stream);
81
82 return $response->withBody(new \Slim\Http\Stream($stream));
83 }
84
85 /**
86 * Logo route
87 *
88 * @param Request $request PSR Request
89 * @param Response $response PSR Response
90 *
91 * @return Response
92 */
93 public function logo(Request $request, Response $response) :Response
94 {
95 return $this->sendResponse($response, $this->logo);
96 }
97
98 /**
99 * Print logo route
100 *
101 * @param Request $request PSR Request
102 * @param Response $response PSR Response
103 *
104 * @return Response
105 */
106 public function printLogo(Request $request, Response $response) :Response
107 {
108 return $this->sendResponse($response, $this->print_logo);
109 }
110
111 /**
112 * Photos
113 *
114 * @param Request $request PSR Request
115 * @param Response $response PSR Response
116 * @param array $args Request arguments ['id']
117 *
118 * @return Response
119 */
120 public function photo(Request $request, Response $response, array $args) :Response
121 {
122 $id = (int)$args['id'];
123
124 $deps = array(
125 'groups' => false,
126 'dues' => false
127 );
128
129 //if loggedin user is a group manager, we have to check
130 //he manages a group requested member belongs to.
131 if ($this->login->isGroupManager()) {
132 $deps['groups'] = true;
133 }
134
135 $adh = new Adherent($this->zdb, (int)$id, $deps);
136
137 $picture = null;
138 if ($adh->canEdit($this->login)
139 || $this->preferences->showPublicPages($this->login)
140 && $adh->appearsInMembersList()
141 ) {
142 $picture = $adh->picture;
143 } else {
144 $picture = new Picture();
145 }
146
147 return $this->sendResponse($response, $picture);
148 }
149 }