special_exports.php 12 KB

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