|
|
|
@ -86,7 +86,8 @@ function get_document_title($name) { |
|
|
|
* @param array $uploaded_file ($_FILES) |
|
|
|
* @param array $uploaded_file ($_FILES) |
|
|
|
* @return true if upload succeeded |
|
|
|
* @return true if upload succeeded |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function process_uploaded_file($uploaded_file, $show_output = true) { |
|
|
|
function process_uploaded_file($uploaded_file, $show_output = true) |
|
|
|
|
|
|
|
{ |
|
|
|
// Checking the error code sent with the file upload. |
|
|
|
// Checking the error code sent with the file upload. |
|
|
|
if (isset($uploaded_file['error'])) { |
|
|
|
if (isset($uploaded_file['error'])) { |
|
|
|
switch ($uploaded_file['error']) { |
|
|
|
switch ($uploaded_file['error']) { |
|
|
|
@ -95,6 +96,7 @@ function process_uploaded_file($uploaded_file, $show_output = true) { |
|
|
|
if ($show_output) { |
|
|
|
if ($show_output) { |
|
|
|
Display::display_error_message(get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize')); |
|
|
|
Display::display_error_message(get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize')); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
case 2: |
|
|
|
case 2: |
|
|
|
// The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. |
|
|
|
// The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. |
|
|
|
@ -103,18 +105,21 @@ function process_uploaded_file($uploaded_file, $show_output = true) { |
|
|
|
if ($show_output) { |
|
|
|
if ($show_output) { |
|
|
|
Display::display_error_message(get_lang('UplExceedMaxPostSize'). format_file_size($max_file_size)); |
|
|
|
Display::display_error_message(get_lang('UplExceedMaxPostSize'). format_file_size($max_file_size)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
case 3: |
|
|
|
case 3: |
|
|
|
// The uploaded file was only partially uploaded. |
|
|
|
// The uploaded file was only partially uploaded. |
|
|
|
if ($show_output) { |
|
|
|
if ($show_output) { |
|
|
|
Display::display_error_message(get_lang('UplPartialUpload').' '.get_lang('PleaseTryAgain')); |
|
|
|
Display::display_error_message(get_lang('UplPartialUpload').' '.get_lang('PleaseTryAgain')); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
case 4: |
|
|
|
case 4: |
|
|
|
// No file was uploaded. |
|
|
|
// No file was uploaded. |
|
|
|
if ($show_output) { |
|
|
|
if ($show_output) { |
|
|
|
Display::display_error_message(get_lang('UplNoFileUploaded').' '. get_lang('UplSelectFileFirst')); |
|
|
|
Display::display_error_message(get_lang('UplNoFileUploaded').' '. get_lang('UplSelectFileFirst')); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -173,7 +178,7 @@ function process_uploaded_file($uploaded_file, $show_output = true) { |
|
|
|
* @param int $unzip 1/0 |
|
|
|
* @param int $unzip 1/0 |
|
|
|
* @param string $what_if_file_exists overwrite, rename or warn if exists (default) |
|
|
|
* @param string $what_if_file_exists overwrite, rename or warn if exists (default) |
|
|
|
* @param boolean Optional output parameter. So far only use for unzip_uploaded_document function. If no output wanted on success, set to false. |
|
|
|
* @param boolean Optional output parameter. So far only use for unzip_uploaded_document function. If no output wanted on success, set to false. |
|
|
|
* @return path of the saved file |
|
|
|
* @return string path of the saved file |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function handle_uploaded_document( |
|
|
|
function handle_uploaded_document( |
|
|
|
$_course, |
|
|
|
$_course, |
|
|
|
@ -189,7 +194,7 @@ function handle_uploaded_document( |
|
|
|
$onlyUploadFile = false |
|
|
|
$onlyUploadFile = false |
|
|
|
) { |
|
|
|
) { |
|
|
|
if (!$user_id) { |
|
|
|
if (!$user_id) { |
|
|
|
die('Not a valid user.'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Strip slashes |
|
|
|
// Strip slashes |
|
|
|
@ -252,11 +257,14 @@ function handle_uploaded_document( |
|
|
|
if (!is_dir($where_to_save)) { |
|
|
|
if (!is_dir($where_to_save)) { |
|
|
|
if (!mkdir($where_to_save, api_get_permissions_for_new_directories())) { |
|
|
|
if (!mkdir($where_to_save, api_get_permissions_for_new_directories())) { |
|
|
|
if ($output) { |
|
|
|
if ($output) { |
|
|
|
Display::display_error_message(get_lang('DestDirectoryDoesntExist').' ('.$upload_path.')'); |
|
|
|
Display::display_error_message( |
|
|
|
|
|
|
|
get_lang('DestDirectoryDoesntExist').' ('.$upload_path.')' |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Full path of the destination |
|
|
|
// Full path of the destination |
|
|
|
$store_path = $where_to_save.$clean_name; |
|
|
|
$store_path = $where_to_save.$clean_name; |
|
|
|
|
|
|
|
|
|
|
|
@ -266,6 +274,7 @@ function handle_uploaded_document( |
|
|
|
$file_size = $uploaded_file['size']; |
|
|
|
$file_size = $uploaded_file['size']; |
|
|
|
$files_perm = api_get_permissions_for_new_files(); |
|
|
|
$files_perm = api_get_permissions_for_new_files(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Just upload file. |
|
|
|
if ($onlyUploadFile) { |
|
|
|
if ($onlyUploadFile) { |
|
|
|
$errorResult = moveUploadedFile($uploaded_file, $store_path); |
|
|
|
$errorResult = moveUploadedFile($uploaded_file, $store_path); |
|
|
|
if ($errorResult) { |
|
|
|
if ($errorResult) { |
|
|
|
@ -275,7 +284,11 @@ function handle_uploaded_document( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$docId = DocumentManager::get_document_id($_course, $file_path, $current_session_id); |
|
|
|
$docId = DocumentManager::get_document_id( |
|
|
|
|
|
|
|
$_course, |
|
|
|
|
|
|
|
$file_path, |
|
|
|
|
|
|
|
$current_session_id |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// What to do if the target file exists |
|
|
|
// What to do if the target file exists |
|
|
|
switch ($what_if_file_exists) { |
|
|
|
switch ($what_if_file_exists) { |
|
|
|
@ -313,7 +326,18 @@ function handle_uploaded_document( |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// There might be cases where the file exists on disk but there is no registration of that in the database |
|
|
|
// There might be cases where the file exists on disk but there is no registration of that in the database |
|
|
|
// In this case, and if we are in overwrite mode, overwrite and create the db record |
|
|
|
// In this case, and if we are in overwrite mode, overwrite and create the db record |
|
|
|
$document_id = add_document($_course, $file_path, 'file', $file_size, $document_name, null, 0, true, null, $current_session_id); |
|
|
|
$document_id = add_document( |
|
|
|
|
|
|
|
$_course, |
|
|
|
|
|
|
|
$file_path, |
|
|
|
|
|
|
|
'file', |
|
|
|
|
|
|
|
$file_size, |
|
|
|
|
|
|
|
$document_name, |
|
|
|
|
|
|
|
null, |
|
|
|
|
|
|
|
0, |
|
|
|
|
|
|
|
true, |
|
|
|
|
|
|
|
null, |
|
|
|
|
|
|
|
$current_session_id |
|
|
|
|
|
|
|
); |
|
|
|
if ($document_id) { |
|
|
|
if ($document_id) { |
|
|
|
// Put the document in item_property update |
|
|
|
// Put the document in item_property update |
|
|
|
api_item_property_update( |
|
|
|
api_item_property_update( |
|
|
|
@ -337,12 +361,26 @@ function handle_uploaded_document( |
|
|
|
item_property_update_on_folder($_course, $upload_path, $user_id); |
|
|
|
item_property_update_on_folder($_course, $upload_path, $user_id); |
|
|
|
// Display success message with extra info to user |
|
|
|
// Display success message with extra info to user |
|
|
|
if ($output) { |
|
|
|
if ($output) { |
|
|
|
Display::display_confirmation_message(get_lang('UplUploadSucceeded').'<br />'.$file_path .' '. get_lang('UplFileOverwritten'), false); |
|
|
|
Display::display_confirmation_message( |
|
|
|
|
|
|
|
get_lang('UplUploadSucceeded') . '<br />' . $file_path . ' ' . get_lang('UplFileOverwritten'), |
|
|
|
|
|
|
|
false |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
return $file_path; |
|
|
|
return $file_path; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// Put the document data in the database |
|
|
|
// Put the document data in the database |
|
|
|
$document_id = add_document($_course, $file_path, 'file', $file_size, $document_name, null, 0, true, null, $current_session_id); |
|
|
|
$document_id = add_document( |
|
|
|
|
|
|
|
$_course, |
|
|
|
|
|
|
|
$file_path, |
|
|
|
|
|
|
|
'file', |
|
|
|
|
|
|
|
$file_size, |
|
|
|
|
|
|
|
$document_name, |
|
|
|
|
|
|
|
null, |
|
|
|
|
|
|
|
0, |
|
|
|
|
|
|
|
true, |
|
|
|
|
|
|
|
null, |
|
|
|
|
|
|
|
$current_session_id |
|
|
|
|
|
|
|
); |
|
|
|
if ($document_id) { |
|
|
|
if ($document_id) { |
|
|
|
// Put the document in item_property update |
|
|
|
// Put the document in item_property update |
|
|
|
api_item_property_update( |
|
|
|
api_item_property_update( |
|
|
|
@ -394,7 +432,16 @@ function handle_uploaded_document( |
|
|
|
chmod($store_path, $files_perm); |
|
|
|
chmod($store_path, $files_perm); |
|
|
|
|
|
|
|
|
|
|
|
// Put the document data in the database |
|
|
|
// Put the document data in the database |
|
|
|
$document_id = add_document($_course, $new_file_path, 'file', $file_size, $document_name, null, 0, true); |
|
|
|
$document_id = add_document( |
|
|
|
|
|
|
|
$_course, |
|
|
|
|
|
|
|
$new_file_path, |
|
|
|
|
|
|
|
'file', |
|
|
|
|
|
|
|
$file_size, |
|
|
|
|
|
|
|
$document_name, |
|
|
|
|
|
|
|
null, |
|
|
|
|
|
|
|
0, |
|
|
|
|
|
|
|
true |
|
|
|
|
|
|
|
); |
|
|
|
if ($document_id) { |
|
|
|
if ($document_id) { |
|
|
|
// Update document item_property |
|
|
|
// Update document item_property |
|
|
|
api_item_property_update( |
|
|
|
api_item_property_update( |
|
|
|
@ -418,7 +465,10 @@ function handle_uploaded_document( |
|
|
|
|
|
|
|
|
|
|
|
// Display success message to user |
|
|
|
// Display success message to user |
|
|
|
if ($output) { |
|
|
|
if ($output) { |
|
|
|
Display::display_confirmation_message(get_lang('UplUploadSucceeded').'<br />'.get_lang('UplFileSavedAs').$new_file_path, false); |
|
|
|
Display::display_confirmation_message( |
|
|
|
|
|
|
|
get_lang('UplUploadSucceeded') . '<br />' . get_lang('UplFileSavedAs') . $new_file_path, |
|
|
|
|
|
|
|
false |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
return $new_file_path; |
|
|
|
return $new_file_path; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|