This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Alpha\Controller; |
||
4 | |||
5 | use Alpha\Util\Logging\Logger; |
||
6 | use Alpha\Util\File\FileUtils; |
||
7 | use Alpha\Util\Http\Response; |
||
8 | use Alpha\Util\Helper\Validator; |
||
9 | use Alpha\Util\Config\ConfigProvider; |
||
10 | use Alpha\Exception\ResourceNotFoundException; |
||
11 | use Alpha\Exception\IllegalArguementException; |
||
12 | use Alpha\Model\Article; |
||
13 | use Alpha\View\View; |
||
14 | |||
15 | /** |
||
16 | * Controller used to view (download) an attachment file on an Article. |
||
17 | * |
||
18 | * @since 1.0 |
||
19 | * |
||
20 | * @author John Collins <[email protected]> |
||
21 | * @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
||
22 | * @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework). |
||
23 | * All rights reserved. |
||
24 | * |
||
25 | * <pre> |
||
26 | * Redistribution and use in source and binary forms, with or |
||
27 | * without modification, are permitted provided that the |
||
28 | * following conditions are met: |
||
29 | * |
||
30 | * * Redistributions of source code must retain the above |
||
31 | * copyright notice, this list of conditions and the |
||
32 | * following disclaimer. |
||
33 | * * Redistributions in binary form must reproduce the above |
||
34 | * copyright notice, this list of conditions and the |
||
35 | * following disclaimer in the documentation and/or other |
||
36 | * materials provided with the distribution. |
||
37 | * * Neither the name of the Alpha Framework nor the names |
||
38 | * of its contributors may be used to endorse or promote |
||
39 | * products derived from this software without specific |
||
40 | * prior written permission. |
||
41 | * |
||
42 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||
43 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||
44 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
||
45 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
46 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
||
47 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
48 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
49 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
50 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
52 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
||
53 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
54 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
55 | * </pre> |
||
56 | */ |
||
57 | class AttachmentController extends Controller implements ControllerInterface |
||
58 | { |
||
59 | /** |
||
60 | * Trace logger. |
||
61 | * |
||
62 | * @var \Alpha\Util\Logging\Logger |
||
63 | * |
||
64 | * @since 1.0 |
||
65 | */ |
||
66 | private static $logger = null; |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
![]() |
|||
67 | |||
68 | /** |
||
69 | * The constructor. |
||
70 | * |
||
71 | * @since 1.0 |
||
72 | */ |
||
73 | public function __construct() |
||
74 | { |
||
75 | self::$logger = new Logger('AttachmentController'); |
||
76 | self::$logger->debug('>>__construct()'); |
||
77 | |||
78 | // ensure that the super class constructor is called, indicating the rights group |
||
79 | parent::__construct('Public'); |
||
80 | |||
81 | self::$logger->debug('<<__construct'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Handle GET requests. |
||
86 | * |
||
87 | * @param \Alpha\Util\Http\Request $request |
||
88 | * |
||
89 | * @since 1.0 |
||
90 | * |
||
91 | * @throws \Alpha\Exception\ResourceNotFoundException |
||
92 | */ |
||
93 | public function doGET($request) |
||
94 | { |
||
95 | self::$logger->debug('>>doGET($request=['.var_export($request, true).'])'); |
||
96 | |||
97 | $params = $request->getParams(); |
||
98 | |||
99 | try { |
||
100 | if (isset($params['articleID']) && isset($params['filename'])) { |
||
101 | if (!Validator::isInteger($params['articleID'])) { |
||
102 | throw new IllegalArguementException('The articleID ['.$params['articleID'].'] provided is invalid'); |
||
103 | } |
||
104 | |||
105 | $article = new Article(); |
||
106 | $article->setID($params['articleID']); |
||
107 | $filePath = $article->getAttachmentsLocation().'/'.$params['filename']; |
||
108 | |||
109 | if (file_exists($filePath)) { |
||
110 | self::$logger->info('Downloading the file ['.$params['filename'].'] from the folder ['.$article->getAttachmentsLocation().']'); |
||
111 | |||
112 | $pathParts = pathinfo($filePath); |
||
113 | $mimeType = FileUtils::getMIMETypeByExtension($pathParts['extension']); |
||
114 | |||
115 | $response = new Response(200, file_get_contents($filePath)); |
||
116 | $response->setHeader('Content-Type', $mimeType); |
||
117 | $response->setHeader('Content-Disposition', 'attachment; filename="'.$pathParts['basename'].'"'); |
||
118 | $response->setHeader('Content-Length', filesize($filePath)); |
||
119 | |||
120 | self::$logger->debug('<<doGET'); |
||
121 | |||
122 | return $response; |
||
123 | } else { |
||
124 | self::$logger->error('Could not access article attachment file ['.$filePath.'] as it does not exist!'); |
||
125 | throw new IllegalArguementException('File not found'); |
||
126 | } |
||
127 | } else { |
||
128 | self::$logger->error('Could not access article attachment as articleID and/or filename were not provided!'); |
||
129 | throw new IllegalArguementException('File not found'); |
||
130 | } |
||
131 | } catch (IllegalArguementException $e) { |
||
132 | self::$logger->error($e->getMessage()); |
||
133 | throw new ResourceNotFoundException($e->getMessage()); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Handle PUT requests. |
||
139 | * |
||
140 | * @param \Alpha\Util\Http\Request $request |
||
141 | * |
||
142 | * @since 3.0 |
||
143 | * |
||
144 | * @throws \Alpha\Exception\ResourceNotFoundException |
||
145 | */ |
||
146 | public function doPUT($request) |
||
147 | { |
||
148 | self::$logger->debug('>>doPUT($request=['.var_export($request, true).'])'); |
||
149 | |||
150 | $params = $request->getParams(); |
||
151 | |||
152 | $config = ConfigProvider::getInstance(); |
||
153 | |||
154 | try { |
||
155 | if (isset($params['articleID'])) { |
||
156 | $article = new Article(); |
||
157 | $article->load($params['articleID']); |
||
158 | |||
159 | if (isset($params['uploadBut'])) { |
||
160 | $source = $request->getFile('userfile')['tmp_name']; |
||
161 | $dest = $article->getAttachmentsLocation().'/'.$request->getFile('userfile')['name']; |
||
162 | |||
163 | // upload the file to the attachments directory |
||
164 | FileUtils::copy($source, $dest); |
||
165 | |||
166 | if (!file_exists($dest)) { |
||
167 | throw new AlphaException('Could not move the uploaded file ['.$request->getFile('userfile')['name'].']'); |
||
168 | } |
||
169 | |||
170 | // set read/write permissions on the file |
||
171 | $success = chmod($dest, 0666); |
||
172 | |||
173 | if (!$success) { |
||
174 | throw new AlphaException('Unable to set read/write permissions on the uploaded file ['.$dest.'].'); |
||
175 | } |
||
176 | |||
177 | if ($success) { |
||
178 | self::$logger->action('File '.$source.' uploaded to '.$dest); |
||
179 | $this->setStatusMessage(View::displayUpdateMessage('File '.$source.' uploaded to '.$dest)); |
||
180 | } |
||
181 | } elseif (isset($params['deletefile']) && $params['deletefile'] != '') { |
||
182 | $success = unlink($article->getAttachmentsLocation().'/'.$params['deletefile']); |
||
183 | |||
184 | if (!$success) { |
||
185 | throw new AlphaException('Could not delete the file ['.$params['deletefile'].']'); |
||
186 | } |
||
187 | |||
188 | if ($success) { |
||
189 | self::$logger->action('File '.$article->getAttachmentsLocation().'/'.$params['deletefile'].' deleted'); |
||
190 | $this->setStatusMessage(View::displayUpdateMessage('File '.$article->getAttachmentsLocation().'/'.$params['deletefile'].' deleted')); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | $response = new Response(301); |
||
195 | |||
196 | if ($this->getNextJob() != '') { |
||
197 | $response->redirect($this->getNextJob()); |
||
198 | } else { |
||
199 | if ($this->request->isSecureURI()) { |
||
200 | $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType=Alpha\Model\Article&ActiveRecordID='.$article->getID().'&view=edit')); |
||
201 | } else { |
||
202 | $title = str_replace(' ', $config->get('cms.url.title.separator'), $article->get('title')); |
||
203 | $response->redirect($config->get('app.url').'/a/'.$title.'/edit'); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | self::$logger->debug('<<doPUT'); |
||
208 | |||
209 | return $response; |
||
210 | } else { |
||
211 | self::$logger->error('Could not process article attachment as articleID was not provided!'); |
||
212 | throw new IllegalArguementException('File not found'); |
||
213 | } |
||
214 | } catch (IllegalArguementException $e) { |
||
215 | self::$logger->error($e->getMessage()); |
||
216 | throw new ResourceNotFoundException($e->getMessage()); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 |