You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

files.php 9.3KB

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