2014-02-09 01:25:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:07:57 +02:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-10-25 12:01:03 +02:00
|
|
|
* @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 17:07:57 +02:00
|
|
|
*
|
2015-03-26 11:44:34 +01:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2020-03-31 10:49:10 +02:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-08-24 14:54:25 +02:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 19:56:05 +02:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2019-12-03 19:57:53 +01:00
|
|
|
* @author marco44 <cousinmarc@gmail.com>
|
2015-03-26 11:44:34 +01:00
|
|
|
* @author Michael Roitzsch <reactorcontrol@icloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 19:57:53 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 11:44:34 +01:00
|
|
|
*
|
2014-02-09 01:25:33 +01:00
|
|
|
*/
|
|
|
|
namespace OC;
|
|
|
|
|
2020-08-20 14:08:18 +02:00
|
|
|
use bantu\IniGetWrapper\IniGetWrapper;
|
|
|
|
|
2014-02-09 01:25:33 +01:00
|
|
|
/**
|
|
|
|
* Helper class for large files on 32-bit platforms.
|
|
|
|
*/
|
|
|
|
class LargeFileHelper {
|
2014-02-15 23:41:58 +01:00
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* pow(2, 53) as a base-10 string.
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-10 16:54:27 +02:00
|
|
|
public const POW_2_53 = '9007199254740992';
|
2014-02-15 23:41:58 +01:00
|
|
|
|
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* pow(2, 53) - 1 as a base-10 string.
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-10 16:54:27 +02:00
|
|
|
public const POW_2_53_MINUS_1 = '9007199254740991';
|
2014-02-15 23:41:58 +01:00
|
|
|
|
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* @brief Checks whether our assumptions hold on the PHP platform we are on.
|
|
|
|
*
|
2022-11-07 13:40:30 +01:00
|
|
|
* @throws \RuntimeException if our assumptions do not hold on the current
|
2020-04-08 22:24:54 +02:00
|
|
|
* PHP platform.
|
|
|
|
*/
|
2014-02-15 23:41:58 +01:00
|
|
|
public function __construct() {
|
2018-01-25 23:06:53 +01:00
|
|
|
$pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0;
|
2014-02-15 23:41:58 +01:00
|
|
|
if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) {
|
2016-10-25 11:37:16 +02:00
|
|
|
throw new \RuntimeException(
|
2014-02-15 23:41:58 +01:00
|
|
|
'This class assumes floats to be double precision or "better".'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 23:21:23 +01:00
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* @brief Formats a signed integer or float as an unsigned integer base-10
|
|
|
|
* string. Passed strings will be checked for being base-10.
|
|
|
|
*
|
|
|
|
* @param int|float|string $number Number containing unsigned integer data
|
|
|
|
*
|
|
|
|
* @throws \UnexpectedValueException if $number is not a float, not an int
|
|
|
|
* and not a base-10 string.
|
|
|
|
*
|
|
|
|
* @return string Unsigned integer base-10 string
|
|
|
|
*/
|
2023-01-20 18:02:45 +01:00
|
|
|
public function formatUnsignedInteger(int|float|string $number): string {
|
2014-02-15 23:21:23 +01:00
|
|
|
if (is_float($number)) {
|
|
|
|
// Undo the effect of the php.ini setting 'precision'.
|
|
|
|
return number_format($number, 0, '', '');
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (is_string($number) && ctype_digit($number)) {
|
2014-02-15 23:21:23 +01:00
|
|
|
return $number;
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (is_int($number)) {
|
2014-02-15 23:21:23 +01:00
|
|
|
// Interpret signed integer as unsigned integer.
|
|
|
|
return sprintf('%u', $number);
|
|
|
|
} else {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Expected int, float or base-10 string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 01:25:33 +01:00
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* @brief Tries to get the size of a file via various workarounds that
|
|
|
|
* even work for large files on 32-bit platforms.
|
|
|
|
*
|
|
|
|
* @param string $filename Path to the file.
|
|
|
|
*
|
2023-03-30 10:30:12 +02:00
|
|
|
* @return int|float Number of bytes as number (float or int)
|
2020-04-08 22:24:54 +02:00
|
|
|
*/
|
2023-03-30 10:30:12 +02:00
|
|
|
public function getFileSize(string $filename): int|float {
|
2014-03-16 19:54:05 +01:00
|
|
|
$fileSize = $this->getFileSizeViaCurl($filename);
|
|
|
|
if (!is_null($fileSize)) {
|
|
|
|
return $fileSize;
|
2014-02-09 01:25:33 +01:00
|
|
|
}
|
2014-03-16 19:54:05 +01:00
|
|
|
$fileSize = $this->getFileSizeViaExec($filename);
|
|
|
|
if (!is_null($fileSize)) {
|
|
|
|
return $fileSize;
|
2014-02-09 01:25:33 +01:00
|
|
|
}
|
2014-03-16 19:54:05 +01:00
|
|
|
return $this->getFileSizeNative($filename);
|
2014-02-09 01:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* @brief Tries to get the size of a file via a CURL HEAD request.
|
|
|
|
*
|
|
|
|
* @param string $fileName Path to the file.
|
|
|
|
*
|
|
|
|
* @return null|int|float Number of bytes as number (float or int) or
|
|
|
|
* null on failure.
|
|
|
|
*/
|
2023-01-20 18:02:45 +01:00
|
|
|
public function getFileSizeViaCurl(string $fileName): null|int|float {
|
2020-08-20 14:08:18 +02:00
|
|
|
if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') {
|
2015-03-12 21:43:41 +01:00
|
|
|
$encodedFileName = rawurlencode($fileName);
|
2018-05-16 12:59:58 +02:00
|
|
|
$ch = curl_init("file:///$encodedFileName");
|
2014-02-09 01:25:33 +01:00
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
|
|
$data = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($data !== false) {
|
2020-03-26 09:30:18 +01:00
|
|
|
$matches = [];
|
2014-02-09 01:25:33 +01:00
|
|
|
preg_match('/Content-Length: (\d+)/', $data, $matches);
|
|
|
|
if (isset($matches[1])) {
|
|
|
|
return 0 + $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* @brief Tries to get the size of a file via an exec() call.
|
|
|
|
*
|
|
|
|
* @param string $filename Path to the file.
|
|
|
|
*
|
|
|
|
* @return null|int|float Number of bytes as number (float or int) or
|
|
|
|
* null on failure.
|
|
|
|
*/
|
2023-01-20 18:02:45 +01:00
|
|
|
public function getFileSizeViaExec(string $filename): null|int|float {
|
2022-08-18 10:47:49 +02:00
|
|
|
if (\OCP\Util::isFunctionEnabled('exec')) {
|
2014-02-09 01:25:33 +01:00
|
|
|
$os = strtolower(php_uname('s'));
|
2014-02-09 16:16:01 +01:00
|
|
|
$arg = escapeshellarg($filename);
|
2014-08-07 15:41:58 +02:00
|
|
|
$result = null;
|
2023-05-15 13:47:19 +02:00
|
|
|
if (str_contains($os, 'linux')) {
|
2014-02-09 16:16:01 +01:00
|
|
|
$result = $this->exec("stat -c %s $arg");
|
2023-05-15 13:47:19 +02:00
|
|
|
} elseif (str_contains($os, 'bsd') || str_contains($os, 'darwin')) {
|
2014-02-09 16:16:01 +01:00
|
|
|
$result = $this->exec("stat -f %z $arg");
|
2014-02-09 01:25:33 +01:00
|
|
|
}
|
2014-02-09 16:16:01 +01:00
|
|
|
return $result;
|
2014-02-09 01:25:33 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-09 16:16:01 +01:00
|
|
|
|
2014-02-18 12:57:44 +01:00
|
|
|
/**
|
2020-04-08 22:24:54 +02:00
|
|
|
* @brief Gets the size of a file via a filesize() call and converts
|
|
|
|
* negative signed int to positive float. As the result of filesize()
|
|
|
|
* will wrap around after a file size of 2^32 bytes = 4 GiB, this
|
|
|
|
* should only be used as a last resort.
|
|
|
|
*
|
|
|
|
* @param string $filename Path to the file.
|
|
|
|
*
|
|
|
|
* @return int|float Number of bytes as number (float or int).
|
|
|
|
*/
|
2023-01-20 18:02:45 +01:00
|
|
|
public function getFileSizeNative(string $filename): int|float {
|
2014-02-18 12:57:44 +01:00
|
|
|
$result = filesize($filename);
|
|
|
|
if ($result < 0) {
|
2014-03-16 19:54:05 +01:00
|
|
|
// For file sizes between 2 GiB and 4 GiB, filesize() will return a
|
2014-02-18 12:57:44 +01:00
|
|
|
// negative int, as the PHP data type int is signed. Interpret the
|
|
|
|
// returned int as an unsigned integer and put it into a float.
|
|
|
|
return (float) sprintf('%u', $result);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-10-25 11:42:16 +02:00
|
|
|
/**
|
|
|
|
* Returns the current mtime for $fullPath
|
|
|
|
*/
|
2023-01-20 18:02:45 +01:00
|
|
|
public function getFileMtime(string $fullPath): int {
|
2018-05-16 12:59:58 +02:00
|
|
|
try {
|
2023-01-23 09:55:03 +01:00
|
|
|
$result = filemtime($fullPath) ?: -1;
|
2018-05-16 12:59:58 +02:00
|
|
|
} catch (\Exception $e) {
|
2020-10-05 15:12:57 +02:00
|
|
|
$result = - 1;
|
2018-05-16 12:59:58 +02:00
|
|
|
}
|
|
|
|
if ($result < 0) {
|
2022-08-18 10:47:49 +02:00
|
|
|
if (\OCP\Util::isFunctionEnabled('exec')) {
|
2018-05-16 12:59:58 +02:00
|
|
|
$os = strtolower(php_uname('s'));
|
2023-05-15 13:47:19 +02:00
|
|
|
if (str_contains($os, 'linux')) {
|
2023-01-23 09:55:03 +01:00
|
|
|
return (int)($this->exec('stat -c %Y ' . escapeshellarg($fullPath)) ?? -1);
|
2018-05-16 12:59:58 +02:00
|
|
|
}
|
2016-10-25 12:01:03 +02:00
|
|
|
}
|
2016-10-25 11:42:16 +02:00
|
|
|
}
|
2018-05-16 12:59:58 +02:00
|
|
|
return $result;
|
2016-10-25 11:42:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 18:02:45 +01:00
|
|
|
protected function exec(string $cmd): null|int|float {
|
2014-02-09 16:16:01 +01:00
|
|
|
$result = trim(exec($cmd));
|
|
|
|
return ctype_digit($result) ? 0 + $result : null;
|
|
|
|
}
|
2014-02-09 01:25:33 +01:00
|
|
|
}
|