downloadfolder.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Functions and main code for the download folder feature
  5. *
  6. * @package chamilo.document
  7. */
  8. use \ChamiloSession as Session;
  9. set_time_limit(0);
  10. require_once '../inc/global.inc.php';
  11. api_protect_course_script();
  12. $sysCoursePath = api_get_path(SYS_COURSE_PATH);
  13. $courseInfo = api_get_course_info();
  14. $courseId = api_get_course_int_id();
  15. $sessionId = api_get_session_id();
  16. $groupId = api_get_group_id();
  17. $courseCode = api_get_course_id();
  18. // Check if folder exists in current course.
  19. $documentInfo = DocumentManager::get_document_data_by_id(
  20. $_GET['id'],
  21. $courseCode,
  22. false,
  23. 0
  24. );
  25. if (!empty($sessionId)) {
  26. /* If no data found and session id exists
  27. try to look the file inside the session */
  28. if (empty($documentInfo)) {
  29. $documentInfo = DocumentManager::get_document_data_by_id(
  30. $_GET['id'],
  31. $courseCode,
  32. false,
  33. $sessionId
  34. );
  35. }
  36. }
  37. $path = $documentInfo['path'];
  38. if (empty($path)) {
  39. $path = '/';
  40. }
  41. // A student should not be able to download a root shared directory
  42. if (($path == '/shared_folder' ||
  43. $path == '/shared_folder_session_' . api_get_session_id()) &&
  44. (!api_is_allowed_to_edit() || !api_is_platform_admin())
  45. ) {
  46. api_not_allowed(true);
  47. exit;
  48. }
  49. // Zip library for creation of the zip file.
  50. require api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  51. // Creating a ZIP file.
  52. $tempZipFile = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  53. $zip = new PclZip($tempZipFile);
  54. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  55. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  56. // We need this path to clean it out of the zip file
  57. // I'm not using dir name as it gives too much problems (cfr.)
  58. $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
  59. // Put the files in the zip
  60. // 2 possibilities: Admins get all files and folders in the selected folder (except for the deleted ones)
  61. // Normal users get only visible files that are in visible folders
  62. function fixDocumentNameCallback($p_event, &$p_header)
  63. {
  64. global $remove_dir;
  65. $files = Session::read('doc_files_to_download');
  66. $storedFile = $remove_dir.$p_header['stored_filename'];
  67. if (!isset($files[$storedFile])) {
  68. return 0;
  69. }
  70. $documentData = $files[$storedFile];
  71. $documentNameFixed = DocumentManager::undoFixDocumentName(
  72. $documentData['path'],
  73. $documentData['c_id'],
  74. $documentData['id_session'],
  75. $documentData['to_group_id']
  76. );
  77. // Changes file.phps to file.php
  78. $basename = basename($documentNameFixed);
  79. $basenamePHPFixed = str_replace('.phps', '.php', $basename);
  80. $documentNameFixed = str_replace($basename, $basenamePHPFixed, $basenamePHPFixed);
  81. $p_header['stored_filename'] = $documentNameFixed;
  82. return 1;
  83. }
  84. // Admins are allowed to download invisible files
  85. if (api_is_allowed_to_edit()) {
  86. // Set the path that will be used in the query
  87. if ($path == '/') {
  88. $querypath = ''; // To prevent ...path LIKE '//%'... in query
  89. } else {
  90. $querypath = $path;
  91. }
  92. $querypath = Database::escape_string($querypath);
  93. // Search for all files that are not deleted => visibility != 2
  94. $sql = "SELECT path, id_session, docs.id, props.to_group_id, docs.c_id
  95. FROM $doc_table AS docs INNER JOIN $prop_table AS props
  96. ON
  97. docs.id = props.ref AND
  98. docs.c_id = props.c_id
  99. WHERE
  100. props.tool ='".TOOL_DOCUMENT."' AND
  101. docs.path LIKE '".$querypath."/%' AND
  102. docs.filetype = 'file' AND
  103. props.visibility <> '2' AND
  104. props.to_group_id = ".$groupId." AND
  105. props.id_session IN ('0', '$sessionId') AND
  106. docs.c_id = ".$courseId." ";
  107. $sql.= DocumentManager::getSessionFolderFilters($querypath, $sessionId);
  108. $result = Database::query($sql);
  109. $files = array();
  110. while ($row = Database::fetch_array($result)) {
  111. $files[$row['path']] = $row;
  112. }
  113. Session::write('doc_files_to_download', $files);
  114. foreach ($files as $not_deleted_file) {
  115. // Filtering folders and
  116. if (strpos($not_deleted_file['path'], 'chat_files') > 0 ||
  117. strpos($not_deleted_file['path'], 'shared_folder') > 0
  118. ) {
  119. if (!empty($sessionId)) {
  120. if ($not_deleted_file['id_session'] != $sessionId) {
  121. continue;
  122. }
  123. }
  124. }
  125. $zip->add(
  126. $sysCoursePath.$courseInfo['path'].'/document'.$not_deleted_file['path'],
  127. PCLZIP_OPT_REMOVE_PATH,
  128. $sysCoursePath.$courseInfo['path'].'/document'.$remove_dir,
  129. PCLZIP_CB_PRE_ADD,
  130. 'fixDocumentNameCallback'
  131. );
  132. }
  133. Session::erase('doc_files_to_download');
  134. } else {
  135. // For other users, we need to create a zip file with only visible files and folders
  136. if ($path == '/') {
  137. $querypath = ''; // To prevent ...path LIKE '//%'... in query
  138. } else {
  139. $querypath = $path;
  140. }
  141. /* A big problem: Visible files that are in a hidden folder are
  142. included when we do a query for visiblity='v'
  143. So... I do it in a couple of steps:
  144. 1st: Get all files that are visible in the given path
  145. */
  146. $querypath = Database::escape_string($querypath);
  147. $sql = "SELECT path, id_session, docs.id, props.to_group_id, docs.c_id
  148. FROM $doc_table AS docs INNER JOIN $prop_table AS props
  149. ON
  150. docs.id = props.ref AND
  151. docs.c_id = props.c_id
  152. WHERE
  153. docs.c_id = $courseId AND
  154. props.tool = '".TOOL_DOCUMENT."' AND
  155. docs.path LIKE '".$querypath."/%' AND
  156. props.visibility = '1' AND
  157. docs.filetype = 'file' AND
  158. props.id_session IN ('0', '$sessionId') AND
  159. props.to_group_id = ".$groupId;
  160. $sql.= DocumentManager::getSessionFolderFilters($querypath, $sessionId);
  161. $result = Database::query($sql);
  162. $files = array();
  163. // Add them to an array
  164. while ($all_visible_files = Database::fetch_assoc($result)) {
  165. if (strpos($all_visible_files['path'], 'chat_files') > 0 ||
  166. strpos($all_visible_files['path'], 'shared_folder') > 0
  167. ) {
  168. if (!empty($sessionId)) {
  169. if ($all_visible_files['id_session'] != $sessionId) {
  170. continue;
  171. }
  172. }
  173. }
  174. $all_visible_files_path[] = $all_visible_files['path'];
  175. $files[$all_visible_files['path']] = $all_visible_files;
  176. }
  177. // 2nd: Get all folders that are invisible in the given path
  178. $sql = "SELECT path, id_session, docs.id, props.to_group_id, docs.c_id
  179. FROM $doc_table AS docs INNER JOIN $prop_table AS props
  180. ON
  181. docs.id = props.ref AND
  182. docs.c_id = props.c_id
  183. WHERE
  184. docs.c_id = $courseId AND
  185. props.tool = '".TOOL_DOCUMENT."' AND
  186. docs.path LIKE '".$querypath."/%' AND
  187. props.visibility <> '1' AND
  188. props.id_session IN ('0', '$sessionId') AND
  189. docs.filetype = 'folder'";
  190. $query2 = Database::query($sql);
  191. // If we get invisible folders, we have to filter out these results from all visible files we found
  192. if (Database::num_rows($query2) > 0) {
  193. $files = array();
  194. // Add item to an array
  195. while ($invisible_folders = Database::fetch_assoc($query2)) {
  196. //3rd: Get all files that are in the found invisible folder (these are "invisible" too)
  197. $sql = "SELECT path, docs.id, props.to_group_id, docs.c_id
  198. FROM $doc_table AS docs
  199. INNER JOIN $prop_table AS props
  200. ON
  201. docs.id = props.ref AND
  202. docs.c_id = props.c_id
  203. WHERE
  204. docs.c_id = $courseId AND
  205. props.tool ='".TOOL_DOCUMENT."' AND
  206. docs.path LIKE '".$invisible_folders['path']."/%' AND
  207. docs.filetype ='file' AND
  208. props.id_session IN ('0', '$sessionId') AND
  209. props.visibility ='1'";
  210. $query3 = Database::query($sql);
  211. // Add tem to an array
  212. while ($files_in_invisible_folder = Database::fetch_assoc($query3)) {
  213. $files_in_invisible_folder_path[] = $files_in_invisible_folder['path'];
  214. $files[$files_in_invisible_folder['path']] = $files_in_invisible_folder;
  215. }
  216. }
  217. // Compare the array with visible files and the array with files in invisible folders
  218. // and keep the difference (= all visible files that are not in an invisible folder)
  219. $files_for_zipfile = diff(
  220. (array) $all_visible_files_path,
  221. (array) $files_in_invisible_folder_path
  222. );
  223. } else {
  224. // No invisible folders found, so all visible files can be added to the zipfile
  225. $files_for_zipfile = $all_visible_files_path;
  226. }
  227. Session::write('doc_files_to_download', $files);
  228. // Add all files in our final array to the zipfile
  229. for ($i = 0; $i < count($files_for_zipfile); $i++) {
  230. $zip->add(
  231. $sysCoursePath . $courseInfo['path'] . '/document' . $files_for_zipfile[$i],
  232. PCLZIP_OPT_REMOVE_PATH,
  233. $sysCoursePath . $courseInfo['path'] . '/document' . $remove_dir,
  234. PCLZIP_CB_PRE_ADD,
  235. 'fixDocumentNameCallback'
  236. );
  237. }
  238. Session::erase('doc_files_to_download');
  239. }
  240. // Launch event
  241. Event::event_download(($path == '/') ? 'documents.zip (folder)' : basename($path).'.zip (folder)');
  242. // Start download of created file
  243. $name = ($path == '/') ? 'documents.zip' : $documentInfo['title'].'.zip';
  244. if (Security::check_abs_path($tempZipFile, api_get_path(SYS_ARCHIVE_PATH))) {
  245. $result = DocumentManager::file_send_for_download($tempZipFile, true, $name);
  246. @unlink($tempZipFile);
  247. exit;
  248. } else {
  249. api_not_allowed(true);
  250. }
  251. /**
  252. * Returns the difference between two arrays, as an array of those key/values
  253. * Use this as array_diff doesn't give the
  254. *
  255. * @param array $arr1 first array
  256. * @param array $arr2 second array
  257. *
  258. * @return array difference between the two arrays
  259. */
  260. function diff($arr1, $arr2)
  261. {
  262. $res = array();
  263. $r = 0;
  264. foreach ($arr1 as & $av) {
  265. if (!in_array($av, $arr2)) {
  266. $res[$r] = $av;
  267. $r++;
  268. }
  269. }
  270. return $res;
  271. }