add_link.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script allows to add cloud file links to the document structure.
  5. *
  6. * @package chamilo.document
  7. */
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. $fileLinkEnabled = api_get_configuration_value('enable_add_file_link');
  10. if (!$fileLinkEnabled) {
  11. api_not_allowed(true);
  12. }
  13. api_protect_course_script();
  14. $courseInfo = api_get_course_info();
  15. if (empty($courseInfo)) {
  16. api_not_allowed(true);
  17. }
  18. $documentId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
  19. $dir = '/';
  20. $document_data = DocumentManager::get_document_data_by_id($documentId, api_get_course_id(), true);
  21. if (empty($document_data)) {
  22. $document_id = $parent_id = 0;
  23. $path = '/';
  24. } else {
  25. if ($document_data['filetype'] == 'folder') {
  26. $document_id = $document_data['id'];
  27. $path = $document_data['path'].'/';
  28. $parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
  29. }
  30. $dir = dirname($document_data['path']);
  31. }
  32. $is_certificate_mode = DocumentManager::is_certificate_mode($dir);
  33. if ($is_certificate_mode) {
  34. api_not_allowed(true);
  35. }
  36. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  37. $groupIid = 0;
  38. if (api_get_group_id()) {
  39. // If the group id is set, check if the user has the right to be here
  40. // Get group info
  41. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  42. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), $group_properties)) {
  43. // Only courseadmin or group members allowed
  44. $groupIid = $group_properties['iid'];
  45. $interbreadcrumb[] = [
  46. 'url' => '../group/group_space.php?'.api_get_cidreq(),
  47. 'name' => get_lang('GroupSpace'),
  48. ];
  49. } else {
  50. api_not_allowed(true);
  51. }
  52. } elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) {
  53. // Admin for "regular" upload, no group documents. And check if is my shared folder
  54. } else { // No course admin and no group member...
  55. api_not_allowed(true);
  56. }
  57. // Group docs can only be uploaded in the group directory
  58. if ($groupIid != 0 && $path == '/') {
  59. $path = $group_properties['directory']."/";
  60. }
  61. // Breadcrumbs
  62. $interbreadcrumb[] = [
  63. 'url' => './document.php?id='.$document_id.'&'.api_get_cidreq(),
  64. 'name' => get_lang('Documents'),
  65. ];
  66. // Interbreadcrumb for the current directory root path
  67. if (empty($document_data['parents'])) {
  68. // Hack in order to not add the document to the breadcrumb in case it is a link
  69. if ($document_data['filetype'] != 'link') {
  70. $interbreadcrumb[] = ['url' => '#', 'name' => $document_data['title']];
  71. }
  72. } else {
  73. foreach ($document_data['parents'] as $document_sub_data) {
  74. // Hack in order to not add the document to the breadcrumb in case it is a link
  75. if ($document_data['filetype'] != 'link') {
  76. $interbreadcrumb[] = [
  77. 'url' => $document_sub_data['document_url'],
  78. 'name' => $document_sub_data['title'],
  79. ];
  80. }
  81. }
  82. }
  83. $this_section = SECTION_COURSES;
  84. $nameTools = get_lang('LinkAdd');
  85. $action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id;
  86. // URLs in whitelist
  87. $urlWL = DocumentManager::getFileHostingWhiteList();
  88. sort($urlWL);
  89. $urlWLRegEx = '/(\/\/|\.)('.implode('|', $urlWL).')/i'; //Matches any of the whitelisted urls preceded by // or .
  90. $urlWLText = "\n\t* ".implode("\n\t* ", $urlWL);
  91. $urlWLHTML = "<ul><li>".implode("</li><li>", $urlWL)."</li></ul>";
  92. $form = new FormValidator('upload', 'POST', $action, '', ['enctype' => 'multipart/form-data']);
  93. $form->addHidden('linkid', $document_id);
  94. $form->addHidden('curdirpath', $path);
  95. $form->addElement('text', 'name', get_lang('LinkName'), ['id' => 'name_link']);
  96. $form->addElement('text', 'url', get_lang('Url'), ['id' => 'url_link']);
  97. $form->addElement(
  98. 'static',
  99. 'info',
  100. '',
  101. '<span class="text-primary" data-toggle="tooltip" title="'.$urlWLHTML.'">'.get_lang(
  102. 'ValidDomainList'
  103. ).' <span class="glyphicon glyphicon-question-sign"></span></span>'
  104. );
  105. $form->addButtonSend(get_lang('AddCloudLink'), 'submitDocument');
  106. $form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'client');
  107. $form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'server');
  108. $form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'client');
  109. $form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'server');
  110. // Well formed url pattern (must have the protocol)
  111. $urlRegEx = DocumentManager::getWellFormedUrlRegex();
  112. $form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'client');
  113. $form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'server');
  114. $form->addRule('url', get_lang('NotValidDomain').$urlWLText, 'regex', $urlWLRegEx, 'client');
  115. $form->addRule('url', get_lang('NotValidDomain').$urlWLHTML, 'regex', $urlWLRegEx, 'server');
  116. if ($form->validate()) {
  117. if (isset($_REQUEST['linkid'])) {
  118. $doc_id = DocumentManager::addCloudLink($courseInfo, $path, $_REQUEST['url'], $_REQUEST['name']);
  119. if ($doc_id) {
  120. Display::addFlash(Display::return_message(get_lang('CloudLinkAdded'), 'success', false));
  121. } else {
  122. if (DocumentManager::cloudLinkExists($courseInfo, $path, $_REQUEST['url'])) {
  123. Display::addFlash(Display::return_message(get_lang('UrlAlreadyExists'), 'warning', false));
  124. } else {
  125. Display::addFlash(Display::return_message(get_lang('ErrorAddCloudLink'), 'warning', false));
  126. }
  127. }
  128. header('Location: document.php?'.api_get_cidreq());
  129. exit;
  130. }
  131. }
  132. // Display the header
  133. Display::display_header($nameTools, 'Doc');
  134. // Actions
  135. echo '<div class="actions">';
  136. // Link back to the documents overview
  137. echo '<a href="document.php?id='.$document_id.'&'.api_get_cidreq().'">'.
  138. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).
  139. '</a>';
  140. echo '</div>';
  141. // Form to select directory
  142. $folders = DocumentManager::get_all_document_folders(
  143. $_course,
  144. $groupIid,
  145. $is_allowed_to_edit
  146. );
  147. echo DocumentManager::build_directory_selector(
  148. $folders,
  149. $document_id,
  150. isset($group_properties['directory']) ? $group_properties['directory'] : []
  151. );
  152. // Add tooltip and correctly parse its inner HTML
  153. echo '<script>
  154. $(function() {
  155. $("[data-toggle=\'tooltip\']").tooltip({
  156. content:
  157. function() {
  158. return $(this).attr("title");
  159. }
  160. });
  161. });
  162. </script>';
  163. echo $form->returnForm();
  164. Display::display_footer();