aicc::parse_config_files()   F
last analyzed

Complexity

Conditions 36
Paths > 20000

Size

Total Lines 151
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 36
eloc 82
nc 14155780
nop 1
dl 0
loc 151
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Class aicc
7
 * Defines the AICC class, which is meant to contain the aicc items (nuclear elements).
8
 *
9
 * @author Yannick Warnier <[email protected]>
10
 * @license GNU/GPL
11
 */
12
class aicc extends learnpath
13
{
14
    public $config = [];
15
    // The configuration files might be multiple and might have
16
    // funny names. We need to keep the name of that file while we
17
    // install the content.
18
    public $config_basename = '';
19
    public $config_files = [];
20
    public $config_exts = [
21
        'crs' => 0, // Course description file (mandatory)
22
        'au' => 0, // Assignable Unit file (mandatory)
23
        'des' => 0, // Descriptor file (mandatory)
24
        'cst' => 0, // Course structure file (mandatory)
25
        'ore' => 0, // Objectives relationshops file (optional)
26
        'pre' => 0, // Prerequisites file (optional)
27
        'cmp' => 0,  // Completion Requirements file (optional)
28
    ];
29
    public $aulist = [];
30
    public $au_order_list = [];
31
    public $au_order_list_new_id = [];
32
    public $deslist = [];
33
    public $cstlist = [];
34
    public $orelist = [];
35
36
    // Path between the scorm/ directory and the config files
37
    // e.g. maritime_nav/maritime_nav.
38
    // This is the path that will be used in the lp_path when importing a package.
39
    public $subdir = '';
40
    // Keeps the zipfile safe for the object's life
41
    // so that we can use it if there is no title available.
42
    public $zipname = '';
43
    // Keeps an index of the number of uses of the zipname so far.
44
    public $lastzipnameindex = 0;
45
    public $config_encoding = 'ISO-8859-1';
46
    public $debug = 0;
47
48
    /**
49
     * Class constructor. Based on the parent constructor.
50
     *
51
     * @param string $course_code
52
     * @param int    $resource_id Learnpath ID in DB
53
     * @param int    $user_id
54
     */
55
    public function __construct($course_code = null, $resource_id = null, $user_id = null)
56
    {
57
        if (!empty($course_code) && !empty($resource_id) && !empty($user_id)) {
58
            parent::__construct($course_code, $resource_id, $user_id);
59
        }
60
    }
61
62
    /**
63
     * Opens a resource.
64
     *
65
     * @param int Database ID of the resource
66
     */
67
    public function open($id)
68
    {
69
    }
70
71
    /**
72
     * Parses a set of AICC config files and puts everything into the $config array.
73
     *
74
     * @param string Path to the config files dir on the system.
75
     * If not defined, uses the base path of the course's scorm dir
76
     *
77
     * @return array Structured array representing the config files' contents
78
     */
79
    public function parse_config_files($dir = '')
80
    {
81
        if ($this->debug > 0) {
82
            error_log('New LP - In aicc::parse_config_files('.$dir.')', 0);
83
        }
84
        if (empty($dir)) {
85
            // Get the path of the AICC config files dir.
86
            $dir = $this->subdir;
87
        }
88
        if (is_dir($dir) && is_readable($dir)) {
89
            // Now go through all the config files one by one and parse everything into AICC objects.
90
            // The basename for the config files is stored in $this->config_basename.
91
            // Parse the Course Description File (.crs) - ini-type.
92
            $crs_file = $dir.'/'.$this->config_files['crs'];
93
            $crs_params = $this->parse_ini_file_quotes_safe($crs_file);
94
            if ($this->debug > 1) {
95
                error_log('New LP - In aicc::parse_config_files() - '.$crs_file.' has been parsed');
96
            }
97
98
            // CRS distribute crs params into the aicc object.
99
            if (!empty($crs_params['course']['course_creator'])) {
100
                $this->course_creator = Database::escape_string($crs_params['course']['course_creator']);
101
            }
102
            if (!empty($crs_params['course']['course_id'])) {
103
                $this->course_id = Database::escape_string($crs_params['course']['course_id']);
104
            }
105
            if (!empty($crs_params['course']['course_system'])) {
106
                $this->course_system = $crs_params['course']['course_system'];
107
            }
108
            if (!empty($crs_params['course']['course_title'])) {
109
                $this->course_title = Database::escape_string($crs_params['course']['course_title']);
110
            }
111
            if (!empty($crs_params['course']['course_level'])) {
112
                $this->course_level = $crs_params['course']['course_level'];
113
            }
114
            if (!empty($crs_params['course']['max_fields_cst'])) {
115
                $this->course_max_fields_cst = $crs_params['course']['max_fields_cst'];
116
            }
117
            if (!empty($crs_params['course']['max_fields_ort'])) {
118
                $this->course_max_fields_ort = $crs_params['course']['max_fields_ort'];
119
            }
120
            if (!empty($crs_params['course']['total_aus'])) {
121
                $this->course_total_aus = $crs_params['course']['total_aus'];
122
            }
123
            if (!empty($crs_params['course']['total_blocks'])) {
124
                $this->course_total_blocks = $crs_params['course']['total_blocks'];
125
            }
126
            if (!empty($crs_params['course']['total_objectives'])) {
127
                $this->course_total_objectives = $crs_params['course']['total_objectives'];
128
            }
129
            if (!empty($crs_params['course']['total_complex_objectives'])) {
130
                $this->course_total_complex_objectives = $crs_params['course']['total_complex_objectives'];
131
            }
132
            if (!empty($crs_params['course']['version'])) {
133
                $this->course_version = $crs_params['course']['version'];
134
            }
135
            if (!empty($crs_params['course_description'])) {
136
                $this->course_description = Database::escape_string($crs_params['course_description']);
137
            }
138
139
            // Parse the Descriptor File (.des) - csv-type.
140
            $des_file = $dir.'/'.$this->config_files['des'];
141
            $des_params = $this->parse_csv_file($des_file);
142
            if ($this->debug > 1) {
143
                error_log('New LP - In aicc::parse_config_files() - '.$des_file.' has been parsed', 0);
144
            }
145
            // Distribute des params into the aicc object.
146
            foreach ($des_params as $des) {
147
                // One AU in AICC is equivalent to one SCO in SCORM (scormItem class).
148
                $oDes = new aiccResource('config', $des);
149
                $this->deslist[$oDes->identifier] = $oDes;
150
            }
151
152
            // Parse the Assignable Unit File (.au) - csv-type.
153
            $au_file = $dir.'/'.$this->config_files['au'];
154
            $au_params = $this->parse_csv_file($au_file);
155
            if ($this->debug > 1) {
156
                error_log('New LP - In aicc::parse_config_files() - '.$au_file.' has been parsed', 0);
157
            }
158
            // Distribute au params into the aicc object.
159
            foreach ($au_params as $au) {
160
                $oAu = new aiccItem('config', $au);
161
                $this->aulist[$oAu->identifier] = $oAu;
162
                $this->au_order_list[] = $oAu->identifier;
163
            }
164
165
            // Parse the Course Structure File (.cst) - csv-type.
166
            $cst_file = $dir.'/'.$this->config_files['cst'];
167
            $cst_params = $this->parse_csv_file($cst_file, ',', '"', true);
168
            if ($this->debug > 1) {
169
                error_log('New LP - In aicc::parse_config_files() - '.$cst_file.' has been parsed', 0);
170
            }
171
            // Distribute cst params into the aicc object.
172
            foreach ($cst_params as $cst) {
173
                $oCst = new aiccBlock('config', $cst);
174
                $this->cstlist[$oCst->identifier] = $oCst;
175
            }
176
177
            // Parse the Objectives Relationships File (.ore) - csv-type - if exists.
178
            // TODO: Implement these objectives. For now they're just parsed.
179
            if (!empty($this->config_files['ore'])) {
180
                $ore_file = $dir.'/'.$this->config_files['ore'];
181
                $ore_params = $this->parse_csv_file($ore_file, ',', '"', true);
182
                if ($this->debug > 1) {
183
                    error_log('New LP - In aicc::parse_config_files() - '.$ore_file.' has been parsed', 0);
184
                }
185
                // Distribute ore params into the aicc object.
186
                foreach ($ore_params as $ore) {
187
                    $oOre = new aiccObjective('config', $ore);
188
                    $this->orelist[$oOre->identifier] = $oOre;
189
                }
190
            }
191
192
            // Parse the Prerequisites File (.pre) - csv-type - if exists.
193
            if (!empty($this->config_files['pre'])) {
194
                $pre_file = $dir.'/'.$this->config_files['pre'];
195
                $pre_params = $this->parse_csv_file($pre_file);
196
                if ($this->debug > 1) {
197
                    error_log('New LP - In aicc::parse_config_files() - '.$pre_file.' has been parsed', 0);
198
                }
199
                // Distribute pre params into the aicc object.
200
                foreach ($pre_params as $pre) {
201
                    // Place a constraint on the corresponding block or AU.
202
                    if (in_array(api_strtolower($pre['structure_element']), array_keys($this->cstlist))) {
203
                        // If this references a block element:
204
                        $this->cstlist[api_strtolower($pre['structure_element'])]->prereq_string = api_strtolower($pre['prerequisite']);
205
                    }
206
                    if (in_array(api_strtolower($pre['structure_element']), array_keys($this->aulist))) {
207
                        // If this references a block element:
208
                        $this->aulist[api_strtolower($pre['structure_element'])]->prereq_string = api_strtolower($pre['prerequisite']);
209
                    }
210
                }
211
            }
212
213
            // Parse the Completion Requirements File (.cmp) - csv-type - if exists.
214
            // TODO: Implement this set of requirements (needs database changes).
215
            if (!empty($this->config_files['cmp'])) {
216
                $cmp_file = $dir.'/'.$this->config_files['cmp'];
217
                $cmp_params = $this->parse_csv_file($cmp_file);
218
                if ($this->debug > 1) {
219
                    error_log('New LP - In aicc::parse_config_files() - '.$cmp_file.' has been parsed', 0);
220
                }
221
                // Distribute cmp params into the aicc object.
222
                foreach ($cmp_params as $cmp) {
223
                    //$oCmp = new aiccCompletionRequirements('config', $cmp);
224
                    //$this->cmplist[$oCmp->identifier] =& $oCmp;
225
                }
226
            }
227
        }
228
229
        return $this->config;
230
    }
231
232
    /**
233
     * Import the aicc object (as a result from the parse_config_files function) into the database structure.
234
     *
235
     * @param string $course_code
236
     *
237
     * @return bool Returns -1 on error
238
     */
239
    public function import_aicc($course_code)
240
    {
241
        $courseInfo = api_get_course_info($course_code);
242
        $course_id = $courseInfo['real_id'];
243
244
        if (empty($course_id)) {
245
            return false;
246
        }
247
248
        if ($this->debug > 0) {
249
            error_log('New LP - In aicc::import_aicc('.$course_code.')', 0);
250
        }
251
252
        $new_lp = Database::get_course_table(TABLE_LP_MAIN);
253
        $new_lp_item = Database::get_course_table(TABLE_LP_ITEM);
254
        $get_max = "SELECT MAX(display_order) FROM $new_lp WHERE c_id = $course_id";
255
        $res_max = Database::query($get_max);
256
        if (Database::num_rows($res_max) < 1) {
257
            $dsp = 1;
258
        } else {
259
            $row = Database::fetch_array($res_max);
260
            $dsp = $row[0] + 1;
261
        }
262
263
        $this->config_encoding = "ISO-8859-1"; // TODO: We may apply detection for this value, see the function api_detect_encoding().
264
265
        $sql = "INSERT INTO $new_lp (c_id, lp_type, name, ref, description, path, force_commit, default_view_mod, default_encoding, js_lib, content_maker,display_order)".
266
                "VALUES ".
267
                "($course_id, 3, '".$this->course_title."', '".$this->course_id."','".$this->course_description."',".
268
                "'".$this->subdir."', 0, 'embedded', '".$this->config_encoding."',".
269
                "'aicc_api.php','".$this->course_creator."',$dsp)";
270
        if ($this->debug > 2) {
271
            error_log('New LP - In import_aicc(), inserting path: '.$sql, 0);
272
        }
273
        Database::query($sql);
274
        $lp_id = Database::insert_id();
275
276
        if ($lp_id) {
277
            $this->lp_id = $lp_id;
278
            /*
279
            api_item_property_update(
280
                $courseInfo,
281
                TOOL_LEARNPATH,
282
                $this->lp_id,
283
                'LearnpathAdded',
284
                api_get_user_id()
285
            );
286
287
            api_item_property_update(
288
                $courseInfo,
289
                TOOL_LEARNPATH,
290
                $this->lp_id,
291
                'visible',
292
                api_get_user_id()
293
            );*/
294
        }
295
296
        $previous = 0;
297
        foreach ($this->aulist as $identifier => $dummy) {
298
            $oAu = &$this->aulist[$identifier];
299
            //echo "Item ".$oAu->identifier;
300
            $field_add = '';
301
            $value_add = '';
302
            if (!empty($oAu->masteryscore)) {
303
                $field_add = 'mastery_score, ';
304
                $value_add = $oAu->masteryscore.',';
305
            }
306
            $title = $oAu->identifier;
307
            if (is_object($this->deslist[$identifier])) {
308
                $title = $this->deslist[$identifier]->title;
309
            }
310
            $path = $oAu->path;
311
            //$max_score = $oAu->max_score // TODO: Check if special constraint exists for this item.
312
            //$min_score = $oAu->min_score // TODO: Check if special constraint exists for this item.
313
            $parent = 0; // TODO: Deal with the parent.
314
            $previous = 0;
315
            $prereq = $oAu->prereq_string;
316
            $sql_item = "INSERT INTO $new_lp_item (c_id, lp_id,item_type,ref,title, path,min_score,max_score, $field_add parent_item_id,previous_item_id,next_item_id, prerequisite,display_order,parameters) ".
317
                    "VALUES ".
318
                    "($course_id, $lp_id, 'au','".$oAu->identifier."','".$title."',".
319
                    "'$path',0,100, $value_add".
320
                    "$parent, $previous, 0, ".
321
                    "'$prereq', 0,'".(!empty($oAu->parameters) ? Database::escape_string($oAu->parameters) : '')."'".
322
                    ")";
323
            Database::query($sql_item);
324
            if ($this->debug > 1) {
325
                error_log('New LP - In aicc::import_aicc() - inserting item : '.$sql_item.' : ', 0);
326
            }
327
            $item_id = Database::insert_id();
328
            // Now update previous item to change next_item_id.
329
            if (0 != $previous) {
330
                $upd = "UPDATE $new_lp_item SET next_item_id = $item_id WHERE c_id = $course_id AND id = $previous";
331
                Database::query($upd);
332
                // Update the previous item id.
333
            }
334
            $previous = $item_id;
335
        }
336
    }
337
338
    /**
339
     * Intermediate to import_package only to allow import from local zip files.
340
     *
341
     * @param string    Path to the zip file, from the dokeos sys root
342
     * @param string    Current path (optional)
343
     *
344
     * @return string Absolute path to the AICC description files or empty string on error
345
     */
346
    public function import_local_package($file_path, $current_dir = '')
347
    {
348
        // TODO: Prepare info as given by the $_FILES[''] vector.
349
        $file_info = [];
350
        $file_info['tmp_name'] = $file_path;
351
        $file_info['name'] = basename($file_path);
352
        // Call the normal import_package function.
353
        return $this->import_package($file_info, $current_dir);
354
    }
355
356
    /**
357
     * Imports a zip file (presumably AICC) into the Chamilo structure.
358
     *
359
     * @param string    Zip file info as given by $_FILES['userFile']
360
     *
361
     * @return string Absolute path to the AICC config files directory or empty string on error
362
     */
363
    public function import_package($zip_file_info, $current_dir = '')
364
    {
365
        if ($this->debug > 0) {
366
            error_log('In aicc::import_package('.print_r($zip_file_info, true).',"'.$current_dir.'") method', 0);
0 ignored issues
show
Bug introduced by
Are you sure print_r($zip_file_info, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

366
            error_log('In aicc::import_package('./** @scrutinizer ignore-type */ print_r($zip_file_info, true).',"'.$current_dir.'") method', 0);
Loading history...
367
        }
368
        //ini_set('error_log', 'E_ALL');
369
        $maxFilledSpace = 1000000000;
370
        $zip_file_path = $zip_file_info['tmp_name'];
371
        $zip_file_name = $zip_file_info['name'];
372
373
        if ($this->debug > 0) {
374
            error_log(
375
                'New LP - aicc::import_package() - Zip file path = '.$zip_file_path.', zip file name = '.$zip_file_name,
376
                0
377
            );
378
        }
379
        $course_rel_dir = api_get_course_path().'/scorm'; // Scorm dir web path starting from /courses
0 ignored issues
show
Bug introduced by
The function api_get_course_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

379
        $course_rel_dir = /** @scrutinizer ignore-call */ api_get_course_path().'/scorm'; // Scorm dir web path starting from /courses
Loading history...
380
        $course_sys_dir = api_get_path(SYS_COURSE_PATH).$course_rel_dir; // The absolute system path of this course.
0 ignored issues
show
Bug introduced by
The constant SYS_COURSE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
381
        $current_dir = api_replace_dangerous_char(trim($current_dir)); // Current dir we are in, inside scorm/
382
        if ($this->debug > 0) {
383
            error_log('New LP - aicc::import_package() - Current_dir = '.$current_dir, 0);
384
        }
385
386
        //$uploaded_filename = $_FILES['userFile']['name'];
387
        // Get the name of the zip file without the extension.
388
        if ($this->debug > 0) {
389
            error_log('New LP - aicc::import_package() - Received zip file name: '.$zip_file_path, 0);
390
        }
391
        $file_info = pathinfo($zip_file_name);
392
        $filename = $file_info['basename'];
393
        $extension = $file_info['extension'];
394
        $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
395
        $this->zipname = $file_base_name; // Save for later in case we don't have a title.
396
397
        if ($this->debug > 0) {
398
            error_log('New LP - aicc::import_package() - Base file name is : '.$file_base_name, 0);
399
        }
400
        $new_dir = api_replace_dangerous_char(trim($file_base_name));
401
        $this->subdir = $new_dir;
402
        if ($this->debug > 0) {
403
            error_log('New LP - aicc::import_package() - Subdir is first set to : '.$this->subdir, 0);
404
        }
405
406
        /*
407
        if (check_name_exist($course_sys_dir.$current_dir.'/'.$new_dir)) {
408
            $dialogBox = get_lang('The operation is impossible, a file with this name already exists.');
409
            $stopping_error = true;
410
        }
411
        */
412
        $zipFile = new PclZip($zip_file_path);
413
        // Check the zip content (real size and file extension).
414
        $zipContentArray = $zipFile->listContent();
415
416
        $package_type = ''; // The type of the package. Should be 'aicc' after the next few lines.
417
        $package = ''; // The basename of the config files (if 'courses.crs' => 'courses').
418
        $at_root = false; // Check if the config files are at zip root.
419
        $config_dir = ''; // The directory in which the config files are. May remain empty.
420
        $files_found = [];
421
        $subdir_isset = false;
422
        // The following loop should be stopped as soon as we found the right config files (.crs, .au, .des and .cst).
423
        $realFileSize = 0;
424
        foreach ($zipContentArray as $thisContent) {
425
            if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
426
                // If a php file is found, do not authorize (security risk).
427
                if ($this->debug > 1) {
428
                    error_log(
429
                        'New LP - aicc::import_package() - Found unauthorized file: '.$thisContent['filename']
430
                    );
431
                }
432
                Display::addFlash(
433
                    Display::return_message(get_lang('The zip file can not contain .PHP files'))
434
                );
435
436
                return false;
437
            } elseif (preg_match('?.*/aicc/$?', $thisContent['filename'])) {
438
                // If a directory named 'aicc' is found, package type = aicc, but continue,
439
                // because we need to find the right AICC files;
440
                if ($this->debug > 1) {
441
                    error_log('New LP - aicc::import_package() - Found aicc directory: '.$thisContent['filename']);
442
                }
443
                $package_type = 'aicc';
444
            } else {
445
                // else, look for one of the files we're searching for (something.crs case insensitive).
446
                $res = [];
447
                if (preg_match('?^(.*)\.(crs|au|des|cst|ore|pre|cmp)$?i', $thisContent['filename'], $res)) {
448
                    if ($this->debug > 1) {
449
                        error_log(
450
                            'New LP - aicc::import_package() - Found AICC config file: '.$thisContent['filename'].'. Now splitting: '.$res[1].' and '.$res[2]
451
                        );
452
                    }
453
                    if ($thisContent['filename'] == basename($thisContent['filename'])) {
454
                        if ($this->debug > 2) {
455
                            error_log('New LP - aicc::import_package() - '.$thisContent['filename'].' is at root level', 0);
456
                        }
457
                        $at_root = true;
458
                        if (!is_array($files_found[$res[1]])) {
459
                            $files_found[$res[1]] = $this->config_exts; // Initialise list of expected extensions (defined in class definition).
460
                        }
461
                        $files_found[$res[1]][api_strtolower($res[2])] = $thisContent['filename'];
462
                        $subdir_isset = true;
463
                    } else {
464
                        if (!$subdir_isset) {
465
                            if (preg_match('?^.*/aicc$?i', dirname($thisContent['filename']))) {
466
                                //echo "Cutting subdir<br/>";
467
                                $this->subdir .= '/'.substr(dirname($thisContent['filename']), 0, -5);
468
                            } else {
469
                                //echo "Not cutting subdir<br/>";
470
                                $this->subdir .= '/'.dirname($thisContent['filename']);
471
                            }
472
                            $subdir_isset = true;
473
                        }
474
                        if ($this->debug > 2) {
475
                            error_log('New LP - aicc::import_package() - '.$thisContent['filename'].' is not at root level - recording subdir '.$this->subdir, 0);
476
                        }
477
                        $config_dir = dirname($thisContent['filename']); // Just the relative directory inside scorm/
478
                        if (!is_array($files_found[basename($res[1])])) {
479
                            $files_found[basename($res[1])] = $this->config_exts;
480
                        }
481
                        $files_found[basename($res[1])][api_strtolower($res[2])] = basename($thisContent['filename']);
482
                    }
483
                    $package_type = 'aicc';
484
                } else {
485
                    if ($this->debug > 3) {
486
                        error_log('New LP - aicc::import_package() - File '.$thisContent['filename'].' didnt match any check', 0);
487
                    }
488
                }
489
            }
490
            $realFileSize += $thisContent['size'];
491
        }
492
        if ($this->debug > 2) {
493
            error_log('New LP - aicc::import_package() - $files_found: '.print_r($files_found, true), 0);
0 ignored issues
show
Bug introduced by
Are you sure print_r($files_found, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

493
            error_log('New LP - aicc::import_package() - $files_found: './** @scrutinizer ignore-type */ print_r($files_found, true), 0);
Loading history...
494
        }
495
        if ($this->debug > 1) {
496
            error_log('New LP - aicc::import_package() - Package type is now '.$package_type, 0);
497
        }
498
        $mandatory = false;
499
        foreach ($files_found as $file_name => $file_exts) {
500
            $temp = (
501
                !empty($files_found[$file_name]['crs'])
502
                && !empty($files_found[$file_name]['au'])
503
                && !empty($files_found[$file_name]['des'])
504
                && !empty($files_found[$file_name]['cst'])
505
            );
506
            if ($temp) {
507
                if ($this->debug > 1) {
508
                    error_log('New LP - aicc::import_package() - Found all config files for '.$file_name, 0);
509
                }
510
                $mandatory = true;
511
                $package = $file_name;
512
                // Store base config file name for reuse in parse_config_files().
513
                $this->config_basename = $file_name;
514
                // Store filenames for reuse in parse_config_files().
515
                $this->config_files = $files_found[$file_name];
516
                // Get out, we only want one config files set.
517
                break;
518
            }
519
        }
520
521
        if ('' == $package_type || !$mandatory) {
522
            Display::addFlash(
523
                Display::return_message(get_lang('The file to upload is not valid.'))
524
            );
525
526
            return false;
527
        }
528
529
        /*if (!enough_size($realFileSize, $course_sys_dir, $maxFilledSpace)) {
530
            Display::addFlash(
531
                Display::return_message(get_lang('The upload has failed. Either you have exceeded your maximum quota, or there is not enough disk space.'))
532
            );
533
534
            return false;
535
        }*/
536
537
        // It happens on Linux that $new_dir sometimes doesn't start with '/'
538
        if ('/' != $new_dir[0]) {
539
            $new_dir = '/'.$new_dir;
540
        }
541
        // Cut trailing slash.
542
        if ('/' == $new_dir[strlen($new_dir) - 1]) {
543
            $new_dir = substr($new_dir, 0, -1);
544
        }
545
546
        /* Uncompressing phase */
547
548
        /*
549
            We need to process each individual file in the zip archive to
550
            - add it to the database
551
            - parse & change relative html links
552
            - make sure the filenames are secure (filter funny characters or php extensions)
553
        */
554
        if (is_dir($course_sys_dir.$new_dir) ||
555
            @mkdir($course_sys_dir.$new_dir, api_get_permissions_for_new_directories())
556
        ) {
557
            // PHP method - slower...
558
            if ($this->debug >= 1) {
559
                error_log('New LP - Changing dir to '.$course_sys_dir.$new_dir, 0);
560
            }
561
            chdir($course_sys_dir.$new_dir);
562
            $zipFile->extract(
563
                PCLZIP_CB_PRE_EXTRACT,
0 ignored issues
show
Bug introduced by
The constant PCLZIP_CB_PRE_EXTRACT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
564
                'clean_up_files_in_zip'
565
            );
566
        } else {
567
            return '';
568
        }
569
570
        return $course_sys_dir.$new_dir.$config_dir;
571
    }
572
573
    /**
574
     * Exports the current AICC object's files as a zip. Excerpts taken from learnpath_functions.inc.php::exportpath().
575
     *
576
     * @param    int    Learnpath ID (optional, taken from object context if not defined)
577
     *
578
     * @return bool
579
     */
580
    public function export_zip($lp_id = null)
581
    {
582
        if ($this->debug > 0) {
583
            error_log('In aicc::export_zip method('.$lp_id.')', 0);
584
        }
585
        if (empty($lp_id)) {
586
            if (!is_object($this)) {
587
                return false;
588
            } else {
589
                $id = $this->get_id();
590
                if (empty($id)) {
591
                    return false;
592
                } else {
593
                    $lp_id = $this->get_id();
594
                }
595
            }
596
        }
597
        // Zip everything that is in the corresponding scorm dir.
598
        // Write the zip file somewhere (might be too big to return).
599
        $course_id = api_get_course_int_id();
600
        $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
601
        $_course = api_get_course_info(api_get_course_id());
602
603
        $sql = "SELECT * FROM $tbl_lp WHERE iid= $lp_id";
604
        $result = Database::query($sql);
605
        $row = Database::fetch_array($result);
606
        $LPname = $row['path'];
607
        $list = explode('/', $LPname);
608
        $LPnamesafe = $list[0];
609
        $zipfoldername = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/temp/'.$LPnamesafe;
0 ignored issues
show
Bug introduced by
The constant SYS_COURSE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
610
        $scormfoldername = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/scorm/'.$LPnamesafe;
611
        $zipfilename = $zipfoldername.'/'.$LPnamesafe.'.zip';
612
613
        // Get a temporary dir for creating the zip file.
614
        removeDir($zipfoldername); //make sure the temp dir is cleared
615
        mkdir($zipfoldername, api_get_permissions_for_new_directories());
616
617
        // @todo use ZipFile
618
        // Create zipfile of given directory.
619
        $zip_folder = new PclZip($zipfilename);
620
        $zip_folder->create($scormfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $scormfoldername.'/');
0 ignored issues
show
Bug introduced by
The constant PCLZIP_OPT_REMOVE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
621
622
        //this file sending implies removing the default mime-type from php.ini
623
        DocumentManager::file_send_for_download($zipfilename, true);
624
625
        return true;
626
    }
627
628
    /**
629
     * Gets a resource's path if available, otherwise return empty string.
630
     *
631
     * @param	string	Resource ID as used in resource array
632
     *
633
     * @return string The resource's path as declared in config file course.crs
634
     */
635
    public function get_res_path($id)
636
    {
637
        if ($this->debug > 0) {
638
            error_log('In aicc::get_res_path('.$id.') method', 0);
639
        }
640
        $path = '';
641
        if (isset($this->resources[$id])) {
0 ignored issues
show
Bug Best Practice introduced by
The property resources does not exist on aicc. Did you maybe forget to declare it?
Loading history...
642
            $oRes = &$this->resources[$id];
643
            $path = @$oRes->get_path();
644
        }
645
646
        return $path;
647
    }
648
649
    /**
650
     * Gets a resource's type if available, otherwise return empty string.
651
     *
652
     * @param	string	Resource ID as used in resource array
653
     *
654
     * @return string The resource's type as declared in the assignable unit (.au) file
655
     */
656
    public function get_res_type($id)
657
    {
658
        if ($this->debug > 0) {
659
            error_log('In aicc::get_res_type('.$id.') method', 0);
660
        }
661
        $type = '';
662
        if (isset($this->resources[$id])) {
0 ignored issues
show
Bug Best Practice introduced by
The property resources does not exist on aicc. Did you maybe forget to declare it?
Loading history...
663
            $oRes = &$this->resources[$id];
664
            $temptype = $oRes->get_scorm_type();
665
            if (!empty($temptype)) {
666
                $type = $temptype;
667
            }
668
        }
669
670
        return $type;
671
    }
672
673
    /**
674
     * Gets the default organisation's title.
675
     *
676
     * @return string The organization's title
677
     */
678
    public function get_title()
679
    {
680
        if ($this->debug > 0) {
681
            error_log('In aicc::get_title() method', 0);
682
        }
683
        $title = '';
684
        if (isset($this->config['organizations']['default'])) {
685
            $title = $this->organizations[$this->config['organizations']['default']]->get_name();
0 ignored issues
show
Bug Best Practice introduced by
The property organizations does not exist on aicc. Did you maybe forget to declare it?
Loading history...
686
        } elseif (1 == count($this->organizations)) {
687
            // This will only get one title but so we don't need to know the index.
688
            foreach ($this->organizations as $id => $value) {
689
                $title = $this->organizations[$id]->get_name();
690
                break;
691
            }
692
        }
693
694
        return $title;
695
    }
696
697
    /**
698
     * TODO: Implement this function to restore items data from a set of AICC config files,
699
     * updating the existing table... This will prove very useful in case initial data
700
     * from config files were not imported well enough.
701
     */
702
    public function reimport_aicc()
703
    {
704
        if ($this->debug > 0) {
705
            error_log('In aicc::reimport_aicc() method', 0);
706
        }
707
        //query current items list
708
        //get the identifiers
709
        //parse the config files
710
        //match both
711
        //update DB accordingly
712
        return true;
713
    }
714
715
    /**
716
     * Static function to parse AICC ini files.
717
     * Based on work by sinedeo at gmail dot com published on php.net (parse_ini_file()).
718
     *
719
     * @param	string	File path
720
     *
721
     * @return array Structured array
722
     */
723
    public function parse_ini_file_quotes_safe($f)
724
    {
725
        $null = '';
726
        $r = $null;
727
        $sec = $null;
728
        $f = @file_get_contents($f);
729
        $f = api_convert_encoding($f, api_get_system_encoding(), $this->config_encoding);
730
        $f = preg_split('/\r?\n/', $f);
731
        for ($i = 0; $i < @count($f); $i++) {
732
            $newsec = 0;
733
            $w = @trim($f[$i]);
734
            if (';' == substr($w, 0, 1)) {
735
                // Ignore comment lines
736
                continue;
737
            }
738
            if ($w) {
739
                if ((!$r) or ($sec)) {
740
                    if (('[' == @substr($w, 0, 1)) and ']' == (@substr($w, -1, 1))) {
741
                        $sec = @substr($w, 1, @strlen($w) - 2);
742
                        $newsec = 1;
743
                    }
744
                }
745
                if (!$newsec) {
746
                    $w = @explode('=', $w);
747
                    $k = @trim($w[0]);
748
                    unset($w[0]);
749
                    $v = @trim(@implode('=', $w));
750
                    if (("\"" == @substr($v, 0, 1)) and ("\"" == @substr($v, -1, 1))) {
751
                        $v = @substr($v, 1, @strlen($v) - 2);
752
                    }
753
                    if ($sec) {
754
                        if ('course_description' == api_strtolower($sec)) { // A special case.
755
                            $r[api_strtolower($sec)] = $k;
756
                        } else {
757
                            $r[api_strtolower($sec)][api_strtolower($k)] = $v;
758
                        }
759
                    } else {
760
                        $r[api_strtolower($k)] = $v;
761
                    }
762
                }
763
            }
764
        }
765
766
        return $r;
767
    }
768
769
    /**
770
     * Static function to parse AICC ini strings.
771
     * Based on work by sinedeo at gmail dot com published on php.net (parse_ini_file()).
772
     *
773
     * @param		string	INI File string
774
     * @param		array	List of names of sections that should be considered
775
     * as containing only hard string data (no variables), provided in lower case
776
     *
777
     * @return array Structured array
778
     */
779
    public function parse_ini_string_quotes_safe($s, $pure_strings = [])
780
    {
781
        $null = '';
782
        $r = $null;
783
        $sec = $null;
784
        $s = api_convert_encoding($s, api_get_system_encoding(), $this->config_encoding);
785
        //$f = split("\r\n", $s);
786
        $f = preg_split('/\r?\n/', $s);
787
        for ($i = 0; $i < @count($f); $i++) {
788
            $newsec = 0;
789
            $w = @trim($f[$i]);
790
            if (';' == substr($w, 0, 1)) {
791
                // Ignore comment lines
792
                continue;
793
            }
794
            if ($w) {
795
                if ((!$r) or ($sec)) {
796
                    if (('[' == @substr($w, 0, 1)) and ']' == (@substr($w, -1, 1))) {
797
                        $sec = @substr($w, 1, @strlen($w) - 2);
798
                        $pure_data = 0;
799
                        if (in_array(api_strtolower($sec), $pure_strings)) {
800
                            // This section can only be considered as pure string data (until the next section).
801
                            $pure_data = 1;
802
                            $r[api_strtolower($sec)] = '';
803
                        }
804
                        $newsec = 1;
805
                    }
806
                }
807
                if (!$newsec) {
808
                    $w = @explode('=', $w);
809
                    $k = @trim($w[0]);
810
                    unset($w[0]);
811
                    $v = @trim(@implode('=', $w));
812
                    if (("\"" == @substr($v, 0, 1)) and ("\"" == @substr($v, -1, 1))) {
813
                        $v = @substr($v, 1, @strlen($v) - 2);
814
                    }
815
                    if ($sec) {
816
                        if ($pure_data) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pure_data does not seem to be defined for all execution paths leading up to this point.
Loading history...
817
                            $r[api_strtolower($sec)] .= $f[$i];
818
                        } else {
819
                            if ('course_description' == api_strtolower($sec)) { // A special case.
820
                                $r[api_strtolower($sec)] = $k;
821
                            } else {
822
                                $r[api_strtolower($sec)][api_strtolower($k)] = $v;
823
                            }
824
                        }
825
                    } else {
826
                        $r[api_strtolower($k)] = $v;
827
                    }
828
                }
829
            }
830
        }
831
832
        return $r;
833
    }
834
835
    /**
836
     * Static function that parses CSV files into simple arrays, based on a function
837
     * by spam at cyber-space dot nl published on php.net (fgetcsv()).
838
     *
839
     * @param	string	Filepath
840
     * @param	string	CSV delimiter
841
     * @param	string	CSV enclosure
842
     * @param	bool	Might one field name happen more than once on the same line? (then split by comma in the values)
843
     *
844
     * @return array Simple structured array
845
     */
846
    public function parse_csv_file($f, $delim = ',', $enclosure = '"', $multiples = false)
847
    {
848
        $data = @file_get_contents($f);
849
        $data = api_convert_encoding($data, api_get_system_encoding(), $this->config_encoding);
850
        $enclosed = false;
851
        $fldcount = 0;
852
        $linecount = 0;
853
        $fldval = '';
854
        for ($i = 0; $i < strlen($data); $i++) {
855
            $chr = $data[$i];
856
            switch ($chr) {
857
                case $enclosure:
858
                    if ($enclosed && $data[$i + 1] == $enclosure) {
859
                        $fldval .= $chr;
860
                        $i++; // Skip the next character.
861
                    } else {
862
                        $enclosed = !$enclosed;
863
                    }
864
                    break;
865
                case $delim:
866
                    if (!$enclosed) {
867
                        $ret_array[$linecount][$fldcount++] = $fldval;
868
                        $fldval = '';
869
                    } else {
870
                        $fldval .= $chr;
871
                    }
872
                    break;
873
                case "\r":
874
                    if (!$enclosed && "\n" == $data[$i + 1]) {
875
                        break;
876
                    }
877
                    // no break
878
                case "\n":
879
                    if (!$enclosed) {
880
                        $ret_array[$linecount++][$fldcount] = $fldval;
881
                        $fldcount = 0;
882
                        $fldval = '';
883
                    } else {
884
                        $fldval .= $chr;
885
                    }
886
                    break;
887
                case "\\r":
888
                    if (!$enclosed && "\\n" == $data[$i + 1]) {
889
                        break;
890
                    }
891
                    // no break
892
                case "\\n":
893
                    if (!$enclosed) {
894
                        $ret_array[$linecount++][$fldcount] = $fldval;
895
                        $fldcount = 0;
896
                        $fldval = '';
897
                    } else {
898
                        $fldval .= $chr;
899
                    }
900
                    break;
901
                default:
902
                    $fldval .= $chr;
903
            }
904
        }
905
        if ($fldval) {
906
            $ret_array[$linecount][$fldcount] = $fldval;
907
        }
908
        // Transform the array to use the first line as titles.
909
        $titles = [];
910
        $ret_ret_array = [];
911
        foreach ($ret_array as $line_idx => $line) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret_array does not seem to be defined for all execution paths leading up to this point.
Loading history...
912
            if (0 == $line_idx) {
913
                $titles = $line;
914
            } else {
915
                $ret_ret_array[$line_idx] = [];
916
                foreach ($line as $idx => $val) {
917
                    if ($multiples && !empty($ret_ret_array[$line_idx][api_strtolower($titles[$idx])])) {
918
                        $ret_ret_array[$line_idx][api_strtolower($titles[$idx])] .= ','.$val;
919
                    } else {
920
                        $ret_ret_array[$line_idx][api_strtolower($titles[$idx])] = $val;
921
                    }
922
                }
923
            }
924
        }
925
926
        return $ret_ret_array;
927
    }
928
}
929