HtaccessGetFile.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('FoldersConfig');
  4. Lib::loadClass('FileStorage');
  5. class Route_HtaccessGetFile extends RouteBase {
  6. public function handleAuth() {
  7. if (!User::logged()) {
  8. throw new HttpException('Unauthorized', 401);
  9. }
  10. }
  11. public function defaultAction() {
  12. try {
  13. $file = V::get('file', '', $_GET);
  14. if (!$file) throw new Exception('Empty parameter');
  15. if (preg_grep('/^\./', explode('/', $file))) throw new Exception("Access denied (dot at the beginning of each path's element not allowed) - {$file}");
  16. $file = FoldersConfig::getRootPoint('mount_point') . DIRECTORY_SEPARATOR . $file;
  17. if (!file_exists($file)) throw new Exception("File not found - {$file}");
  18. if (!is_file($file)) throw new Exception("It's not a file - {$file}");
  19. if (!is_readable($file)) throw new Exception("Cannot read file - {$file}");
  20. $fileType = FileStorage::getMimeType($file);
  21. $fileName = basename($file);
  22. $fileSize = filesize($file);
  23. header("Content-Type: {$fileType}");
  24. header("Content-Disposition: filename={$fileName};");
  25. header("Content-Transfer-Encoding: binary");
  26. header("Content-Length: {$fileSize}");
  27. echo file_get_contents($file);
  28. } catch (Exception $e) {
  29. error_log($e->getMessage());
  30. header('Location: ' . Request::getPathUri());
  31. }
  32. }
  33. }