3 * @copyright Copyright (C) 2010-2024, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Network\Factory;
24 use Friendica\BaseFactory;
25 use Friendica\Network\Entity;
28 * Implementation of the Content-Type header value from the MIME type RFC
30 * @see https://www.rfc-editor.org/rfc/rfc2045#section-5
32 class MimeType extends BaseFactory
34 public function createFromContentType(?string $contentType): Entity\MimeType
37 $parameterStrings = explode(';', $contentType);
38 $mimetype = array_shift($parameterStrings);
40 $types = explode('/', $mimetype);
41 if (count($types) >= 2) {
42 $filetype = strtolower($types[0]);
43 $subtype = strtolower($types[1]);
45 $this->logger->notice('Unknown MimeType', ['type' => $contentType]);
49 foreach ($parameterStrings as $parameterString) {
50 $parameterString = trim($parameterString);
51 $parameterParts = explode('=', $parameterString, 2);
52 if (count($parameterParts) < 2) {
56 $attribute = trim($parameterParts[0]);
57 $valueString = trim($parameterParts[1]);
59 if ($valueString[0] == '"' && $valueString[strlen($valueString) - 1] == '"') {
60 $valueString = substr(str_replace(['\\"', '\\\\', "\\\r"], ['"', '\\', "\r"], $valueString), 1, -1);
63 $value = preg_replace('#\s*\([^()]*?\)#', '', $valueString);
65 $parameters[$attribute] = $value;
69 return new Entity\MimeType(