123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- Use Model\Document;
- Use Model\Course;
- KeyAuth::enable();
- require_once __DIR__ . '/../inc/global.inc.php';
- $has_access = api_protect_course_script();
- if (!$has_access) {
- exit;
- }
- session_cache_limiter('none');
- $ids = Request::get('id', '');
- $ids = $ids ? explode(',', $ids) : array();
- $course = Course::current();
- if (count($ids) == 0 || empty($course)) {
- Response::not_found();
- }
- if (count($ids) == 1) {
- $id = reset($ids);
- $doc = Document::get_by_id($course, $id);
- if (empty($doc)) {
- Response::not_found();
- }
- if ($doc->is_file()) {
- $has_access = $doc->is_accessible();
- if (!$has_access) {
- Response::not_found();
- }
- event_download(Uri::here());
- DocumentManager::file_send_for_download($doc);
- exit;
- }
- }
- $files = array();
- $folders = array();
- foreach ($ids as $id) {
- $doc = Document::get_by_id($course, $id);
- if (!$doc->is_accessible()) {
- break;
- }
- if ($doc->is_file()) {
- $files[] = $doc;
- }
- if ($doc->is_folder()) {
- $folders[] = $doc;
- }
- }
- $requested_folders = $folders;
- while ($folders) {
- $items = $folders;
- $folders = array();
- foreach ($items as $item) {
- $children = $item->get_children();
- foreach ($children as $child) {
- if (!$child->is_accessible()) {
- break;
- }
- if ($child->is_file()) {
- $files[] = $child;
- }
- if ($child->is_folder()) {
- $folders[] = $child;
- }
- }
- }
- }
- $folders = $requested_folders;
- if (count($files) == 0) {
- Response::not_found();
- }
- $root_dir = '';
- $items = array_merge($folders, $files);
- foreach ($items as $item) {
- $path = $item->get_absolute_path();
- $path = realpath($path);
- $dir = dirname($path);
- if (empty($root_dir) || strlen($root_dir) > strlen($dir)) {
- $root_dir = $dir;
- }
- }
- $temp_zip_path = Chamilo::temp_file('zip');
- $zip_folder = new PclZip($temp_zip_path);
- foreach ($files as $file) {
- if (empty($root_dir)) {
- $root_dir = dirname($file);
- }
- $file = (string) $file;
- $zip_folder->add($file, PCLZIP_OPT_REMOVE_PATH, $root_dir);
- }
- event_download(Uri::here());
- DocumentManager::file_send_for_download($temp_zip_path, false, get_lang('Documents') . '.zip');
|