Get Accept MIME Type

@return array
485585
public static function getAccept(): array
    {
        if (!self::$accept) {
            // get header from request
            self::$accept = self::parseAcceptHeader(self::getHeader('Accept'));
        }
        return self::$accept;
    }
998522
public function parseHeader($accept)
    {
        // Array of supported mime types and their params
        $supported = [];

        // Separate the header into parts based on the comma (,) char
        $ranges = array_map('trim', explode(',', $accept));

        $index = 0;
        // Loop through all ranges
        foreach ($ranges as $range) {
            // Separate each part based on the semi colon (;) so that we get
            // the mime type and maybe some parameters
            $parts = array_map('trim', explode(';', $range));

            // Parse the parts of the range
            $support = $this->parseParts($parts);

            // Save current index
            $support['index'] = $index;

            // Save the result to the array with all supported mime types
            $supported[] = $support;

            // Update index
            $index++;
        }

        return $supported;
    }
771257
protected function _getTypes() {
		$handle = new File(FILE_CACHE . 'mime_types.txt', true, 0770);
		if (!$handle->exists()) {
			# create and fill: ext||type||name||img (array serialized? separated by ||?)
			$MimeTypes = TableRegistry::get('Data.MimeTypes');
			$mimeTypes = $MimeTypes->find('all', ['fields' => ['name', 'ext', 'type', 'MimeTypeImages.name', 'MimeTypeImages.ext'], 'conditions' => ['MimeTypes.active' => 1], 'contain' => ['MimeTypeImages']]);

			$content = [];
			foreach ($mimeTypes as $m) {
				$img = (!empty($m->mime_type_image['ext']) ? $m->mime_type_image['name'] . '.' . $m->mime_type_image['ext'] : '');
				$content[] = [
					'ext' => $m['ext'],
					'name' => $m['name'],
					'type' => $m['type'],
					'img' => $img,
				];
			}
			# add special types? (file not found icon, default fallback icon, generic file ext icon...)

			if (!$handle->write(serialize($content), 'w', true)) {
				throw new Exception('Write error');
			}
		} else {
			$content = $handle->read();
			if ($content === false) {
				return [];
			}
			$content = unserialize($content);
			if ($content === false || !is_array($content)) {
				return [];
			}
		}
		return $content;
	}
1050555
public function getMimeType()
    {
        if ($this->isUploaded() && $this->mime_type === null) {
            $temp_file_name = $this->getTempFileName();
            if (file_exists($temp_file_name)) {
                if (extension_loaded('fileinfo')) {
                    // Use the fileinfo extension if available.
                    $finfo = $this->getFinfo();
                    $this->mime_type = explode(
                        ';',
                        $finfo->file($temp_file_name)
                    )[0];
                } elseif (function_exists('mime_content_type')) {
                    // Fall back to mime_content_type() if available.
                    $this->mime_type = mime_content_type($temp_file_name);
                }

                // No mime-detection functions, or mime-detection function
                // failed to detect the type. Default to
                // 'application/octet-stream'. Relying on HTTP headers could
                // be a security problem so we never fall back to that option.
                if ($this->mime_type == '') {
                    $this->mime_type = 'application/octet-stream';
                }
            } else {
                $this->mime_type = $this->file['type'];
            }
        }

        return $this->mime_type;
    }
62377
public function getMimeType($mime)
    {
        if (isset($this->mimes[$mime]))
        {
            return $this->mimes[$mime];
        }
        else
        {
            return false;
        }
    }
63763
public function type()
    {
        $accept = $this->server('HTTP_ACCEPT');
        if (empty($accept)) {
            return false;
        }

        foreach ($this->mimeType as $key => $val) {
            $array = explode(',', $val);
            foreach ($array as $k => $v) {
                if (stristr($accept, $v)) {
                    return $key;
                }
            }
        }
        return false;
    }
629426
public function detectMime() {

        $fileInfo = new \finfo(FILEINFO_MIME);

        return $fileInfo->buffer($this->getContent(), FILEINFO_MIME) ?: null;
    }
458155
public static function get_primary_accept() {
	if (empty($_SERVER['HTTP_ACCEPT'])) {
		return '*';
	}
	
	// catch the most common formats
	if (strpos($_SERVER['HTTP_ACCEPT'], 'text/html,') === 0) {
		return 'html';
	}
	if (strpos($_SERVER['HTTP_ACCEPT'], 'application/json,') === 0) {
		return 'json';
	}
	
	// use a generic method
	return self::get_primary_mime_type($_SERVER['HTTP_ACCEPT']);
}
985554
private function _load_mime_types()
    {

        // if file with mime types was not already loaded
        if (!isset($this->form_properties['mimes'])) {

            // read file into an array
            $rows = file($this->form_properties['assets_server_path'] . 'mimes.json');

            // convert JSON to array
            // i'm aware that in PHP 5.2+ there is json_decode, but i want this library to be
            // as backward compatible as possible so, since the values in mimes.json has a
            // specific structure, i wrote my own decoder
            $this->form_properties['mimes'] = array();

            // iterate through all the rows
            foreach ($rows as $row) {

                // if valid row found
                if (strpos($row, ':') !== false) {

                    // explode the string by :
                    $items = explode(':', $row);

                    // the file type (extension)
                    $index = trim(str_replace('"', '', $items[0]));

                    // if there are more mime types attached
                    if (strpos($items[1], '[') !== false)

                        // convert to array
                        $value = array_diff(array_map('trim', explode(',', str_replace(array('[', ']', '"', '\/'), array('', '', '', '/'), $items[1]))), array(''));

                    // if a single mime type is attached
                    else

                        // convert to string
                        $value = trim(str_replace(array('"', ',', '\/'), array('', '', '/'), $items[1]));

                    // save entry
                    $this->form_properties['mimes'][$index] = $value;

                }

            }

        }

    }
452088
static function defaultMimeType()
    {
        return array( 'name' => 'application/octet-stream',
                      'url' => false,
                      'filename' => false,
                      'dirpath' => false,
                      'basename' => false,
                      'suffix' => false,
                      'prefix' => false,
                      'suffixes' => false,
                      'prefixes' => false,
                      'is_valid' => true );
    }
1023545
public function isMime()
    {
        $contentType = $this->getHeaderValue('Content-Type');
        $mimeVersion = $this->getHeaderValue('Mime-Version');
        return ($contentType !== null || $mimeVersion !== null);
    }
3744
protected function _getMimetypes()
    {
        if(!isset($this->_mimetypes))
        {
            $file = $this->getConfig()->file;

            if (is_readable($file)) {
                $this->_mimetypes = json_decode(file_get_contents($file), true);
            }
        }

        return $this->_mimetypes;
    }
834024
public function getMimeType()
	{
		$field	= $this->headers->getField( 'Content-Type' );
		$parts	= explode( ';', $field->getValue() );
		return $parts[0];
	}
279786
protected function getMimeType(Request $request)
    {
        $acceptHeader = $this->negotiator->getBest(
            $request->headers->get('Accept'),
            $this->getMimeTypes()
        );

        if ($acceptHeader && $this->isAcceptableMimeType($acceptHeader->getType())) {
            return $acceptHeader->getType();
        }

        return $this->defaultMimeType;
    }
1284182
public function getSupportedMimes()
    {
        if ($this->mimes) {
            return $this->mimes;
        }

        $response = $this->get(
            'mime-types',
            $this->getGuzzleOptions([
                'headers' => [
                    'Accept' => 'application/json',
                ],
            ])
        );

        return $this->mimes = Convert::json2array($response->getBody());
    }
1062283
private function getNotAcceptableHttpException(string $accept, array $mimeTypes = null): NotAcceptableHttpException
    {
        if (null === $mimeTypes) {
            $mimeTypes = array_keys($this->mimeTypes);
        }

        return new NotAcceptableHttpException(sprintf(
            'Requested format "%s" is not supported. Supported MIME types are "%s".',
            $accept,
            implode('", "', $mimeTypes)
        ));
    }
1031550
public function getMime()
    {
        $mimes = $this->getMimes();
        $ext = strtolower($this->getExtension());

        if (isset($mimes[ $ext ])) {
            return $mimes[ $ext ];
        }

        return false;
    }