create_document.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. // $Id: create_document.php 14886 2008-04-14 17:42:08Z juliomontoya $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004-2008 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) Olivier Brouckaert
  10. Copyright (c) Bart Mollet, Hogeschool Gent
  11. For a full list of contributors, see "credits.txt".
  12. The full license can be read in "license.txt".
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or (at your option) any later version.
  17. See the GNU General Public License for more details.
  18. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  19. Mail: info@dokeos.com
  20. ==============================================================================
  21. */
  22. /**
  23. ==============================================================================
  24. * This file allows creating new html documents with an online WYSIWYG html
  25. * editor.
  26. *
  27. * @package dokeos.document
  28. ==============================================================================
  29. */
  30. /*
  31. ==============================================================================
  32. INIT SECTION
  33. ==============================================================================
  34. */
  35. // name of the language file that needs to be included
  36. $language_file = 'document';
  37. include ('../inc/global.inc.php');
  38. $_SESSION['whereami'] = 'document/create';
  39. $this_section = SECTION_COURSES;
  40. $htmlHeadXtra[]='<script>
  41. var temp=false;
  42. var temp2=false;
  43. var use_document_title='.api_get_setting('use_document_title').';
  44. function launch_templates(){
  45. window.frames[0].FCKToolbarItems.GetItem("Templates").Click();
  46. }
  47. function FCKeditor_OnComplete( editorInstance )
  48. {
  49. editorInstance.Events.AttachEvent( \'OnSelectionChange\', check_for_title ) ;
  50. }
  51. function check_for_title()
  52. {
  53. if(temp==true){
  54. // This functions shows that you can interact directly with the editor area
  55. // DOM. In this way you have the freedom to do anything you want with it.
  56. // Get the editor instance that we want to interact with.
  57. var oEditor = FCKeditorAPI.GetInstance(\'content\') ;
  58. // Get the Editor Area DOM (Document object).
  59. var oDOM = oEditor.EditorDocument ;
  60. var iLength ;
  61. var contentText ;
  62. var contentTextArray;
  63. var bestandsnaamNieuw = "";
  64. var bestandsnaamOud = "";
  65. // The are two diffent ways to get the text (without HTML markups).
  66. // It is browser specific.
  67. if( document.all ) // If Internet Explorer.
  68. {
  69. contentText = oDOM.body.innerText ;
  70. }
  71. else // If Gecko.
  72. {
  73. var r = oDOM.createRange() ;
  74. r.selectNodeContents( oDOM.body ) ;
  75. contentText = r.toString() ;
  76. }
  77. var index=contentText.indexOf("/*<![CDATA");
  78. contentText=contentText.substr(0,index);
  79. // Compose title if there is none
  80. contentTextArray = contentText.split(\' \') ;
  81. var x=0;
  82. for(x=0; (x<5 && x<contentTextArray.length); x++)
  83. {
  84. if(x < 4)
  85. {
  86. bestandsnaamNieuw += contentTextArray[x] + \' \';
  87. }
  88. else
  89. {
  90. bestandsnaamNieuw += contentTextArray[x];
  91. }
  92. }
  93. if(document.getElementById(\'title_edited\').value == "false")
  94. {
  95. document.getElementById(\'filename\').value = bestandsnaamNieuw;
  96. if(use_document_title){
  97. document.getElementById(\'title\').value = bestandsnaamNieuw;
  98. }
  99. }
  100. }
  101. temp=true;
  102. }
  103. function trim(s) {
  104. while(s.substring(0,1) == \' \') {
  105. s = s.substring(1,s.length);
  106. }
  107. while(s.substring(s.length-1,s.length) == \' \') {
  108. s = s.substring(0,s.length-1);
  109. }
  110. return s;
  111. }
  112. function check_if_still_empty()
  113. {
  114. if(trim(document.getElementById(\'filename\').value) != "")
  115. {
  116. document.getElementById(\'title_edited\').value = "true";
  117. }
  118. }
  119. </script>';
  120. include (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  121. include (api_get_path(LIBRARY_PATH).'document.lib.php');
  122. include (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  123. include (api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  124. include (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  125. $nameTools = get_lang('CreateDocument');
  126. $fck_attribute['Width'] = '100%';
  127. $fck_attribute['Height'] = '950';
  128. $fck_attribute['ToolbarSet'] = 'Full';
  129. $fck_attribute['Config']['FullPage'] = true;
  130. /*
  131. -----------------------------------------------------------
  132. Constants and variables
  133. -----------------------------------------------------------
  134. */
  135. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // please do not modify this dirname formatting
  136. /*
  137. ==============================================================================
  138. MAIN CODE
  139. ==============================================================================
  140. */
  141. if (strstr($dir, '..'))
  142. {
  143. $dir = '/';
  144. }
  145. if ($dir[0] == '.')
  146. {
  147. $dir = substr($dir, 1);
  148. }
  149. if ($dir[0] != '/')
  150. {
  151. $dir = '/'.$dir;
  152. }
  153. if ($dir[strlen($dir) - 1] != '/')
  154. {
  155. $dir .= '/';
  156. }
  157. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  158. if (!is_dir($filepath))
  159. {
  160. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  161. $dir = '/';
  162. }
  163. /**************************************************/
  164. $to_group_id = 0;
  165. if (isset ($_SESSION['_gid']) && $_SESSION['_gid'] != '')
  166. {
  167. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  168. $interbreadcrumb[] = array ("url" => "../group/group_space.php?gidReq=".$_SESSION['_gid'], "name" => get_lang('GroupSpace'));
  169. $noPHP_SELF = true;
  170. $to_group_id = $_SESSION['_gid'];
  171. $group = GroupManager :: get_group_properties($to_group_id);
  172. $path = explode('/', $dir);
  173. if ('/'.$path[1] != $group['directory'])
  174. {
  175. api_not_allowed(true);
  176. }
  177. }
  178. $interbreadcrumb[] = array ("url" => "./document.php?curdirpath=".urlencode($_GET['dir']).$req_gid, "name" => get_lang('Documents'));
  179. if (!$is_allowed_in_course)
  180. api_not_allowed(true);
  181. $is_allowedToEdit = api_is_allowed_to_edit();
  182. if (!($is_allowedToEdit || $_SESSION['group_member_with_upload_rights']))
  183. {
  184. api_not_allowed(true);
  185. }
  186. /*
  187. -----------------------------------------------------------
  188. Header
  189. -----------------------------------------------------------
  190. */
  191. event_access_tool(TOOL_DOCUMENT);
  192. $display_dir = $dir;
  193. if (isset ($group))
  194. {
  195. $display_dir = explode('/', $dir);
  196. unset ($display_dir[0]);
  197. unset ($display_dir[1]);
  198. $display_dir = implode('/', $display_dir);
  199. }
  200. // Create a new form
  201. $form = new FormValidator('create_document');
  202. // Hidden element with current directory
  203. $form->addElement('hidden', 'dir');
  204. $default['dir'] = $dir;
  205. // Filename
  206. $form->addElement('hidden','title_edited','false','id="title_edited"');
  207. /**
  208. * Check if a document width the choosen filename allready exists
  209. */
  210. function document_exists($filename)
  211. {
  212. global $filepath;
  213. $filename = replace_dangerous_char($filename);
  214. return !file_exists($filepath.$filename.'.html');
  215. }
  216. // Change the default renderer for the filename-field to display the dir and extension
  217. $renderer = & $form->defaultRenderer();
  218. //$filename_template = str_replace('{element}', "<tt>$display_dir</tt> {element} <tt>.html</tt>", $renderer->_elementTemplate);
  219. $filename_template = str_replace('{element}', "{element}", $renderer->_elementTemplate);
  220. $renderer->setElementTemplate($filename_template, 'filename');
  221. // initialize group array
  222. $group = array();
  223. // If allowed, add element for document title
  224. if (api_get_setting('use_document_title') == 'true')
  225. {
  226. //$group[]= $form->add_textfield('title', get_lang('Title'),true,'class="input_titles" id="title"');
  227. // replace the add_textfield with this
  228. $group[]=$form->createElement('text','title',get_lang('Title'),'class="input_titles" id="title"');
  229. //$form->applyFilter('title','trim');
  230. //$form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  231. }
  232. else
  233. {
  234. //$form->add_textfield('filename', get_lang('FileName'),true,'class="input_titles" id="filename" onblur="check_if_still_empty()"');
  235. // replace the add_textfield with this
  236. $group[]=$form->createElement('text','filename',get_lang('FileName'),'class="input_titles" id="filename" onblur="check_if_still_empty()"');
  237. //$form->applyFilter('filename','trim');
  238. //$form->addRule('filename', get_lang('ThisFieldIsRequired'), 'required');
  239. //$form->addRule('filename', get_lang('FileExists'), 'callback', 'document_exists');
  240. }
  241. //$renderer->setElementTemplate('<div class="row"><div class="label"></div><div class="formw">{element}{label}</div></div>', 'readonly');
  242. $group[]= $form->createElement('checkbox','readonly','',get_lang('ReadOnly'));
  243. // add group to the form
  244. $form->addGroup($group, 'filename_group', get_lang('FileName') ,'&nbsp;&nbsp;&nbsp;', false);
  245. $form->addRule('filename_group', get_lang('ThisFieldIsRequired'), 'required');
  246. if (api_get_setting('use_document_title') == 'true')
  247. {
  248. $form->addGroupRule('filename_group', array(
  249. 'title' => array(
  250. array(get_lang('ThisFieldIsRequired'), 'required'),
  251. array(get_lang('FileExists'),'callback', 'document_exists')
  252. )
  253. ));
  254. }
  255. else
  256. {
  257. $form->addGroupRule('filename_group', array(
  258. 'filename' => array(
  259. array(get_lang('ThisFieldIsRequired'), 'required'),
  260. array(get_lang('FileExists'),'callback', 'document_exists')
  261. )
  262. ));
  263. }
  264. $form->addElement('submit', 'submit', get_lang('Ok'));
  265. // HTML-editor
  266. $form->add_html_editor('content', '<a style="cursor:pointer" onclick="launch_templates()"><img src="'.api_get_path(WEB_IMG_PATH).'templates.gif" /></a>', false, true);
  267. // Comment-field
  268. //$form->addElement('textarea', 'comment', get_lang('Comment'), array ('rows' => 5, 'cols' => 50));
  269. $form->addElement('submit', 'submit', get_lang('Ok'));
  270. $form->setDefaults($default);
  271. // If form validates -> save the new document
  272. if ($form->validate())
  273. {
  274. $values = $form->exportValues();
  275. $readonly = isset($values['readonly']) ? 1 : 0;
  276. $values['title']=trim($values['title']);
  277. $values['filename']=trim($values['filename']);
  278. if (api_get_setting('use_document_title') != 'true')
  279. {
  280. $values['title'] = $values['filename'];
  281. }
  282. else
  283. {
  284. $values['filename'] = $values['title'];
  285. }
  286. $filename = replace_accents($values['filename']);
  287. $texte = $values['content'];
  288. $title = $values['filename'];
  289. $extension = 'html';
  290. if (!strstr($texte, '/css/frames.css'))
  291. {
  292. $texte = str_replace('</head>', '<link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>', $texte);
  293. }
  294. if ($fp = @ fopen($filepath.$filename.'.'.$extension, 'w'))
  295. {
  296. $texte = text_filter($texte);
  297. $content = str_replace(api_get_path('WEB_COURSE_PATH'), $_configuration['url_append'].'/courses/', $texte);
  298. // replace fake by flv player if needed (fake is present only in templates)
  299. $content = str_replace('<img src="'.api_get_path(REL_PATH).'main/inc/lib/fckeditor/editor/css/images/flv.gif?flv=',
  300. '<object type="application/x-shockwave-flash" data="'.api_get_path(REL_PATH).'main/inc/lib/flv_player/player_flv_mini.swf" height="240" width="320">
  301. <param name="movie" value="'.api_get_path(REL_PATH).'main/inc/lib/flv_player/player_flv_mini.swf" />
  302. <param name="FlashVars" value="flv=',$content);
  303. $content = str_replace('&amp;endflv" alt="" />','&autoplay=1" /></object><style type="text/css">body{}</style>',$content);
  304. $texte = str_replace('mp3player.swf?son='.urlencode($path_to_remove), 'mp3player.swf?son=.%2F', $texte);
  305. fputs($fp, $content);
  306. fclose($fp);
  307. $files_perm = api_get_setting('permissions_for_new_files');
  308. $files_perm = octdec(!empty($files_perm)?$files_perm:'0770');
  309. chmod($filepath.$filename.'.'.$extension,$files_perm);
  310. $perm = api_get_setting('permissions_for_new_directories');
  311. $perm = octdec(!empty($perm)?$perm:'0770');
  312. if (!is_dir($filepath.'css'))
  313. {
  314. mkdir($filepath.'css');
  315. chmod($filepath.'css', $perm);
  316. $doc_id = add_document($_course, $dir.'css', 'folder', 0, 'css');
  317. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id']);
  318. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  319. }
  320. if (!is_file($filepath.'css/frames.css'))
  321. {
  322. //make a copy of the current css for the new document
  323. copy(api_get_path(SYS_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css', $filepath.'css/frames.css');
  324. $doc_id = add_document($_course, $dir.'css/frames.css', 'file', filesize($filepath.'css/frames.css'), 'frames.css');
  325. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id']);
  326. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  327. }
  328. $file_size = filesize($filepath.$filename.'.'.$extension);
  329. $save_file_path = $dir.$filename.'.'.$extension;
  330. $document_id = add_document($_course, $save_file_path, 'file', $file_size, $filename,null,$readonly);
  331. if ($document_id)
  332. {
  333. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_user['user_id'], $to_group_id);
  334. //update parent folders
  335. item_property_update_on_folder($_course, $_GET['dir'], $_user['user_id']);
  336. $new_comment = isset ($_POST['comment']) ? trim($_POST['comment']) : '';
  337. $new_title = isset ($_POST['title']) ? trim($_POST['title']) : '';
  338. if ($new_comment || $new_title)
  339. {
  340. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  341. $ct = '';
  342. if ($new_comment)
  343. $ct .= ", comment='$new_comment'";
  344. if ($new_title)
  345. $ct .= ", title='$new_title'";
  346. api_sql_query("UPDATE $TABLE_DOCUMENT SET".substr($ct, 1)." WHERE id = '$document_id'", __FILE__, __LINE__);
  347. }
  348. $dir= substr($dir,0,-1);
  349. header('Location: document.php?curdirpath='.urlencode($dir));
  350. exit ();
  351. }
  352. }
  353. else
  354. {
  355. Display :: display_header($nameTools, "Doc");
  356. //api_display_tool_title($nameTools);
  357. Display :: display_error_message(get_lang('Impossible'));
  358. Display :: display_footer();
  359. }
  360. }
  361. else
  362. {
  363. Display :: display_header($nameTools, "Doc");
  364. //api_display_tool_title($nameTools);
  365. $form->display();
  366. Display :: display_footer();
  367. }
  368. ?>