Skip to content

Commit ecb1890

Browse files
committed
Remove use of Course::unserialize() when exporting/importing course bk
1 parent 0683b23 commit ecb1890

File tree

8 files changed

+58
-79
lines changed

8 files changed

+58
-79
lines changed

main/coursecopy/copy_course.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535
Display::display_header(get_lang('CopyCourse'));
3636
echo Display::page_header(get_lang('CopyCourse'));
3737

38-
/* MAIN CODE */
38+
$action = isset($_POST['action']) ? $_POST['action'] : '';
3939

4040
// If a CourseSelectForm is posted or we should copy all resources, then copy them
4141
if (Security::check_token('post') && (
42-
(isset($_POST['action']) && $_POST['action'] == 'course_select_form') ||
43-
(isset($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')
42+
($action === 'course_select_form') ||
43+
(isset($_POST['copy_option']) && $_POST['copy_option'] === 'full_copy')
4444
)
4545
) {
4646
// Clear token
4747
Security::clear_token();
48-
if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
48+
if ($action === 'course_select_form') {
4949
$course = CourseSelectForm::get_posted_course('copy_course');
5050
} else {
5151
$cb = new CourseBuilder();
@@ -63,7 +63,7 @@
6363
);
6464
} elseif (Security::check_token('post') && (
6565
isset($_POST['copy_option']) &&
66-
$_POST['copy_option'] == 'select_items'
66+
$_POST['copy_option'] === 'select_items'
6767
)
6868
) {
6969
// Clear token

main/coursecopy/copy_course_session.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
api_protect_global_admin_script();
2222
api_protect_limit_for_session_admin();
23+
api_set_more_memory_and_time_limits();
2324

2425
$xajax = new xajax();
2526
$xajax->registerFunction('search_courses');
@@ -28,8 +29,7 @@
2829
api_not_allowed(true);
2930
}
3031

31-
api_set_more_memory_and_time_limits();
32-
32+
$action = isset($_POST['action']) ? $_POST['action'] : '';
3333
$this_section = SECTION_PLATFORM_ADMIN;
3434

3535
$nameTools = get_lang('CopyCourse');
@@ -163,7 +163,7 @@ function search_courses($id_session, $type)
163163
$return = null;
164164

165165
if (!empty($type)) {
166-
$id_session = intval($id_session);
166+
$id_session = (int) $id_session;
167167
if ($type == 'origin') {
168168
$course_list = SessionManager::get_course_list_by_session_id($id_session);
169169
$temp_course_list = [];
@@ -279,10 +279,7 @@ function checkSelected(id_select,id_radio,id_title,id_destination) {
279279

280280
/* MAIN CODE */
281281
if (Security::check_token('post') && (
282-
(
283-
isset($_POST['action']) &&
284-
$_POST['action'] == 'course_select_form'
285-
) || (
282+
($action === 'course_select_form') || (
286283
isset($_POST['copy_option']) &&
287284
$_POST['copy_option'] == 'full_copy'
288285
)
@@ -291,7 +288,7 @@ function checkSelected(id_select,id_radio,id_title,id_destination) {
291288
// Clear token
292289
Security::clear_token();
293290
$destination_course = $origin_course = $destination_session = $origin_session = '';
294-
if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
291+
if ($action === 'course_select_form') {
295292
$destination_course = $_POST['destination_course'];
296293
$origin_course = $_POST['origin_course'];
297294
$destination_session = $_POST['destination_session'];

main/coursecopy/copy_course_session_selected.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$current_course_tool = TOOL_COURSE_MAINTENANCE;
2121

2222
api_protect_course_script(true, true);
23+
api_set_more_memory_and_time_limits();
2324

2425
$xajax = new xajax();
2526
$xajax->registerFunction('searchCourses');
@@ -32,6 +33,8 @@
3233
api_not_allowed(true);
3334
}
3435

36+
$action = isset($_POST['action']) ? $_POST['action'] : '';
37+
3538
$courseId = api_get_course_int_id();
3639
$courseInfo = api_get_course_info_by_id($courseId);
3740
$courseCode = $courseInfo['code'];
@@ -41,8 +44,6 @@
4144
api_not_allowed(true);
4245
}
4346

44-
api_set_more_memory_and_time_limits();
45-
4647
$this_section = SECTION_COURSES;
4748
$nameTools = get_lang('CopyCourse');
4849
$returnLink = api_get_path(WEB_CODE_PATH).'course_info/maintenance_coach.php?'.api_get_cidreq();
@@ -58,7 +59,6 @@
5859
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
5960
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
6061

61-
/* FUNCTIONS */
6262
/**
6363
* @param string $name
6464
*/
@@ -128,8 +128,7 @@ function displayForm()
128128
get_lang('CopyCourseFromSessionToSessionExplanation')
129129
);
130130

131-
$html .= '<form name="formulaire" method="post" action="'.api_get_self(
132-
).'?'.api_get_cidreq().'" >';
131+
$html .= '<form name="formulaire" method="post" action="'.api_get_self().'?'.api_get_cidreq().'" >';
133132
$html .= '<table border="0" cellpadding="5" cellspacing="0" width="100%">';
134133

135134
// Source
@@ -199,7 +198,7 @@ function searchCourses($idSession, $type)
199198
$courseCode = api_get_course_id();
200199

201200
if (!empty($type)) {
202-
$idSession = intval($idSession);
201+
$idSession = (int) $idSession;
203202
$courseList = SessionManager::get_course_list_by_session_id($idSession);
204203

205204
$return .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" >';
@@ -236,8 +235,6 @@ function searchCourses($idSession, $type)
236235

237236
$xajax->processRequests();
238237

239-
/* HTML head extra */
240-
241238
$htmlHeadXtra[] = $xajax->getJavascript(
242239
api_get_path(WEB_LIBRARY_PATH).'xajax/'
243240
);
@@ -283,14 +280,14 @@ function checkSelected(id_select,id_radio,id_title,id_destination) {
283280

284281
/* MAIN CODE */
285282

286-
if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form') ||
283+
if (($action === 'course_select_form') ||
287284
(isset($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')
288285
) {
289286
$destinationCourse = $destinationSession = '';
290287
$originCourse = api_get_course_id();
291288
$originSession = api_get_session_id();
292289

293-
if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
290+
if ($action === 'course_select_form') {
294291
$destinationCourse = $_POST['destination_course'];
295292
$destinationSession = $_POST['destination_session'];
296293
$course = CourseSelectForm::get_posted_course(

main/coursecopy/import_backup.php

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
55
use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
66
use Chamilo\CourseBundle\Component\CourseCopy\CourseSelectForm;
7+
use ChamiloSession as Session;
78

89
/**
910
* Import a backup.
@@ -40,45 +41,34 @@
4041
// Display the tool title
4142
echo Display::page_header($nameTools);
4243

44+
$action = isset($_POST['action']) ? $_POST['action'] : '';
45+
$importOption = isset($_POST['import_option']) ? $_POST['import_option'] : '';
46+
4347
/* MAIN CODE */
4448
$filename = '';
45-
if (Security::check_token('post') && (
46-
(
47-
isset($_POST['action']) &&
48-
$_POST['action'] == 'course_select_form'
49-
) || (
50-
isset($_POST['import_option']) &&
51-
$_POST['import_option'] == 'full_backup'
52-
)
53-
)
54-
) {
49+
if (Security::check_token('post') && ($action === 'course_select_form' || $importOption === 'full_backup')) {
5550
// Clear token
5651
Security::clear_token();
5752

5853
$error = false;
59-
if (isset($_POST['action']) &&
60-
$_POST['action'] == 'course_select_form'
61-
) {
54+
if ($action === 'course_select_form') {
6255
// Partial backup here we recover the documents posted
63-
// This gets $_POST['course']. Beware that when using Suhosin,
64-
// the post.max_value_length limit might get in the way of the
65-
// restoration of a course with many items. A value of 1,000,000 bytes
66-
// might be too short.
67-
$course = CourseSelectForm::get_posted_course();
56+
$filename = Session::read('backup_file');
57+
$course = CourseArchiver::readCourse($filename, false);
58+
$course = CourseSelectForm::get_posted_course(null, null, null, $course);
6859
} else {
69-
if ($_POST['backup_type'] == 'server') {
60+
if ($_POST['backup_type'] === 'server') {
7061
$filename = $_POST['backup_server'];
7162
$delete_file = false;
7263
} else {
7364
if ($_FILES['backup']['error'] == 0) {
74-
$filename = CourseArchiver::importUploadedFile(
75-
$_FILES['backup']['tmp_name']
76-
);
65+
$filename = CourseArchiver::importUploadedFile($_FILES['backup']['tmp_name']);
7766
if ($filename === false) {
7867
$error = true;
7968
} else {
80-
$delete_file = true;
69+
$delete_file = false;
8170
}
71+
Session::write('backup_file', $filename);
8272
} else {
8373
$error = true;
8474
}
@@ -115,24 +105,21 @@
115105
}
116106
}
117107
CourseArchiver::cleanBackupDir();
118-
} elseif (Security::check_token('post') && (
119-
isset($_POST['import_option']) &&
120-
$_POST['import_option'] == 'select_items'
121-
)
122-
) {
108+
} elseif (Security::check_token('post') && $importOption === 'select_items') {
123109
// Clear token
124110
Security::clear_token();
125111

126-
if ($_POST['backup_type'] == 'server') {
112+
if ($_POST['backup_type'] === 'server') {
127113
$filename = $_POST['backup_server'];
128114
$delete_file = false;
129115
} else {
130116
$filename = CourseArchiver::importUploadedFile($_FILES['backup']['tmp_name']);
131-
$delete_file = true;
117+
$delete_file = false;
118+
Session::write('backup_file', $filename);
132119
}
133120
$course = CourseArchiver::readCourse($filename, $delete_file);
134121

135-
if ($course->has_resources() && ($filename !== false)) {
122+
if ($course->has_resources() && $filename !== false) {
136123
$hiddenFields['same_file_name_option'] = $_POST['same_file_name_option'];
137124
// Add token to Course select form
138125
$hiddenFields['sec_token'] = Security::get_token();
@@ -146,9 +133,7 @@
146133
}
147134
} else {
148135
$user = api_get_user_info();
149-
$backups = CourseArchiver::getAvailableBackups(
150-
$is_platformAdmin ? null : $user['user_id']
151-
);
136+
$backups = CourseArchiver::getAvailableBackups($is_platformAdmin ? null : $user['user_id']);
152137
$backups_available = count($backups) > 0;
153138

154139
$form = new FormValidator(
@@ -282,4 +267,9 @@
282267
$form->display();
283268
}
284269

270+
if (!isset($_POST['action'])) {
271+
Session::erase('backup_file');
272+
}
273+
274+
285275
Display::display_footer();

main/coursecopy/recycle_course.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636

3737
// Display the tool title
3838
echo Display::page_header($nameTools);
39+
$action = isset($_POST['action']) ? $_POST['action'] : '';
3940

4041
if (Security::check_token('post') && (
41-
isset($_POST['action']) &&
42-
$_POST['action'] == 'course_select_form' ||
42+
$action === 'course_select_form' ||
4343
(
4444
isset($_POST['recycle_option']) &&
4545
$_POST['recycle_option'] == 'full_backup'
@@ -48,25 +48,24 @@
4848
) {
4949
// Clear token
5050
Security::clear_token();
51-
52-
if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
51+
if (isset($_POST['action']) && $_POST['action'] === 'course_select_form') {
5352
$course = CourseSelectForm::get_posted_course();
5453
} else {
5554
$cb = new CourseBuilder();
5655
$course = $cb->build();
5756
}
5857
$recycle_type = '';
59-
if (isset($_POST['recycle_option']) && $_POST['recycle_option'] == 'full_backup') {
58+
if (isset($_POST['recycle_option']) && $_POST['recycle_option'] === 'full_backup') {
6059
$recycle_type = 'full_backup';
61-
} elseif (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
60+
} elseif (isset($_POST['action']) && $_POST['action'] === 'course_select_form') {
6261
$recycle_type = 'select_items';
6362
}
6463
$cr = new CourseRecycler($course);
6564
$cr->recycle($recycle_type);
6665
echo Display::return_message(get_lang('RecycleFinished'), 'confirm');
6766
} elseif (Security::check_token('post') && (
6867
isset($_POST['recycle_option']) &&
69-
$_POST['recycle_option'] == 'select_items'
68+
$_POST['recycle_option'] === 'select_items'
7069
)
7170
) {
7271
// Clear token

src/Chamilo/CourseBundle/Component/CourseCopy/CourseArchiver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public static function getAvailableBackups($user_id = null)
251251
*/
252252
public static function importUploadedFile($file)
253253
{
254-
$new_filename = uniqid('').'.zip';
254+
$new_filename = uniqid('import_file', true).'.zip';
255255
$new_dir = self::getBackupDir();
256256
if (!is_dir($new_dir)) {
257257
$fs = new Filesystem();

src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ public function restore(
153153
$this->destination_course_id = $course_info['real_id'];
154154

155155
// Getting first teacher (for the forums)
156-
$teacher_list = CourseManager::get_teacher_list_from_course_code(
157-
$course_info['code']
158-
);
156+
$teacher_list = CourseManager::get_teacher_list_from_course_code($course_info['code']);
159157
$this->first_teacher_id = api_get_user_id();
160158

161159
if (!empty($teacher_list)) {

src/Chamilo/CourseBundle/Component/CourseCopy/CourseSelectForm.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public static function display_hidden_quiz_questions($course)
444444
{
445445
if (is_array($course->resources)) {
446446
foreach ($course->resources as $type => $resources) {
447-
if (count($resources) > 0) {
447+
if (!empty($resources) && count($resources) > 0) {
448448
switch ($type) {
449449
case RESOURCE_QUIZQUESTION:
450450
foreach ($resources as $id => $resource) {
@@ -467,7 +467,7 @@ public static function display_hidden_scorm_directories($course)
467467
{
468468
if (is_array($course->resources)) {
469469
foreach ($course->resources as $type => $resources) {
470-
if (count($resources) > 0) {
470+
if (!empty($resources) && count($resources) > 0) {
471471
switch ($type) {
472472
case RESOURCE_SCORM:
473473
foreach ($resources as $id => $resource) {
@@ -497,13 +497,11 @@ public static function display_hidden_scorm_directories($course)
497497
*/
498498
public static function get_posted_course($from = '', $session_id = 0, $course_code = '', $postedCourse = null)
499499
{
500-
$course = null;
501-
if (isset($_POST['course'])) {
502-
$course = Course::unserialize(base64_decode($_POST['course']));
503-
}
504-
505-
if ($postedCourse) {
506-
$course = $postedCourse;
500+
$course = $postedCourse;
501+
if (empty($postedCourse)) {
502+
$cb = new CourseBuilder();
503+
$postResource = isset($_POST['resource']) ? $_POST['resource'] : [];
504+
$course = $cb->build(0, null, false, array_keys($postResource), $postResource);
507505
}
508506

509507
if (empty($course)) {
@@ -527,7 +525,7 @@ public static function get_posted_course($from = '', $session_id = 0, $course_co
527525
foreach ($resource as $resource_item) {
528526
$conditionSession = '';
529527
if (!empty($session_id)) {
530-
$session_id = intval($session_id);
528+
$session_id = (int) $session_id;
531529
$conditionSession = ' AND d.session_id ='.$session_id;
532530
}
533531

@@ -645,7 +643,7 @@ public static function get_posted_course($from = '', $session_id = 0, $course_co
645643
$documents = isset($_POST['resource'][RESOURCE_DOCUMENT]) ? $_POST['resource'][RESOURCE_DOCUMENT] : null;
646644
if (!empty($resources) && is_array($resources)) {
647645
foreach ($resources as $id => $obj) {
648-
if (isset($obj->file_type) && $obj->file_type == 'folder' &&
646+
if (isset($obj->file_type) && $obj->file_type === 'folder' &&
649647
!isset($_POST['resource'][RESOURCE_DOCUMENT][$id]) &&
650648
is_array($documents)
651649
) {

0 commit comments

Comments
 (0)