files.php 7.8 KB

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