Fixes after merge cloud file links #2229

pull/2487/head
jmontoyaa 8 years ago
parent 1b73435d63
commit 958f1f59b6
  1. 184
      main/document/add_link.php
  2. 48
      main/document/document.php
  3. 52
      main/document/edit_document.php
  4. 126
      main/inc/lib/document.lib.php
  5. 56
      main/inc/lib/urlUtils.lib.php

@ -4,77 +4,74 @@
* This script allows to add cloud file links to the document structure * This script allows to add cloud file links to the document structure
* @package chamilo.document * @package chamilo.document
*/ */
/**
* Code
*/
// Including the global initialization file
require_once __DIR__.'/../inc/global.inc.php'; require_once __DIR__.'/../inc/global.inc.php';
// Including additional libraries
require_once __DIR__.'/../inc/lib/document.lib.php';
require_once __DIR__.'/../inc/lib/urlUtils.lib.php';
$fileLinkEnabled = api_get_configuration_value('enable_add_file_link'); $fileLinkEnabled = api_get_configuration_value('enable_add_file_link');
if (!$fileLinkEnabled) { if (!$fileLinkEnabled) {
api_not_allowed(true); api_not_allowed(true);
} }
$course_info = api_get_course_info();
if (empty($course_info)) {
api_not_allowed(true);
}
if ($is_certificate_mode) { $courseInfo = api_get_course_info();
if (empty($courseInfo)) {
api_not_allowed(true); api_not_allowed(true);
} }
$dir = '/';
$document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true); $document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true);
if (empty($document_data)) { if (empty($document_data)) {
$document_id = $parent_id = 0; $document_id = $parent_id = 0;
$path = '/'; $path = '/';
} else { } else {
if ($document_data['filetype'] == 'folder') { if ($document_data['filetype'] == 'folder') {
$document_id = $document_data['id']; $document_id = $document_data['id'];
$path = $document_data['path'].'/'; $path = $document_data['path'].'/';
$parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path)); $parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
} }
$dir = dirname($document_data['path']);
} }
$is_certificate_mode = DocumentManager::is_certificate_mode($dir);
if ($is_certificate_mode) {
api_not_allowed(true);
}
$is_allowed_to_edit = api_is_allowed_to_edit(null, true); $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
$groupIid = 0;
if (api_get_group_id()) { if (api_get_group_id()) {
// If the group id is set, check if the user has the right to be here // If the group id is set, check if the user has the right to be here
// Needed for group related stuff
require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
// Get group info // Get group info
$group_properties = GroupManager::get_group_properties(api_get_group_id()); $group_properties = GroupManager::get_group_properties(api_get_group_id());
if ($is_allowed_to_edit || GroupManager::is_user_in_group($_user['user_id'], api_get_group_id())) {
if ($is_allowed_to_edit || GroupManager::is_user_in_group($_user['user_id'], api_get_group_id())) { // Only courseadmin or group members allowed // Only courseadmin or group members allowed
$to_group_id = api_get_group_id(); $groupIid = $group_properties['iid'];
$req_gid = '&gidReq='.api_get_group_id(); $interbreadcrumb[] = array(
$interbreadcrumb[] = array('url' => '../group/group_space.php?gidReq='.api_get_group_id(), 'name' => get_lang('GroupSpace')); 'url' => '../group/group_space.php?'.api_get_cidreq(),
'name' => get_lang('GroupSpace'),
);
} else { } else {
api_not_allowed(true); api_not_allowed(true);
} }
} elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) { } elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) {
// Admin for "regular" upload, no group documents. And check if is my shared folder // Admin for "regular" upload, no group documents. And check if is my shared folder
$to_group_id = 0;
$req_gid = '';
} else { // No course admin and no group member... } else { // No course admin and no group member...
api_not_allowed(true); api_not_allowed(true);
} }
// Group docs can only be uploaded in the group directory // Group docs can only be uploaded in the group directory
if ($to_group_id != 0 && $path == '/') { if ($groupIid != 0 && $path == '/') {
$path = $group_properties['directory'] . "/"; $path = $group_properties['directory'] . "/";
} }
// Breadcrumbs // Breadcrumbs
$interbreadcrumb[] = array('url' => './document.php?id='.$document_id.$req_gid, 'name'=> get_lang('Documents')); $interbreadcrumb[] = array(
'url' => './document.php?id='.$document_id.'&'.api_get_cidreq(),
'name' => get_lang('Documents'),
);
// Interbreadcrumb for the current directory root path // Interbreadcrumb for the current directory root path
if (empty($document_data['parents'])) { if (empty($document_data['parents'])) {
// Hack in order to not add the document to the breadcrumb in case it is a link // Hack in order to not add the document to the breadcrumb in case it is a link
@ -85,95 +82,106 @@ if (empty($document_data['parents'])) {
foreach ($document_data['parents'] as $document_sub_data) { foreach ($document_data['parents'] as $document_sub_data) {
// Hack in order to not add the document to the breadcrumb in case it is a link // Hack in order to not add the document to the breadcrumb in case it is a link
if ($document_data['filetype'] != 'link') { if ($document_data['filetype'] != 'link') {
$interbreadcrumb[] = array('url' => $document_sub_data['document_url'], 'name' => $document_sub_data['title']); $interbreadcrumb[] = array(
'url' => $document_sub_data['document_url'],
'name' => $document_sub_data['title'],
);
} }
} }
} }
$this_section = SECTION_COURSES;
$nameTools = get_lang('LinkAdd');
// Display the header $this_section = SECTION_COURSES;
Display::display_header($nameTools, 'Doc');
/* Here we do all the work */
// Actions
echo '<div class="actions">';
// Link back to the documents overview
echo '<a href="document.php?id='.$document_id.'">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).'</a>';
echo '</div>';
// Form to select directory
$folders = DocumentManager::get_all_document_folders(
$_course,
$groupIid,
$is_allowed_to_edit
);
echo DocumentManager::build_directory_selector( $nameTools = get_lang('LinkAdd');
$folders,
$document_id,
(isset($group_properties['directory']) ? $group_properties['directory'] : array())
);
$action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id; $action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id;
// URLs in whitelist // URLs in whitelist
$urlWL = URLUtils::getFileHostingsWL(); $urlWL = DocumentManager::getFileHostingWhiteList();
sort($urlWL); sort($urlWL);
$urlWLRegEx = '/(\/\/|\.)('.implode('|', $urlWL).')/i'; //Matches any of the whitelisted urls preceded by // or . $urlWLRegEx = '/(\/\/|\.)('.implode('|', $urlWL).')/i'; //Matches any of the whitelisted urls preceded by // or .
$urlWLText = "\n\t* ".implode("\n\t* ", $urlWL); $urlWLText = "\n\t* ".implode("\n\t* ", $urlWL);
$urlWLHTML = "<ul><li>".implode("</li><li>", $urlWL)."</li></ul>"; $urlWLHTML = "<ul><li>".implode("</li><li>", $urlWL)."</li></ul>";
$form = new FormValidator('upload', 'POST', $action, '', array('enctype' => 'multipart/form-data')); $form = new FormValidator('upload', 'POST', $action, '', array('enctype' => 'multipart/form-data'));
$form->addElement('hidden', 'linkid', $document_id); $form->addHidden('linkid', $document_id);
$form->addElement('hidden', 'curdirpath', $path); $form->addHidden('curdirpath', $path);
$form->addElement('text', 'name', get_lang('LinkName'), array('id' => 'name_link')); $form->addElement('text', 'name', get_lang('LinkName'), array('id' => 'name_link'));
$form->addElement('text', 'url', get_lang('Url'), array('id' => 'url_link')); $form->addElement('text', 'url', get_lang('Url'), array('id' => 'url_link'));
$form->addElement('static', 'info', '', '<span class="text-primary" data-toggle="tooltip" title="'.$urlWLHTML.'">'.get_lang('ValidDomainList').' <span class="glyphicon glyphicon-question-sign"></span></span>'); $form->addElement(
'static',
'info',
'',
'<span class="text-primary" data-toggle="tooltip" title="'.$urlWLHTML.'">'.get_lang(
'ValidDomainList'
).' <span class="glyphicon glyphicon-question-sign"></span></span>'
);
$form->addButtonSend(get_lang('AddCloudLink'), 'submitDocument'); $form->addButtonSend(get_lang('AddCloudLink'), 'submitDocument');
$form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'client'); $form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'client');
$form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'server'); $form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'server');
$form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'client'); $form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'client');
$form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'server'); $form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'server');
// Well formed url pattern (must have the protocol) // Well formed url pattern (must have the protocol)
$urlRegEx = URLUtils::getWellformedUrlRegex(); $urlRegEx = DocumentManager::getWellFormedUrlRegex();
$form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'client'); $form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'client');
$form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'server'); $form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'server');
$form->addRule('url', get_lang('NotValidDomain').$urlWLText, 'regex', $urlWLRegEx, 'client'); $form->addRule('url', get_lang('NotValidDomain').$urlWLText, 'regex', $urlWLRegEx, 'client');
$form->addRule('url', get_lang('NotValidDomain').$urlWLHTML, 'regex', $urlWLRegEx, 'server'); $form->addRule('url', get_lang('NotValidDomain').$urlWLHTML, 'regex', $urlWLRegEx, 'server');
if ($form->validate()) { if ($form->validate()) {
if (isset($_REQUEST['linkid'])) { if (isset($_REQUEST['linkid'])) {
$doc_id = DocumentManager::addCloudLink($course_info, $path, $_REQUEST['url'], $_REQUEST['name']); $doc_id = DocumentManager::addCloudLink($courseInfo, $path, $_REQUEST['url'], $_REQUEST['name']);
if ($doc_id) { if ($doc_id) {
Display::display_confirmation_message(get_lang('CloudLinkAdded') . '<br />', false); Display::addFlash(Display::return_message(get_lang('CloudLinkAdded'), 'success', false));
} else { } else {
if (DocumentManager::cloudLinkExists($course_info, $path, $_REQUEST['url'])) { if (DocumentManager::cloudLinkExists($courseInfo, $path, $_REQUEST['url'])) {
Display::display_error_message(get_lang('UrlAlreadyExists'), false); Display::addFlash(Display::return_message(get_lang('UrlAlreadyExists'), 'warning', false));
} else { } else {
Display::display_error_message(get_lang('ErrorAddCloudLink'), false); Display::addFlash(Display::return_message(get_lang('ErrorAddCloudLink'), 'warning', false));
} }
} }
header('Location: document.php?'.api_get_cidreq());
exit;
} }
} }
// Display the header
Display::display_header($nameTools, 'Doc');
// Actions
echo '<div class="actions">';
// Link back to the documents overview
echo '<a href="document.php?id='.$document_id.'&'.api_get_cidreq().'">'.
Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).
'</a>';
echo '</div>';
// Form to select directory
$folders = DocumentManager::get_all_document_folders(
$_course,
$groupIid,
$is_allowed_to_edit
);
echo DocumentManager::build_directory_selector(
$folders,
$document_id,
isset($group_properties['directory']) ? $group_properties['directory'] : array()
);
// Add tooltip and correctly parse its inner HTML // Add tooltip and correctly parse its inner HTML
echo '<script> echo '<script>
$(document).ready(function() { $(document).ready(function() {
$("[data-toggle=\'tooltip\']").tooltip( $("[data-toggle=\'tooltip\']").tooltip({
{ content:
content: function() {
function() { return $(this).attr("title");
return $(this).attr("title"); }
} });
}
);
}); });
</script>'; </script>';
echo $form->return_form(); echo $form->returnForm();
Display::display_footer(); Display::display_footer();

@ -262,37 +262,35 @@ switch ($action) {
$_GET['deleteid'], $_GET['deleteid'],
$groupIid $groupIid
); );
if ($deleteDocument) {
$certificateId = isset($_GET['delete_certificate_id']) ? $_GET['delete_certificate_id'] : null;
DocumentManager::remove_attach_certificate(
api_get_course_id(),
$certificateId
);
Display::addFlash(
Display::return_message(
get_lang('DocDeleted').': '.$documentInfo['title'],
'success'
)
);
} else {
Display::addFlash(Display::return_message(get_lang('DocDeleteError'), 'warning'));
}
} else { } else {
// Cloud Links // Cloud Links
$deleteDocument = DocumentManager::deleteCloudLink($_course, $_GET['deleteid']); $deleteDocument = DocumentManager::deleteCloudLink($courseInfo, $_GET['deleteid']);
} if ($deleteDocument) {
if ($deleteDocument) {
$certificateId = isset($_GET['delete_certificate_id']) ? $_GET['delete_certificate_id'] : null;
DocumentManager::remove_attach_certificate(
api_get_course_id(),
$certificateId
);
if ($documentInfo['filetype'] != 'link') {
Display::addFlash(Display::return_message( Display::addFlash(Display::return_message(
get_lang('DocDeleted').': '.$documentInfo['title'], get_lang('CloudLinkDeleted').': '.$documentInfo['title'],
'success' 'success'
)); ));
} else { } else {
if ($documentInfo['filetype'] != 'link') { Display::addFlash(Display::return_message(
Display::addFlash(Display::return_message( get_lang('CloudLinkDeleteError').': '.$documentInfo['title'],
get_lang('CloudLinkDeleted').': '.$data['title'], 'error'
'success' ));
));
} else {
Display::addFlash(Display::return_message(
get_lang('CloudLinkDeleteError').': '.$data['title'],
'error'
));
}
} }
} else {
Display::addFlash(Display::return_message(get_lang('DocDeleteError'), 'warning'));
} }
} else { } else {
Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning')); Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning'));
@ -1067,7 +1065,7 @@ if ($isAllowedToEdit || $group_member_with_upload_rights ||
if (!DocumentManager::cloudLinkExists($_course, $moveTo, $document_to_move['comment'])) { if (!DocumentManager::cloudLinkExists($_course, $moveTo, $document_to_move['comment'])) {
$doc_id = $moveFile; $doc_id = $moveFile;
DocumentManager::updateDBInfoCloudLink($document_to_move['path'], $moveTo.'/', $doc_id); DocumentManager::updateDBInfoCloudLink($document_to_move['path'], $moveTo.'/', $doc_id);
//update database item property //update database item property
api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FileMoved', api_get_user_id(), $to_group_id, null, null, null, $session_id); api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FileMoved', api_get_user_id(), $to_group_id, null, null, null, $session_id);
Display::addFlash( Display::addFlash(

@ -28,7 +28,6 @@ use ChamiloSession as Session;
*/ */
require_once __DIR__.'/../inc/global.inc.php'; require_once __DIR__.'/../inc/global.inc.php';
require_once __DIR__.'/../inc/lib/urlUtils.lib.php';
$groupRights = Session::read('group_member_with_upload_rights'); $groupRights = Session::read('group_member_with_upload_rights');
@ -143,9 +142,6 @@ if ($is_certificate_mode) {
$is_allowed_to_edit = api_is_allowed_to_edit(null, true) || $groupRights || $is_allowed_to_edit = api_is_allowed_to_edit(null, true) || $groupRights ||
DocumentManager::is_my_shared_folder(api_get_user_id(), $dir, $sessionId); DocumentManager::is_my_shared_folder(api_get_user_id(), $dir, $sessionId);
$noPHP_SELF = true;
/* Other initialization code */
$dbTable = Database::get_course_table(TABLE_DOCUMENT); $dbTable = Database::get_course_table(TABLE_DOCUMENT);
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
@ -156,7 +152,6 @@ if (!empty($group_id)) {
'name' => get_lang('GroupSpace'), 'name' => get_lang('GroupSpace'),
); );
$group_document = true; $group_document = true;
$noPHP_SELF = true;
} }
if (!$is_certificate_mode) { if (!$is_certificate_mode) {
@ -219,7 +214,7 @@ if (isset($_POST['comment'])) {
if ($file_type == 'link') { if ($file_type == 'link') {
$linkExists = DocumentManager::cloudLinkExists($course_info, $file, $_POST['comment']); $linkExists = DocumentManager::cloudLinkExists($course_info, $file, $_POST['comment']);
} }
if (!$linkExists || $linkExists == $document_id) { if (!$linkExists || $linkExists == $document_id) {
$params = [ $params = [
'comment' => $comment, 'comment' => $comment,
@ -230,7 +225,7 @@ if (isset($_POST['comment'])) {
$params, $params,
['c_id = ? AND id = ?' => [$course_id, $document_id]] ['c_id = ? AND id = ?' => [$course_id, $document_id]]
); );
if ($file_type != 'link') { if ($file_type != 'link') {
Display::addFlash(Display::return_message(get_lang('fileModified'))); Display::addFlash(Display::return_message(get_lang('fileModified')));
} else { } else {
@ -391,15 +386,8 @@ if ($owner_id == api_get_user_id() ||
); );
// Form title // Form title
$form->addElement('header', $nameTools); $form->addHeader($nameTools);
$form->addElement('hidden', 'filename'); $key_label_title = $file_type != 'link' ? 'Title' : 'LinkName';
$form->addElement('hidden', 'extension');
$form->addElement('hidden', 'file_path');
$form->addElement('hidden', 'commentPath');
$form->addElement('hidden', 'showedit');
$form->addElement('hidden', 'origin');
$form->addElement('hidden', 'origin_opt');
$key_label_title = ($file_type != 'link' ? 'Title' : 'LinkName');
$form->addText( $form->addText(
'title', 'title',
get_lang($key_label_title), get_lang($key_label_title),
@ -408,10 +396,7 @@ if ($owner_id == api_get_user_id() ||
); );
$defaults['title'] = $document_data['title']; $defaults['title'] = $document_data['title'];
$form->addElement('hidden', 'formSent');
$defaults['formSent'] = 1; $defaults['formSent'] = 1;
$read_only_flag = isset($_POST['readonly']) ? $_POST['readonly'] : null; $read_only_flag = isset($_POST['readonly']) ? $_POST['readonly'] : null;
// Desactivation of IE proprietary commenting tags inside the text before loading it on the online editor. // Desactivation of IE proprietary commenting tags inside the text before loading it on the online editor.
@ -448,16 +433,24 @@ if ($owner_id == api_get_user_id() ||
} }
} }
} }
if ($file_type == 'link') { if ($file_type == 'link') {
// URLs in whitelist // URLs in whitelist
$urlWL = URLUtils::getFileHostingsWL(); $urlWL = DocumentManager::getFileHostingWhiteList();
sort($urlWL); sort($urlWL);
$urlWLRegEx = '/(\/\/|\.)('.implode('|', $urlWL).')/i'; //Matches any of the whitelisted urls preceded by // or . //Matches any of the whitelisted urls preceded by // or .
$urlWLRegEx = '/(\/\/|\.)('.implode('|', $urlWL).')/i';
$urlWLText = "\n\t* ".implode("\n\t* ", $urlWL); $urlWLText = "\n\t* ".implode("\n\t* ", $urlWL);
$urlWLHTML = "<ul><li>".implode("</li><li>", $urlWL)."</li></ul>"; $urlWLHTML = "<ul><li>".implode("</li><li>", $urlWL)."</li></ul>";
$form->addText('comment', get_lang('Url')); $form->addText('comment', get_lang('Url'));
$form->addElement('static', 'info', '', '<span class="text-primary" data-toggle="tooltip" title="'.$urlWLHTML.'">'.get_lang('ValidDomainList').' <span class="glyphicon glyphicon-question-sign"></span></span>'); $form->addElement(
'static',
'info',
'',
'<span class="text-primary" data-toggle="tooltip" title="'.$urlWLHTML.'">'.get_lang(
'ValidDomainList'
).' <span class="glyphicon glyphicon-question-sign"></span></span>'
);
} else { } else {
$form->addElement('textarea', 'comment', get_lang('Comment'), ['cols-size' => [2, 10, 0]]); $form->addElement('textarea', 'comment', get_lang('Comment'), ['cols-size' => [2, 10, 0]]);
} }
@ -472,16 +465,14 @@ if ($owner_id == api_get_user_id() ||
} }
if ($file_type == 'link') { if ($file_type == 'link') {
$form->addRule('title', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'client'); $form->addRule('title', get_lang('PleaseEnterCloudLinkName'), 'required');
$form->addRule('title', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'server'); $form->addRule('comment', get_lang('PleaseEnterURL'), 'required');
$form->addRule('comment', get_lang('PleaseEnterURL'), 'required', null, 'client');
$form->addRule('comment', get_lang('PleaseEnterURL'), 'required', null, 'server');
// Well formed url pattern (must have the protocol) // Well formed url pattern (must have the protocol)
$urlRegEx = URLUtils::getWellformedUrlRegex(); $urlRegEx = DocumentManager::getWellFormedUrlRegex();
$form->addRule('comment', get_lang('NotValidURL'), 'regex', $urlRegEx, 'client'); $form->addRule('comment', get_lang('NotValidURL'), 'regex', $urlRegEx, 'client');
$form->addRule('comment', get_lang('NotValidURL'), 'regex', $urlRegEx, 'server'); $form->addRule('comment', get_lang('NotValidURL'), 'regex', $urlRegEx, 'server');
$form->addRule('comment', get_lang('NotValidDomain').$urlWLText,'regex', $urlWLRegEx, 'client'); $form->addRule('comment', get_lang('NotValidDomain').$urlWLText, 'regex', $urlWLRegEx, 'client');
$form->addRule('comment', get_lang('NotValidDomain').$urlWLHTML,'regex', $urlWLRegEx, 'server'); $form->addRule('comment', get_lang('NotValidDomain').$urlWLHTML, 'regex', $urlWLRegEx, 'server');
} }
if ($is_certificate_mode) { if ($is_certificate_mode) {
@ -601,7 +592,6 @@ function change_name($base_work_dir, $source_file, $rename_to, $dir, $doc)
function show_return($document_id, $path, $call_from_tool = '', $slide_id = 0, $is_certificate_mode = false) function show_return($document_id, $path, $call_from_tool = '', $slide_id = 0, $is_certificate_mode = false)
{ {
$actionsLeft = null; $actionsLeft = null;
global $parent_id; global $parent_id;
$url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&id='.$parent_id; $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&id='.$parent_id;

@ -975,7 +975,7 @@ class DocumentManager
*/ */
public static function deleteDocumentFromDb( public static function deleteDocumentFromDb(
$document_id, $document_id,
$course_info = array(), $course_info = [],
$session_id = 0, $session_id = 0,
$remove_content_from_db = false $remove_content_from_db = false
) { ) {
@ -993,7 +993,6 @@ class DocumentManager
if (empty($session_id)) { if (empty($session_id)) {
$session_id = api_get_session_id(); $session_id = api_get_session_id();
} }
// Soft DB delete // Soft DB delete
api_item_property_update( api_item_property_update(
$course_info, $course_info,
@ -6604,104 +6603,161 @@ class DocumentManager
* @param array $_course * @param array $_course
* @param string $path * @param string $path
* @param string $url * @param string $url
* @param string $name
* @return int id of document or 0 if already exists or there was a problem creating it * @return int id of document or 0 if already exists or there was a problem creating it
*/ */
public static function addCloudLink($_course, $path, $url, $name) public static function addCloudLink($_course, $path, $url, $name)
{ {
require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
$file_path = $path; $file_path = $path;
if (!self::cloudLinkExists($_course, $path, $url)) { if (!self::cloudLinkExists($_course, $path, $url)) {
$doc_id = add_document($_course, $file_path, "link", 0, $name, $url); $doc_id = add_document($_course, $file_path, 'link', 0, $name, $url);
if ($doc_id) { if ($doc_id) {
// Update document item_property // Update document item_property
api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', api_get_user_id(), api_get_group_id(), api_get_user_id(), null, null, api_get_session_id()); api_item_property_update(
$_course,
TOOL_DOCUMENT,
$doc_id,
'DocumentAdded',
api_get_user_id(),
api_get_group_id(),
api_get_user_id(),
null,
null,
api_get_session_id()
);
} }
// If the file is in a folder, we need to update all parent folders // If the file is in a folder, we need to update all parent folders
item_property_update_on_folder($_course, $file_path, api_get_user_id()); item_property_update_on_folder($_course, $file_path, api_get_user_id());
return $doc_id; return $doc_id;
} else { } else {
return 0; return 0;
} }
} }
/** /**
* Deletes a cloud link from the database * Deletes a cloud link from the database
* *
* @author - Aquilino Blanco Cores <aqblanco@gmail.com> * @author - Aquilino Blanco Cores <aqblanco@gmail.com>
* @param array $_course * @param array $courseInfo
* @param string $id * @param string $documentId
* @param string $url
* @return boolean true if success / false if an error occurred * @return boolean true if success / false if an error occurred
*/ */
public static function deleteCloudLink($_course, $id) public static function deleteCloudLink($courseInfo, $documentId)
{ {
if (empty($documentId) || empty($courseInfo)) {
if (empty($id)) {
return false; return false;
} }
$documentId = (int) $id; $documentId = (int) $documentId;
$fileDeletedFromDb = false; $fileDeletedFromDb = false;
if (!empty($documentId)) {
if ($document_id) { self::deleteDocumentFromDb($documentId, $courseInfo, 0, true);
self::deleteDocumentFromDb($documentId, array(), 0, true); // checking
//checking $table = Database::get_course_table(TABLE_DOCUMENT);
$TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); $courseId = $courseInfo['real_id'];
$sql = "SELECT * FROM $TABLE_DOCUMENT WHERE id = $documentId"; echo $sql = "SELECT * FROM $table WHERE id = $documentId AND c_id = $courseId";
$result = Database::query($sql); $result = Database::query($sql);
$exists = Database::num_rows($result) > 0; $exists = Database::num_rows($result) > 0;
$fileDeletedFromDb = !$exists ; $fileDeletedFromDb = !$exists;
} }
return $fileDeletedFromDb; return $fileDeletedFromDb;
} }
/** /**
* Gets the id of a cloud link with a given path * Gets the id of a cloud link with a given path
* *
* @author - Aquilino Blanco Cores <aqblanco@gmail.com> * @author - Aquilino Blanco Cores <aqblanco@gmail.com>
* @param array $_course * @param array $courseInfo
* @param string $path * @param string $path
* @param string $url * @param string $url
* @return int link's id / false if no link found * @return int link's id / false if no link found
*/ */
public static function getCloudLinkId($_course, $path, $url) public static function getCloudLinkId($courseInfo, $path, $url)
{ {
$TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); $table = Database::get_course_table(TABLE_DOCUMENT);
$courseId = (int) $_course['real_id'];
if (empty($courseInfo)) {
return false;
}
$courseId = (int) $courseInfo['real_id'];
$path = Database::escape_string($path); $path = Database::escape_string($path);
if (substr($path, -1) != '/') { if (substr($path, -1) != '/') {
// Add final slash to path if not present // Add final slash to path if not present
$path .= '/'; $path .= '/';
} }
if (!empty($courseId) && !empty($path)) { if (!empty($courseId) && !empty($path)) {
$sql = "SELECT id FROM $TABLE_DOCUMENT WHERE c_id = $courseId AND path LIKE BINARY '$path' AND comment = '$url' AND filetype = 'link' LIMIT 1"; $sql = "SELECT id FROM $table
WHERE
c_id = $courseId AND
path LIKE BINARY '$path' AND
comment = '$url' AND
filetype = 'link'
LIMIT 1";
$result = Database::query($sql); $result = Database::query($sql);
if ($result && Database::num_rows($result)) { if ($result && Database::num_rows($result)) {
$row = Database::fetch_array($result); $row = Database::fetch_array($result);
return intval($row[0]); return intval($row[0]);
} }
} }
return false; return false;
} }
/** /**
* Checks if a cloud link exists * Checks if a cloud link exists
* *
* @author - Aquilino Blanco Cores <aqblanco@gmail.com> * @author - Aquilino Blanco Cores <aqblanco@gmail.com>
* @param array $_course * @param array $courseInfo
* @param string $path * @param string $path
* @param string $url * @param string $url
* @return boolean true if it exists false in other case * @return boolean true if it exists false in other case
*/ */
public static function cloudLinkExists($_course, $path, $url) public static function cloudLinkExists($courseInfo, $path, $url)
{ {
$exists = self::getCloudLinkId($_course, $path, $url); $exists = self::getCloudLinkId($courseInfo, $path, $url);
return $exists; return $exists;
} }
/**
* Gets the wellformed URLs regular expression in order to use it on forms' verifications
*
* @author Aquilino Blanco Cores <aqblanco@gmail.com>
* @return string the well formed URLs regular expressions string
*/
public static function getWellFormedUrlRegex()
{
return '/\(?((http|https|ftp):\/\/)(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/i';
}
/**
* Gets the files hosting sites' whitelist
*
* @author Aquilino Blanco Cores <aqblanco@gmail.com>
* @return array the sites list.
*/
public static function getFileHostingWhiteList()
{
return [
'asuswebstorage.com',
'dropbox.com',
'dropboxusercontent.com',
'fileserve.com',
'drive.google.com',
'icloud.com',
'mediafire.com',
'mega.nz',
'onedrive.live.com',
'slideshare.net',
'scribd.com',
'wetransfer.com',
'box.com',
'livefilestore.com' // OneDrive
];
}
} }

@ -1,56 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Class URLUtils
* This class contains some utilities to work with urls
*
* @package chamilo.library
*/
class URLUtils
{
/**
* Construct
*/
private function __construct()
{
}
/**
* Gets the wellformed URLs regular expression in order to use it on forms' verifications
*
* @author Aquilino Blanco Cores <aqblanco@gmail.com>
* @return the wellformed URLs regular expressions string
*/
public static function getWellformedUrlRegex()
{
return '/\(?((http|https|ftp):\/\/)(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/i';
}
/**
* Gets the files hosting sites' whitelist
*
* @author Aquilino Blanco Cores <aqblanco@gmail.com>
* @return array the sites list.
*/
public static function getFileHostingsWL()
{
return array(
"asuswebstorage.com",
"dropbox.com",
"dropboxusercontent.com",
"fileserve.com",
"drive.google.com",
"icloud.com",
"mediafire.com",
"mega.nz",
"onedrive.live.com",
"slideshare.net",
"scribd.com",
"wetransfer.com",
"box.com",
"livefilestore.com" // OneDrive
);
}
}
Loading…
Cancel
Save