1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
*
* @author Andreas Fischer <bantu@owncloud.com>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author marco44 <cousinmarc@gmail.com>
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC;
use bantu\IniGetWrapper\IniGetWrapper;
/**
* Helper class for large files on 32-bit platforms.
*/
class LargeFileHelper {
/**
* pow(2, 53) as a base-10 string.
* @var string
*/
public const POW_2_53 = '9007199254740992';
/**
* pow(2, 53) - 1 as a base-10 string.
* @var string
*/
public const POW_2_53_MINUS_1 = '9007199254740991';
/**
* @brief Checks whether our assumptions hold on the PHP platform we are on.
*
* @throws \RunTimeException if our assumptions do not hold on the current
* PHP platform.
*/
public function __construct() {
$pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0;
if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) {
throw new \RuntimeException(
'This class assumes floats to be double precision or "better".'
);
}
}
/**
* @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
*/
public function formatUnsignedInteger($number) {
if (is_float($number)) {
// Undo the effect of the php.ini setting 'precision'.
return number_format($number, 0, '', '');
} elseif (is_string($number) && ctype_digit($number)) {
return $number;
} elseif (is_int($number)) {
// Interpret signed integer as unsigned integer.
return sprintf('%u', $number);
} else {
throw new \UnexpectedValueException(
'Expected int, float or base-10 string'
);
}
}
/**
* @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.
*
* @return null|int|float Number of bytes as number (float or int) or
* null on failure.
*/
public function getFileSize($filename) {
$fileSize = $this->getFileSizeViaCurl($filename);
if (!is_null($fileSize)) {
return $fileSize;
}
$fileSize = $this->getFileSizeViaExec($filename);
if (!is_null($fileSize)) {
return $fileSize;
}
return $this->getFileSizeNative($filename);
}
/**
* @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.
*/
public function getFileSizeViaCurl($fileName) {
if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') {
$encodedFileName = rawurlencode($fileName);
$ch = curl_init("file:///$encodedFileName");
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) {
$matches = [];
preg_match('/Content-Length: (\d+)/', $data, $matches);
if (isset($matches[1])) {
return 0 + $matches[1
|