These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @copyright 2017, Roeland Jago Douma <[email protected]> |
||
4 | * |
||
5 | * @author Roeland Jago Douma <[email protected]> |
||
6 | * |
||
7 | * @license GNU AGPL version 3 or any later version |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU Affero General Public License as |
||
11 | * published by the Free Software Foundation, either version 3 of the |
||
12 | * License, or (at your option) any later version. |
||
13 | * |
||
14 | * This program is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU Affero General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Affero General Public License |
||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
21 | * |
||
22 | */ |
||
23 | namespace OCA\FilesTextEditor\Controller; |
||
24 | |||
25 | use OCP\AppFramework\Controller; |
||
26 | use OCP\AppFramework\Http; |
||
27 | use OCP\AppFramework\Http\DataResponse; |
||
28 | use OCP\Files\Folder; |
||
29 | use OCP\Files\NotFoundException; |
||
30 | use OCP\IL10N; |
||
31 | use OCP\IRequest; |
||
32 | use OCP\ISession; |
||
33 | use OCP\Share\Exceptions\ShareNotFound; |
||
34 | use OCP\Share\IManager as ShareManager; |
||
35 | |||
36 | class PublicFileHandlingController extends Controller{ |
||
37 | |||
38 | /** @var IL10N */ |
||
39 | private $l; |
||
40 | |||
41 | /** @var ShareManager */ |
||
42 | private $shareManager; |
||
43 | |||
44 | /** @var ISession */ |
||
45 | private $session; |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | * @param string $AppName |
||
50 | * @param IRequest $request |
||
51 | * @param IL10N $l10n |
||
52 | * @param ShareManager $shareManager |
||
53 | * @param ISession $session |
||
54 | */ |
||
55 | public function __construct($AppName, |
||
56 | IRequest $request, |
||
57 | IL10N $l10n, |
||
58 | ShareManager $shareManager, |
||
59 | ISession $session) { |
||
60 | parent::__construct($AppName, $request); |
||
61 | $this->l = $l10n; |
||
62 | $this->shareManager = $shareManager; |
||
63 | $this->session = $session; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * load text file |
||
68 | * |
||
69 | * @NoAdminRequired |
||
70 | * @PublicPage |
||
71 | * @NoCSRFRequired |
||
72 | * |
||
73 | * @param string $token |
||
74 | * @return DataResponse |
||
75 | */ |
||
76 | public function load($token) { |
||
77 | try { |
||
78 | $share = $this->shareManager->getShareByToken($token); |
||
79 | } catch (ShareNotFound $e) { |
||
80 | return new DataResponse(['message' => $this->l->t('Share not found'), Http::STATUS_NOT_FOUND]); |
||
81 | } |
||
82 | |||
83 | if ($share->getPassword() !== null && |
||
84 | (!$this->session->exists('public_link_authenticated') |
||
85 | || $this->session->get('public_link_authenticated') !== (string)$share->getId())) { |
||
86 | return new DataResponse(['message' => $this->l->t('You are not authorized to open this share'), Http::STATUS_BAD_REQUEST]); |
||
87 | } |
||
88 | |||
89 | try { |
||
90 | $node = $share->getNode(); |
||
91 | } catch (NotFoundException $e) { |
||
92 | return new DataResponse(['message' => $this->l->t('Share not found'), Http::STATUS_NOT_FOUND]); |
||
93 | } |
||
94 | |||
95 | View Code Duplication | if ($node instanceof Folder) { |
|
0 ignored issues
–
show
|
|||
96 | return new DataResponse(['message' => $this->l->t('You can not open a folder')], Http::STATUS_BAD_REQUEST); |
||
97 | } |
||
98 | |||
99 | $range = $this->request->getHeader('Range'); |
||
100 | if ($range !== '') { |
||
101 | $matches = []; |
||
102 | View Code Duplication | if (preg_match('/bytes=0-(\d+)$/', $range, $matches) === 0) { |
|
103 | return new DataResponse(['message' => $this->l->t('Invalid range request')], Http::STATUS_REQUEST_RANGE_NOT_SATISFIABLE); |
||
104 | } |
||
105 | $range = (int)$matches[1]; |
||
106 | } else { |
||
107 | $range = -1; |
||
108 | } |
||
109 | |||
110 | // default of 4MB |
||
111 | $maxSize = 4194304; |
||
112 | View Code Duplication | if ($node->getSize() > $maxSize) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
113 | return new DataResponse(['message' => $this->l->t('This file is too big to be opened. Please download the file instead.')], Http::STATUS_BAD_REQUEST); |
||
114 | } |
||
115 | |||
116 | $fileContents = $node->getContent(); |
||
117 | if ($fileContents !== false) { |
||
118 | $encoding = mb_detect_encoding($fileContents . 'a', 'UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true); |
||
119 | if ($encoding === '') { |
||
120 | // set default encoding if it couldn't be detected |
||
121 | $encoding = 'ISO-8859-15'; |
||
122 | } |
||
123 | $fileContents = iconv($encoding, 'UTF-8', $fileContents); |
||
124 | |||
125 | if ($range !== -1) { |
||
126 | $fileContents = mb_substr($fileContents, 0, $range); |
||
127 | } |
||
128 | |||
129 | return new Http\DataDisplayResponse( |
||
130 | $fileContents, |
||
131 | Http::STATUS_OK, |
||
132 | ['Content-Type' => 'text/plain; charset="utf-8"'] |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | return new DataResponse(['message' => $this->l->t('Cannot read the file.')], Http::STATUS_BAD_REQUEST); |
||
137 | } |
||
138 | } |
||
139 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.