HumHub Documentation (unofficial)

ExcludeMimeType extends MimeType
in package
uses FileInformationTrait

Validator for the mime type of a file

Table of Contents

Constants

FALSE_TYPE  = 'fileExcludeMimeTypeFalse'
NOT_DETECTED  = 'fileExcludeMimeTypeNotDetected'
NOT_READABLE  = 'fileExcludeMimeTypeNotReadable'

Properties

$messageTemplates  : array<string, string>
$messageVariables  : array<string, mixed>
$options  : array<string, mixed>
$abstractOptions  : AbstractOptions|array<string, mixed>
$defaultTranslator  : TranslatorInterface
Default translation object for all validate objects
$defaultTranslatorTextDomain  : string
Default text domain to be used with translator
$finfo  : resource
Finfo object to use
$magicFiles  : array<string|int, mixed>
If no environment variable 'MAGIC' is set, try and autodiscover it based on common locations
$messageLength  : int
Limits the maximum returned length of an error message
$messageTemplates  : array<string|int, mixed>
$messageVariables  : array<string|int, mixed>
$options  : array<string|int, mixed>
Options for this validator
$type  : string
$value  : mixed
The value to be validated

Methods

__construct()  : mixed
Sets validator options
__get()  : mixed
Magic function returns the value of the requested property, if and only if it is the value or a message variable.
__invoke()  : bool
Invoke as command
addMimeType()  : $this
Adds the mimetypes
disableMagicFile()  : self
Disables usage of MagicFile
enableHeaderCheck()  : $this
Defines if the http header should be used Note that this is unsafe and therefor the default value is false
getDefaultTranslator()  : TranslatorInterface|null
Get default translation object for all validate objects
getDefaultTranslatorTextDomain()  : string
Get default translation text domain for all validate objects
getHeaderCheck()  : bool
Returns the Header Check option
getMagicFile()  : string
Returns the actual set magicfile
getMessageLength()  : int
Returns the maximum allowed message length
getMessages()  : array<string, string>
Returns array of validation failure messages
getMessageTemplates()  : array<string, string>
Returns the message templates from the validator
getMessageVariables()  : array<int, string>
Returns an array of the names of variables that are used in constructing validation failure messages
getMimeType()  : string|array<string|int, mixed>
Returns the set mimetypes
getOption()  : mixed
Returns an option
getOptions()  : array<string, mixed>
Returns all available options
getTranslator()  : TranslatorInterface|null
Return translation object
getTranslatorTextDomain()  : string
Return the translation text domain
hasDefaultTranslator()  : bool
Is there a default translation object set?
hasTranslator()  : bool
Does this validator have its own specific translator?
isMagicFileDisabled()  : bool
Is usage of MagicFile disabled?
isTranslatorEnabled()  : bool
Is translation enabled?
isValid()  : bool
Returns true if the mimetype of the file does not matche the given ones. Also parts of mimetypes can be checked. If you give for example "image" all image mime types will not be accepted like "image/gif", "image/jpeg" and so on.
isValueObscured()  : bool
Retrieve flag indicating whether or not value should be obfuscated in messages
setDefaultTranslator()  : void
Set default translation object for all validate objects
setDefaultTranslatorTextDomain()  : void
Set default translation text domain for all validate objects
setMagicFile()  : $this
Sets the magicfile to use if null, the MAGIC constant from php is used if the MAGIC file is erroneous, no file will be set if false, the default MAGIC file from PHP will be used
setMessage()  : $this
Sets the validation failure message template for a particular key
setMessageLength()  : void
Sets the maximum allowed message length
setMessages()  : $this
Sets validation failure message templates given as an array, where the array keys are the message keys, and the array values are the message template strings.
setMimeType()  : $this
Sets the mimetypes
setOptions()  : $this
Sets one or multiple options
setTranslator()  : $this
Set translation object
setTranslatorEnabled()  : $this
Indicate whether or not translation should be enabled
setTranslatorTextDomain()  : $this
Set translation text domain
setValueObscured()  : $this
Set flag indicating whether or not value should be obfuscated in messages
createMessage()  : null|string
Constructs and returns a validation failure message with the given message key and value.
error()  : void
getFileInfo()  : array<string|int, mixed>
Returns array if the procedure is identified
getValue()  : mixed
Returns the validation value
setValue()  : void
Sets the value to be validated and clears the messages and errors arrays
translateMessage()  : string
Translate a validation message
getFileBasedFileInfo()  : array<string|int, mixed>
Generate file information array with base method
getLegacyFileInfo()  : array<string|int, mixed>
Generate file information array with legacy Laminas_File_Transfer API
getPsr7FileInfo()  : array<string|int, mixed>
Generate file information array with PSR-7 UploadedFileInterface
getSapiFileInfo()  : array<string|int, mixed>
Generate file information array with SAPI

Constants

NOT_DETECTED

public mixed NOT_DETECTED = 'fileExcludeMimeTypeNotDetected'

NOT_READABLE

public mixed NOT_READABLE = 'fileExcludeMimeTypeNotReadable'

Properties

$abstractOptions

protected AbstractOptions|array<string, mixed> $abstractOptions = [ 'messages' => [], // Array of validation failure messages 'messageTemplates' => [], // Array of validation failure message templates 'messageVariables' => [], // Array of additional variables available for validation failure messages 'translator' => null, // Translation object to used -> Translator\TranslatorInterface 'translatorTextDomain' => null, // Translation text domain 'translatorEnabled' => true, // Is translation enabled? 'valueObscured' => false, ]

$defaultTranslatorTextDomain

Default text domain to be used with translator

protected static string $defaultTranslatorTextDomain = 'default'

$finfo

Finfo object to use

protected resource $finfo

$magicFiles

If no environment variable 'MAGIC' is set, try and autodiscover it based on common locations

protected array<string|int, mixed> $magicFiles = ['/usr/share/misc/magic', '/usr/share/misc/magic.mime', '/usr/share/misc/magic.mgc', '/usr/share/mime/magic', '/usr/share/mime/magic.mime', '/usr/share/mime/magic.mgc', '/usr/share/file/magic', '/usr/share/file/magic.mime', '/usr/share/file/magic.mgc']

$messageLength

Limits the maximum returned length of an error message

protected static int $messageLength = -1

$messageTemplates

protected array<string|int, mixed> $messageTemplates = [self::FALSE_TYPE => "File has an incorrect mimetype of '%type%'", self::NOT_DETECTED => 'The mimetype could not be detected from the file', self::NOT_READABLE => 'File is not readable or does not exist']

Error message templates

$messageVariables

protected array<string|int, mixed> $messageVariables = ['type' => 'type']

$options

Options for this validator

protected array<string|int, mixed> $options = [ 'enableHeaderCheck' => false, // Allow header check 'disableMagicFile' => false, // Disable usage of magicfile 'magicFile' => null, // Magicfile to use 'mimeType' => null, ]

Methods

__construct()

Sets validator options

public __construct([string|array<string|int, mixed>|Traversable $options = null ]) : mixed

Mimetype to accept

  • NULL means default PHP usage by using the environment variable 'magic'
  • FALSE means disabling searching for mimetype, should be used for PHP 5.3
  • A string is the mimetype file to use
Parameters
$options : string|array<string|int, mixed>|Traversable = null

__get()

Magic function returns the value of the requested property, if and only if it is the value or a message variable.

public __get(string $property) : mixed
Parameters
$property : string
Tags
throws
InvalidArgumentException

__invoke()

Invoke as command

public __invoke(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

addMimeType()

Adds the mimetypes

public addMimeType(string|array<string|int, mixed> $mimetype) : $this
Parameters
$mimetype : string|array<string|int, mixed>

The mimetypes to add for validation

Tags
throws
InvalidArgumentException
Return values
$this

Provides a fluent interface

disableMagicFile()

Disables usage of MagicFile

public disableMagicFile(bool $disable) : self
Parameters
$disable : bool

False disables usage of magic file; true enables it.

Return values
self

Provides fluid interface

enableHeaderCheck()

Defines if the http header should be used Note that this is unsafe and therefor the default value is false

public enableHeaderCheck([bool $headerCheck = true ]) : $this
Parameters
$headerCheck : bool = true
Return values
$this

Provides fluid interface

getDefaultTranslatorTextDomain()

Get default translation text domain for all validate objects

public static getDefaultTranslatorTextDomain() : string
Return values
string

getHeaderCheck()

Returns the Header Check option

public getHeaderCheck() : bool
Return values
bool

getMagicFile()

Returns the actual set magicfile

public getMagicFile() : string
Return values
string

getMessageLength()

Returns the maximum allowed message length

public static getMessageLength() : int
Return values
int

getMessages()

Returns array of validation failure messages

public getMessages() : array<string, string>
Return values
array<string, string>

getMessageTemplates()

Returns the message templates from the validator

public getMessageTemplates() : array<string, string>
Return values
array<string, string>

getMessageVariables()

Returns an array of the names of variables that are used in constructing validation failure messages

public getMessageVariables() : array<int, string>
Return values
array<int, string>

getMimeType()

Returns the set mimetypes

public getMimeType([bool $asArray = false ]) : string|array<string|int, mixed>
Parameters
$asArray : bool = false

Returns the values as array, when false a concatenated string is returned

Tags
psalm-return

($asArray is true ? list : string)

Return values
string|array<string|int, mixed>

getOption()

Returns an option

public getOption(string $option) : mixed
Parameters
$option : string

Option to be returned

Tags
throws
InvalidArgumentException
Return values
mixed

Returned option

getOptions()

Returns all available options

public getOptions() : array<string, mixed>
Return values
array<string, mixed>

Array with all available options

getTranslatorTextDomain()

Return the translation text domain

public getTranslatorTextDomain() : string
Return values
string

hasDefaultTranslator()

Is there a default translation object set?

public static hasDefaultTranslator() : bool
Return values
bool

hasTranslator()

Does this validator have its own specific translator?

public hasTranslator() : bool
Return values
bool

isMagicFileDisabled()

Is usage of MagicFile disabled?

public isMagicFileDisabled() : bool
Return values
bool

isTranslatorEnabled()

Is translation enabled?

public isTranslatorEnabled() : bool
Return values
bool

isValid()

Returns true if the mimetype of the file does not matche the given ones. Also parts of mimetypes can be checked. If you give for example "image" all image mime types will not be accepted like "image/gif", "image/jpeg" and so on.

public isValid(string|array<string|int, mixed> $value[, array<string|int, mixed> $file = null ]) : bool
Parameters
$value : string|array<string|int, mixed>

Real file to check for mimetype

$file : array<string|int, mixed> = null

File data from \Laminas\File\Transfer\Transfer (optional)

Return values
bool

isValueObscured()

Retrieve flag indicating whether or not value should be obfuscated in messages

public isValueObscured() : bool
Return values
bool

setDefaultTranslatorTextDomain()

Set default translation text domain for all validate objects

public static setDefaultTranslatorTextDomain([string $textDomain = 'default' ]) : void
Parameters
$textDomain : string = 'default'

setMagicFile()

Sets the magicfile to use if null, the MAGIC constant from php is used if the MAGIC file is erroneous, no file will be set if false, the default MAGIC file from PHP will be used

public setMagicFile(string $file) : $this
Parameters
$file : string
Tags
throws
RuntimeException

When finfo can not read the magicfile.

throws
InvalidArgumentException
throws
InvalidMagicMimeFileException
Return values
$this

Provides fluid interface

setMessage()

Sets the validation failure message template for a particular key

public setMessage(string $messageString[, string|null $messageKey = null ]) : $this
Parameters
$messageString : string
$messageKey : string|null = null

OPTIONAL

Tags
throws
InvalidArgumentException
Return values
$this

Provides a fluent interface

setMessageLength()

Sets the maximum allowed message length

public static setMessageLength([int $length = -1 ]) : void
Parameters
$length : int = -1

setMessages()

Sets validation failure message templates given as an array, where the array keys are the message keys, and the array values are the message template strings.

public setMessages(array<string, string> $messages) : $this
Parameters
$messages : array<string, string>
Return values
$this

setMimeType()

Sets the mimetypes

public setMimeType(string|array<string|int, mixed> $mimetype) : $this
Parameters
$mimetype : string|array<string|int, mixed>

The mimetypes to validate

Return values
$this

Provides a fluent interface

setOptions()

Sets one or multiple options

public setOptions([array<string, mixed>|Traversable<string, mixed> $options = [] ]) : $this
Parameters
$options : array<string, mixed>|Traversable<string, mixed> = []

Options to set

Tags
throws
InvalidArgumentException

If $options is not an array or Traversable.

Return values
$this

Provides fluid interface

setTranslatorEnabled()

Indicate whether or not translation should be enabled

public setTranslatorEnabled([bool $enabled = true ]) : $this
Parameters
$enabled : bool = true
Return values
$this

setTranslatorTextDomain()

Set translation text domain

public setTranslatorTextDomain([string $textDomain = 'default' ]) : $this
Parameters
$textDomain : string = 'default'
Return values
$this

setValueObscured()

Set flag indicating whether or not value should be obfuscated in messages

public setValueObscured(bool $flag) : $this
Parameters
$flag : bool
Return values
$this

createMessage()

Constructs and returns a validation failure message with the given message key and value.

protected createMessage(string $messageKey, string|array<string|int, mixed>|object $value) : null|string

Returns null if and only if $messageKey does not correspond to an existing template.

If a translator is available and a translation exists for $messageKey, the translation will be used.

Parameters
$messageKey : string
$value : string|array<string|int, mixed>|object
Return values
null|string

error()

protected error(string|null $messageKey[, null|string|array<string|int, mixed>|object $value = null ]) : void
Parameters
$messageKey : string|null
$value : null|string|array<string|int, mixed>|object = null

OPTIONAL

getFileInfo()

Returns array if the procedure is identified

protected getFileInfo(string|array<string|int, mixed>|object $value[, null|array<string|int, mixed> $file = null ][, bool $hasType = false ][, bool $hasBasename = false ]) : array<string|int, mixed>
Parameters
$value : string|array<string|int, mixed>|object

Filename to check

$file : null|array<string|int, mixed> = null

File data (when using legacy Laminas_File_Transfer API)

$hasType : bool = false

Return with filetype (optional)

$hasBasename : bool = false

Return with basename - is calculated from location path (optional)

Return values
array<string|int, mixed>

getValue()

Returns the validation value

protected getValue() : mixed
Return values
mixed

Value to be validated

setValue()

Sets the value to be validated and clears the messages and errors arrays

protected setValue(mixed $value) : void
Parameters
$value : mixed

translateMessage()

Translate a validation message

protected translateMessage(string $messageKey, string $message) : string
Parameters
$messageKey : string
$message : string
Return values
string

getFileBasedFileInfo()

Generate file information array with base method

private getFileBasedFileInfo(string $file[, bool $hasType = false ][, bool $hasBasename = false ]) : array<string|int, mixed>
Parameters
$file : string

File path

$hasType : bool = false

Return with filetype

$hasBasename : bool = false

Filename is calculated from location path

Return values
array<string|int, mixed>

getLegacyFileInfo()

Generate file information array with legacy Laminas_File_Transfer API

private getLegacyFileInfo(array<string|int, mixed> $file[, bool $hasType = false ][, bool $hasBasename = false ]) : array<string|int, mixed>
Parameters
$file : array<string|int, mixed>

File data

$hasType : bool = false

Return with filetype

$hasBasename : bool = false

Basename is calculated from location path

Return values
array<string|int, mixed>

getPsr7FileInfo()

Generate file information array with PSR-7 UploadedFileInterface

private getPsr7FileInfo(UploadedFileInterface $file[, bool $hasType = false ][, bool $hasBasename = false ]) : array<string|int, mixed>
Parameters
$file : UploadedFileInterface
$hasType : bool = false

Return with filetype

$hasBasename : bool = false

Filename is calculated from location path

Return values
array<string|int, mixed>

getSapiFileInfo()

Generate file information array with SAPI

private getSapiFileInfo(array<string|int, mixed> $file[, bool $hasType = false ][, bool $hasBasename = false ]) : array<string|int, mixed>
Parameters
$file : array<string|int, mixed>

File data from SAPI

$hasType : bool = false

Return with filetype

$hasBasename : bool = false

Filename is calculated from location path

Return values
array<string|int, mixed>

        
On this page

Search results