Skip to content
Permalink
Browse files

Use "clean_up_files_in_zip" function before extracting content

Blocks php/htaccess files
  • Loading branch information...
jmontoyaa committed Feb 26, 2019
1 parent 4a72b55 commit c245b03308f8274b93f2a39e5435d5e9e4b6aecf
Showing with 18 additions and 132 deletions.
  1. +4 −61 main/lp/aicc.class.php
  2. +7 −70 main/lp/scorm.class.php
  3. +7 −1 src/Chamilo/CourseBundle/Component/CourseCopy/CourseArchiver.php
@@ -577,68 +577,11 @@ public function import_package($zip_file_info, $current_dir = '')
if ($this->debug >= 1) {
error_log('New LP - Changing dir to '.$course_sys_dir.$new_dir, 0);
}
$saved_dir = getcwd();
chdir($course_sys_dir.$new_dir);
$unzippingState = $zipFile->extract();
for ($j = 0; $j < count($unzippingState); $j++) {
$state = $unzippingState[$j];
// TODO: Fix relative links in html files (?)
$extension = strrchr($state["stored_filename"], '.');
//if ($this->debug > 1) { error_log('New LP - found extension '.$extension.' in '.$state['stored_filename'], 0); }
}
if (!empty($new_dir)) {
$new_dir = $new_dir.'/';
}
// Rename files, for example with \\ in it.
if ($dir = @opendir($course_sys_dir.$new_dir)) {
if ($this->debug == 1) {
error_log('New LP - Opened dir '.$course_sys_dir.$new_dir, 0);
}
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
$filetype = 'file';
if (is_dir($course_sys_dir.$new_dir.$file)) {
$filetype = 'folder';
}
// TODO: RENAMING FILES CAN BE VERY DANGEROUS AICC-WISE, avoid that as much as possible!
//$safe_file = api_replace_dangerous_char($file, 'strict');
$find_str = ['\\', '.php', '.phtml'];
$repl_str = ['/', '.txt', '.txt'];
$safe_file = str_replace($find_str, $repl_str, $file);
if ($safe_file != $file) {
//@rename($course_sys_dir.$new_dir, $course_sys_dir.'/'.$safe_file);
$mydir = dirname($course_sys_dir.$new_dir.$safe_file);
if (!is_dir($mydir)) {
$mysubdirs = split('/', $mydir);
$mybasedir = '/';
foreach ($mysubdirs as $mysubdir) {
if (!empty($mysubdir)) {
$mybasedir = $mybasedir.$mysubdir.'/';
if (!is_dir($mybasedir)) {
@mkdir($mybasedir, api_get_permissions_for_new_directories());
if ($this->debug == 1) {
error_log('New LP - Dir '.$mybasedir.' doesnt exist. Creating.');
}
}
}
}
}
@rename($course_sys_dir.$new_dir.$file, $course_sys_dir.$new_dir.$safe_file);
if ($this->debug == 1) {
error_log('New LP - Renaming '.$course_sys_dir.$new_dir.$file.' to '.$course_sys_dir.$new_dir.$safe_file);
}
}
}
}
closedir($dir);
chdir($saved_dir);
}
$zipFile->extract(
PCLZIP_CB_PRE_EXTRACT,
'clean_up_files_in_zip'
);
} else {
return '';
}
@@ -742,81 +742,18 @@ public function import_package(
if ($this->debug >= 1) {
error_log('New LP - Changing dir to '.$courseSysDir.$newDir);
}
$savedDir = getcwd();
chdir($courseSysDir.$newDir);
$unzippingState = $zipFile->extract();
for ($j = 0; $j < count($unzippingState); $j++) {
$state = $unzippingState[$j];
// TODO: Fix relative links in html files (?)
$extension = strrchr($state['stored_filename'], '.');
if ($this->debug >= 1) {
error_log('New LP - found extension '.$extension.' in '.$state['stored_filename']);
}
}
$zipFile->extract(
PCLZIP_CB_PRE_EXTRACT,
'clean_up_files_in_zip'
);
if (!empty($newDir)) {
$newDir = $newDir.'/';
}
// Rename files, for example with \\ in it.
if ($this->debug >= 1) {
error_log('New LP - try to open: '.$courseSysDir.$newDir);
}
if ($dir = @opendir($courseSysDir.$newDir)) {
if ($this->debug >= 1) {
error_log('New LP - Opened dir '.$courseSysDir.$newDir);
}
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
// TODO: RENAMING FILES CAN BE VERY DANGEROUS SCORM-WISE, avoid that as much as possible!
//$safeFile = api_replace_dangerous_char($file, 'strict');
$findStr = ['\\', '.php', '.phtml'];
$replStr = ['/', '.txt', '.txt'];
$safeFile = str_replace($findStr, $replStr, $file);
if ($this->debug >= 1) {
error_log('Comparing: '.$safeFile);
error_log('and: '.$file);
}
if ($safeFile != $file) {
$mydir = dirname($courseSysDir.$newDir.$safeFile);
if (!is_dir($mydir)) {
$mysubdirs = explode('/', $mydir);
$mybasedir = '/';
foreach ($mysubdirs as $mysubdir) {
if (!empty($mysubdir)) {
$mybasedir = $mybasedir.$mysubdir.'/';
if (!is_dir($mybasedir)) {
@mkdir($mybasedir, api_get_permissions_for_new_directories());
if ($this->debug >= 1) {
error_log('New LP - Dir '.$mybasedir.' doesnt exist. Creating.');
}
}
}
}
}
@rename($courseSysDir.$newDir.$file, $courseSysDir.$newDir.$safeFile);
if ($this->debug >= 1) {
error_log(
'New LP - Renaming '.$courseSysDir.$newDir.$file.' to '.$courseSysDir.$newDir
.$safeFile
);
}
}
}
}
closedir($dir);
chdir($savedDir);
api_chmod_R($courseSysDir.$newDir, api_get_permissions_for_new_directories());
if ($this->debug > 1) {
error_log('New LP - changed back to init dir: '.$courseSysDir.$newDir);
}
}
api_chmod_R($courseSysDir.$newDir, api_get_permissions_for_new_directories());
} else {
return false;
}
@@ -293,7 +293,13 @@ public static function readCourse($filename, $delete = false)
// unzip the archive
$zip = new \PclZip($unzip_dir.'/backup.zip');
@chdir($unzip_dir);
$zip->extract(PCLZIP_OPT_TEMP_FILE_ON);
$zip->extract(
PCLZIP_OPT_TEMP_FILE_ON,
PCLZIP_CB_PRE_EXTRACT,
'clean_up_files_in_zip'
);
// remove the archive-file
if ($delete) {
@unlink($filePath);

0 comments on commit c245b03

Please sign in to comment.
You can’t perform that action at this time.