special_exports.php 12 KB

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