2014-02-09 04:25:33 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 13:37:37 +03:00
|
|
|
* Copyright (c) 2014 Andreas Fischer <bantu@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
2014-02-09 04:25:33 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-02-09 04:25:33 +04:00
|
|
|
namespace OC;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for large files on 32-bit platforms.
|
|
|
|
*/
|
|
|
|
class LargeFileHelper {
|
2014-02-16 02:41:58 +04:00
|
|
|
/**
|
|
|
|
* pow(2, 53) as a base-10 string.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const POW_2_53 = '9007199254740992';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pow(2, 53) - 1 as a base-10 string.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const POW_2_53_MINUS_1 = '9007199254740991';
|
|
|
|
|
|
|
|
/**
|
2014-03-16 23:05:06 +04:00
|
|
|
* @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.
|
2014-02-16 02:41:58 +04:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$pow_2_53 = floatval(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".'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 02:21:23 +04: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
|
|
|
|
*
|
2014-03-16 23:05:06 +04:00
|
|
|
* @throws \UnexpectedValueException if $number is not a float, not an int
|
|
|
|
* and not a base-10 string.
|
|
|
|
*
|
2014-02-16 02:21:23 +04:00
|
|
|
* @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, '', '');
|
|
|
|
} else if (is_string($number) && ctype_digit($number)) {
|
|
|
|
return $number;
|
|
|
|
} else if (is_int($number)) {
|
|
|
|
// Interpret signed integer as unsigned integer.
|
|
|
|
return sprintf('%u', $number);
|
|
|
|
} else {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Expected int, float or base-10 string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 04:25:33 +04:00
|
|
|
/**
|
2014-03-16 22:54:05 +04:00
|
|
|
* @brief Tries to get the size of a file via various workarounds that
|
2014-02-09 04:25:33 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-03-16 22:54:05 +04:00
|
|
|
public function getFileSize($filename) {
|
|
|
|
$fileSize = $this->getFileSizeViaCurl($filename);
|
|
|
|
if (!is_null($fileSize)) {
|
|
|
|
return $fileSize;
|
2014-02-09 04:25:33 +04:00
|
|
|
}
|
2014-03-16 22:54:05 +04:00
|
|
|
$fileSize = $this->getFileSizeViaCOM($filename);
|
|
|
|
if (!is_null($fileSize)) {
|
|
|
|
return $fileSize;
|
2014-02-09 04:25:33 +04:00
|
|
|
}
|
2014-03-16 22:54:05 +04:00
|
|
|
$fileSize = $this->getFileSizeViaExec($filename);
|
|
|
|
if (!is_null($fileSize)) {
|
|
|
|
return $fileSize;
|
2014-02-09 04:25:33 +04:00
|
|
|
}
|
2014-03-16 22:54:05 +04:00
|
|
|
return $this->getFileSizeNative($filename);
|
2014-02-09 04:25:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-16 22:54:05 +04:00
|
|
|
* @brief Tries to get the size of a file via a CURL HEAD request.
|
2014-02-09 04:25:33 +04:00
|
|
|
*
|
2015-03-12 23:43:41 +03:00
|
|
|
* @param string $fileName Path to the file.
|
2014-02-09 04:25:33 +04:00
|
|
|
*
|
|
|
|
* @return null|int|float Number of bytes as number (float or int) or
|
|
|
|
* null on failure.
|
|
|
|
*/
|
2015-03-12 23:43:41 +03:00
|
|
|
public function getFileSizeViaCurl($fileName) {
|
|
|
|
if (\OC::$server->getIniWrapper()->getString('open_basedir') === '') {
|
|
|
|
$encodedFileName = rawurlencode($fileName);
|
|
|
|
$ch = curl_init("file://$encodedFileName");
|
2014-02-09 04:25:33 +04: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) {
|
|
|
|
$matches = array();
|
|
|
|
preg_match('/Content-Length: (\d+)/', $data, $matches);
|
|
|
|
if (isset($matches[1])) {
|
|
|
|
return 0 + $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-16 22:54:05 +04:00
|
|
|
* @brief Tries to get the size of a file via the Windows DOM extension.
|
2014-02-09 04:25:33 +04:00
|
|
|
*
|
|
|
|
* @param string $filename Path to the file.
|
|
|
|
*
|
|
|
|
* @return null|int|float Number of bytes as number (float or int) or
|
|
|
|
* null on failure.
|
|
|
|
*/
|
2014-03-16 22:54:05 +04:00
|
|
|
public function getFileSizeViaCOM($filename) {
|
2014-02-09 04:25:33 +04:00
|
|
|
if (class_exists('COM')) {
|
2015-03-12 23:43:41 +03:00
|
|
|
$fsObj = new \COM("Scripting.FileSystemObject");
|
|
|
|
$file = $fsObj->GetFile($filename);
|
2014-02-09 04:25:33 +04:00
|
|
|
return 0 + $file->Size;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-16 22:54:05 +04:00
|
|
|
* @brief Tries to get the size of a file via an exec() call.
|
2014-02-09 04:25:33 +04:00
|
|
|
*
|
|
|
|
* @param string $filename Path to the file.
|
|
|
|
*
|
|
|
|
* @return null|int|float Number of bytes as number (float or int) or
|
|
|
|
* null on failure.
|
|
|
|
*/
|
2014-03-16 22:54:05 +04:00
|
|
|
public function getFileSizeViaExec($filename) {
|
2014-02-09 04:25:33 +04:00
|
|
|
if (\OC_Helper::is_function_enabled('exec')) {
|
|
|
|
$os = strtolower(php_uname('s'));
|
2014-02-09 19:16:01 +04:00
|
|
|
$arg = escapeshellarg($filename);
|
2014-08-07 17:41:58 +04:00
|
|
|
$result = null;
|
2014-02-09 04:25:33 +04:00
|
|
|
if (strpos($os, 'linux') !== false) {
|
2014-02-09 19:16:01 +04:00
|
|
|
$result = $this->exec("stat -c %s $arg");
|
2014-10-14 14:58:00 +04:00
|
|
|
} else if (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) {
|
2014-02-09 19:16:01 +04:00
|
|
|
$result = $this->exec("stat -f %z $arg");
|
|
|
|
} else if (strpos($os, 'win') !== false) {
|
|
|
|
$result = $this->exec("for %F in ($arg) do @echo %~zF");
|
|
|
|
if (is_null($result)) {
|
|
|
|
// PowerShell
|
|
|
|
$result = $this->exec("(Get-Item $arg).length");
|
|
|
|
}
|
2014-02-09 04:25:33 +04:00
|
|
|
}
|
2014-02-09 19:16:01 +04:00
|
|
|
return $result;
|
2014-02-09 04:25:33 +04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-09 19:16:01 +04:00
|
|
|
|
2014-02-18 15:57:44 +04:00
|
|
|
/**
|
2014-03-16 22:54:05 +04: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.
|
2014-02-18 15:57:44 +04:00
|
|
|
*
|
|
|
|
* @param string $filename Path to the file.
|
|
|
|
*
|
|
|
|
* @return int|float Number of bytes as number (float or int).
|
|
|
|
*/
|
2014-03-16 22:54:05 +04:00
|
|
|
public function getFileSizeNative($filename) {
|
2014-02-18 15:57:44 +04:00
|
|
|
$result = filesize($filename);
|
|
|
|
if ($result < 0) {
|
2014-03-16 22:54:05 +04:00
|
|
|
// For file sizes between 2 GiB and 4 GiB, filesize() will return a
|
2014-02-18 15:57:44 +04: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;
|
|
|
|
}
|
|
|
|
|
2014-02-09 19:16:01 +04:00
|
|
|
protected function exec($cmd) {
|
|
|
|
$result = trim(exec($cmd));
|
|
|
|
return ctype_digit($result) ? 0 + $result : null;
|
|
|
|
}
|
2014-02-09 04:25:33 +04:00
|
|
|
}
|