largefilehelper.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Michael Roitzsch <reactorcontrol@icloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC;
  26. /**
  27. * Helper class for large files on 32-bit platforms.
  28. */
  29. class LargeFileHelper {
  30. /**
  31. * pow(2, 53) as a base-10 string.
  32. * @var string
  33. */
  34. const POW_2_53 = '9007199254740992';
  35. /**
  36. * pow(2, 53) - 1 as a base-10 string.
  37. * @var string
  38. */
  39. const POW_2_53_MINUS_1 = '9007199254740991';
  40. /**
  41. * @brief Checks whether our assumptions hold on the PHP platform we are on.
  42. *
  43. * @throws \RunTimeException if our assumptions do not hold on the current
  44. * PHP platform.
  45. */
  46. public function __construct() {
  47. $pow_2_53 = floatval(self::POW_2_53_MINUS_1) + 1.0;
  48. if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) {
  49. throw new \RunTimeException(
  50. 'This class assumes floats to be double precision or "better".'
  51. );
  52. }
  53. }
  54. /**
  55. * @brief Formats a signed integer or float as an unsigned integer base-10
  56. * string. Passed strings will be checked for being base-10.
  57. *
  58. * @param int|float|string $number Number containing unsigned integer data
  59. *
  60. * @throws \UnexpectedValueException if $number is not a float, not an int
  61. * and not a base-10 string.
  62. *
  63. * @return string Unsigned integer base-10 string
  64. */
  65. public function formatUnsignedInteger($number) {
  66. if (is_float($number)) {
  67. // Undo the effect of the php.ini setting 'precision'.
  68. return number_format($number, 0, '', '');
  69. } else if (is_string($number) && ctype_digit($number)) {
  70. return $number;
  71. } else if (is_int($number)) {
  72. // Interpret signed integer as unsigned integer.
  73. return sprintf('%u', $number);
  74. } else {
  75. throw new \UnexpectedValueException(
  76. 'Expected int, float or base-10 string'
  77. );
  78. }
  79. }
  80. /**
  81. * @brief Tries to get the size of a file via various workarounds that
  82. * even work for large files on 32-bit platforms.
  83. *
  84. * @param string $filename Path to the file.
  85. *
  86. * @return null|int|float Number of bytes as number (float or int) or
  87. * null on failure.
  88. */
  89. public function getFileSize($filename) {
  90. $fileSize = $this->getFileSizeViaCurl($filename);
  91. if (!is_null($fileSize)) {
  92. return $fileSize;
  93. }
  94. $fileSize = $this->getFileSizeViaCOM($filename);
  95. if (!is_null($fileSize)) {
  96. return $fileSize;
  97. }
  98. $fileSize = $this->getFileSizeViaExec($filename);
  99. if (!is_null($fileSize)) {
  100. return $fileSize;
  101. }
  102. return $this->getFileSizeNative($filename);
  103. }
  104. /**
  105. * @brief Tries to get the size of a file via a CURL HEAD request.
  106. *
  107. * @param string $fileName Path to the file.
  108. *
  109. * @return null|int|float Number of bytes as number (float or int) or
  110. * null on failure.
  111. */
  112. public function getFileSizeViaCurl($fileName) {
  113. if (\OC::$server->getIniWrapper()->getString('open_basedir') === '') {
  114. $encodedFileName = rawurlencode($fileName);
  115. $ch = curl_init("file://$encodedFileName");
  116. curl_setopt($ch, CURLOPT_NOBODY, true);
  117. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  118. curl_setopt($ch, CURLOPT_HEADER, true);
  119. $data = curl_exec($ch);
  120. curl_close($ch);
  121. if ($data !== false) {
  122. $matches = array();
  123. preg_match('/Content-Length: (\d+)/', $data, $matches);
  124. if (isset($matches[1])) {
  125. return 0 + $matches[1];
  126. }
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * @brief Tries to get the size of a file via the Windows DOM extension.
  133. *
  134. * @param string $filename Path to the file.
  135. *
  136. * @return null|int|float Number of bytes as number (float or int) or
  137. * null on failure.
  138. */
  139. public function getFileSizeViaCOM($filename) {
  140. if (class_exists('COM')) {
  141. $fsObj = new \COM("Scripting.FileSystemObject");
  142. $file = $fsObj->GetFile($filename);
  143. return 0 + $file->Size;
  144. }
  145. return null;
  146. }
  147. /**
  148. * @brief Tries to get the size of a file via an exec() call.
  149. *
  150. * @param string $filename Path to the file.
  151. *
  152. * @return null|int|float Number of bytes as number (float or int) or
  153. * null on failure.
  154. */
  155. public function getFileSizeViaExec($filename) {
  156. if (\OC_Helper::is_function_enabled('exec')) {
  157. $os = strtolower(php_uname('s'));
  158. $arg = escapeshellarg($filename);
  159. $result = null;
  160. if (strpos($os, 'linux') !== false) {
  161. $result = $this->exec("stat -c %s $arg");
  162. } else if (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) {
  163. $result = $this->exec("stat -f %z $arg");
  164. } else if (strpos($os, 'win') !== false) {
  165. $result = $this->exec("for %F in ($arg) do @echo %~zF");
  166. if (is_null($result)) {
  167. // PowerShell
  168. $result = $this->exec("(Get-Item $arg).length");
  169. }
  170. }
  171. return $result;
  172. }
  173. return null;
  174. }
  175. /**
  176. * @brief Gets the size of a file via a filesize() call and converts
  177. * negative signed int to positive float. As the result of filesize()
  178. * will wrap around after a file size of 2^32 bytes = 4 GiB, this
  179. * should only be used as a last resort.
  180. *
  181. * @param string $filename Path to the file.
  182. *
  183. * @return int|float Number of bytes as number (float or int).
  184. */
  185. public function getFileSizeNative($filename) {
  186. $result = filesize($filename);
  187. if ($result < 0) {
  188. // For file sizes between 2 GiB and 4 GiB, filesize() will return a
  189. // negative int, as the PHP data type int is signed. Interpret the
  190. // returned int as an unsigned integer and put it into a float.
  191. return (float) sprintf('%u', $result);
  192. }
  193. return $result;
  194. }
  195. protected function exec($cmd) {
  196. $result = trim(exec($cmd));
  197. return ctype_digit($result) ? 0 + $result : null;
  198. }
  199. }