special_exports.php 12 KB

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