Util

class OCP\Util
This class provides different helper functions to make the life of a developer easier
Source:lib/public/Util.php#61

Constants

DEBUG = 0
Source:lib/public/Util.php#65
Deprecated:14.0.0 use \OCP\ILogger::DEBUG
INFO = 1
Source:lib/public/Util.php#69
Deprecated:14.0.0 use \OCP\ILogger::INFO
WARN = 2
Source:lib/public/Util.php#73
Deprecated:14.0.0 use \OCP\ILogger::WARN
ERROR = 3
Source:lib/public/Util.php#77
Deprecated:14.0.0 use \OCP\ILogger::ERROR
FATAL = 4
Source:lib/public/Util.php#81
Deprecated:14.0.0 use \OCP\ILogger::FATAL

Properties

protected property OCP\Util::$needUpgradeCache
Source:lib/public/Util.php#510

Methods

public static OCP\Util::getVersion()
get the current installed version of Nextcloud
Source:lib/public/Util.php#91
Returns:array
Since:4.0.0
public static OCP\Util::hasExtendedSupport()
Source:lib/public/Util.php#98
Since:17.0.0
public static OCP\Util::setChannel($channel)
Set current update channel
Source:

lib/public/Util.php#113

Parameters:
  • $channel (string)
Since:

8.1.0

public static OCP\Util::getChannel()
Get current update channel
Source:lib/public/Util.php#122
Returns:string
Since:8.1.0
public static OCP\Util::writeLog($app, $message, $level)
write a message in the log
Source:

lib/public/Util.php#134

Parameters:
  • $app (string)
  • $message (string)
  • $level (int)
Since:

4.0.0

Deprecated:

13.0.0 use log of \OCP\ILogger

public static OCP\Util::isSharingDisabledForUser()
check if sharing is disabled for the current user
Source:lib/public/Util.php#146
Returns:bool
Since:7.0.0
Deprecated:9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser
public static OCP\Util::getL10N($application, $language=null)
get l10n object
Source:

lib/public/Util.php#166

Parameters:
  • $application (string)
  • $language (string | null)
Returns:

\OCP\IL10N

Since:

6.0.0 - parameter $language was added in 8.0.0

public static OCP\Util::addStyle($application, $file=null)
add a css file
Source:

lib/public/Util.php#176

Parameters:
  • $application (string)
  • $file (string)
Since:

4.0.0

public static OCP\Util::addScript($application, $file=null)
add a javascript file
Source:

lib/public/Util.php#186

Parameters:
  • $application (string)
  • $file (string)
Since:

4.0.0

public static OCP\Util::addTranslations($application, $languageCode=null)
Add a translation JS file
Source:

lib/public/Util.php#196

Parameters:
  • $application (string) application id
  • $languageCode (string) language code, defaults to the current locale
Since:

8.0.0

public static OCP\Util::addHeader($tag, $attributes, $text=null)
Add a custom element to the header
If $text is null then the element will be written as empty element.
So use “” to get a closing tag.
Source:

lib/public/Util.php#209

Parameters:
  • $tag (string) tag name of the element
  • $attributes (array) array of attributes for the element
  • $text (string) the text content for the element
Since:

4.0.0

public static OCP\Util::linkToAbsolute($app, $file, $args=[])
Creates an absolute url to the given app and file.
Source:

lib/public/Util.php#222

Parameters:
  • $app (string) app
  • $file (string) file
  • $args (array) array with param=>value, will be appended to the returned url

The value of $args will be urlencoded

Returns:

string the url

Since:

4.0.0 - parameter $args was added in 4.5.0

public static OCP\Util::linkToRemote($service)
Creates an absolute url for remote use.
Source:

lib/public/Util.php#235

Parameters:
  • $service (string) id
Returns:

string the url

Since:

4.0.0

public static OCP\Util::linkToPublic($service)
Creates an absolute url for public use
Source:

lib/public/Util.php#250

Parameters:
  • $service (string) id
Returns:

string the url

Since:

4.5.0

Deprecated:

15.0.0 - use OCP\IURLGenerator

public static OCP\Util::getServerHostName()
Returns the server host name without an eventual port number
Source:lib/public/Util.php#263
Returns:string the server hostname
Since:5.0.0
public static OCP\Util::getDefaultEmailAddress($user_part)
Returns the default email address
Source:

lib/public/Util.php#289

Parameters:
  • $user_part (string) the user part of the address
Returns:

string the default email address

Assembles a default email address (using the server hostname and the given user part, and returns it Example: when given lostpassword-noreply as $user_part param,

and is currently accessed via http(s)://example.com/, it would return 'lostpassword-noreply@example.com

If the configuration value ‘mail_from_address’ is set in config.php, this value will override the $user_part that is passed to this function

Since:

5.0.0

public static OCP\Util::humanFileSize($bytes)
Make a human file size (2048 to 2 kB)
Source:

lib/public/Util.php#311

Parameters:
  • $bytes (int) file size in bytes
Returns:

string a human readable file size

Since:

4.0.0

public static OCP\Util::computerFileSize($str)
Make a computer file size (2 kB to 2048)
Source:

lib/public/Util.php#323

Parameters:
  • $str (string) file size in a fancy format
Returns:

float a file size in bytes

Inspired by: https://www.php.net/manual/en/function.filesize.php#92418

Since:

4.0.0

public static OCP\Util::connectHook($signalClass, $signalName, $slotClass, $slotName)
connects a function to a hook
Source:

lib/public/Util.php#342

Parameters:
  • $signalClass (string) class name of emitter
  • $signalName (string) name of signal
  • $slotClass (string | object) class name of slot
  • $slotName (string) name of slot
Returns:

bool This function makes it very easy to connect to use hooks.

TODO: write example

Since:

4.0.0

Deprecated:

21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener

public static OCP\Util::emitHook($signalclass, $signalname, $params=[])
Emits a signal. To get data from the slot use references!
Source:

lib/public/Util.php#357

Parameters:
  • $signalclass (string) class name of emitter
  • $signalname (string) name of signal
  • $params (array) default: array() array with additional data
Returns:

bool true if slots exists or false if not

TODO: write example

Since:

4.0.0

Deprecated:

21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent

public static OCP\Util::callRegister()
Register an get/post call. This is important to prevent CSRF attacks
Source:lib/public/Util.php#373
Since:4.5.0
public static OCP\Util::sanitizeHTML($value)
Used to sanitize HTML
This function is used to sanitize HTML and should be applied on any
string or array of strings before displaying it on a web page.
Source:

lib/public/Util.php#390

Parameters:
  • $value (string | array)
Returns:

string | array an array of sanitized strings or a single sanitized string, depends on the input parameter.

Since:

4.5.0

public static OCP\Util::encodePath($component)
Public function to encode url parameters
This function is used to encode path to file before output.
Encoding is done according to RFC 3986 with one exception:
Character ‘/’ is preserved as is.
Source:

lib/public/Util.php#405

Parameters:
  • $component (string) part of URI to encode
Returns:

string

Since:

6.0.0

public static OCP\Util::mb_array_change_key_case($input, $case=MB_CASE_LOWER, $encoding=UTF-8)
Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
Source:

lib/public/Util.php#418

Parameters:
  • $input (array) The array to work on
  • $case (int) Either MB_CASE_UPPER or MB_CASE_LOWER (default)
  • $encoding (string) The encoding parameter is the character encoding. Defaults to UTF-8
Returns:

array

Since:

4.5.0

public static OCP\Util::recursiveArraySearch($haystack, $needle, $index=null)
performs a search in a nested array
Source:

lib/public/Util.php#432

Parameters:
  • $haystack (array) the array to be searched
  • $needle (string) the search string
  • $index (mixed) optional, only search this key name
Returns:

mixed the key of the matching field, otherwise false

Since:

4.5.0

Deprecated:

15.0.0

public static OCP\Util::maxUploadFilesize($dir, $free=null)
calculates the maximum upload size respecting system settings, free space and user quota
Source:

lib/public/Util.php#444

Parameters:
  • $dir (string) the current folder where the user currently operates
  • $free (int) the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
Returns:

int number of bytes representing

Since:

5.0.0

public static OCP\Util::freeSpace($dir)
Calculate free space left within user quota
Source:

lib/public/Util.php#454

Parameters:
  • $dir (string) the current folder where the user currently operates
Returns:

int number of bytes representing

Since:

7.0.0

public static OCP\Util::uploadLimit()
Calculate PHP upload limit
Source:lib/public/Util.php#464
Returns:int number of bytes representing
Since:7.0.0
public static OCP\Util::isValidFileName($file)
Returns whether the given file name is valid
Source:

lib/public/Util.php#476

Parameters:
  • $file (string) file name to check
Returns:

bool true if the file name is valid, false otherwise

Deprecated:

8.1.0 use \OC\Files\View::verifyPath()

Since:

7.0.0

public static OCP\Util::naturalSortCompare($a, $b)
Compare two strings to provide a natural sort
Source:

lib/public/Util.php#488

Parameters:
  • $a (string) first string to compare
  • $b (string) second string to compare
Returns:

int -1 if $b comes before $a, 1 if $a comes before $b or 0 if the strings are identical

Since:

7.0.0

public static OCP\Util::isPublicLinkPasswordRequired()
check if a password is required for each public link
Source:lib/public/Util.php#497
Returns:bool
Since:7.0.0
public static OCP\Util::isDefaultExpireDateEnforced()
check if share API enforces a default expire date
Source:lib/public/Util.php#506
Returns:bool
Since:8.0.0
public static OCP\Util::needUpgrade()
Checks whether the current version needs upgrade.
Source:lib/public/Util.php#518
Returns:bool true if upgrade is needed, false otherwise
Since:7.0.0