UTILS
Table of Contents
Functions
- listCoreMandatoryPHPExtensions() : array<string|int, string>
- Return an array of mandatory PHP extensions used by the WIKINDX core
- listCoreOptionalPHPExtensions() : array<string|int, string>
- Return an array of optional PHP extensions used by the WIKINDX core
- OSName() : string
- Return the normalized name of the current os
- matchPrefix() : bool
- Check if a prefix match against a string
- matchSuffix() : bool
- Check if a suffix match against a string
- curl_download_file_http() : bool
- Download a file from an url with HTTP
- uuid() : string
- Return a (pseudo) unique string of variable length using random_bytes().
- createDataFolders() : void
- Create default data folders used by the application
- checkFileSystemPerms() : mixed
- Check the permissions of various folders and files which must be writable
- arrayCombinations() : void
- Generate an array of all possible combinations of array values From https://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array with minor adaptation ($space)
- splitDate() : array<string|int, mixed>
- Take input from HTML FORM <input type=date> and split into separate fields.
- balanceBraces() : string
- Balance braces after removing noSort in resource input/edit of title or for bibTeX input
- sumBraces() : int
- Balance braces after removing noSort or for bibTeX input — return result of sum of open - close braces
- noSortPattern() : string
- Create a preg pattern for checking presence of a WIKINDX_NO_SORT value at the beginning of a string.
- writeUserPassword() : void
- Write a hashed version of a user password in users table
- verifyUserPassword() : bool
- Verify that a password matches a password hash of a user
- array_value_select() : mixed
- Return the corresponding value of an array key or a default value
- createHelpTopicLink() : string
- Create a popup link for a Help Topic on the website
- canIResumeExecution() : bool
- Can I resume execution?
- command_exists() : bool
- Does an unix command exist?
- insertExportImages() : void
- Insert cached images inside exported files, and delete them
- reduceLongText() : string
- Reduce the size of long text (in select boxes usually) to keep web browser display tidy.
- computeBibtexKeyAuthorYear() : string
- Generate a BibTeX citation key for one resource with format 'AuthorYear'
- computeBibtexKeyAuthorID() : string
- Generate a BibTeX citation key for one resource with format 'Author.ResourceId'
- clearXmlString() : string
- Clear forbidden characters from an XML string
- clearDOI() : string
- Clear a DOI number
- validateDOI() : bool
- Validate a DOI number
Functions
listCoreMandatoryPHPExtensions()
Return an array of mandatory PHP extensions used by the WIKINDX core
listCoreMandatoryPHPExtensions() : array<string|int, string>
The name of each extension is the value of an array entry. An extension name use the same casing as get_loaded_extensions().
Return values
array<string|int, string>listCoreOptionalPHPExtensions()
Return an array of optional PHP extensions used by the WIKINDX core
listCoreOptionalPHPExtensions() : array<string|int, string>
The name of each extension is the value of an array entry. An extension name use the same casing as get_loaded_extensions().
Return values
array<string|int, string>OSName()
Return the normalized name of the current os
OSName() : string
Values can be:
- "windows" for Cygwin, mingw, msys, or Windows
- "mac" forall version of MAC OSX
- The raw name in lowercase (a-z chars only) for others
Return values
stringmatchPrefix()
Check if a prefix match against a string
matchPrefix(string $string, string $prefix[, bool $casesensitive = FALSE ]) : bool
Parameters
- $string : string
-
A string
- $prefix : string
-
A prefix
- $casesensitive : bool = FALSE
-
Perform a case sensitive comparison if TRUE (FALSE by default)
Return values
boolmatchSuffix()
Check if a suffix match against a string
matchSuffix(string $string, string $suffix[, bool $casesensitive = FALSE ]) : bool
Parameters
- $string : string
-
A string
- $suffix : string
-
A suffix
- $casesensitive : bool = FALSE
-
Perform a case sensitive comparison if TRUE (FALSE by default)
Return values
boolcurl_download_file_http()
Download a file from an url with HTTP
curl_download_file_http(string $url, string $file) : bool
NB: follow 32 redirects (for SourceForge) and don't check certificats and fingerprints
Parameters
- $url : string
-
The url of a web resource
- $file : string
-
The path of a local file
Return values
bool —TRUE if the download is successful, FALSE otherwise
uuid()
Return a (pseudo) unique string of variable length using random_bytes().
uuid([int $length = 16 ]) : string
Parameters
- $length : int = 16
-
Length of the returned string (Default is 16)
Return values
stringcreateDataFolders()
Create default data folders used by the application
createDataFolders() : void
checkFileSystemPerms()
Check the permissions of various folders and files which must be writable
checkFileSystemPerms([bool $withFiles = FALSE ]) : mixed
Parameters
- $withFiles : bool = FALSE
-
Also check file perms (Default is FALSE)
arrayCombinations()
Generate an array of all possible combinations of array values From https://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array with minor adaptation ($space)
arrayCombinations(array<string|int, mixed> $arr, string $temp_string, string $space, array<string|int, mixed> &$collect) : void
Parameters
- $arr : array<string|int, mixed>
- $temp_string : string
- $space : string
- $collect : array<string|int, mixed>
splitDate()
Take input from HTML FORM <input type=date> and split into separate fields.
splitDate(string $dateInput) : array<string|int, mixed>
Date comes in as 'yyyy-mm-dd' (but displayed on web form as 'dd / mm / yyyy'). All three fields must have a valid value else the form input is FALSE. This should be tested before calling this function.
Parameters
- $dateInput : string
Return values
array<string|int, mixed> —array(year, month, day)
balanceBraces()
Balance braces after removing noSort in resource input/edit of title or for bibTeX input
balanceBraces(string $string) : string
Parameters
- $string : string
Return values
stringsumBraces()
Balance braces after removing noSort or for bibTeX input — return result of sum of open - close braces
sumBraces(string $string) : int
Parameters
- $string : string
Return values
intnoSortPattern()
Create a preg pattern for checking presence of a WIKINDX_NO_SORT value at the beginning of a string.
noSortPattern([array<string|int, mixed> $noSortArray = [] ]) : string
If empty($noSortArray), then we use WIKINDX_NO_SORT (and assume that WIKINDX_NO_SORT has been checked for content). Otherwise, $noSortArray is from admin::configure::updateNoSort()
Parameters
- $noSortArray : array<string|int, mixed> = []
-
Default is []
Return values
stringwriteUserPassword()
Write a hashed version of a user password in users table
writeUserPassword(SQL $db, string $usersId, string $usersPassword) : void
Implementation history: see \UTILS\verifyUserPassword()
Parameters
- $db : SQL
-
An SQL object
- $usersId : string
-
A user Id
- $usersPassword : string
-
A clear password to encrypt/hash
verifyUserPassword()
Verify that a password matches a password hash of a user
verifyUserPassword(SQL $db, string $usersId, string $usersPassword) : bool
It hides the implementation changes linked to the evolution of cryptographic techniques.
Parameters
- $db : SQL
-
An SQL object
- $usersId : string
-
A user Id
- $usersPassword : string
-
A clear password to verify
Tags
Return values
boolarray_value_select()
Return the corresponding value of an array key or a default value
array_value_select(array<string|int, mixed> $array, mixed $key[, mixed $defaultKey = NULL ][, mixed $defaultValue = NULL ]) : mixed
This function is an access to the value of an array with fallback solutions in the event of absence.
This function is useful when it is necessary to select a value from a list and one of the inputs is also the default key or it is mandatory to obtain a consistent value at the output of the selection without having to write additional code.
Parameters
- $array : array<string|int, mixed>
-
An array to search the key for
- $key : mixed
-
The key whose value must be returned
- $defaultKey : mixed = NULL
-
The default key whose value must be returned if the key was not found (NULL by default)
- $defaultValue : mixed = NULL
-
The value returned if no key was found (NULL by default)
createHelpTopicLink()
Create a popup link for a Help Topic on the website
createHelpTopicLink(mixed $helpTopic) : string
Parameters
- $helpTopic : mixed
Return values
string —HTML link tag
canIResumeExecution()
Can I resume execution?
canIResumeExecution([int $memorySizeRemaining = 1048576 ][, int $excutionTimeRemaining = 6 ]) : bool
Says if it is possible to continue the execution of the program according to the consumption of memory and time.
Use this for memory or time intensive tasks you intend to interrupt without crash.
Say TRUE:
- if 10 MB or less remains before the PHP max memory limit
- or if 6 s. or less time remains before max_execution_time.
Otherwise FALSE.
NB:
- If memory_limit option is set to -1, the memory condition is not checked.
- If max_execution_time option is set to 0, the time condition is not checked.
Parameters
- $memorySizeRemaining : int = 1048576
-
Memory size in bytes of remaining before the limit (Default is 10MB = 1024 * 1024 bytes)
- $excutionTimeRemaining : int = 6
-
Execution time remaining before the max executionlimit (Default is 6 seconds)
Return values
boolcommand_exists()
Does an unix command exist?
command_exists(string $command) : bool
Parameters
- $command : string
-
Command to test with the default shell
Return values
bool —TRUE on success
insertExportImages()
Insert cached images inside exported files, and delete them
insertExportImages(string $fileName) : void
The file is written to consume as little memory as possible because the number of images can be large.
Parameters
- $fileName : string
reduceLongText()
Reduce the size of long text (in select boxes usually) to keep web browser display tidy.
reduceLongText(string $text[, bool|int $override = FALSE ][, int $words = 0 ][, bool $balance = TRUE ]) : string
Optional $override allows the programmer to override the user set preferences for number of characters. If optional $words is greater than 0, then that number of words is used rather than a number of characters. If optional $balance l= TRUE, the return string is something like 'xxxx ... xxxx' else it is 'xxxx ...' (if there are chars/words removed). If $words is being used, then $balance is ignored and the first $words are returned.
Parameters
- $text : string
- $override : bool|int = FALSE
-
Default is FALSE
- $words : int = 0
-
Default is 0
- $balance : bool = TRUE
-
Default TRUE
Return values
stringcomputeBibtexKeyAuthorYear()
Generate a BibTeX citation key for one resource with format 'AuthorYear'
computeBibtexKeyAuthorYear(int $resourceId, string $creatorPrefix, string $creatorSurname, string|int $year, array<string|int, string> $bibtexKeys) : string
LkpPo(TODO): fix the max length
Parameters
- $resourceId : int
-
Resource Id
- $creatorPrefix : string
-
Prefix of the first creator of the resource
- $creatorSurname : string
-
Surname of the first creator of the resource
- $year : string|int
-
Year of publication of the resource
- $bibtexKeys : array<string|int, string>
-
An array of existing BibTeX citation keys already stored in db
Return values
stringcomputeBibtexKeyAuthorID()
Generate a BibTeX citation key for one resource with format 'Author.ResourceId'
computeBibtexKeyAuthorID(int $resourceId, string $creatorPrefix, string $creatorSurname, array<string|int, string> $bibtexKeys) : string
LkpPo(TODO): fix the max length
Parameters
- $resourceId : int
-
Resource Id
- $creatorPrefix : string
-
Prefix of the first creator of the resource
- $creatorSurname : string
-
Surname of the first creator of the resource
- $bibtexKeys : array<string|int, string>
-
An array of existing BibTeX citation keys already stored in db
Return values
stringclearXmlString()
Clear forbidden characters from an XML string
clearXmlString(string $xmlstring) : string
If a forbidden character is in a XML string, PHP XML libraries crash.
This function should be used to remove forbidden characters from an XML file or string when the source in untrusted.
Parameters
- $xmlstring : string
-
An XML string
Tags
Return values
stringclearDOI()
Clear a DOI number
clearDOI(string $string) : string
Parameters
- $string : string
Tags
Return values
stringvalidateDOI()
Validate a DOI number
validateDOI(string $string) : bool
The DOI prefix is <directoryIndicator>.<registrantCode>
The following rules apply:
-
The directory indicator can contain only numeric values. The directory indicator is usually 10 but other indicators may be designated as compliant by the DOI Foundation.
-
The registrant code can contain only numeric values and one or several full stops which are used to subdivide the code. For example: 10.1000, 10.500.100, etc. If the directory indicator is 10 then a registrant code is mandatory
Parameters
- $string : string