]> git.mxchange.org Git - friendica.git/blob - src/Network/Factory/MimeType.php
Merge pull request #13488 from MrPetovan/bug/frio-search-result-padding
[friendica.git] / src / Network / Factory / MimeType.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Core\System;
26 use Friendica\Network\Entity;
27
28 /**
29  * Implementation of the Content-Type header value from the MIME type RFC
30  *
31  * @see https://www.rfc-editor.org/rfc/rfc2045#section-5
32  */
33 class MimeType extends BaseFactory
34 {
35         public function createFromContentType(?string $contentType): Entity\MimeType
36         {
37                 if ($contentType) {
38                         $parameterStrings = explode(';', $contentType);
39                         $mimetype = array_shift($parameterStrings);
40
41                         $types = explode('/', $mimetype);
42                         if (count($types) >= 2) {
43                                 $filetype = strtolower($types[0]);
44                                 $subtype = strtolower($types[1]);
45                         } else {
46                                 $this->logger->notice('Unknown MimeType', ['type' => $contentType, 'callstack' => System::callstack(10)]);
47                         }
48
49                         $parameters = [];
50                         foreach ($parameterStrings as $parameterString) {
51                                 $parameterString = trim($parameterString);
52                                 $parameterParts = explode('=', $parameterString, 2);
53                                 if (count($parameterParts) < 2) {
54                                         continue;
55                                 }
56
57                                 $attribute = trim($parameterParts[0]);
58                                 $valueString = trim($parameterParts[1]);
59
60                                 if ($valueString[0] == '"' && $valueString[strlen($valueString) - 1] == '"') {
61                                         $valueString = substr(str_replace(['\\"', '\\\\', "\\\r"], ['"', '\\', "\r"], $valueString), 1, -1);
62                                 }
63
64                                 $value = preg_replace('#\s*\([^()]*?\)#', '', $valueString);
65
66                                 $parameters[$attribute] = $value;
67                         }
68                 }
69
70                 return new Entity\MimeType(
71                         $filetype ?? 'unkn',
72                         $subtype ?? 'unkn',
73                         $parameters ?? [],
74                 );
75         }
76 }