create_document.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file allows creating new html documents with an online WYSIWYG html editor.
  5. *
  6. * @package chamilo.document
  7. */
  8. /**
  9. * Code
  10. */
  11. /* INIT SECTION */
  12. // Name of the language file that needs to be included
  13. $language_file = array('document', 'gradebook');
  14. ////require_once '../inc/global.inc.php';
  15. api_protect_course_script();
  16. $_SESSION['whereami'] = 'document/create';
  17. $this_section = SECTION_COURSES;
  18. $htmlHeadXtra[] = '<script>
  19. var hide_bar = function() {
  20. $("#template_col").hide();
  21. $("#doc_form").removeClass("span9");
  22. $("#doc_form").addClass("span11");
  23. }
  24. $(document).ready(function() {
  25. if ($(window).width() <= 785 ) {
  26. hide_bar();
  27. }
  28. $("#hide_bar_template").toggle(
  29. function() {
  30. hide_bar();
  31. },
  32. function() {
  33. $("#template_col").show();
  34. $("#doc_form").removeClass("span11");
  35. $("#doc_form").addClass("span9");
  36. }
  37. );
  38. });
  39. function setFocus() {
  40. $("#document_title").focus();
  41. }
  42. $(window).load(function () {
  43. setFocus();
  44. });
  45. </script>';
  46. require_once api_get_path(SYS_CODE_PATH).'document/document.inc.php';
  47. //I'm in the certification module?
  48. $is_certificate_mode = false;
  49. $_course = api_get_course_info();
  50. $groupId = api_get_group_id();
  51. if (isset($_REQUEST['certificate']) && $_REQUEST['certificate'] == 'true') {
  52. $is_certificate_mode = true;
  53. }
  54. if ($is_certificate_mode) {
  55. $nameTools = get_lang('CreateCertificate');
  56. } else {
  57. $nameTools = get_lang('CreateDocument');
  58. }
  59. /* Constants and variables */
  60. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  61. $course_id = api_get_course_int_id();
  62. $document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true);
  63. if (empty($document_data)) {
  64. if (api_is_in_group()) {
  65. $group_properties = GroupManager::get_group_properties($groupId);
  66. $document_id = DocumentManager::get_document_id(api_get_course_info(), $group_properties['directory']);
  67. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  68. $dir = $document_data['path'];
  69. $folder_id = $document_data['id'];
  70. } else {
  71. $dir = '/';
  72. $folder_id = 0;
  73. }
  74. } else {
  75. $folder_id = $document_data['id'];
  76. $dir = $document_data['path'];
  77. }
  78. /* MAIN CODE */
  79. // Please, do not modify this dirname formatting
  80. if (strstr($dir, '..')) {
  81. $dir = '/';
  82. }
  83. if ($dir[0] == '.') {
  84. $dir = substr($dir, 1);
  85. }
  86. if ($dir[0] != '/') {
  87. $dir = '/'.$dir;
  88. }
  89. if ($dir[strlen($dir) - 1] != '/') {
  90. $dir .= '/';
  91. }
  92. if ($is_certificate_mode) {
  93. $document_id = DocumentManager::get_document_id(api_get_course_info(), '/certificates');
  94. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), true);
  95. $folder_id = $document_data['id'];
  96. $dir = '/certificates/';
  97. }
  98. // Configuration for the FCKEDITOR
  99. $doc_tree = explode('/', $dir);
  100. $count_dir = count($doc_tree) - 2; // "2" because at the begin and end there are 2 "/"
  101. if (api_is_in_group()) {
  102. $group_properties = GroupManager::get_group_properties($groupId);
  103. // Level correction for group documents.
  104. if (!empty($group_properties['directory'])) {
  105. $count_dir = $count_dir > 0 ? $count_dir - 1 : 0;
  106. }
  107. }
  108. $relative_url = '';
  109. for ($i = 0; $i < ($count_dir); $i++) {
  110. $relative_url .= '../';
  111. }
  112. // We do this in order to avoid the condition in html_editor.php ==> if ($this -> fck_editor->Config['CreateDocumentWebDir']=='' || $this -> fck_editor->Config['CreateDocumentDir']== '')
  113. if ($relative_url == '') {
  114. $relative_url = '/';
  115. }
  116. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  117. $html_editor_config = array(
  118. 'ToolbarSet' => ($is_allowed_to_edit ? 'Documents' : 'DocumentsStudent'),
  119. 'Width' => '100%',
  120. 'Height' => '500',
  121. 'FullPage' => true,
  122. 'InDocument' => true,
  123. 'CreateDocumentDir' => $relative_url,
  124. 'CreateDocumentWebDir' => (empty($group_properties['directory']))
  125. ? api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/'
  126. : api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/',
  127. 'BaseHref' => api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir
  128. );
  129. if ($is_certificate_mode) {
  130. $html_editor_config['CreateDocumentDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  131. $html_editor_config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  132. $html_editor_config['BaseHref'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir;
  133. }
  134. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  135. if (!is_dir($filepath)) {
  136. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  137. $dir = '/';
  138. }
  139. if (!$is_certificate_mode) {
  140. $req_gid = null;
  141. if (api_is_in_group()) {
  142. $req_gid = '&amp;gidReq='.$groupId;
  143. $interbreadcrumb[] = array(
  144. "url" => "../group/group_space.php?gidReq=".$groupId,
  145. "name" => get_lang('GroupSpace')
  146. );
  147. $noPHP_SELF = true;
  148. $path = explode('/', $dir);
  149. if ('/'.$path[1] != $group_properties['directory']) {
  150. api_not_allowed(true);
  151. }
  152. }
  153. $interbreadcrumb[] = array(
  154. "url" => "./document.php?curdirpath=".urlencode($dir).$req_gid,
  155. "name" => get_lang('Documents')
  156. );
  157. } else {
  158. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('Gradebook'));
  159. }
  160. if (!($is_allowed_to_edit || GroupManager::groupMemberWithUploadRights() || is_my_shared_folder(
  161. $_user['user_id'],
  162. Security::remove_XSS($dir),
  163. api_get_session_id()
  164. ))
  165. ) {
  166. api_not_allowed(true);
  167. }
  168. Event::event_access_tool(TOOL_DOCUMENT);
  169. $display_dir = $dir;
  170. if (isset ($group_properties)) {
  171. $display_dir = explode('/', $dir);
  172. unset ($display_dir[0]);
  173. unset ($display_dir[1]);
  174. $display_dir = implode('/', $display_dir);
  175. }
  176. $select_cat = isset($_GET['selectcat']) ? intval($_GET['selectcat']) : null;
  177. // Create a new form
  178. $form = new FormValidator('create_document', 'post', api_get_self().'?'.api_get_cidreq().'&dir='.Security::remove_XSS(
  179. urlencode($dir)
  180. ).'&selectcat='.$select_cat, null, array('class' => 'form-horizontal'));
  181. // form title
  182. $form->addElement('header', $nameTools);
  183. if ($is_certificate_mode) { //added condition for certicate in gradebook
  184. $form->addElement('hidden', 'certificate', 'true', array('id' => 'certificate'));
  185. if (isset($_GET['selectcat'])) {
  186. $form->addElement('hidden', 'selectcat', $select_cat);
  187. }
  188. }
  189. // Hidden element with current directory
  190. $form->addElement('hidden', 'id');
  191. $defaults = array();
  192. $defaults['id'] = $folder_id;
  193. // Filename
  194. $form->addElement('hidden', 'title_edited', 'false', 'id="title_edited"');
  195. /**
  196. * Check if a document width the choosen filename allready exists
  197. */
  198. function document_exists($filename)
  199. {
  200. global $filepath;
  201. $filename = addslashes(trim($filename));
  202. $filename = Security::remove_XSS($filename);
  203. $filename = api_replace_dangerous_char($filename);
  204. $filename = FileManager::disable_dangerous_file($filename);
  205. return !file_exists($filepath.$filename.'.html');
  206. }
  207. // Add group to the form
  208. if ($is_certificate_mode) {
  209. $form->addElement('text', 'title', get_lang('CertificateName'), array('id' => 'document_title'));
  210. } else {
  211. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'document_title'));
  212. }
  213. // Show read-only box only in groups
  214. if (!empty($groupId)) {
  215. $group[] = $form->createElement('checkbox', 'readonly', '', get_lang('ReadOnly'));
  216. }
  217. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  218. $form->addRule('title', get_lang('FileExists'), 'callback', 'document_exists');
  219. $current_session_id = api_get_session_id();
  220. $form->add_html_editor('content', '', false, false, $html_editor_config);
  221. // Comment-field
  222. $folders = DocumentManager::get_all_document_folders($_course, $groupId, $is_allowed_to_edit);
  223. // If we are not in the certificates creation, display a folder chooser for the
  224. // new document created
  225. if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'], $dir, $current_session_id)) {
  226. $folders = DocumentManager::get_all_document_folders($_course, $groupId, $is_allowed_to_edit);
  227. $parent_select = $form->addElement('select', 'curdirpath', array(null, get_lang('DestinationDirectory')));
  228. // Following two conditions copied from document.inc.php::build_directory_selector()
  229. $folder_titles = array();
  230. if (is_array($folders)) {
  231. $escaped_folders = array();
  232. foreach ($folders as $key => & $val) {
  233. //Hide some folders
  234. if ($val == '/HotPotatoes_files' || $val == '/certificates' || basename($val) == 'css') {
  235. continue;
  236. }
  237. //Admin setting for Hide/Show the folders of all users
  238. if (api_get_setting('show_users_folders') == 'false' && (strstr($val, '/shared_folder') || strstr(
  239. $val,
  240. 'shared_folder_session_'
  241. ))
  242. ) {
  243. continue;
  244. }
  245. //Admin setting for Hide/Show Default folders to all users
  246. if (api_get_setting(
  247. 'show_default_folders'
  248. ) == 'false' && ($val == '/images' || $val == '/flash' || $val == '/audio' || $val == '/video' || strstr(
  249. $val,
  250. '/images/gallery'
  251. ) || $val == '/video/flv')
  252. ) {
  253. continue;
  254. }
  255. //Admin setting for Hide/Show chat history folder
  256. if (api_get_setting('show_chat_folder') == 'false' && $val == '/chat_files') {
  257. continue;
  258. }
  259. $escaped_folders[$key] = Database::escape_string($val);
  260. }
  261. $folder_sql = implode("','", $escaped_folders);
  262. $sql = "SELECT * FROM $doc_table WHERE c_id = $course_id AND filetype='folder' AND path IN ('".$folder_sql."')";
  263. $res = Database::query($sql);
  264. $folder_titles = array();
  265. while ($obj = Database::fetch_object($res)) {
  266. $folder_titles[$obj->path] = $obj->title;
  267. }
  268. }
  269. if (empty($group_dir)) {
  270. $parent_select->addOption(get_lang('HomeDirectory'), '/');
  271. if (is_array($folders)) {
  272. foreach ($folders as & $folder) {
  273. //Hide some folders
  274. if ($folder == '/HotPotatoes_files' || $folder == '/certificates' || basename($folder) == 'css') {
  275. continue;
  276. }
  277. //Admin setting for Hide/Show the folders of all users
  278. if (api_get_setting('show_users_folders') == 'false' && (strstr($folder, '/shared_folder') || strstr(
  279. $folder,
  280. 'shared_folder_session_'
  281. ))
  282. ) {
  283. continue;
  284. }
  285. //Admin setting for Hide/Show Default folders to all users
  286. if (api_get_setting(
  287. 'show_default_folders'
  288. ) == 'false' && ($folder == '/images' || $folder == '/flash' || $folder == '/audio' || $folder == '/video' || strstr(
  289. $folder,
  290. '/images/gallery'
  291. ) || $folder == '/video/flv')
  292. ) {
  293. continue;
  294. }
  295. //Admin setting for Hide/Show chat history folder
  296. if (api_get_setting('show_chat_folder') == 'false' && $folder == '/chat_files') {
  297. continue;
  298. }
  299. $selected = (substr($dir, 0, -1) == $folder) ? ' selected="selected"' : '';
  300. $path_parts = explode('/', $folder);
  301. $folder_titles[$folder] = Text::cut($folder_titles[$folder], 80);
  302. $space_counter = count($path_parts) - 2;
  303. if ($space_counter > 0) {
  304. $label = str_repeat('&nbsp;&nbsp;&nbsp;', $space_counter).' &mdash; '.$folder_titles[$folder];
  305. } else {
  306. $label = ' &mdash; '.$folder_titles[$folder];
  307. }
  308. $parent_select->addOption($label, $folder);
  309. if ($selected != '') {
  310. $parent_select->setSelected($folder);
  311. }
  312. }
  313. }
  314. } else {
  315. foreach ($folders as & $folder) {
  316. $selected = (substr($dir, 0, -1) == $folder) ? ' selected="selected"' : '';
  317. $label = $folder_titles[$folder];
  318. if ($folder == $group_dir) {
  319. $label = '/ ('.get_lang('HomeDirectory').')';
  320. } else {
  321. $path_parts = explode('/', str_replace($group_dir, '', $folder));
  322. $label = Text::cut($label, 80);
  323. $label = str_repeat('&nbsp;&nbsp;&nbsp;', count($path_parts) - 2).' &mdash; '.$label;
  324. }
  325. $parent_select->addOption($label, $folder);
  326. if ($selected != '') {
  327. $parent_select->setSelected($folder);
  328. }
  329. }
  330. }
  331. }
  332. if ($is_certificate_mode) {
  333. $form->addElement('style_submit_button', 'submit', get_lang('CreateCertificate'), 'class="save"');
  334. } else {
  335. $form->addElement('style_submit_button', 'submit', get_lang('CreateDoc'), 'class="save"');
  336. }
  337. $form->setDefaults($defaults);
  338. // If form validates -> save the new document
  339. if ($form->validate()) {
  340. $values = $form->exportValues();
  341. $readonly = isset($values['readonly']) ? 1 : 0;
  342. $values['title'] = trim($values['title']);
  343. if (!empty($values['curdirpath'])) {
  344. $dir = $values['curdirpath'];
  345. }
  346. if ($dir[strlen($dir) - 1] != '/') {
  347. $dir .= '/';
  348. }
  349. //Setting the filename
  350. $filename = $values['title'];
  351. $filename = addslashes(trim($filename));
  352. $filename = Security::remove_XSS($filename);
  353. $filename = api_replace_dangerous_char($filename);
  354. $filename = FileManager::disable_dangerous_file($filename);
  355. //Setting the title
  356. $title = $values['title'];
  357. //Setting the extension
  358. $extension = 'html';
  359. $content = Security::remove_XSS($values['content'], COURSEMANAGERLOWSECURITY);
  360. if (strpos($content, '/css/frames.css') === false) {
  361. $content = str_replace(
  362. '</head>',
  363. '<style> body{margin:10px;}</style><link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>',
  364. $content
  365. );
  366. }
  367. if ($fp = @fopen($filepath.$filename.'.'.$extension, 'w')) {
  368. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].'/courses/', $content);
  369. // change the path of mp3 to absolute
  370. // first regexp deals with ../../../ urls
  371. // Disabled by Ivan Tcholakov.
  372. //$content = preg_replace("|(flashvars=\"file=)(\.+/)+|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',$content);
  373. //second regexp deals with audio/ urls
  374. // Disabled by Ivan Tcholakov.
  375. //$content = preg_replace("|(flashvars=\"file=)([^/]+)/|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',$content);
  376. fputs($fp, $content);
  377. fclose($fp);
  378. chmod($filepath.$filename.'.'.$extension, api_get_permissions_for_new_files());
  379. if (!is_dir($filepath.'css')) {
  380. mkdir($filepath.'css', api_get_permissions_for_new_directories());
  381. $doc_id = FileManager::add_document($_course, $dir.'css', 'folder', 0, 'css');
  382. api_item_property_update(
  383. $_course,
  384. TOOL_DOCUMENT,
  385. $doc_id,
  386. 'FolderCreated',
  387. $_user['user_id'],
  388. null,
  389. null,
  390. null,
  391. null,
  392. $current_session_id
  393. );
  394. api_item_property_update(
  395. $_course,
  396. TOOL_DOCUMENT,
  397. $doc_id,
  398. 'invisible',
  399. $_user['user_id'],
  400. null,
  401. null,
  402. null,
  403. null,
  404. $current_session_id
  405. );
  406. }
  407. if (!is_file($filepath.'css/frames.css')) {
  408. // Make a copy of the current css for the new document
  409. copy(
  410. api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/frames.css',
  411. $filepath.'css/frames.css'
  412. );
  413. $doc_id = FileManager::add_document(
  414. $_course,
  415. $dir.'css/frames.css',
  416. 'file',
  417. filesize($filepath.'css/frames.css'),
  418. 'frames.css'
  419. );
  420. api_item_property_update(
  421. $_course,
  422. TOOL_DOCUMENT,
  423. $doc_id,
  424. 'DocumentAdded',
  425. $_user['user_id'],
  426. null,
  427. null,
  428. null,
  429. null,
  430. $current_session_id
  431. );
  432. api_item_property_update(
  433. $_course,
  434. TOOL_DOCUMENT,
  435. $doc_id,
  436. 'invisible',
  437. $_user['user_id'],
  438. null,
  439. null,
  440. null,
  441. null,
  442. $current_session_id
  443. );
  444. }
  445. $file_size = filesize($filepath.$filename.'.'.$extension);
  446. $save_file_path = $dir.$filename.'.'.$extension;
  447. $document_id = FileManager::add_document(
  448. $_course,
  449. $save_file_path,
  450. 'file',
  451. $file_size,
  452. $title,
  453. null,
  454. $readonly
  455. );
  456. if ($document_id) {
  457. api_item_property_update(
  458. $_course,
  459. TOOL_DOCUMENT,
  460. $document_id,
  461. 'DocumentAdded',
  462. $_user['user_id'],
  463. $groupId,
  464. null,
  465. null,
  466. null,
  467. $current_session_id
  468. );
  469. // Update parent folders
  470. FileManager::item_property_update_on_folder($_course, $dir, $_user['user_id']);
  471. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  472. $new_comment = Database::escape_string($new_comment);
  473. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  474. $new_title = htmlspecialchars($new_title);
  475. $new_title = Database::escape_string($new_title);
  476. if ($new_comment || $new_title) {
  477. $ct = '';
  478. if ($new_comment) {
  479. $ct .= ", comment='$new_comment'";
  480. }
  481. if ($new_title) {
  482. $ct .= ", title='$new_title'";
  483. }
  484. Database::query(
  485. "UPDATE $doc_table SET".substr($ct, 1)." WHERE c_id = $course_id AND id = '$document_id'"
  486. );
  487. }
  488. $dir = substr($dir, 0, -1);
  489. $selectcat = '';
  490. if (isset($_REQUEST['selectcat'])) {
  491. $selectcat = "&selectcat=".Security::remove_XSS($_REQUEST['selectcat']);
  492. }
  493. $certificate_condition = '';
  494. if ($is_certificate_mode) {
  495. $df = DocumentManager::get_default_certificate_id($_course['code']);
  496. if (!isset($df)) {
  497. DocumentManager::attach_gradebook_certificate($_course['code'], $document_id);
  498. }
  499. $certificate_condition = '&certificate=true';
  500. }
  501. header('Location: document.php?'.api_get_cidreq().'&id='.$folder_id.$selectcat.$certificate_condition);
  502. exit();
  503. } else {
  504. Display :: display_header($nameTools, 'Doc');
  505. Display :: display_error_message(get_lang('Impossible'));
  506. Display :: display_footer();
  507. }
  508. } else {
  509. Display :: display_header($nameTools, 'Doc');
  510. Display :: display_error_message(get_lang('Impossible'));
  511. Display :: display_footer();
  512. }
  513. } else {
  514. // Interbreadcrumb for the current directory root path
  515. // Copied from document.php
  516. $dir_array = explode('/', $dir);
  517. $array_len = count($dir_array);
  518. // Interbreadcrumb for the current directory root path
  519. if (empty($document_data['parents'])) {
  520. $interbreadcrumb[] = array('url' => '#', 'name' => $document_data['title']);
  521. } else {
  522. foreach ($document_data['parents'] as $document_sub_data) {
  523. $interbreadcrumb[] = array(
  524. 'url' => $document_sub_data['document_url'],
  525. 'name' => $document_sub_data['title']
  526. );
  527. }
  528. }
  529. $interbreadcrumb[] = array('url' => '#', 'name' => $nameTools);
  530. Display :: display_header($nameTools, "Doc");
  531. if ($is_certificate_mode) {
  532. $all_information_by_create_certificate = DocumentManager::get_all_info_to_certificate(
  533. api_get_user_id(),
  534. api_get_course_id()
  535. );
  536. $str_info = '';
  537. foreach ($all_information_by_create_certificate[0] as $info_value) {
  538. $str_info .= $info_value.'<br/>';
  539. }
  540. $create_certificate = get_lang('CreateCertificateWithTags');
  541. Display::display_normal_message($create_certificate.': <br /><br/>'.$str_info, false);
  542. }
  543. echo $form->return_form();
  544. Display :: display_footer();
  545. }