3 * @copyright Copyright (C) 2010-2023, 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\Core\System;
26 use Friendica\Network\Entity;
29 * Implementation of the Content-Type header value from the MIME type RFC
31 * @see https://www.rfc-editor.org/rfc/rfc2045#section-5
33 class MimeType extends BaseFactory
35 public function createFromContentType(?string $contentType): Entity\MimeType
38 $parameterStrings = explode(';', $contentType);
39 $mimetype = array_shift($parameterStrings);
41 $types = explode('/', $mimetype);
42 if (count($types) >= 2) {
43 $filetype = strtolower($types[0]);
44 $subtype = strtolower($types[1]);
46 $this->logger->notice('Unknown MimeType', ['type' => $contentType, 'callstack' => System::callstack(10)]);
50 foreach ($parameterStrings as $parameterString) {
51 $parameterString = trim($parameterString);
52 $parameterParts = explode('=', $parameterString, 2);
53 if (count($parameterParts) < 2) {
57 $attribute = trim($parameterParts[0]);
58 $valueString = trim($parameterParts[1]);
60 if ($valueString[0] == '"' && $valueString[strlen($valueString) - 1] == '"') {
61 $valueString = substr(str_replace(['\\"', '\\\\', "\\\r"], ['"', '\\', "\r"], $valueString), 1, -1);
64 $value = preg_replace('#\s*\([^()]*?\)#', '', $valueString);
66 $parameters[$attribute] = $value;
70 return new Entity\MimeType(