Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 14 additions & 48 deletions lib/private/legacy/OC_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,49 +246,30 @@ public static function buildNotExistingFileNameForView($path, $filename, \OC\Fil

/**
* Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
* Based on https://www.php.net/manual/en/function.array-change-key-case.php#107715
*
* @param array $input The array to work on
* @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
* @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
* @return array
*
* Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
* based on https://www.php.net/manual/en/function.array-change-key-case.php#107715
*
* @deprecated 4.5.0 use \OCP\Util::mb_array_change_key_case instead
*/
public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
$case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER;
$ret = [];
foreach ($input as $k => $v) {
$ret[mb_convert_case($k, $case, $encoding)] = $v;
}
return $ret;
return \OCP\Util::mb_array_change_key_case($input, $case, $encoding);
}

/**
* performs a search in a nested array
* Performs a search in a nested array.
* Taken from https://www.php.net/manual/en/function.array-search.php#97645
*
* @param array $haystack the array to be searched
* @param string $needle the search string
* @param mixed $index optional, only search this key name
* @return mixed the key of the matching field, otherwise false
*
* performs a search in a nested array
*
* taken from https://www.php.net/manual/en/function.array-search.php#97645
* @deprecated 4.5.0 - use \OCP\Util::recursiveArraySearch
*/
public static function recursiveArraySearch($haystack, $needle, $index = null) {
$aIt = new RecursiveArrayIterator($haystack);
$it = new RecursiveIteratorIterator($aIt);

while ($it->valid()) {
if (((isset($index) and ($it->key() == $index)) or !isset($index)) and ($it->current() == $needle)) {
return $aIt->key();
}

$it->next();
}

return false;
return \OCP\Util::recursiveArraySearch($haystack, $needle, $index);
}

/**
Expand All @@ -297,46 +278,31 @@ public static function recursiveArraySearch($haystack, $needle, $index = null) {
* @param string $dir the current folder where the user currently operates
* @param int|float $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
* @return int|float number of bytes representing
* @deprecated 5.0.0 - use \OCP\Util::maxUploadFilesize
*/
public static function maxUploadFilesize($dir, $freeSpace = null) {
if (is_null($freeSpace) || $freeSpace < 0) {
$freeSpace = self::freeSpace($dir);
}
return min($freeSpace, self::uploadLimit());
return \OCP\Util::maxUploadFilesize($dir, $freeSpace);
}

/**
* Calculate free space left within user quota
*
* @param string $dir the current folder where the user currently operates
* @return int|float number of bytes representing
* @deprecated 7.0.0 - use \OCP\Util::freeSpace
*/
public static function freeSpace($dir) {
$freeSpace = \OC\Files\Filesystem::free_space($dir);
if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) {
$freeSpace = max($freeSpace, 0);
return $freeSpace;
} else {
return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
}
return \OCP\Util::freeSpace($dir);
}

/**
* Calculate PHP upload limit
*
* @return int|float PHP upload file size limit
* @deprecated 7.0.0 - use \OCP\Util::uploadLimit
*/
public static function uploadLimit() {
$ini = \OC::$server->get(IniGetWrapper::class);
$upload_max_filesize = Util::computerFileSize($ini->get('upload_max_filesize')) ?: 0;
$post_max_size = Util::computerFileSize($ini->get('post_max_size')) ?: 0;
if ($upload_max_filesize === 0 && $post_max_size === 0) {
return INF;
} elseif ($upload_max_filesize === 0 || $post_max_size === 0) {
return max($upload_max_filesize, $post_max_size); //only the non 0 value counts
} else {
return min($upload_max_filesize, $post_max_size);
}
return \OCP\Util::uploadLimit();
}

/**
Expand Down
44 changes: 39 additions & 5 deletions lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,12 @@ public static function encodePath($component) {
* @since 4.5.0
*/
public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
return \OC_Helper::mb_array_change_key_case($input, $case, $encoding);
$case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER;
$ret = [];
foreach ($input as $k => $v) {
$ret[mb_convert_case($k, $case, $encoding)] = $v;
}
return $ret;
}

/**
Expand All @@ -505,7 +510,18 @@ public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $
* @deprecated 15.0.0
*/
public static function recursiveArraySearch($haystack, $needle, $index = null) {
return \OC_Helper::recursiveArraySearch($haystack, $needle, $index);
$aIt = new \RecursiveArrayIterator($haystack);
$it = new \RecursiveIteratorIterator($aIt);

while ($it->valid()) {
if (((isset($index) and ($it->key() == $index)) or !isset($index)) and ($it->current() == $needle)) {
return $aIt->key();
}

$it->next();
}

return false;
}

/**
Expand All @@ -517,7 +533,10 @@ public static function recursiveArraySearch($haystack, $needle, $index = null) {
* @since 5.0.0
*/
public static function maxUploadFilesize(string $dir, int|float|null $free = null): int|float {
return \OC_Helper::maxUploadFilesize($dir, $free);
if (is_null($free) || $free < 0) {
$free = self::freeSpace($dir);
}
return min($free, self::uploadLimit());
}

/**
Expand All @@ -527,7 +546,13 @@ public static function maxUploadFilesize(string $dir, int|float|null $free = nul
* @since 7.0.0
*/
public static function freeSpace(string $dir): int|float {
return \OC_Helper::freeSpace($dir);
$freeSpace = \OC\Files\Filesystem::free_space($dir);
if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) {
$freeSpace = max($freeSpace, 0);
return $freeSpace;
} else {
return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
}
}

/**
Expand All @@ -537,7 +562,16 @@ public static function freeSpace(string $dir): int|float {
* @since 7.0.0
*/
public static function uploadLimit(): int|float {
return \OC_Helper::uploadLimit();
$ini = Server::get(IniGetWrapper::class);
$upload_max_filesize = self::computerFileSize($ini->get('upload_max_filesize')) ?: 0;
$post_max_size = self::computerFileSize($ini->get('post_max_size')) ?: 0;
if ($upload_max_filesize === 0 && $post_max_size === 0) {
return INF;
} elseif ($upload_max_filesize === 0 || $post_max_size === 0) {
return max($upload_max_filesize, $post_max_size); //only the non 0 value counts
} else {
return min($upload_max_filesize, $post_max_size);
}
}

/**
Expand Down
Loading