create_document.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. /* INIT SECTION */
  9. // Name of the language file that needs to be included
  10. $language_file = 'document';
  11. require_once '../inc/global.inc.php';
  12. $_SESSION['whereami'] = 'document/create';
  13. $this_section = SECTION_COURSES;
  14. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  15. $htmlHeadXtra[] = '<script type="text/javascript">
  16. function InnerDialogLoaded()
  17. {
  18. /*
  19. var B=new window.frames[0].FCKToolbarButton(\'Templates\',window.frames[0].FCKLang.Templates);
  20. return B.ClickFrame();
  21. */
  22. var isIE = (navigator.appVersion.indexOf(\'MSIE\') != -1) ? true : false ;
  23. var EditorFrame = null ;
  24. if ( !isIE )
  25. {
  26. EditorFrame = window.frames[0] ;
  27. }
  28. else
  29. {
  30. // For this dynamic page window.frames[0] enumerates frames in a different order in IE.
  31. // We need a sure method to locate the frame that contains the online editor.
  32. for ( var i = 0, n = window.frames.length ; i < n ; i++ )
  33. {
  34. if ( window.frames[i].location.toString().indexOf(\'InstanceName=content\') != -1 )
  35. {
  36. EditorFrame = window.frames[i] ;
  37. }
  38. }
  39. }
  40. if ( !EditorFrame )
  41. {
  42. return null ;
  43. }
  44. var B = new EditorFrame.FCKToolbarButton(\'Templates\', EditorFrame.FCKLang.Templates);
  45. return B.ClickFrame();
  46. };
  47. var temp=false;
  48. var temp2=false;
  49. var use_document_title='.api_get_setting('use_document_title').';
  50. var load_default_template = '. ((isset($_POST['submit']) || empty($_SERVER['QUERY_STRING'])) ? 'false' : 'true' ) .';
  51. function launch_templates()
  52. {
  53. //document.getElementById(\'frmModel\').style.display="block";
  54. //document.getElementById(\'content___Frame\').width=\'70%\';
  55. //window.frames[0].FCKToolbarItems.GetItem("Template").Click;
  56. }
  57. function FCKeditor_OnComplete( editorInstance )
  58. {
  59. editorInstance.Events.AttachEvent( \'OnSelectionChange\', check_for_title ) ;
  60. document.getElementById(\'frmModel\').innerHTML = "<iframe style=\'height: 525px; width: 180px;\' scrolling=\'no\' frameborder=\'0\' src=\''.api_get_path(WEB_LIBRARY_PATH).'fckeditor/editor/fckdialogframe.html \'>";
  61. }
  62. function check_for_title()
  63. {
  64. if(temp==true){
  65. // This functions shows that you can interact directly with the editor area
  66. // DOM. In this way you have the freedom to do anything you want with it.
  67. // Get the editor instance that we want to interact with.
  68. var oEditor = FCKeditorAPI.GetInstance(\'content\') ;
  69. // Get the Editor Area DOM (Document object).
  70. var oDOM = oEditor.EditorDocument ;
  71. var iLength ;
  72. var contentText ;
  73. var contentTextArray;
  74. var bestandsnaamNieuw = "";
  75. var bestandsnaamOud = "";
  76. // The are two diffent ways to get the text (without HTML markups).
  77. // It is browser specific.
  78. if( document.all ) // If Internet Explorer.
  79. {
  80. contentText = oDOM.body.innerText ;
  81. }
  82. else // If Gecko.
  83. {
  84. var r = oDOM.createRange() ;
  85. r.selectNodeContents( oDOM.body ) ;
  86. contentText = r.toString() ;
  87. }
  88. var index=contentText.indexOf("/*<![CDATA");
  89. contentText=contentText.substr(0,index);
  90. // Compose title if there is none
  91. contentTextArray = contentText.split(\' \') ;
  92. var x=0;
  93. for(x=0; (x<5 && x<contentTextArray.length); x++)
  94. {
  95. if(x < 4)
  96. {
  97. bestandsnaamNieuw += contentTextArray[x] + \' \';
  98. }
  99. else
  100. {
  101. bestandsnaamNieuw += contentTextArray[x];
  102. }
  103. }
  104. // comment see FS#3335
  105. // if(document.getElementById(\'title_edited\').value == "false")
  106. // {
  107. // document.getElementById(\'filename\').value = bestandsnaamNieuw;
  108. // if(use_document_title){
  109. // document.getElementById(\'title\').value = bestandsnaamNieuw;
  110. // }
  111. // }
  112. }
  113. temp=true;
  114. }
  115. function trim(s)
  116. {
  117. while(s.substring(0,1) == \' \') {
  118. s = s.substring(1,s.length);
  119. }
  120. while(s.substring(s.length-1,s.length) == \' \') {
  121. s = s.substring(0,s.length-1);
  122. }
  123. return s;
  124. }
  125. function check_if_still_empty()
  126. {
  127. if(trim(document.getElementById(\'filename\').value) != "")
  128. {
  129. document.getElementById(\'title_edited\').value = "true";
  130. }
  131. }
  132. function setFocus(){
  133. $("#document_title").focus();
  134. }
  135. $(window).load(function () {
  136. setFocus();
  137. });
  138. </script>';
  139. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  140. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  141. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  142. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  143. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  144. if (isset($_REQUEST['certificate'])) {
  145. $nameTools = get_lang('CreateCertificate');
  146. } else {
  147. $nameTools = get_lang('CreateDocument');
  148. }
  149. $nameTools = get_lang('CreateDocument');
  150. /* Constants and variables */
  151. $dir = isset($_GET['dir']) ? Security::remove_XSS($_GET['dir']) : Security::remove_XSS($_POST['dir']);
  152. /* MAIN CODE */
  153. if (api_is_in_group()) {
  154. $group_properties = GroupManager::get_group_properties($_SESSION['_gid']);
  155. }
  156. // Please, do not modify this dirname formatting
  157. if (strstr($dir, '..')) {
  158. $dir = '/';
  159. }
  160. if ($dir[0] == '.') {
  161. $dir = substr($dir, 1);
  162. }
  163. if ($dir[0] != '/') {
  164. $dir = '/'.$dir;
  165. }
  166. if ($dir[strlen($dir) - 1] != '/') {
  167. $dir .= '/';
  168. }
  169. // Configuration for the FCKEDITOR
  170. $doc_tree= explode('/', $dir);
  171. $count_dir = count($doc_tree) -2; // "2" because at the begin and end there are 2 "/"
  172. // Level correction for group documents.
  173. if (!empty($group_properties['directory']))
  174. {
  175. $count_dir = $count_dir > 0 ? $count_dir - 1 : 0;
  176. }
  177. $relative_url = '';
  178. for ($i = 0; $i < ($count_dir); $i++) {
  179. $relative_url .= '../';
  180. }
  181. // We do this in order to avoid the condition in html_editor.php ==> if ($this -> fck_editor->Config['CreateDocumentWebDir']=='' || $this -> fck_editor->Config['CreateDocumentDir']== '')
  182. if ($relative_url== '') {
  183. $relative_url = '/';
  184. }
  185. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  186. $html_editor_config = array(
  187. 'ToolbarSet' => ($is_allowed_to_edit ? 'Documents' :'DocumentsStudent'),
  188. 'Width' => '100%',
  189. 'Height' => '600',
  190. 'FullPage' => true,
  191. 'InDocument' => true,
  192. 'CreateDocumentDir' => $relative_url,
  193. 'CreateDocumentWebDir' => (empty($group_properties['directory']))
  194. ? api_get_path('WEB_COURSE_PATH').$_course['path'].'/document/'
  195. : api_get_path('WEB_COURSE_PATH').api_get_course_path().'/document'.$group_properties['directory'].'/',
  196. 'BaseHref' => api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir
  197. );
  198. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  199. if (!is_dir($filepath)) {
  200. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  201. $dir = '/';
  202. }
  203. //I'm in the certification module?
  204. $is_certificate_mode = false;
  205. $is_certificate_array = explode('/',$_GET['dir']);
  206. array_shift($is_certificate_array);
  207. if ($is_certificate_array[0]=='certificates') {
  208. $is_certificate_mode = true;
  209. }
  210. $to_group_id = 0;
  211. if (!$is_certificate_mode) {
  212. if (isset ($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  213. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  214. $interbreadcrumb[] = array ("url" => "../group/group_space.php?gidReq=".$_SESSION['_gid'], "name" => get_lang('GroupSpace'));
  215. $noPHP_SELF = true;
  216. $to_group_id = $_SESSION['_gid'];
  217. $group = GroupManager :: get_group_properties($to_group_id);
  218. $path = explode('/', $dir);
  219. if ('/'.$path[1] != $group['directory']) {
  220. api_not_allowed(true);
  221. }
  222. }
  223. $interbreadcrumb[] = array ("url" => "./document.php?curdirpath=".urlencode($_GET['dir']).$req_gid, "name" => get_lang('Documents'));
  224. } else {
  225. $interbreadcrumb[]= array ( 'url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('Gradebook'));
  226. }
  227. if (!$is_allowed_in_course) {
  228. api_not_allowed(true);
  229. }
  230. if (!($is_allowed_to_edit || $_SESSION['group_member_with_upload_rights'])) {
  231. api_not_allowed(true);
  232. }
  233. /* Header */
  234. event_access_tool(TOOL_DOCUMENT);
  235. $display_dir = $dir;
  236. if (isset ($group)) {
  237. $display_dir = explode('/', $dir);
  238. unset ($display_dir[0]);
  239. unset ($display_dir[1]);
  240. $display_dir = implode('/', $display_dir);
  241. }
  242. // Create a new form
  243. $form = new FormValidator('create_document','post',api_get_self().'?dir='.Security::remove_XSS(urlencode($_GET['dir'])).'&selectcat='.Security::remove_XSS($_GET['selectcat']));
  244. // form title
  245. $form->addElement('header', '', $nameTools);
  246. if (isset($_REQUEST['certificate'])) {//added condition for certicate in gradebook
  247. $form->addElement('hidden','certificate','true',array('id'=>'certificate'));
  248. if (isset($_GET['selectcat']))
  249. $form->addElement('hidden','selectcat',intval($_GET['selectcat']));
  250. }
  251. $renderer = & $form->defaultRenderer();
  252. // Hidden element with current directory
  253. $form->addElement('hidden', 'dir');
  254. $default['dir'] = $dir;
  255. // Filename
  256. $form->addElement('hidden', 'title_edited', 'false', 'id="title_edited"');
  257. /**
  258. * Check if a document width the choosen filename allready exists
  259. */
  260. function document_exists($filename) {
  261. global $filepath;
  262. $filename = addslashes(trim($filename));
  263. $filename = Security::remove_XSS($filename);
  264. $filename = replace_dangerous_char($filename);
  265. $filename = disable_dangerous_file($filename);
  266. return !file_exists($filepath.$filename.'.html');
  267. }
  268. // Change the default renderer for the filename-field to display the dir and extension
  269. /*
  270. $renderer = & $form->defaultRenderer();
  271. */
  272. //$filename_template = str_replace('{element}', "<tt>$display_dir</tt> {element} <tt>.html</tt>", $renderer->_elementTemplate);
  273. $filename_template = str_replace('{element}', '{element}', $renderer->_elementTemplate); // TODO: What is the point of this statement?
  274. $renderer->setElementTemplate($filename_template, 'filename');
  275. // Initialize group array
  276. $group = array();
  277. // If allowed, add element for document title
  278. if (api_get_setting('use_document_title') == 'true') {
  279. //$group[]= $form->add_textfield('title', get_lang('Title'),true,'class="input_titles" id="title"');
  280. // replace the add_textfield with this
  281. $group[]=$form->createElement('text','title',get_lang('Title'),'class="input_titles" id="document_title"');
  282. //$form->applyFilter('title','trim');
  283. //$form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  284. // Added by Ivan Tcholakov, 10-OCT-2009.
  285. $form->addElement('hidden', 'filename', '', array('id' => 'filename'));
  286. //
  287. } else {
  288. //$form->add_textfield('filename', get_lang('FileName'),true,'class="input_titles" id="filename" onblur="javascript: check_if_still_empty();"');
  289. // replace the add_textfield with this
  290. $group[]=$form->createElement('text', 'filename', get_lang('FileName'), 'class="input_titles" id="document_title" onblur="javascript: check_if_still_empty();"');
  291. //$form->applyFilter('filename','trim');
  292. //$form->addRule('filename', get_lang('ThisFieldIsRequired'), 'required');
  293. //$form->addRule('filename', get_lang('FileExists'), 'callback', 'document_exists');
  294. // Added by Ivan Tcholakov, 10-OCT-2009.
  295. $form->addElement('hidden', 'title', '', array('id' => 'title'));
  296. //
  297. }
  298. // Show read-only box only in groups
  299. if (!empty($_SESSION['_gid'])) {
  300. //$renderer->setElementTemplate('<div class="row"><div class="label"></div><div class="formw">{element}{label}</div></div>', 'readonly');
  301. $group[]= $form->createElement('checkbox', 'readonly', '', get_lang('ReadOnly'));
  302. }
  303. // Add group to the form
  304. if ($is_certificate_mode)
  305. $form->addGroup($group, 'filename_group', get_lang('CertificateName') ,'&nbsp;&nbsp;&nbsp;', false);
  306. else
  307. $form->addGroup($group, 'filename_group', api_get_setting('use_document_title') == 'true' ? get_lang('Title') : get_lang('FileName') ,'&nbsp;&nbsp;&nbsp;', false);
  308. $form->addRule('filename_group', get_lang('ThisFieldIsRequired'), 'required');
  309. if (api_get_setting('use_document_title') == 'true') {
  310. $form->addGroupRule('filename_group', array(
  311. 'title' => array(
  312. array(get_lang('ThisFieldIsRequired'), 'required'),
  313. array(get_lang('FileExists'),'callback', 'document_exists')
  314. )
  315. ));
  316. } else {
  317. $form->addGroupRule('filename_group', array(
  318. 'filename' => array(
  319. array(get_lang('ThisFieldIsRequired'), 'required'),
  320. array(get_lang('FileExists'),'callback', 'document_exists')
  321. )
  322. ));
  323. }
  324. $current_session_id = api_get_session_id();
  325. //$form->addElement('style_submit_button', 'submit', get_lang('SaveDocument'), 'class="save"');
  326. // HTML-editor
  327. $renderer->setElementTemplate('<div class="row"><div class="label" id="frmModel" style="overflow: visible;"></div><div class="formw">{element}</div></div>', 'content');
  328. $form->add_html_editor('content','', false, false, $html_editor_config);
  329. // Comment-field
  330. //$form->addElement('textarea', 'comment', get_lang('Comment'), array ('rows' => 5, 'cols' => 50));
  331. if ($is_certificate_mode)
  332. $form->addElement('style_submit_button', 'submit', get_lang('CreateCertificate'), 'class="save"');
  333. else
  334. $form->addElement('style_submit_button', 'submit', get_lang('langCreateDoc'), 'class="save"');
  335. $form->setDefaults($default);
  336. // HTML
  337. /*
  338. $form->addElement('html','<div id="frmModel" style="display:block; height:525px; width:240px; position:absolute; top:115px; left:1px;"></div>');
  339. */
  340. // If form validates -> save the new document
  341. if ($form->validate()) {
  342. $values = $form->exportValues();
  343. $readonly = isset($values['readonly']) ? 1 : 0;
  344. $values['title'] = addslashes(trim($values['title']));
  345. $values['title'] = Security::remove_XSS($values['title']);
  346. $values['title'] = replace_dangerous_char($values['title']);
  347. $values['title'] = disable_dangerous_file($values['title']);
  348. $values['filename'] = addslashes(trim($values['filename']));
  349. $values['filename'] = Security::remove_XSS($values['filename']);
  350. $values['filename'] = replace_dangerous_char($values['filename']);
  351. $values['filename'] = disable_dangerous_file($values['filename']);
  352. if (api_get_setting('use_document_title') != 'true') {
  353. $values['title'] = $values['filename'];
  354. } else {
  355. $values['filename'] = $values['title'];
  356. }
  357. $filename = $values['filename'];
  358. $title = $values['title'];
  359. $extension = 'html';
  360. $content = Security::remove_XSS($values['content'], COURSEMANAGERLOWSECURITY);
  361. if (strpos($content, '/css/frames.css') === false) {
  362. $content = str_replace('</head>', '<style> body{margin:10px;}</style> <link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>', $content);
  363. }
  364. if ($fp = @fopen($filepath.$filename.'.'.$extension, 'w')) {
  365. $content = text_filter($content);
  366. $content = str_replace(api_get_path('WEB_COURSE_PATH'), $_configuration['url_append'].'/courses/', $content);
  367. // change the path of mp3 to absolute
  368. // first regexp deals with ../../../ urls
  369. // Disabled by Ivan Tcholakov.
  370. //$content = preg_replace("|(flashvars=\"file=)(\.+/)+|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',$content);
  371. //second regexp deals with audio/ urls
  372. // Disabled by Ivan Tcholakov.
  373. //$content = preg_replace("|(flashvars=\"file=)([^/]+)/|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',$content);
  374. fputs($fp, $content);
  375. fclose($fp);
  376. chmod($filepath.$filename.'.'.$extension, api_get_permissions_for_new_files());
  377. if (!is_dir($filepath.'css')) {
  378. mkdir($filepath.'css', api_get_permissions_for_new_directories());
  379. $doc_id = add_document($_course, $dir.'css', 'folder', 0, 'css');
  380. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id'], null, null, null, null, $current_session_id);
  381. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id'], null, null, null, null, $current_session_id);
  382. }
  383. if (!is_file($filepath.'css/frames.css')) {
  384. // Make a copy of the current css for the new document
  385. copy(api_get_path(SYS_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css', $filepath.'css/frames.css');
  386. $doc_id = add_document($_course, $dir.'css/frames.css', 'file', filesize($filepath.'css/frames.css'), 'frames.css');
  387. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], null, null, null, null, $current_session_id);
  388. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id'], null, null, null, null, $current_session_id);
  389. }
  390. $file_size = filesize($filepath.$filename.'.'.$extension);
  391. $save_file_path = $dir.$filename.'.'.$extension;
  392. $document_id = add_document($_course, $save_file_path, 'file', $file_size, $filename, null, $readonly);
  393. if ($document_id) {
  394. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_user['user_id'], $to_group_id, null, null, null, $current_session_id);
  395. // Update parent folders
  396. item_property_update_on_folder($_course, $_GET['dir'], $_user['user_id']);
  397. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  398. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  399. if ($new_comment || $new_title) {
  400. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  401. $ct = '';
  402. if ($new_comment)
  403. $ct .= ", comment='$new_comment'";
  404. if ($new_title)
  405. $ct .= ", title='$new_title'";
  406. Database::query("UPDATE $TABLE_DOCUMENT SET".substr($ct, 1)." WHERE id = '$document_id'");
  407. }
  408. $dir= substr($dir,0,-1);
  409. $selectcat = '';
  410. if (isset($_REQUEST['selectcat']))
  411. $selectcat = "&selectcat=".Security::remove_XSS($_REQUEST['selectcat']);
  412. header('Location: document.php?curdirpath='.urlencode($dir).$selectcat);
  413. exit ();
  414. } else {
  415. Display :: display_header($nameTools, 'Doc');
  416. Display :: display_error_message(get_lang('Impossible'));
  417. Display :: display_footer();
  418. }
  419. } else {
  420. Display :: display_header($nameTools, 'Doc');
  421. //api_display_tool_title($nameTools);
  422. Display :: display_error_message(get_lang('Impossible'));
  423. Display :: display_footer();
  424. }
  425. } else {
  426. Display :: display_header($nameTools, "Doc");
  427. //api_display_tool_title($nameTools);
  428. // actions
  429. if (isset($_REQUEST['certificate'])) {
  430. $all_information_by_create_certificate=DocumentManager::get_all_info_to_certificate();
  431. $str_info='';
  432. foreach ($all_information_by_create_certificate[0] as $info_value) {
  433. $str_info.=$info_value.'<br/>';
  434. }
  435. $create_certificate=get_lang('CreateCertificateWithTags');
  436. Display::display_normal_message($create_certificate.': <br /><br/>'.$str_info,false);
  437. }
  438. echo '<div class="actions">';
  439. // link back to the documents overview
  440. if ($is_certificate_mode)
  441. echo '<a href="document.php?curdirpath='.Security::remove_XSS($_GET['dir']).'&selectcat=' . Security::remove_XSS($_GET['selectcat']).'">'.Display::return_icon('back.png',get_lang('Back').' '.get_lang('To').' '.get_lang('CertificateOverview')).get_lang('Back').' '.get_lang('To').' '.get_lang('CertificateOverview').'</a>';
  442. else
  443. echo '<a href="document.php?curdirpath='.Security::remove_XSS($_GET['dir']).'">'.Display::return_icon('back.png',get_lang('Back').' '.get_lang('To').' '.get_lang('DocumentsOverview')).get_lang('BackTo').' '.get_lang('DocumentsOverview').'</a>';
  444. echo '</div>';
  445. $form->display();
  446. Display :: display_footer();
  447. }