create_document.php 17 KB

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