files.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // TODO: get rid of this using proper composer packages
  23. require_once 'mcnetic/phpzipstreamer/ZipStreamer.php';
  24. /**
  25. * Class for file server access
  26. *
  27. */
  28. class OC_Files {
  29. const FILE = 1;
  30. const ZIP_FILES = 2;
  31. const ZIP_DIR = 3;
  32. /**
  33. * @param string $filename
  34. * @param string $name
  35. * @param bool $zip
  36. */
  37. private static function sendHeaders($filename, $name, $zip = false) {
  38. OC_Response::setContentDispositionHeader($name, 'attachment');
  39. header('Content-Transfer-Encoding: binary');
  40. OC_Response::disableCaching();
  41. if ($zip) {
  42. header('Content-Type: application/zip');
  43. } else {
  44. $filesize = \OC\Files\Filesystem::filesize($filename);
  45. header('Content-Type: '.\OC_Helper::getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)));
  46. if ($filesize > -1) {
  47. header("Content-Length: ".$filesize);
  48. }
  49. }
  50. }
  51. /**
  52. * return the content of a file or return a zip file containing multiple files
  53. *
  54. * @param string $dir
  55. * @param string $files ; separated list of files to download
  56. * @param boolean $only_header ; boolean to only send header of the request
  57. */
  58. public static function get($dir, $files, $only_header = false) {
  59. $xsendfile = false;
  60. if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) ||
  61. isset($_SERVER['MOD_X_SENDFILE2_ENABLED']) ||
  62. isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
  63. $xsendfile = true;
  64. }
  65. if (is_array($files) && count($files) === 1) {
  66. $files = $files[0];
  67. }
  68. if (is_array($files)) {
  69. $get_type = self::ZIP_FILES;
  70. $basename = basename($dir);
  71. if ($basename) {
  72. $name = $basename . '.zip';
  73. } else {
  74. $name = 'download.zip';
  75. }
  76. $filename = $dir . '/' . $name;
  77. } else {
  78. $filename = $dir . '/' . $files;
  79. if (\OC\Files\Filesystem::is_dir($dir . '/' . $files)) {
  80. $get_type = self::ZIP_DIR;
  81. // downloading root ?
  82. if ($files === '') {
  83. $name = 'download.zip';
  84. } else {
  85. $name = $files . '.zip';
  86. }
  87. } else {
  88. $get_type = self::FILE;
  89. $name = $files;
  90. }
  91. }
  92. if ($get_type === self::FILE) {
  93. $zip = false;
  94. if ($xsendfile && OC_App::isEnabled('files_encryption')) {
  95. $xsendfile = false;
  96. }
  97. } else {
  98. $zip = new ZipStreamer(false);
  99. }
  100. OC_Util::obEnd();
  101. if ($zip or \OC\Files\Filesystem::isReadable($filename)) {
  102. self::sendHeaders($filename, $name, $zip);
  103. } elseif (!\OC\Files\Filesystem::file_exists($filename)) {
  104. header("HTTP/1.0 404 Not Found");
  105. $tmpl = new OC_Template('', '404', 'guest');
  106. $tmpl->printPage();
  107. } else {
  108. header("HTTP/1.0 403 Forbidden");
  109. die('403 Forbidden');
  110. }
  111. if($only_header) {
  112. return ;
  113. }
  114. if ($zip) {
  115. $executionTime = intval(ini_get('max_execution_time'));
  116. set_time_limit(0);
  117. if ($get_type === self::ZIP_FILES) {
  118. foreach ($files as $file) {
  119. $file = $dir . '/' . $file;
  120. if (\OC\Files\Filesystem::is_file($file)) {
  121. $fh = \OC\Files\Filesystem::fopen($file, 'r');
  122. $zip->addFileFromStream($fh, basename($file));
  123. fclose($fh);
  124. } elseif (\OC\Files\Filesystem::is_dir($file)) {
  125. self::zipAddDir($file, $zip);
  126. }
  127. }
  128. } elseif ($get_type === self::ZIP_DIR) {
  129. $file = $dir . '/' . $files;
  130. self::zipAddDir($file, $zip);
  131. }
  132. $zip->finalize();
  133. set_time_limit($executionTime);
  134. } else {
  135. if ($xsendfile) {
  136. $view = \OC\Files\Filesystem::getView();
  137. /** @var $storage \OC\Files\Storage\Storage */
  138. list($storage) = $view->resolvePath($filename);
  139. if ($storage->isLocal()) {
  140. self::addSendfileHeader($filename);
  141. } else {
  142. \OC\Files\Filesystem::readfile($filename);
  143. }
  144. } else {
  145. \OC\Files\Filesystem::readfile($filename);
  146. }
  147. }
  148. }
  149. /**
  150. * @param false|string $filename
  151. */
  152. private static function addSendfileHeader($filename) {
  153. $filename = \OC\Files\Filesystem::getLocalFile($filename);
  154. if (isset($_SERVER['MOD_X_SENDFILE_ENABLED'])) {
  155. header("X-Sendfile: " . $filename);
  156. }
  157. if (isset($_SERVER['MOD_X_SENDFILE2_ENABLED'])) {
  158. if (isset($_SERVER['HTTP_RANGE']) &&
  159. preg_match("/^bytes=([0-9]+)-([0-9]*)$/", $_SERVER['HTTP_RANGE'], $range)) {
  160. $filelength = filesize($filename);
  161. if ($range[2] === "") {
  162. $range[2] = $filelength - 1;
  163. }
  164. header("Content-Range: bytes $range[1]-$range[2]/" . $filelength);
  165. header("HTTP/1.1 206 Partial content");
  166. header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " $range[1]-$range[2]");
  167. } else {
  168. header("X-Sendfile: " . $filename);
  169. }
  170. }
  171. if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
  172. header("X-Accel-Redirect: " . $filename);
  173. }
  174. }
  175. /**
  176. * @param string $dir
  177. * @param ZipStreamer $zip
  178. * @param string $internalDir
  179. */
  180. public static function zipAddDir($dir, ZipStreamer $zip, $internalDir='') {
  181. $dirname=basename($dir);
  182. $rootDir = $internalDir.$dirname;
  183. if (!empty($rootDir)) {
  184. $zip->addEmptyDir($rootDir);
  185. }
  186. $internalDir.=$dirname.='/';
  187. // prevent absolute dirs
  188. $internalDir = ltrim($internalDir, '/');
  189. $files=\OC\Files\Filesystem::getDirectoryContent($dir);
  190. foreach($files as $file) {
  191. $filename=$file['name'];
  192. $file=$dir.'/'.$filename;
  193. if(\OC\Files\Filesystem::is_file($file)) {
  194. $fh = \OC\Files\Filesystem::fopen($file, 'r');
  195. $zip->addFileFromStream($fh, $internalDir.$filename);
  196. fclose($fh);
  197. }elseif(\OC\Files\Filesystem::is_dir($file)) {
  198. self::zipAddDir($file, $zip, $internalDir);
  199. }
  200. }
  201. }
  202. /**
  203. * set the maximum upload size limit for apache hosts using .htaccess
  204. *
  205. * @param int $size file size in bytes
  206. * @return bool false on failure, size on success
  207. */
  208. static function setUploadLimit($size) {
  209. //don't allow user to break his config -- upper boundary
  210. if ($size > PHP_INT_MAX) {
  211. //max size is always 1 byte lower than computerFileSize returns
  212. if ($size > PHP_INT_MAX + 1)
  213. return false;
  214. $size -= 1;
  215. } else {
  216. $size = OC_Helper::phpFileSize($size);
  217. }
  218. //don't allow user to break his config -- broken or malicious size input
  219. if (intval($size) === 0) {
  220. return false;
  221. }
  222. //suppress errors in case we don't have permissions for
  223. $htaccess = @file_get_contents(OC::$SERVERROOT . '/.htaccess');
  224. if (!$htaccess) {
  225. return false;
  226. }
  227. $phpValueKeys = array(
  228. 'upload_max_filesize',
  229. 'post_max_size'
  230. );
  231. foreach ($phpValueKeys as $key) {
  232. $pattern = '/php_value ' . $key . ' (\S)*/';
  233. $setting = 'php_value ' . $key . ' ' . $size;
  234. $hasReplaced = 0;
  235. $content = preg_replace($pattern, $setting, $htaccess, 1, $hasReplaced);
  236. if ($content !== null) {
  237. $htaccess = $content;
  238. }
  239. if ($hasReplaced === 0) {
  240. $htaccess .= "\n" . $setting;
  241. }
  242. }
  243. //check for write permissions
  244. if (is_writable(OC::$SERVERROOT . '/.htaccess')) {
  245. file_put_contents(OC::$SERVERROOT . '/.htaccess', $htaccess);
  246. return OC_Helper::computerFileSize($size);
  247. } else {
  248. OC_Log::write('files',
  249. 'Can\'t write upload limit to ' . OC::$SERVERROOT . '/.htaccess. Please check the file permissions',
  250. OC_Log::WARN);
  251. }
  252. return false;
  253. }
  254. }