largefilehelper.php 5.6 KB

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