create_document.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. // $Id: create_document.php 13337 2007-09-27 17:09:51Z elixir_inter $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004 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: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. ==============================================================================
  23. * This file allows creating new html documents with an online WYSIWYG html
  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. var temp=false;
  41. var temp2=false;
  42. var use_document_title='.api_get_setting('use_document_title').';
  43. function launch_templates(){
  44. window.frames[0].FCKToolbarItems.GetItem("Templates").Click();
  45. }
  46. function FCKeditor_OnComplete( editorInstance )
  47. {
  48. editorInstance.Events.AttachEvent( \'OnSelectionChange\', check_for_title ) ;
  49. }
  50. function check_for_title()
  51. {
  52. if(temp==true){
  53. // This functions shows that you can interact directly with the editor area
  54. // DOM. In this way you have the freedom to do anything you want with it.
  55. // Get the editor instance that we want to interact with.
  56. var oEditor = FCKeditorAPI.GetInstance(\'content\') ;
  57. // Get the Editor Area DOM (Document object).
  58. var oDOM = oEditor.EditorDocument ;
  59. var iLength ;
  60. var contentText ;
  61. var contentTextArray;
  62. var bestandsnaamNieuw = "";
  63. var bestandsnaamOud = "";
  64. // The are two diffent ways to get the text (without HTML markups).
  65. // It is browser specific.
  66. if( document.all ) // If Internet Explorer.
  67. {
  68. contentText = oDOM.body.innerText ;
  69. }
  70. else // If Gecko.
  71. {
  72. var r = oDOM.createRange() ;
  73. r.selectNodeContents( oDOM.body ) ;
  74. contentText = r.toString() ;
  75. }
  76. var index=contentText.indexOf("/*<![CDATA");
  77. contentText=contentText.substr(0,index);
  78. // Compose title if there is none
  79. contentTextArray = contentText.split(\' \') ;
  80. var x=0;
  81. for(x=0; (x<5 && x<contentTextArray.length); x++)
  82. {
  83. if(x < 4)
  84. {
  85. bestandsnaamNieuw += contentTextArray[x] + \' \';
  86. }
  87. else
  88. {
  89. bestandsnaamNieuw += contentTextArray[x];
  90. }
  91. }
  92. if(document.getElementById(\'title_edited\').value == "false")
  93. {
  94. document.getElementById(\'filename\').value = bestandsnaamNieuw;
  95. if(use_document_title){
  96. document.getElementById(\'title\').value = bestandsnaamNieuw;
  97. }
  98. }
  99. }
  100. temp=true;
  101. }
  102. function trim(s) {
  103. while(s.substring(0,1) == \' \') {
  104. s = s.substring(1,s.length);
  105. }
  106. while(s.substring(s.length-1,s.length) == \' \') {
  107. s = s.substring(0,s.length-1);
  108. }
  109. return s;
  110. }
  111. function check_if_still_empty()
  112. {
  113. if(trim(document.getElementById(\'filename\').value) != "")
  114. {
  115. document.getElementById(\'title_edited\').value = "true";
  116. }
  117. }
  118. </script>';
  119. include (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  120. include (api_get_path(LIBRARY_PATH).'document.lib.php');
  121. include (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  122. include (api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  123. include (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  124. $nameTools = get_lang('CreateDocument');
  125. $fck_attribute['Width'] = '100%';
  126. $fck_attribute['Height'] = '950';
  127. $fck_attribute['ToolbarSet'] = 'Full';
  128. $fck_attribute['Config']['FullPage'] = true;
  129. /*
  130. -----------------------------------------------------------
  131. Constants and variables
  132. -----------------------------------------------------------
  133. */
  134. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // please do not modify this dirname formatting
  135. /*
  136. ==============================================================================
  137. MAIN CODE
  138. ==============================================================================
  139. */
  140. if (strstr($dir, '..'))
  141. {
  142. $dir = '/';
  143. }
  144. if ($dir[0] == '.')
  145. {
  146. $dir = substr($dir, 1);
  147. }
  148. if ($dir[0] != '/')
  149. {
  150. $dir = '/'.$dir;
  151. }
  152. if ($dir[strlen($dir) - 1] != '/')
  153. {
  154. $dir .= '/';
  155. }
  156. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  157. if (!is_dir($filepath))
  158. {
  159. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  160. $dir = '/';
  161. }
  162. /**************************************************/
  163. $to_group_id = 0;
  164. if (isset ($_SESSION['_gid']) && $_SESSION['_gid'] != '')
  165. {
  166. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  167. $interbreadcrumb[] = array ("url" => "../group/group_space.php?gidReq=".$_SESSION['_gid'], "name" => get_lang('GroupSpace'));
  168. $noPHP_SELF = true;
  169. $to_group_id = $_SESSION['_gid'];
  170. $group = GroupManager :: get_group_properties($to_group_id);
  171. $path = explode('/', $dir);
  172. if ('/'.$path[1] != $group['directory'])
  173. {
  174. api_not_allowed(true);
  175. }
  176. }
  177. $interbreadcrumb[] = array ("url" => "./document.php?curdirpath=".urlencode($_GET['dir']).$req_gid, "name" => get_lang('Documents'));
  178. if (!$is_allowed_in_course)
  179. api_not_allowed(true);
  180. $is_allowedToEdit = api_is_allowed_to_edit();
  181. if (!($is_allowedToEdit || $_SESSION['group_member_with_upload_rights']))
  182. {
  183. api_not_allowed(true);
  184. }
  185. /*
  186. -----------------------------------------------------------
  187. Header
  188. -----------------------------------------------------------
  189. */
  190. event_access_tool(TOOL_DOCUMENT);
  191. $display_dir = $dir;
  192. if (isset ($group))
  193. {
  194. $display_dir = explode('/', $dir);
  195. unset ($display_dir[0]);
  196. unset ($display_dir[1]);
  197. $display_dir = implode('/', $display_dir);
  198. }
  199. // Create a new form
  200. $form = new FormValidator('create_document');
  201. // Hidden element with current directory
  202. $form->addElement('hidden', 'dir');
  203. $default['dir'] = $dir;
  204. // Filename
  205. $form->addElement('hidden','title_edited','false','id="title_edited"');
  206. /**
  207. * Check if a document width the choosen filename allready exists
  208. */
  209. function document_exists($filename)
  210. {
  211. global $filepath;
  212. $filename = replace_dangerous_char($filename);
  213. return !file_exists($filepath.$filename.'.html');
  214. }
  215. // Change the default renderer for the filename-field to display the dir and extension
  216. $renderer = & $form->defaultRenderer();
  217. //$filename_template = str_replace('{element}', "<tt>$display_dir</tt> {element} <tt>.html</tt>", $renderer->_elementTemplate);
  218. $filename_template = str_replace('{element}', "{element}", $renderer->_elementTemplate);
  219. $renderer->setElementTemplate($filename_template, 'filename');
  220. // If allowed, add element for document title
  221. if (api_get_setting('use_document_title') == 'true')
  222. {
  223. $form->add_textfield('title', get_lang('Title'),true,'class="input_titles" id="title"');
  224. }
  225. else
  226. {
  227. $form->add_textfield('filename', get_lang('FileName'),true,'class="input_titles" id="filename" onblur="check_if_still_empty()"');
  228. $form->addRule('filename', get_lang('FileExists'), 'callback', 'document_exists');
  229. }
  230. $form->addElement('submit', 'submit', get_lang('Ok'));
  231. // HTML-editor
  232. $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);
  233. // Comment-field
  234. //$form->addElement('textarea', 'comment', get_lang('Comment'), array ('rows' => 5, 'cols' => 50));
  235. $form->addElement('submit', 'submit', get_lang('Ok'));
  236. $form->setDefaults($default);
  237. // If form validates -> save the new document
  238. if ($form->validate())
  239. {
  240. $values = $form->exportValues();
  241. if (api_get_setting('use_document_title') != 'true')
  242. {
  243. $values['title'] = $values['filename'];
  244. }
  245. else
  246. {
  247. $values['filename'] = $values['title'];
  248. }
  249. $filename = replace_dangerous_char($values['filename']);
  250. $texte = $values['content'];
  251. $title = $values['filename'];
  252. $extension = 'html';
  253. if (!strstr($texte, '/css/frames.css'))
  254. {
  255. $texte = str_replace('</head>', '<link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>', $texte);
  256. }
  257. if ($fp = @ fopen($filepath.$filename.'.'.$extension, 'w'))
  258. {
  259. $texte = text_filter($texte);
  260. $content = str_replace(api_get_path('WEB_COURSE_PATH'), $_configuration['url_append'].'/courses/', $texte);
  261. // replace fake by flv player if needed (fake is present only in templates)
  262. $content = str_replace('<img src="'.api_get_path(REL_PATH).'main/inc/lib/fckeditor/editor/css/images/flv.gif?flv=',
  263. '<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">
  264. <param name="movie" value="'.api_get_path(REL_PATH).'main/inc/lib/flv_player/player_flv_mini.swf" />
  265. <param name="FlashVars" value="flv=',$content);
  266. $content = str_replace('&amp;endflv" alt="" />','&autoplay=1" /></object><style type="text/css">body{}</style>',$content);
  267. $texte = str_replace('mp3player.swf?son='.urlencode($path_to_remove), 'mp3player.swf?son=.%2F', $texte);
  268. fputs($fp, $content);
  269. fclose($fp);
  270. $perm = api_get_setting('permissions_for_new_directories');
  271. $perm = octdec(!empty($perm)?$perm:'0770');
  272. if (!is_dir($filepath.'css'))
  273. {
  274. mkdir($filepath.'css');
  275. chmod($filepath.'css', $perm);
  276. $doc_id = add_document($_course, $dir.'css', 'folder', 0, 'css');
  277. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id']);
  278. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  279. }
  280. if (!is_file($filepath.'css/frames.css'))
  281. {
  282. //make a copy of the current css for the new document
  283. copy(api_get_path(SYS_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css', $filepath.'css/frames.css');
  284. $doc_id = add_document($_course, $dir.'css/frames.css', 'file', filesize($filepath.'css/frames.css'), 'frames.css');
  285. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id']);
  286. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  287. }
  288. $file_size = filesize($filepath.$filename.'.'.$extension);
  289. $save_file_path = $dir.$filename.'.'.$extension;
  290. $document_id = add_document($_course, $save_file_path, 'file', $file_size, $filename);
  291. if ($document_id)
  292. {
  293. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_user['user_id'], $to_group_id);
  294. //update parent folders
  295. item_property_update_on_folder($_course, $_GET['dir'], $_user['user_id']);
  296. $new_comment = isset ($_POST['comment']) ? trim($_POST['comment']) : '';
  297. $new_title = isset ($_POST['title']) ? trim($_POST['title']) : '';
  298. if ($new_comment || $new_title)
  299. {
  300. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  301. $ct = '';
  302. if ($new_comment)
  303. $ct .= ", comment='$new_comment'";
  304. if ($new_title)
  305. $ct .= ", title='$new_title'";
  306. api_sql_query("UPDATE $TABLE_DOCUMENT SET".substr($ct, 1)." WHERE id = '$document_id'", __FILE__, __LINE__);
  307. }
  308. $dir= substr($dir,0,-1);
  309. header('Location: document.php?curdirpath='.urlencode($dir));
  310. exit ();
  311. }
  312. }
  313. else
  314. {
  315. Display :: display_header($nameTools, "Doc");
  316. //api_display_tool_title($nameTools);
  317. Display :: display_error_message(get_lang('Impossible'));
  318. Display :: display_footer();
  319. }
  320. }
  321. else
  322. {
  323. Display :: display_header($nameTools, "Doc");
  324. //api_display_tool_title($nameTools);
  325. $form->display();
  326. Display :: display_footer();
  327. }
  328. ?>