create_document.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. // $Id: create_document.php 9246 2006-09-25 13:24:53Z bmol $
  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. $langFile = "document";
  35. include ('../inc/global.inc.php');
  36. $this_section = SECTION_COURSES;
  37. include (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  38. include (api_get_path(LIBRARY_PATH).'document.lib.php');
  39. include (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  40. include (api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  41. include (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  42. $nameTools = get_lang('CreateDocument');
  43. /*
  44. -----------------------------------------------------------
  45. Constants and variables
  46. -----------------------------------------------------------
  47. */
  48. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // please do not modify this dirname formatting
  49. /*
  50. ==============================================================================
  51. MAIN CODE
  52. ==============================================================================
  53. */
  54. if (strstr($dir, '..'))
  55. {
  56. $dir = '/';
  57. }
  58. if ($dir[0] == '.')
  59. {
  60. $dir = substr($dir, 1);
  61. }
  62. if ($dir[0] != '/')
  63. {
  64. $dir = '/'.$dir;
  65. }
  66. if ($dir[strlen($dir) - 1] != '/')
  67. {
  68. $dir .= '/';
  69. }
  70. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  71. if (!is_dir($filepath))
  72. {
  73. $filepath = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  74. $dir = '/';
  75. }
  76. /**************************************************/
  77. $to_group_id = 0;
  78. if (isset ($_SESSION['_gid']) && $_SESSION['_gid'] != '')
  79. {
  80. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  81. $interbreadcrumb[] = array ("url" => "../group/group_space.php?gidReq=".$_SESSION['_gid'], "name" => get_lang('GroupSpace'));
  82. $noPHP_SELF = true;
  83. $to_group_id = $_SESSION['_gid'];
  84. $group = GroupManager :: get_group_properties($to_group_id);
  85. $path = explode('/', $dir);
  86. if ('/'.$path[1] != $group['directory'])
  87. {
  88. api_not_allowed();
  89. }
  90. }
  91. $interbreadcrumb[] = array ("url" => "./document.php?curdirpath=".urlencode($_GET['dir']).$req_gid, "name" => get_lang('Documents'));
  92. if (!$is_allowed_in_course)
  93. api_not_allowed();
  94. $is_allowedToEdit = $is_courseAdmin;
  95. if (!($is_allowedToEdit || $_SESSION['group_member_with_upload_rights']))
  96. {
  97. api_not_allowed();
  98. }
  99. /*
  100. -----------------------------------------------------------
  101. Header
  102. -----------------------------------------------------------
  103. */
  104. event_access_tool(TOOL_DOCUMENT);
  105. $display_dir = $dir;
  106. if (isset ($group))
  107. {
  108. $display_dir = explode('/', $dir);
  109. unset ($display_dir[0]);
  110. unset ($display_dir[1]);
  111. $display_dir = implode('/', $display_dir);
  112. }
  113. // Create a new form
  114. $form = new FormValidator('create_document');
  115. // Hidden element with current directory
  116. $form->addElement('hidden', 'dir');
  117. $default['dir'] = $dir;
  118. // Filename
  119. $form->add_textfield('filename', get_lang('FileName'));
  120. $form->addRule('filename', get_lang('FileExists'), 'callback', 'document_exists');
  121. /**
  122. * Check if a document width the choosen filename allready exists
  123. */
  124. function document_exists($filename)
  125. {
  126. global $filepath;
  127. $filename = replace_dangerous_char($filename);
  128. return !file_exists($filepath.$filename.'.html');
  129. }
  130. // Change the default renderer for the filename-field to display the dir and extension
  131. $renderer = & $form->defaultRenderer();
  132. $filename_template = str_replace('{element}', "<tt>$display_dir</tt> {element} <tt>.html</tt>", $renderer->_elementTemplate);
  133. $renderer->setElementTemplate($filename_template, 'filename');
  134. // If allowed, add element for document title
  135. if (get_setting('use_document_title') == 'true')
  136. {
  137. $form->add_textfield('title', get_lang('Title'));
  138. }
  139. // HTML-editor
  140. $form->add_html_editor('content', get_lang('Content'), false, true);
  141. // Comment-field
  142. $form->addElement('textarea', 'comment', get_lang('Comment'), array ('rows' => 5, 'cols' => 50));
  143. $form->addElement('submit', 'submit', get_lang('Ok'));
  144. $form->setDefaults($default);
  145. // If form validates -> save the new document
  146. if ($form->validate())
  147. {
  148. $values = $form->exportValues();
  149. if (get_setting('use_document_title') != 'true')
  150. {
  151. $values['title'] = $values['filename'];
  152. }
  153. $filename = replace_dangerous_char($values['filename']);
  154. $texte = $values['content'];
  155. $title = $values['filename'];
  156. $extension = 'html';
  157. if (!strstr($texte, '/css/frames.css'))
  158. {
  159. $texte = str_replace('</head>', '<link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>', $texte);
  160. }
  161. if ($fp = @ fopen($filepath.$filename.'.'.$extension, 'w'))
  162. {
  163. $texte = text_filter($texte);
  164. $path_to_remove = api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  165. $texte = str_replace($path_to_remove, './', $texte);
  166. $texte = str_replace('mp3player.swf?son='.urlencode($path_to_remove), 'mp3player.swf?son=.%2F', $texte);
  167. fputs($fp, $texte);
  168. fclose($fp);
  169. if (!is_dir($filepath.'css'))
  170. {
  171. mkdir($filepath.'css', 0777);
  172. $doc_id = add_document($_course, $dir.'css', 'folder', 0, 'css');
  173. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_uid);
  174. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_uid);
  175. }
  176. if (!is_file($filepath.'css/frames.css'))
  177. {
  178. copy(api_get_path(SYS_CODE_PATH).'css/frames.css', $filepath.'css/frames.css');
  179. $doc_id = add_document($_course, $dir.'css/frames.css', 'file', filesize($filepath.'css/frames.css'), 'frames.css');
  180. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_uid);
  181. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_uid);
  182. }
  183. $file_size = filesize($filepath.$filename.'.'.$extension);
  184. $save_file_path = $dir.$filename.'.'.$extension;
  185. $document_id = add_document($_course, $save_file_path, 'file', $file_size, $filename);
  186. if ($document_id)
  187. {
  188. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_uid, $to_group_id);
  189. //update parent folders
  190. item_property_update_on_folder($_course, $_GET['dir'], $_uid);
  191. $new_comment = isset ($_POST['comment']) ? trim($_POST['comment']) : '';
  192. $new_title = isset ($_POST['title']) ? trim($_POST['title']) : '';
  193. if ($new_comment || $new_title)
  194. {
  195. $TABLE_DOCUMENT = Database::get_course_table(DOCUMENT_TABLE);
  196. $ct = '';
  197. if ($new_comment)
  198. $ct .= ", comment='$new_comment'";
  199. if ($new_title)
  200. $ct .= ", title='$new_title'";
  201. api_sql_query("UPDATE $TABLE_DOCUMENT SET".substr($ct, 1)." WHERE id = '$document_id'", __FILE__, __LINE__);
  202. }
  203. $dir= substr($dir,0,-1);
  204. header('Location: document.php?curdirpath='.urlencode($dir));
  205. exit ();
  206. }
  207. }
  208. else
  209. {
  210. Display :: display_header($nameTools, "Doc");
  211. api_display_tool_title($nameTools);
  212. Display :: display_error_message(get_lang('Impossible'));
  213. Display :: display_footer();
  214. }
  215. }
  216. else
  217. {
  218. Display :: display_header($nameTools, "Doc");
  219. api_display_tool_title($nameTools);
  220. $form->display();
  221. Display :: display_footer();
  222. }
  223. ?>