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