Issues (2128)

main/lp/lp_upload.php (1 issue)

Severity
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
6
use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
7
8
/**
9
 * Script managing the learnpath upload. To best treat the uploaded file, make sure we can identify it.
10
 *
11
 * @author Yannick Warnier <[email protected]>
12
 */
13
require_once __DIR__.'/../inc/global.inc.php';
14
api_protect_course_script();
15
$course_dir = api_get_course_path().'/scorm';
16
$course_sys_dir = api_get_path(SYS_COURSE_PATH).$course_dir;
17
if (empty($_POST['current_dir'])) {
18
    $current_dir = '';
19
} else {
20
    $current_dir = api_replace_dangerous_char(trim($_POST['current_dir']));
21
}
22
$uncompress = 1;
23
24
$allowHtaccess = false;
25
if (api_get_configuration_value('allow_htaccess_import_from_scorm') && isset($_POST['allow_htaccess'])) {
26
    $allowHtaccess = true;
27
}
28
29
/*
30
 * Check the request method in place of a variable from POST
31
 * because if the file size exceed the maximum file upload
32
 * size set in php.ini, all variables from POST are cleared !
33
 */
34
$user_file = isset($_GET['user_file']) ? $_GET['user_file'] : [];
35
$user_file = $user_file ? $user_file : [];
36
$is_error = isset($user_file['error']) ? $user_file['error'] : false;
37
if (isset($_POST) && $is_error) {
38
    Display::addFlash(
39
        Display::return_message(get_lang('UplFileTooBig'))
40
    );
41
42
    return false;
43
    unset($_FILES['user_file']);
44
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && count($_FILES) > 0 && !empty($_FILES['user_file']['name'])) {
45
    // A file upload has been detected, now deal with the file...
46
    // Directory creation.
47
    $stopping_error = false;
48
    $s = $_FILES['user_file']['name'];
49
50
    // Get name of the zip file without the extension.
51
    $info = pathinfo($s);
52
    $filename = $info['basename'];
53
    $extension = $info['extension'];
54
    $file_base_name = str_replace('.'.$extension, '', $filename);
55
56
    $new_dir = api_replace_dangerous_char(trim($file_base_name));
57
    $type = learnpath::getPackageType($_FILES['user_file']['tmp_name'], $_FILES['user_file']['name']);
58
59
    $proximity = 'local';
60
    if (!empty($_REQUEST['content_proximity'])) {
61
        $proximity = Database::escape_string($_REQUEST['content_proximity']);
62
    }
63
64
    $maker = 'Scorm';
65
    if (!empty($_REQUEST['content_maker'])) {
66
        $maker = Database::escape_string($_REQUEST['content_maker']);
67
    }
68
69
    switch ($type) {
70
        case 'chamilo':
71
            $filename = CourseArchiver::importUploadedFile($_FILES['user_file']['tmp_name']);
72
            if ($filename) {
73
                $course = CourseArchiver::readCourse($filename, false);
74
                $courseRestorer = new CourseRestorer($course);
75
                // FILE_SKIP, FILE_RENAME or FILE_OVERWRITE
76
                $courseRestorer->set_file_option(FILE_OVERWRITE);
77
                $courseRestorer->restore('', api_get_session_id());
78
                Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
79
            }
80
            break;
81
        case 'scorm':
82
            $oScorm = new scorm();
83
            $manifest = $oScorm->import_package(
84
                $_FILES['user_file'],
85
                $current_dir,
86
                [],
87
                false,
88
                null,
89
                $allowHtaccess
90
            );
91
            if (!empty($manifest)) {
92
                $oScorm->parse_manifest($manifest);
93
                $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
94
                Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
95
            }
96
            $oScorm->set_proximity($proximity);
97
            $oScorm->set_maker($maker);
98
            $oScorm->set_jslib('scorm_api.php');
99
            break;
100
        case 'aicc':
101
            $oAICC = new aicc();
102
            $config_dir = $oAICC->import_package($_FILES['user_file']);
103
            if (!empty($config_dir)) {
104
                $oAICC->parse_config_files($config_dir);
105
                $oAICC->import_aicc(api_get_course_id());
106
                Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
107
            }
108
            $oAICC->set_proximity($proximity);
109
            $oAICC->set_maker($maker);
110
            $oAICC->set_jslib('aicc_api.php');
111
            break;
112
        case 'oogie':
113
            require_once 'openoffice_presentation.class.php';
114
            $take_slide_name = empty($_POST['take_slide_name']) ? false : true;
115
            $o_ppt = new OpenofficePresentation($take_slide_name);
116
            $first_item_id = $o_ppt->convert_document($_FILES['user_file'], 'make_lp', $_POST['slide_size']);
117
            Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
118
            break;
119
        case 'woogie':
120
            require_once 'openoffice_text.class.php';
121
            $split_steps = (empty($_POST['split_steps']) || $_POST['split_steps'] == 'per_page') ? 'per_page' : 'per_chapter';
122
            $o_doc = new OpenofficeText($split_steps);
123
            $first_item_id = $o_doc->convert_document($_FILES['user_file']);
124
            Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
125
            break;
126
        case '':
127
        default:
128
            Display::addFlash(Display::return_message(get_lang('ScormUnknownPackageFormat'), 'warning'));
129
130
            return false;
131
            break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
132
    }
133
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST' || ('bigUpload' === $_REQUEST['from'] && !empty($_REQUEST['name']))) {
134
    // end if is_uploaded_file
135
    // If file name given to get in /upload/, try importing this way.
136
    // A file upload has been detected, now deal with the file...
137
    // Directory creation.
138
    $stopping_error = false;
139
140
    // When it is used from bigupload input
141
    if ('bigUpload' === $_REQUEST['from']) {
142
        if (empty($_REQUEST['name'])) {
143
            return false;
144
        }
145
        $tempName = $_REQUEST['name'];
146
    } else {
147
        if (!isset($_POST['file_name'])) {
148
            return false;
149
        }
150
        $tempName = $_POST['file_name'];
151
    }
152
153
    // Escape path with basename so it can only be directly into the archive/ directory.
154
    $s = api_get_path(SYS_ARCHIVE_PATH).basename($tempName);
155
    // Get name of the zip file without the extension
156
    $info = pathinfo($s);
157
    $filename = $info['basename'];
158
    $extension = $info['extension'];
159
    $file_base_name = str_replace('.'.$extension, '', $filename);
160
    $new_dir = api_replace_dangerous_char(trim($file_base_name));
161
162
    $result = learnpath::verify_document_size($s);
163
    if ($result) {
164
        Display::addFlash(
165
            Display::return_message(get_lang('UplFileTooBig'))
166
        );
167
    }
168
    $type = learnpath::getPackageType($s, basename($s));
169
170
    switch ($type) {
171
        case 'scorm':
172
            $oScorm = new scorm();
173
            $manifest = $oScorm->import_local_package($s, $current_dir);
174
            // The file was treated, it can now be cleaned from the temp dir
175
            unlink($s);
176
            if (!empty($manifest)) {
177
                $oScorm->parse_manifest($manifest);
178
                $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
179
                Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
180
            }
181
182
            $proximity = '';
183
            if (!empty($_REQUEST['content_proximity'])) {
184
                $proximity = Database::escape_string($_REQUEST['content_proximity']);
185
            }
186
            $maker = '';
187
            if (!empty($_REQUEST['content_maker'])) {
188
                $maker = Database::escape_string($_REQUEST['content_maker']);
189
            }
190
            $oScorm->set_proximity($proximity);
191
            $oScorm->set_maker($maker);
192
            $oScorm->set_jslib('scorm_api.php');
193
            break;
194
        case 'aicc':
195
            $oAICC = new aicc();
196
            $config_dir = $oAICC->import_local_package($s, $current_dir);
197
            // The file was treated, it can now be cleaned from the temp dir
198
            unlink($s);
199
            if (!empty($config_dir)) {
200
                $oAICC->parse_config_files($config_dir);
201
                $oAICC->import_aicc(api_get_course_id());
202
                Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
203
            }
204
            $proximity = '';
205
            if (!empty($_REQUEST['content_proximity'])) {
206
                $proximity = Database::escape_string($_REQUEST['content_proximity']);
207
            }
208
            $maker = '';
209
            if (!empty($_REQUEST['content_maker'])) {
210
                $maker = Database::escape_string($_REQUEST['content_maker']);
211
            }
212
            $oAICC->set_proximity($proximity);
213
            $oAICC->set_maker($maker);
214
            $oAICC->set_jslib('aicc_api.php');
215
            break;
216
        case '':
217
        default:
218
            // There was an error, clean the file from the temp dir
219
            if (is_file($s)) {
220
                unlink($s);
221
            }
222
            Display::addFlash(
223
                Display::return_message(get_lang('ScormUnknownPackageFormat'), 'warning')
224
            );
225
226
            return false;
227
            break;
228
    }
229
}
230