files.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /**
  23. * Class for fileserver access
  24. *
  25. */
  26. class OC_Files {
  27. static $tmpFiles = array();
  28. static public function getFileInfo($path){
  29. return \OC\Files\Filesystem::getFileInfo($path);
  30. }
  31. static public function getDirectoryContent($path){
  32. return \OC\Files\Filesystem::getDirectoryContent($path);
  33. }
  34. /**
  35. * return the content of a file or return a zip file containing multiple files
  36. *
  37. * @param string $dir
  38. * @param string $file ; separated list of files to download
  39. * @param boolean $only_header ; boolean to only send header of the request
  40. */
  41. public static function get($dir, $files, $only_header = false) {
  42. $xsendfile = false;
  43. if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) ||
  44. isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
  45. $xsendfile = true;
  46. }
  47. if(strpos($files, ';')) {
  48. $files=explode(';', $files);
  49. }
  50. if (is_array($files)) {
  51. self::validateZipDownload($dir, $files);
  52. $executionTime = intval(ini_get('max_execution_time'));
  53. set_time_limit(0);
  54. $zip = new ZipArchive();
  55. if ($xsendfile) {
  56. $filename = OC_Helper::tmpFileNoClean('.zip');
  57. }else{
  58. $filename = OC_Helper::tmpFile('.zip');
  59. }
  60. if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) {
  61. exit("cannot open <$filename>\n");
  62. }
  63. foreach ($files as $file) {
  64. $file = $dir . '/' . $file;
  65. if (\OC\Files\Filesystem::is_file($file)) {
  66. $tmpFile = \OC\Files\Filesystem::toTmpFile($file);
  67. self::$tmpFiles[] = $tmpFile;
  68. $zip->addFile($tmpFile, basename($file));
  69. } elseif (\OC\Files\Filesystem::is_dir($file)) {
  70. self::zipAddDir($file, $zip);
  71. }
  72. }
  73. $zip->close();
  74. $name = basename($dir) . '.zip';
  75. set_time_limit($executionTime);
  76. } elseif (\OC\Files\Filesystem::is_dir($dir . '/' . $files)) {
  77. self::validateZipDownload($dir, $files);
  78. $executionTime = intval(ini_get('max_execution_time'));
  79. set_time_limit(0);
  80. $zip = new ZipArchive();
  81. if ($xsendfile) {
  82. $filename = OC_Helper::tmpFileNoClean('.zip');
  83. }else{
  84. $filename = OC_Helper::tmpFile('.zip');
  85. }
  86. if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) {
  87. exit("cannot open <$filename>\n");
  88. }
  89. $file = $dir . '/' . $files;
  90. self::zipAddDir($file, $zip);
  91. $zip->close();
  92. $name = $files . '.zip';
  93. set_time_limit($executionTime);
  94. } else {
  95. $zip = false;
  96. $filename = $dir . '/' . $files;
  97. $name = $files;
  98. }
  99. OC_Util::obEnd();
  100. if ($zip or \OC\Files\Filesystem::isReadable($filename)) {
  101. if ( preg_match( "/MSIE/", $_SERVER["HTTP_USER_AGENT"] ) ) {
  102. header( 'Content-Disposition: attachment; filename="' . rawurlencode($name) . '"' );
  103. } else {
  104. header( 'Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode($name)
  105. . '; filename="' . rawurlencode($name) . '"' );
  106. }
  107. header('Content-Transfer-Encoding: binary');
  108. OC_Response::disableCaching();
  109. if ($zip) {
  110. ini_set('zlib.output_compression', 'off');
  111. header('Content-Type: application/zip');
  112. header('Content-Length: ' . filesize($filename));
  113. self::addSendfileHeader($filename);
  114. }else{
  115. header('Content-Type: '.\OC\Files\Filesystem::getMimeType($filename));
  116. header("Content-Length: ".\OC\Files\Filesystem::filesize($filename));
  117. list($storage) = \OC\Files\Filesystem::resolvePath($filename);
  118. if ($storage instanceof \OC\File\Storage\Local) {
  119. self::addSendfileHeader(\OC\Files\Filesystem::getLocalFile($filename));
  120. }
  121. }
  122. } elseif ($zip or !\OC\Files\Filesystem::file_exists($filename)) {
  123. header("HTTP/1.0 404 Not Found");
  124. $tmpl = new OC_Template('', '404', 'guest');
  125. $tmpl->assign('file', $name);
  126. $tmpl->printPage();
  127. } else {
  128. header("HTTP/1.0 403 Forbidden");
  129. die('403 Forbidden');
  130. }
  131. if($only_header) {
  132. return ;
  133. }
  134. if ($zip) {
  135. $handle = fopen($filename, 'r');
  136. if ($handle) {
  137. $chunkSize = 8 * 1024; // 1 MB chunks
  138. while (!feof($handle)) {
  139. echo fread($handle, $chunkSize);
  140. flush();
  141. }
  142. }
  143. if (!$xsendfile) {
  144. unlink($filename);
  145. }
  146. }else{
  147. \OC\Files\Filesystem::readfile($filename);
  148. }
  149. foreach (self::$tmpFiles as $tmpFile) {
  150. if (file_exists($tmpFile) and is_file($tmpFile)) {
  151. unlink($tmpFile);
  152. }
  153. }
  154. }
  155. private static function addSendfileHeader($filename) {
  156. if (isset($_SERVER['MOD_X_SENDFILE_ENABLED'])) {
  157. header("X-Sendfile: " . $filename);
  158. }
  159. if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
  160. header("X-Accel-Redirect: " . $filename);
  161. }
  162. }
  163. public static function zipAddDir($dir, $zip, $internalDir='') {
  164. $dirname=basename($dir);
  165. $zip->addEmptyDir($internalDir.$dirname);
  166. $internalDir.=$dirname.='/';
  167. $files=OC_Files::getDirectoryContent($dir);
  168. foreach($files as $file) {
  169. $filename=$file['name'];
  170. $file=$dir.'/'.$filename;
  171. if(\OC\Files\Filesystem::is_file($file)) {
  172. $tmpFile=\OC\Files\Filesystem::toTmpFile($file);
  173. OC_Files::$tmpFiles[]=$tmpFile;
  174. $zip->addFile($tmpFile, $internalDir.$filename);
  175. }elseif(\OC\Files\Filesystem::is_dir($file)) {
  176. self::zipAddDir($file, $zip, $internalDir);
  177. }
  178. }
  179. }
  180. /**
  181. * checks if the selected files are within the size constraint. If not, outputs an error page.
  182. *
  183. * @param dir $dir
  184. * @param files $files
  185. */
  186. static function validateZipDownload($dir, $files) {
  187. if (!OC_Config::getValue('allowZipDownload', true)) {
  188. $l = OC_L10N::get('lib');
  189. header("HTTP/1.0 409 Conflict");
  190. $tmpl = new OC_Template('', 'error', 'user');
  191. $errors = array(
  192. array(
  193. 'error' => $l->t('ZIP download is turned off.'),
  194. 'hint' => $l->t('Files need to be downloaded one by one.') . '<br/><a href="javascript:history.back()">' . $l->t('Back to Files') . '</a>',
  195. )
  196. );
  197. $tmpl->assign('errors', $errors);
  198. $tmpl->printPage();
  199. exit;
  200. }
  201. $zipLimit = OC_Config::getValue('maxZipInputSize', OC_Helper::computerFileSize('800 MB'));
  202. if ($zipLimit > 0) {
  203. $totalsize = 0;
  204. if (is_array($files)) {
  205. foreach ($files as $file) {
  206. $totalsize += \OC\Files\Filesystem::filesize($dir . '/' . $file);
  207. }
  208. } else {
  209. $totalsize += \OC\Files\Filesystem::filesize($dir . '/' . $files);
  210. }
  211. if ($totalsize > $zipLimit) {
  212. $l = OC_L10N::get('lib');
  213. header("HTTP/1.0 409 Conflict");
  214. $tmpl = new OC_Template('', 'error', 'user');
  215. $errors = array(
  216. array(
  217. 'error' => $l->t('Selected files too large to generate zip file.'),
  218. 'hint' => 'Download the files in smaller chunks, seperately or kindly ask your administrator.<br/><a href="javascript:history.back()">' . $l->t('Back to Files') . '</a>',
  219. )
  220. );
  221. $tmpl->assign('errors', $errors);
  222. $tmpl->printPage();
  223. exit;
  224. }
  225. }
  226. }
  227. /**
  228. * set the maximum upload size limit for apache hosts using .htaccess
  229. *
  230. * @param int size filesisze in bytes
  231. * @return false on failure, size on success
  232. */
  233. static function setUploadLimit($size) {
  234. //don't allow user to break his config -- upper boundary
  235. if ($size > PHP_INT_MAX) {
  236. //max size is always 1 byte lower than computerFileSize returns
  237. if ($size > PHP_INT_MAX + 1)
  238. return false;
  239. $size -= 1;
  240. } else {
  241. $size = OC_Helper::humanFileSize($size);
  242. $size = substr($size, 0, -1); //strip the B
  243. $size = str_replace(' ', '', $size); //remove the space between the size and the postfix
  244. }
  245. //don't allow user to break his config -- broken or malicious size input
  246. if (intval($size) == 0) {
  247. return false;
  248. }
  249. $htaccess = @file_get_contents(OC::$SERVERROOT . '/.htaccess'); //supress errors in case we don't have permissions for
  250. if (!$htaccess) {
  251. return false;
  252. }
  253. $phpValueKeys = array(
  254. 'upload_max_filesize',
  255. 'post_max_size'
  256. );
  257. foreach ($phpValueKeys as $key) {
  258. $pattern = '/php_value ' . $key . ' (\S)*/';
  259. $setting = 'php_value ' . $key . ' ' . $size;
  260. $hasReplaced = 0;
  261. $content = preg_replace($pattern, $setting, $htaccess, 1, $hasReplaced);
  262. if ($content !== null) {
  263. $htaccess = $content;
  264. }
  265. if ($hasReplaced == 0) {
  266. $htaccess .= "\n" . $setting;
  267. }
  268. }
  269. //check for write permissions
  270. if (is_writable(OC::$SERVERROOT . '/.htaccess')) {
  271. file_put_contents(OC::$SERVERROOT . '/.htaccess', $htaccess);
  272. return OC_Helper::computerFileSize($size);
  273. } else {
  274. OC_Log::write('files', 'Can\'t write upload limit to ' . OC::$SERVERROOT . '/.htaccess. Please check the file permissions', OC_Log::WARN);
  275. }
  276. return false;
  277. }
  278. }