]> git.mxchange.org Git - friendica.git/blob - src/Network/Factory/MimeType.php
Merge pull request #13831 from friendica/warning
[friendica.git] / src / Network / Factory / MimeType.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Network\Factory;
23
24 use Friendica\BaseFactory;
25 use Friendica\Network\Entity;
26
27 /**
28  * Implementation of the Content-Type header value from the MIME type RFC
29  *
30  * @see https://www.rfc-editor.org/rfc/rfc2045#section-5
31  */
32 class MimeType extends BaseFactory
33 {
34         public function createFromContentType(?string $contentType): Entity\MimeType
35         {
36                 if ($contentType) {
37                         $parameterStrings = explode(';', $contentType);
38                         $mimetype = array_shift($parameterStrings);
39
40                         $types = explode('/', $mimetype);
41                         if (count($types) >= 2) {
42                                 $filetype = strtolower($types[0]);
43                                 $subtype = strtolower($types[1]);
44                         } else {
45                                 $this->logger->notice('Unknown MimeType', ['type' => $contentType]);
46                         }
47
48                         $parameters = [];
49                         foreach ($parameterStrings as $parameterString) {
50                                 $parameterString = trim($parameterString);
51                                 $parameterParts = explode('=', $parameterString, 2);
52                                 if (count($parameterParts) < 2) {
53                                         continue;
54                                 }
55
56                                 $attribute = trim($parameterParts[0]);
57                                 $valueString = trim($parameterParts[1]);
58
59                                 if ($valueString[0] == '"' && $valueString[strlen($valueString) - 1] == '"') {
60                                         $valueString = substr(str_replace(['\\"', '\\\\', "\\\r"], ['"', '\\', "\r"], $valueString), 1, -1);
61                                 }
62
63                                 $value = preg_replace('#\s*\([^()]*?\)#', '', $valueString);
64
65                                 $parameters[$attribute] = $value;
66                         }
67                 }
68
69                 return new Entity\MimeType(
70                         $filetype ?? 'unkn',
71                         $subtype ?? 'unkn',
72                         $parameters ?? [],
73                 );
74         }
75 }