* @author Bert Vanderkimpen
*
* @param array $_course
* @param array $uploaded_file_collection - follows the $_FILES Structure
* @param string $base_work_dir
* @param string $missing_files_dir
* @param int $user_id
* @param int $to_group_id group.id
*/
function move_uploaded_file_collection_into_directory(
$_course,
$uploaded_file_collection,
$base_work_dir,
$missing_files_dir,
$user_id,
$to_group_id,
$to_user_id,
$max_filled_space
) {
$number_of_uploaded_images = count($uploaded_file_collection['name']);
$list = [];
for ($i = 0; $i < $number_of_uploaded_images; $i++) {
$missing_file['name'] = $uploaded_file_collection['name'][$i];
$missing_file['type'] = $uploaded_file_collection['type'][$i];
$missing_file['tmp_name'] = $uploaded_file_collection['tmp_name'][$i];
$missing_file['error'] = $uploaded_file_collection['error'][$i];
$missing_file['size'] = $uploaded_file_collection['size'][$i];
$upload_ok = process_uploaded_file($missing_file);
if ($upload_ok) {
$list[] = handle_uploaded_document(
$_course,
$missing_file,
$base_work_dir,
$missing_files_dir,
$user_id,
$to_group_id,
$to_user_id,
$max_filled_space,
0,
'overwrite'
);
}
unset($missing_file);
}
return $list;
}
/**
* Opens the old html file and replace the src path into the img tag
* This also works for files in subdirectories.
*
* @param $original_img_path is an array
* @param $new_img_path is an array
*/
function replace_img_path_in_html_file($original_img_path, $new_img_path, $html_file)
{
// Open the file
$fp = fopen($html_file, 'r');
$buffer = fread($fp, filesize($html_file));
$new_html_content = '';
// Fix the image tags
for ($i = 0, $fileNb = count($original_img_path); $i < $fileNb; $i++) {
$replace_what = $original_img_path[$i];
// We only need the directory and the filename /path/to/file_html_files/missing_file.gif -> file_html_files/missing_file.gif
$exploded_file_path = explode('/', $new_img_path[$i]);
$replace_by = $exploded_file_path[count($exploded_file_path) - 2].'/'.$exploded_file_path[count($exploded_file_path) - 1];
$buffer = str_replace($replace_what, $replace_by, $buffer);
}
$new_html_content .= $buffer;
@fclose($fp);
// Write the resulted new file
if (!$fp = fopen($html_file, 'w')) {
return;
}
if (!fwrite($fp, $new_html_content)) {
return;
}
}
/**
* Checks the extension of a file, if it's .htm or .html
* we use search_img_from_html to get all image paths in the file.
*
* @param string $file
*
* @return array paths
*
* @see check_for_missing_files() uses search_img_from_html()
*/
function check_for_missing_files($file)
{
if (strrchr($file, '.') == '.htm' || strrchr($file, '.') == '.html') {
$img_file_path = search_img_from_html($file);
return $img_file_path;
}
return false;
}
/**
* This function builds a form that asks for the missing images in a html file
* maybe we should do this another way?
*
* @param array $missing_files
* @param string $upload_path
* @param string $file_name
*
* @return string the form
*/
function build_missing_files_form($missing_files, $upload_path, $file_name)
{
// Do we need a / or not?
$added_slash = ($upload_path == '/') ? '' : '/';
$folder_id = DocumentManager::get_document_id(api_get_course_info(), $upload_path);
// Build the form
$form = "".get_lang('MissingImagesDetected')."
"
."";
return $form;
}
/**
* This recursive function can be used during the upgrade process form older
* versions of Chamilo
* It crawls the given directory, checks if the file is in the DB and adds
* it if it's not.
*
* @param array $courseInfo
* @param array $userInfo
* @param string $base_work_dir course document dir
* @param string $folderPath folder to read
* @param int $sessionId
* @param int $groupId group.id
* @param bool $output
* @param array $parent
* @param string $whatIfFileExists
*
* @return bool
*/
function add_all_documents_in_folder_to_database(
$courseInfo,
$userInfo,
$base_work_dir,
$folderPath,
$sessionId = 0,
$groupId = 0,
$output = false,
$parent = [],
$whatIfFileExists = 'overwrite'
) {
if (empty($userInfo) || empty($courseInfo)) {
return false;
}
$userId = $userInfo['user_id'];
// Open dir
$handle = opendir($folderPath);
if (is_dir($folderPath)) {
// Run trough
while ($file = readdir($handle)) {
if ($file == '.' || $file == '..') {
continue;
}
$parentPath = '';
if (!empty($parent) && isset($parent['path'])) {
$parentPath = $parent['path'];
if ($parentPath == '/') {
$parentPath = '';
}
}
$completePath = $parentPath.'/'.$file;
$sysFolderPath = $folderPath.'/'.$file;
// Is directory?
if (is_dir($sysFolderPath)) {
$folderExists = DocumentManager::folderExists(
$completePath,
$courseInfo,
$sessionId,
$groupId
);
if ($folderExists === true) {
switch ($whatIfFileExists) {
case 'overwrite':
$documentId = DocumentManager::get_document_id($courseInfo, $completePath, $sessionId);
if ($documentId) {
$newFolderData = DocumentManager::get_document_data_by_id(
$documentId,
$courseInfo['code'],
false,
$sessionId
);
}
break;
case 'rename':
$newFolderData = create_unexisting_directory(
$courseInfo,
$userId,
$sessionId,
$groupId,
null,
$base_work_dir,
$completePath,
null,
null,
true
);
break;
case 'nothing':
if ($output) {
$documentId = DocumentManager::get_document_id($courseInfo, $completePath, $sessionId);
if ($documentId) {
$folderData = DocumentManager::get_document_data_by_id(
$documentId,
$courseInfo['code'],
false,
$sessionId
);
Display::addFlash(
Display::return_message(
$folderData['path'].' '.get_lang('UplAlreadyExists'),
'warning'
)
);
}
}
continue 2;
break;
}
} else {
$newFolderData = create_unexisting_directory(
$courseInfo,
$userId,
$sessionId,
$groupId,
null,
$base_work_dir,
$completePath,
null,
null,
false
);
}
// Recursive
add_all_documents_in_folder_to_database(
$courseInfo,
$userInfo,
$base_work_dir,
$sysFolderPath,
$sessionId,
$groupId,
$output,
$newFolderData,
$whatIfFileExists
);
} else {
// Rename
$uploadedFile = [
'name' => $file,
'tmp_name' => $sysFolderPath,
'size' => filesize($sysFolderPath),
'type' => null,
'from_file' => true,
'move_file' => true,
];
handle_uploaded_document(
$courseInfo,
$uploadedFile,
$base_work_dir,
$parentPath,
$userId,
$groupId,
null,
0,
$whatIfFileExists,
$output,
false,
null,
$sessionId
);
}
}
}
}