special_exports.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  4. use Chamilo\CourseBundle\Component\CourseCopy\CourseSelectForm;
  5. /**
  6. * Special exports.
  7. *
  8. * @author Jhon Hinojosa
  9. * @author Julio Montoya Fixing pclzip folder + some clean <gugli100@gmail.com>
  10. *
  11. * @package chamilo.include.export
  12. */
  13. // including the global file
  14. $cidReset = true;
  15. require_once __DIR__.'/../inc/global.inc.php';
  16. $this_section = SECTION_PLATFORM_ADMIN;
  17. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('PlatformAdmin')];
  18. // Access restrictions
  19. api_protect_admin_script(true);
  20. $nameTools = get_lang('SpecialExports');
  21. $export = '';
  22. $querypath = '';
  23. api_set_more_memory_and_time_limits();
  24. // Displaying the header
  25. Display::display_header($nameTools);
  26. // Display the tool title
  27. echo Display::page_header($nameTools);
  28. if (count($_POST) == 0) {
  29. echo Display::return_message(get_lang('SpecialExportsIntroduction'));
  30. }
  31. $error = 0;
  32. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  33. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  34. if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form') ||
  35. (isset($_POST['backup_option']) && $_POST['backup_option'] == 'full_backup')
  36. ) {
  37. $export = false;
  38. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  39. $FileZip = create_zip();
  40. $to_group_id = 0;
  41. $sql_session = "SELECT id, name FROM $tbl_session ";
  42. $query_session = Database::query($sql_session);
  43. $ListSession = [];
  44. while ($rows_session = Database::fetch_assoc($query_session)) {
  45. $ListSession[$rows_session['id']] = $rows_session['name'];
  46. }
  47. $groupCondition = " props.to_group_id = $to_group_id";
  48. if (empty($to_group_id)) {
  49. $groupCondition = " (props.to_group_id = 0 OR props.to_group_id IS NULL)";
  50. }
  51. $zip_folder = new PclZip($FileZip['TEMP_FILE_ZIP']);
  52. if (!isset($_POST['resource']) || count($_POST['resource']) == 0) {
  53. echo Display::return_message(get_lang('ErrorMsgSpecialExport'), 'error');
  54. } else {
  55. $Resource = $_POST['resource'];
  56. foreach ($Resource as $Code_course => $Sessions) {
  57. $_course = api_get_course_info($Code_course);
  58. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  59. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  60. $course_id = $_course['real_id'];
  61. //Add item to the zip file course
  62. $sql = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  63. WHERE props.tool='".TOOL_DOCUMENT."'
  64. AND docs.id=props.ref
  65. AND docs.path LIKE '".$querypath."/%'
  66. AND docs.filetype='file'
  67. AND (docs.session_id = '0' OR docs.session_id IS NULL)
  68. AND props.visibility<>'2'
  69. AND $groupCondition
  70. AND docs.c_id = $course_id
  71. AND props.c_id = $course_id";
  72. $query = Database::query($sql);
  73. while ($rows_course_file = Database::fetch_assoc($query)) {
  74. $zip_folder->add(
  75. $FileZip['PATH_COURSE'].$_course['directory']."/document".$rows_course_file['path'],
  76. PCLZIP_OPT_ADD_PATH,
  77. $_course['directory'],
  78. PCLZIP_OPT_REMOVE_PATH,
  79. $FileZip['PATH_COURSE'].$_course['directory']."/document".$FileZip['PATH_REMOVE']
  80. );
  81. }
  82. foreach ($Sessions as $IdSession => $value) {
  83. $session_id = (int) $IdSession;
  84. //Add tem to the zip file session course
  85. $sql_session_doc = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  86. WHERE props.tool='".TOOL_DOCUMENT."'
  87. AND docs.id=props.ref
  88. AND docs.path LIKE '".$querypath."/%'
  89. AND docs.filetype='file'
  90. AND docs.session_id = '$session_id'
  91. AND props.visibility<>'2'
  92. AND $groupCondition
  93. AND docs.c_id = $course_id
  94. AND props.c_id = $course_id";
  95. $query_session_doc = Database::query($sql_session_doc);
  96. while ($rows_course_session_file = Database::fetch_assoc($query_session_doc)) {
  97. $zip_folder->add(
  98. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$rows_course_session_file['path'],
  99. PCLZIP_OPT_ADD_PATH,
  100. $_course['directory']."/".$ListSession[$session_id],
  101. PCLZIP_OPT_REMOVE_PATH,
  102. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$FileZip['PATH_REMOVE']
  103. );
  104. }
  105. }
  106. }
  107. $name = rename_zip($FileZip);
  108. $export = true;
  109. }
  110. } else {
  111. $name = fullexportspecial();
  112. }
  113. }
  114. if ($export && $name) {
  115. echo Display::return_message(get_lang('BackupCreated'), 'confirm');
  116. echo '<br /><a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'course_info/download.php?archive_path=&archive='.urlencode($name).'">'.get_lang('Download').'</a>';
  117. } else {
  118. // Display forms especial export
  119. if (isset($_POST['backup_option']) && $_POST['backup_option'] == 'select_items') {
  120. $cb = new CourseBuilder();
  121. $course = $cb->build_session_course();
  122. if ($course === false) {
  123. echo Display::return_message(get_lang('ErrorMsgSpecialExport'), 'error');
  124. form_special_export();
  125. } else {
  126. echo Display::return_message(get_lang('ToExportSpecialSelect'), 'normal');
  127. CourseSelectForm :: display_form_session_export($course);
  128. }
  129. } else {
  130. form_special_export();
  131. }
  132. }
  133. /* FOOTER */
  134. Display::display_footer();
  135. function form_special_export()
  136. {
  137. $form = new FormValidator('special_exports', 'post');
  138. $renderer = $form->defaultRenderer();
  139. $renderer->setCustomElementTemplate('<div>{element}</div> ');
  140. $form->addElement('radio', 'backup_option', '', get_lang('SpecialCreateFullBackup'), 'full_backup');
  141. $form->addElement('radio', 'backup_option', '', get_lang('SpecialLetMeSelectItems'), 'select_items');
  142. $form->addElement('html', '<br />');
  143. $form->addButtonExport(get_lang('CreateBackup'));
  144. $form->addProgress();
  145. $values['backup_option'] = 'full_backup';
  146. $form->setDefaults($values);
  147. $form->display();
  148. }
  149. function create_zip()
  150. {
  151. $path = '';
  152. if (empty($path)) {
  153. $path = '/';
  154. }
  155. $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
  156. $sys_archive_path = api_get_path(SYS_ARCHIVE_PATH).'special_export/';
  157. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  158. $temp_zip_dir = $sys_archive_path;
  159. if (!is_dir($temp_zip_dir)) {
  160. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  161. } else {
  162. $handle = opendir($temp_zip_dir);
  163. while (false !== ($file = readdir($handle))) {
  164. if ($file != "." && $file != "..") {
  165. $Diff = (time() - filemtime("$temp_zip_dir/$file")) / 60 / 60; //the "age" of the file in hours
  166. if ($Diff > 4) {
  167. unlink("$temp_zip_dir/$file");
  168. } //delete files older than 4 hours
  169. }
  170. }
  171. closedir($handle);
  172. }
  173. $temp_zip_file = $temp_zip_dir."/".md5(time()).".zip"; //create zipfile of given directory
  174. return [
  175. 'PATH' => $path,
  176. 'PATH_TEMP_ARCHIVE' => $temp_zip_dir,
  177. 'PATH_COURSE' => $sys_course_path,
  178. 'TEMP_FILE_ZIP' => $temp_zip_file,
  179. 'PATH_REMOVE' => $remove_dir,
  180. ];
  181. }
  182. function rename_zip($FileZip)
  183. {
  184. Event::event_download(($FileZip['PATH'] == '/') ? 'full_export_'.date('Ymd').'.zip (folder)' : basename($FileZip['PATH']).'.zip (folder)');
  185. $name = ($FileZip['PATH'] == '/') ? 'full_export_'.date('Ymd').'.zip' : basename($FileZip['PATH']).'.zip';
  186. if (file_exists($FileZip['PATH_TEMP_ARCHIVE'].'/'.$name)) {
  187. unlink($FileZip['PATH_TEMP_ARCHIVE'].'/'.$name);
  188. }
  189. if (file_exists($FileZip['TEMP_FILE_ZIP'])) {
  190. rename(
  191. $FileZip['TEMP_FILE_ZIP'],
  192. $FileZip['PATH_TEMP_ARCHIVE'].'/'.$name
  193. );
  194. return $name;
  195. } else {
  196. return false;
  197. }
  198. }
  199. function fullexportspecial()
  200. {
  201. global $tbl_session, $tbl_session_course, $export;
  202. $FileZip = create_zip();
  203. $to_group_id = 0;
  204. $zip_folder = new PclZip($FileZip['TEMP_FILE_ZIP']);
  205. $list_course = CourseManager::get_course_list();
  206. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  207. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  208. $groupCondition = " props.to_group_id = $to_group_id";
  209. if (empty($to_group_id)) {
  210. $groupCondition = " (props.to_group_id = 0 OR props.to_group_id IS NULL)";
  211. }
  212. if (count($list_course) > 0) {
  213. foreach ($list_course as $_course) {
  214. if ($FileZip['PATH'] == '/') {
  215. $querypath = ''; // to prevent ...path LIKE '//%'... in query
  216. } else {
  217. $querypath = $FileZip['PATH'];
  218. }
  219. $course_id = $_course['real_id'];
  220. //Add tem to the zip file course
  221. $sql = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  222. WHERE props.tool='".TOOL_DOCUMENT."'
  223. AND docs.id=props.ref
  224. AND docs.path LIKE '".$querypath."/%'
  225. AND docs.filetype='file'
  226. AND (docs.session_id = '0' OR docs.session_id IS NULL)
  227. AND props.visibility<>'2'
  228. AND $groupCondition
  229. AND docs.c_id = $course_id
  230. AND props.c_id = $course_id";
  231. $query = Database::query($sql);
  232. while ($rows_course_file = Database::fetch_assoc($query)) {
  233. $zip_folder->add(
  234. $FileZip['PATH_COURSE'].$_course['directory']."/document".$rows_course_file['path'],
  235. PCLZIP_OPT_ADD_PATH,
  236. $_course['directory'],
  237. PCLZIP_OPT_REMOVE_PATH,
  238. $FileZip['PATH_COURSE'].$_course['directory']."/document".$FileZip['PATH_REMOVE']
  239. );
  240. }
  241. //Add tem to the zip file session course
  242. $sql = "SELECT s.id, name, c_id
  243. FROM $tbl_session_course sc
  244. INNER JOIN $tbl_session s
  245. ON sc.session_id = s.id
  246. WHERE c_id = '$course_id' ";
  247. $query_session = Database::query($sql);
  248. while ($rows_session = Database::fetch_assoc($query_session)) {
  249. $session_id = $rows_session['id'];
  250. $sql_session_doc = "SELECT path
  251. FROM $tbl_document AS docs, $tbl_property AS props
  252. WHERE props.tool='".TOOL_DOCUMENT."'
  253. AND docs.id=props.ref
  254. AND docs.path LIKE '".$querypath."/%'
  255. AND docs.filetype='file'
  256. AND docs.session_id = '$session_id'
  257. AND props.visibility<>'2'
  258. AND $groupCondition
  259. AND docs.c_id = $course_id
  260. AND props.c_id = $course_id ";
  261. $query_session_doc = Database::query($sql_session_doc);
  262. while ($rows_course_session_file = Database::fetch_assoc($query_session_doc)) {
  263. $zip_folder->add(
  264. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$rows_course_session_file['path'],
  265. PCLZIP_OPT_ADD_PATH,
  266. $_course['directory']."/".$rows_session['name'],
  267. PCLZIP_OPT_REMOVE_PATH,
  268. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$FileZip['PATH_REMOVE']
  269. );
  270. }
  271. }
  272. }
  273. $name = rename_zip($FileZip);
  274. if ($name === false) {
  275. $export = false;
  276. return false;
  277. } else {
  278. $export = true;
  279. return $name;
  280. }
  281. } else {
  282. echo Display::return_message(get_lang('ErrorMsgSpecialExport'), 'error'); //main API
  283. $export = false;
  284. return false;
  285. }
  286. }