edit_document.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This file allows editing documents.
  6. *
  7. * Based on create_document, this file allows
  8. * - edit name
  9. * - edit comments
  10. * - edit metadata (requires a document table entry)
  11. * - edit html content (only for htm/html files)
  12. *
  13. * For all files
  14. * - show editable name field
  15. * - show editable comments field
  16. * Additionally, for html and text files
  17. * - show RTE
  18. *
  19. * Remember, all files and folders must always have an entry in the
  20. * database, regardless of wether they are visible/invisible, have
  21. * comments or not.
  22. *
  23. * @package chamilo.document
  24. *
  25. * @todo improve script structure (FormValidator is used to display form, but
  26. * not for validation at the moment)
  27. */
  28. require_once __DIR__.'/../inc/global.inc.php';
  29. $groupRights = Session::read('group_member_with_upload_rights');
  30. // Template's javascript
  31. $htmlHeadXtra[] = '
  32. <script>
  33. $(document).ready(function() {
  34. $(".scrollbar-light").scrollbar();
  35. expandColumnToogle("#hide_bar_template", {
  36. selector: "#template_col",
  37. width: 3
  38. }, {
  39. selector: "#doc_form",
  40. width: 9
  41. });
  42. CKEDITOR.on("instanceReady", function (e) {
  43. showTemplates();
  44. });
  45. });
  46. </script>';
  47. $this_section = SECTION_COURSES;
  48. $lib_path = api_get_path(LIBRARY_PATH);
  49. $course_info = api_get_course_info();
  50. $group_id = api_get_group_id();
  51. $sessionId = api_get_session_id();
  52. if (api_is_in_group()) {
  53. $group_properties = GroupManager::get_group_properties($group_id);
  54. }
  55. $dir = '/';
  56. $currentDirPath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : null;
  57. $readonly = false;
  58. if (isset($_GET['id'])) {
  59. $document_data = DocumentManager::get_document_data_by_id(
  60. $_GET['id'],
  61. api_get_course_id(),
  62. true,
  63. 0
  64. );
  65. if (!empty($sessionId) && empty($document_data)) {
  66. $document_data = DocumentManager::get_document_data_by_id(
  67. $_REQUEST['id'],
  68. api_get_course_id(),
  69. true,
  70. $sessionId
  71. );
  72. }
  73. $document_id = $document_data['id'];
  74. $file = $document_data['path'];
  75. $parent_id = DocumentManager::get_document_id($course_info, dirname($file));
  76. $dir = dirname($document_data['path']);
  77. $dir_original = $dir;
  78. $doc = basename($file);
  79. $readonly = $document_data['readonly'];
  80. }
  81. if (empty($document_data)) {
  82. api_not_allowed(true);
  83. }
  84. $is_certificate_mode = DocumentManager::is_certificate_mode($dir);
  85. //Call from
  86. $call_from_tool = api_get_origin();
  87. $slide_id = isset($_GET['origin_opt']) ? Security::remove_XSS($_GET['origin_opt']) : null;
  88. $file_name = $doc;
  89. $group_document = false;
  90. $_course = api_get_course_info();
  91. $sessionId = api_get_session_id();
  92. $user_id = api_get_user_id();
  93. $doc_tree = explode('/', $file);
  94. $count_dir = count($doc_tree) - 2; // "2" because at the begin and end there are 2 "/"
  95. // Level correction for group documents.
  96. if (!empty($group_properties['directory'])) {
  97. $count_dir = $count_dir > 0 ? $count_dir - 1 : 0;
  98. }
  99. $relative_url = '';
  100. for ($i = 0; $i < ($count_dir); $i++) {
  101. $relative_url .= '../';
  102. }
  103. $editorConfig = [
  104. 'ToolbarSet' => (api_is_allowed_to_edit(null, true) ? 'Documents' : 'DocumentsStudent'),
  105. 'Width' => '100%',
  106. 'Height' => '400',
  107. 'cols-size' => [2, 10, 0],
  108. 'FullPage' => true,
  109. 'InDocument' => true,
  110. 'CreateDocumentDir' => $relative_url,
  111. 'CreateDocumentWebDir' => (empty($group_properties['directory']))
  112. ? api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/'
  113. : api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/',
  114. 'BaseHref' => api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir,
  115. ];
  116. if ($is_certificate_mode) {
  117. $editorConfig['CreateDocumentDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  118. $editorConfig['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  119. $editorConfig['BaseHref'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir;
  120. }
  121. $is_allowed_to_edit = api_is_allowed_to_edit(null, true) || $groupRights ||
  122. DocumentManager::is_my_shared_folder(api_get_user_id(), $dir, $sessionId);
  123. $noPHP_SELF = true;
  124. /* Other initialization code */
  125. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  126. $course_id = api_get_course_int_id();
  127. if (!empty($group_id)) {
  128. $interbreadcrumb[] = [
  129. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  130. 'name' => get_lang('GroupSpace'),
  131. ];
  132. $group_document = true;
  133. $noPHP_SELF = true;
  134. }
  135. if (!$is_certificate_mode) {
  136. $interbreadcrumb[] = [
  137. "url" => api_get_path(WEB_CODE_PATH)."document/document.php?curdirpath=".urlencode($currentDirPath).'&'.api_get_cidreq(),
  138. "name" => get_lang('Documents'),
  139. ];
  140. } else {
  141. $interbreadcrumb[] = [
  142. 'url' => Category::getUrl(),
  143. 'name' => get_lang('Gradebook'),
  144. ];
  145. }
  146. if (empty($document_data['parents'])) {
  147. $interbreadcrumb[] = ['url' => '#', 'name' => $document_data['title']];
  148. } else {
  149. foreach ($document_data['parents'] as $document_sub_data) {
  150. if ($document_data['title'] == $document_sub_data['title']) {
  151. continue;
  152. }
  153. $interbreadcrumb[] = ['url' => $document_sub_data['document_url'], 'name' => $document_sub_data['title']];
  154. }
  155. }
  156. if (!($is_allowed_to_edit ||
  157. $groupRights ||
  158. DocumentManager::is_my_shared_folder($user_id, $dir, api_get_session_id()))
  159. ) {
  160. api_not_allowed(true);
  161. }
  162. Event::event_access_tool(TOOL_DOCUMENT);
  163. //TODO:check the below code and his funcionality
  164. if (!api_is_allowed_to_edit()) {
  165. if (DocumentManager::check_readonly($course_info, $user_id, $file)) {
  166. api_not_allowed();
  167. }
  168. }
  169. /* MAIN TOOL CODE */
  170. /* Code to change the comment */
  171. if (isset($_POST['comment'])) {
  172. // Fixing the path if it is wrong
  173. $comment = trim($_POST['comment']);
  174. $title = trim($_POST['title']);
  175. // Just in case see BT#3525
  176. if (empty($title)) {
  177. $title = $document_data['title'];
  178. }
  179. if (empty($title)) {
  180. $title = get_document_title($_POST['filename']);
  181. }
  182. if (!empty($document_id)) {
  183. $params = [
  184. 'comment' => $comment,
  185. 'title' => $title,
  186. ];
  187. Database::update(
  188. $dbTable,
  189. $params,
  190. ['c_id = ? AND id = ?' => [$course_id, $document_id]]
  191. );
  192. Display::addFlash(Display::return_message(get_lang('fileModified')));
  193. }
  194. }
  195. /* WYSIWYG HTML EDITOR - Program Logic */
  196. if ($is_allowed_to_edit) {
  197. if (isset($_POST['formSent']) && $_POST['formSent'] == 1) {
  198. $filename = stripslashes($_POST['filename']);
  199. $extension = $_POST['extension'];
  200. $content = isset($_POST['content']) ? trim(str_replace(["\r", "\n"], '', stripslashes($_POST['content']))) : null;
  201. $content = Security::remove_XSS($content, COURSEMANAGERLOWSECURITY);
  202. if ($dir == '/') {
  203. $dir = '';
  204. }
  205. $file = $dir.'/'.$filename.'.'.$extension;
  206. $read_only_flag = isset($_POST['readonly']) ? $_POST['readonly'] : null;
  207. $read_only_flag = empty($read_only_flag) ? 0 : 1;
  208. if (empty($filename)) {
  209. Display::addFlash(Display::return_message(get_lang('NoFileName'), 'warning'));
  210. } else {
  211. $file_size = filesize($document_data['absolute_path']);
  212. if ($read_only_flag == 0) {
  213. if (!empty($content)) {
  214. if ($fp = @fopen($document_data['absolute_path'], 'w')) {
  215. // For flv player, change absolute path temporarily to prevent from erasing it in the following lines
  216. $content = str_replace(['flv=h', 'flv=/'], ['flv=h|', 'flv=/|'], $content);
  217. fputs($fp, $content);
  218. fclose($fp);
  219. $filepath = $document_data['absolute_parent_path'];
  220. // "WHAT'S NEW" notification: update table item_property
  221. $document_id = DocumentManager::get_document_id($_course, $file);
  222. if ($document_id) {
  223. update_existing_document(
  224. $_course,
  225. $document_id,
  226. $file_size,
  227. $read_only_flag
  228. );
  229. api_item_property_update(
  230. $_course,
  231. TOOL_DOCUMENT,
  232. $document_id,
  233. 'DocumentUpdated',
  234. api_get_user_id(),
  235. null,
  236. null,
  237. null,
  238. null,
  239. $sessionId
  240. );
  241. // Update parent folders
  242. item_property_update_on_folder(
  243. $_course,
  244. $dir,
  245. api_get_user_id()
  246. );
  247. } else {
  248. Display::addFlash(Display::return_message(get_lang('Impossible'), 'warning'));
  249. }
  250. } else {
  251. Display::addFlash(Display::return_message(get_lang('Impossible'), 'warning'));
  252. }
  253. } else {
  254. if ($document_id) {
  255. update_existing_document($_course, $document_id, $file_size, $read_only_flag);
  256. }
  257. }
  258. } else {
  259. if ($document_id) {
  260. update_existing_document($_course, $document_id, $file_size, $read_only_flag);
  261. }
  262. }
  263. header('Location: document.php?id='.$document_data['parent_id'].'&'.api_get_cidreq().($is_certificate_mode ? '&curdirpath=/certificates&selectcat=1' : ''));
  264. exit;
  265. }
  266. }
  267. }
  268. // Replace relative paths by absolute web paths (e.g. './' => 'http://www.chamilo.org/courses/ABC/document/')
  269. $content = null;
  270. $extension = null;
  271. $filename = null;
  272. if (file_exists($document_data['absolute_path'])) {
  273. $path_info = pathinfo($document_data['absolute_path']);
  274. $filename = $path_info['filename'];
  275. if (is_file($document_data['absolute_path'])) {
  276. $extension = $path_info['extension'];
  277. if (in_array($extension, ['html', 'htm'])) {
  278. $content = file($document_data['absolute_path']);
  279. $content = implode('', $content);
  280. }
  281. }
  282. }
  283. // Display the header
  284. $nameTools = get_lang('EditDocument').': '.Security::remove_XSS($document_data['title']);
  285. Display::display_header($nameTools, 'Doc');
  286. $document_info = api_get_item_property_info(
  287. api_get_course_int_id(),
  288. 'document',
  289. $document_id,
  290. 0
  291. );
  292. // Try to find this document in the session
  293. if (!empty($sessionId)) {
  294. $document_info = api_get_item_property_info(
  295. api_get_course_int_id(),
  296. 'document',
  297. $document_id,
  298. $sessionId
  299. );
  300. }
  301. $owner_id = $document_info['insert_user_id'];
  302. $last_edit_date = $document_info['lastedit_date'];
  303. $groupInfo = GroupManager::get_group_properties(api_get_group_id());
  304. if ($owner_id == api_get_user_id() ||
  305. api_is_platform_admin() ||
  306. $is_allowed_to_edit || GroupManager:: is_user_in_group(
  307. api_get_user_id(),
  308. $groupInfo
  309. )
  310. ) {
  311. $action = api_get_self().'?id='.$document_data['id'].'&'.api_get_cidreq();
  312. if ($is_certificate_mode) {
  313. $action .= '&curdirpath=/certificates&selectcat=1';
  314. }
  315. $form = new FormValidator(
  316. 'formEdit',
  317. 'post',
  318. $action,
  319. null,
  320. ['class' => 'form-vertical']
  321. );
  322. // Form title
  323. $form->addElement('header', $nameTools);
  324. $form->addElement('hidden', 'filename');
  325. $form->addElement('hidden', 'extension');
  326. $form->addElement('hidden', 'file_path');
  327. $form->addElement('hidden', 'commentPath');
  328. $form->addElement('hidden', 'showedit');
  329. $form->addElement('hidden', 'origin');
  330. $form->addElement('hidden', 'origin_opt');
  331. $form->addText(
  332. 'title',
  333. get_lang('Title'),
  334. true,
  335. ['cols-size' => [2, 10, 0], 'autofocus']
  336. );
  337. $defaults['title'] = $document_data['title'];
  338. $form->addElement('hidden', 'formSent');
  339. $defaults['formSent'] = 1;
  340. $read_only_flag = isset($_POST['readonly']) ? $_POST['readonly'] : null;
  341. // Desactivation of IE proprietary commenting tags inside the text before loading it on the online editor.
  342. // This fix has been proposed by Hubert Borderiou, see Bug #573, http://support.chamilo.org/issues/573
  343. $defaults['content'] = str_replace('<!--[', '<!-- [', $content);
  344. // HotPotatoes tests are html files, but they should not be edited in order their functionality to be preserved.
  345. $showSystemFolders = api_get_course_setting('show_system_folders');
  346. $condition = stripos($dir, '/HotPotatoes_files') === false;
  347. if ($showSystemFolders == 1) {
  348. $condition = true;
  349. }
  350. if (($extension == 'htm' || $extension == 'html') && $condition) {
  351. if (empty($readonly) && $readonly == 0) {
  352. $form->addHtmlEditor('content', '', true, true, $editorConfig);
  353. }
  354. }
  355. if (!$group_document && !DocumentManager::is_my_shared_folder(api_get_user_id(), $currentDirPath, $sessionId)) {
  356. // Updated on field
  357. $display_date = date_to_str_ago($last_edit_date).
  358. ' <span class="dropbox_date">'.api_format_date(api_get_local_time($last_edit_date)).'</span>';
  359. $form->addElement('static', null, get_lang('UpdatedOn'), $display_date);
  360. }
  361. $form->addElement('textarea', 'comment', get_lang('Comment'), ['cols-size' => [2, 10, 0]]);
  362. if ($owner_id == api_get_user_id() || api_is_platform_admin()) {
  363. $checked = &$form->addElement('checkbox', 'readonly', null, get_lang('ReadOnly'));
  364. if ($readonly == 1) {
  365. $checked->setChecked(true);
  366. }
  367. }
  368. if ($is_certificate_mode) {
  369. $form->addButtonUpdate(get_lang('SaveCertificate'));
  370. } else {
  371. $form->addButtonUpdate(get_lang('SaveDocument'));
  372. }
  373. $defaults['filename'] = $filename;
  374. $defaults['extension'] = $extension;
  375. $defaults['file_path'] = isset($_GET['file']) ? Security::remove_XSS($_GET['file']) : null;
  376. $defaults['commentPath'] = $file;
  377. $defaults['renameTo'] = $file_name;
  378. $defaults['comment'] = $document_data['comment'];
  379. $defaults['origin'] = api_get_origin();
  380. $defaults['origin_opt'] = isset($_GET['origin_opt']) ? Security::remove_XSS($_GET['origin_opt']) : null;
  381. $form->setDefaults($defaults);
  382. show_return(
  383. $parent_id,
  384. $dir_original,
  385. $call_from_tool,
  386. $slide_id,
  387. $is_certificate_mode
  388. );
  389. if ($is_certificate_mode) {
  390. $all_information_by_create_certificate = DocumentManager::get_all_info_to_certificate(
  391. api_get_user_id(),
  392. api_get_course_id()
  393. );
  394. $str_info = '';
  395. foreach ($all_information_by_create_certificate[0] as $info_value) {
  396. $str_info .= $info_value.'<br/>';
  397. }
  398. $create_certificate = get_lang('CreateCertificateWithTags');
  399. echo Display::return_message(
  400. $create_certificate.': <br /><br />'.$str_info,
  401. 'normal',
  402. false
  403. );
  404. }
  405. if ($extension == 'svg' && !api_browser_support('svg') && api_get_setting('enabled_support_svg') == 'true') {
  406. echo Display::return_message(get_lang('BrowserDontSupportsSVG'), 'warning');
  407. }
  408. // HTML-editor
  409. echo '<div class="page-create">
  410. <div class="row" style="overflow:hidden">
  411. <div id="template_col" class="col-md-3">
  412. <div class="panel panel-default">
  413. <div class="panel-body">
  414. <div id="frmModel" class="items-templates scrollbar-light"></div>
  415. </div>
  416. </div>
  417. </div>
  418. <div id="doc_form" class="col-md-9">
  419. '.$form->returnForm().'
  420. </div>
  421. </div></div>';
  422. }
  423. Display::display_footer();
  424. /**
  425. This function changes the name of a certain file.
  426. It needs no global variables, it takes all info from parameters.
  427. It returns nothing.
  428. @todo check if this function is used
  429. */
  430. function change_name($base_work_dir, $source_file, $rename_to, $dir, $doc)
  431. {
  432. $file_name_for_change = $base_work_dir.$dir.$source_file;
  433. $rename_to = disable_dangerous_file($rename_to); // Avoid renaming to .htaccess file
  434. $rename_to = my_rename($file_name_for_change, stripslashes($rename_to)); // fileManage API
  435. if ($rename_to) {
  436. if (isset($dir) && $dir != '') {
  437. $source_file = $dir.$source_file;
  438. $new_full_file_name = dirname($source_file).'/'.$rename_to;
  439. } else {
  440. $source_file = '/'.$source_file;
  441. $new_full_file_name = '/'.$rename_to;
  442. }
  443. update_db_info('update', $source_file, $new_full_file_name); // fileManage API
  444. Display::addFlash(Display::return_message(get_lang('fileModified')));
  445. return true;
  446. } else {
  447. Display::addFlash(Display::return_message(get_lang('FileExists')));
  448. }
  449. }
  450. //return button back to
  451. function show_return($document_id, $path, $call_from_tool = '', $slide_id = 0, $is_certificate_mode = false)
  452. {
  453. $actionsLeft = null;
  454. global $parent_id;
  455. $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&id='.$parent_id;
  456. if ($is_certificate_mode) {
  457. $selectedCategory = (isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : '');
  458. $actionsLeft .= '<a href="document.php?curdirpath='.$selectedCategory.'&selectcat='.$selectedCategory.'">'.
  459. Display::return_icon('back.png', get_lang('Back').' '.get_lang('To').' '.get_lang('CertificateOverview'), '', ICON_SIZE_MEDIUM).'</a>';
  460. $actionsLeft .= '<a id="hide_bar_template" href="#" role="button">'.Display::return_icon('expand.png', get_lang('Expand'), ['id' => 'expand'], ICON_SIZE_MEDIUM).Display::return_icon('contract.png', get_lang('Collapse'), ['id' => 'contract', 'class' => 'hide'], ICON_SIZE_MEDIUM).'</a>';
  461. } elseif ($call_from_tool == 'slideshow') {
  462. $actionsLeft .= '<a href="'.api_get_path(WEB_PATH).'main/document/slideshow.php?slide_id='.$slide_id.'&curdirpath='.Security::remove_XSS(urlencode($_GET['curdirpath'])).'">'.
  463. Display::return_icon('slideshow.png', get_lang('BackTo').' '.get_lang('ViewSlideshow'), '', ICON_SIZE_MEDIUM).'</a>';
  464. } elseif ($call_from_tool == 'editdraw') {
  465. $actionsLeft .= '<a href="'.$url.'">'.
  466. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).'</a>';
  467. $actionsLeft .= '<a href="javascript:history.back(1)">'.Display::return_icon('draw.png', get_lang('BackTo').' '.get_lang('Draw'), [], 32).'</a>';
  468. } elseif ($call_from_tool == 'editodf') {
  469. $actionsLeft .= '<a href="'.$url.'">'.
  470. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).'</a>';
  471. $actionsLeft .= '<a href="javascript:history.back(1)">'.Display::return_icon('draw.png', get_lang('BackTo').' '.get_lang('Write'), [], 32).'</a>';
  472. $actionsLeft .= '<a id="hide_bar_template" href="#" role="button">'.Display::return_icon('expand.png', get_lang('Expand'), ['id' => 'expand'], ICON_SIZE_MEDIUM).Display::return_icon('contract.png', get_lang('Collapse'), ['id' => 'contract', 'class' => 'hide'], ICON_SIZE_MEDIUM).'</a>';
  473. } elseif ($call_from_tool == 'editpaint') {
  474. $actionsLeft .= '<a href="'.$url.'">'.
  475. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), [], ICON_SIZE_MEDIUM).'</a>';
  476. $actionsLeft .= '<a href="javascript:history.back(1)">'.Display::return_icon('paint.png', get_lang('BackTo').' '.get_lang('Paint'), [], 32).'</a>';
  477. } else {
  478. $actionsLeft .= '<a href="'.$url.'">'.
  479. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).'</a>';
  480. $actionsLeft .= '<a id="hide_bar_template" href="#" role="button">'.Display::return_icon('expand.png', get_lang('Expand'), ['id' => 'expand'], ICON_SIZE_MEDIUM).Display::return_icon('contract.png', get_lang('Collapse'), ['id' => 'contract', 'class' => 'hide'], ICON_SIZE_MEDIUM).'</a>';
  481. }
  482. echo $toolbar = Display::toolbarAction('actions-documents', [$actionsLeft]);
  483. }