create_document.php 8.3 KB

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