summaryrefslogtreecommitdiffstats
path: root/lib/private/largefilehelper.php
blob: 08869d7c82a80ef03a334222620a014105204ff0 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
 * 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.
 */

namespace OC;

/**
 * Helper class for large files on 32-bit platforms.
 */
class LargeFileHelper {
	/**
	* 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';

	/**
	* @brief Constructor. Checks whether our assumptions hold on the platform
	*        we are on, throws an exception if they do not hold.
	*/
	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".'
			);
		}
	}

	/**
	* @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
	*
	* @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'
			);
		}
	}

	/**
	* @brief Tries to get the filesize 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->getFilesizeViaCOM($filename);
		if (!is_null($filesize)) {
			return $filesize;
		}
		$filesize = $this->getFilesizeViaExec($filename);
		if (!is_null($filesize)) {
			return $filesize;
		}
		return null;
	}

	/**
	* @brief Tries to get the filesize 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 (function_exists('curl_init')) {
			$ch = curl_init("file://$filename");
			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;
	}

	/**
	* @brief Tries to get the filesize of a file via the Windows DOM extension.
	*
	* @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 getFilesizeViaCOM($filename) {
		if (class_exists('COM')) {
			$fsobj = new \COM("Scripting.FileSystemObject");
			$file = $fsobj->GetFile($filename);
			return 0 + $file->Size;
		}
		return null;
	}

	/**
	* @brief Tries to get the filesize 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.
	*/
	public function getFilesizeViaExec($filename) {
		if (\OC_Helper::is_function_enabled('exec')) {
			$os = strtolower(php_uname('s'));
			$arg = escapeshellarg($filename);
			$result = '';
			if (strpos($os, 'linux') !== false) {
				$result = $this->exec("stat -c %s $arg");
			} else if (strpos($os, 'bsd') !== false) {
				$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");
				}
			}
			return $result;
		}
		return null;
	}

	protected function exec($cmd) {
		$result = trim(exec($cmd));
		return ctype_digit($result) ? 0 + $result : null;
	}
}