edit_document.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php // $Id: edit_document.php 9246 2006-09-25 13:24:53Z bmol $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. Copyright (c) Roan Embrechts
  10. Copyright (c) Rene Haentjens (RH) (update 2004/09/30)
  11. Copyright (c) Bart Mollet, Hogeschool Gent
  12. For a full list of contributors, see "credits.txt".
  13. The full license can be read in "license.txt".
  14. This program is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License
  16. as published by the Free Software Foundation; either version 2
  17. of the License, or (at your option) any later version.
  18. See the GNU General Public License for more details.
  19. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  20. ==============================================================================
  21. */
  22. /**
  23. ==============================================================================
  24. * This file allows editing documents.
  25. *
  26. * Based on create_document, this file allows
  27. * - edit name
  28. * - edit comments
  29. * - edit metadata (requires a document table entry)
  30. * - edit html content (only for htm/html files)
  31. *
  32. * For all files
  33. * - show editable name field
  34. * - show editable comments field
  35. * Additionally, for html and text files
  36. * - show RTE
  37. *
  38. * Remember, all files and folders must always have an entry in the
  39. * database, regardless of wether they are visible/invisible, have
  40. * comments or not.
  41. *
  42. * @package dokeos.document
  43. * @todo improve script structure (FormValidator is used to display form, but
  44. * not for validation at the moment)
  45. ==============================================================================
  46. */
  47. /*
  48. ==============================================================================
  49. INIT SECTION
  50. ==============================================================================
  51. */
  52. $langFile = "document";
  53. /*
  54. ------------------------------------------------------------------------------
  55. Included libraries
  56. ------------------------------------------------------------------------------
  57. */
  58. include('../inc/global.inc.php');
  59. $this_section=SECTION_COURSES;
  60. include(api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  61. include(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  62. include(api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  63. include(api_get_path(LIBRARY_PATH).'document.lib.php');
  64. require_once(api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  65. /*
  66. ------------------------------------------------------------------------------
  67. Constants & Variables
  68. ------------------------------------------------------------------------------
  69. */
  70. $file = $_GET['file'];
  71. //echo('file: '.$file.'<br>');
  72. $doc=basename($file);
  73. //echo('doc: '.$doc.'<br>');
  74. $dir=$_GET['curdirpath'];
  75. //echo('dir: '.$dir.'<br>');
  76. $file_name = $doc;
  77. //echo('file_name: '.$file_name.'<br>');
  78. $baseServDir = api_get_path(SYS_COURSE_PATH);
  79. $baseServUrl = $urlAppend."/";
  80. $courseDir = $_course['path']."/document";
  81. $baseWorkDir = $baseServDir.$courseDir;
  82. $group_document = false;
  83. $langFile = "document";
  84. $use_document_title = (get_setting('use_document_title')=='true')?true:false;
  85. $noPHP_SELF=true;
  86. /*
  87. ------------------------------------------------------------------------------
  88. Other init code
  89. ------------------------------------------------------------------------------
  90. */
  91. /* please do not modify this dirname formatting */
  92. if(strstr($dir,'..'))
  93. {
  94. $dir='/';
  95. }
  96. if($dir[0] == '.')
  97. {
  98. $dir=substr($dir,1);
  99. }
  100. if($dir[0] != '/')
  101. {
  102. $dir='/'.$dir;
  103. }
  104. if($dir[strlen($dir)-1] != '/')
  105. {
  106. $dir.='/';
  107. }
  108. $filepath=api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  109. if(!is_dir($filepath))
  110. {
  111. $filepath=api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  112. $dir='/';
  113. }
  114. /**************************************************/
  115. $nameTools = get_lang('EditDocument');
  116. $dbTable = Database::get_course_table(DOCUMENT_TABLE);
  117. if(isset($_SESSION['_gid']) && $_SESSION['_gid']!='')
  118. {
  119. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  120. $interbreadcrumb[]= array ("url"=>"../group/group_space.php?gidReq=".$_SESSION['_gid'], "name"=> get_lang('GroupSpace'));
  121. $group_document = true;
  122. $noPHP_SELF=true;
  123. }
  124. $interbreadcrumb[]=array("url"=>"./document.php?curdirpath=".urlencode($_GET['curdirpath']).$req_gid, "name"=> get_lang('Documents'));
  125. $is_allowedToEdit = is_allowed_to_edit() || $_SESSION['group_member_with_upload_rights'];
  126. if(!$is_allowedToEdit)
  127. {
  128. api_not_allowed();
  129. }
  130. event_access_tool(TOOL_DOCUMENT);
  131. /*
  132. ==============================================================================
  133. MAIN TOOL CODE
  134. ==============================================================================
  135. */
  136. /*
  137. ------------------------------------------------------------------------------
  138. General functions
  139. ------------------------------------------------------------------------------
  140. */
  141. /*
  142. ------------------------------------------------------------------------------
  143. Workhorse functions
  144. These do the actual work that is expected from of this tool, other functions
  145. are only there to support these ones.
  146. ------------------------------------------------------------------------------
  147. */
  148. /**
  149. This function changes the name of a certain file.
  150. It needs no global variables, it takes all info from parameters.
  151. It returns nothing.
  152. */
  153. function change_name($baseWorkDir, $sourceFile, $renameTo, $dir, $doc)
  154. {
  155. $file_name_for_change = $baseWorkDir.$dir.$sourceFile;
  156. //api_display_debug_info("call my_rename: params $file_name_for_change, $renameTo");
  157. $renameTo = my_rename($file_name_for_change, stripslashes($renameTo)); //fileManage API
  158. if ($renameTo)
  159. {
  160. if (isset($dir) && $dir != "")
  161. {
  162. $sourceFile = $dir.$sourceFile;
  163. $new_full_file_name = dirname($sourceFile)."/".$renameTo;
  164. }
  165. else
  166. {
  167. $sourceFile = "/".$sourceFile;
  168. $new_full_file_name = "/".$renameTo;
  169. }
  170. update_db_info("update", $sourceFile, $new_full_file_name); //fileManage API
  171. $name_changed = get_lang("ElRen");
  172. $info_message = get_lang('fileModified');
  173. $GLOBALS['file_name'] = $renameTo;
  174. $GLOBALS['doc'] = $renameTo;
  175. return $info_message;
  176. }
  177. else
  178. {
  179. $dialogBox = get_lang('FileExists');
  180. /* return to step 1 */
  181. $rename = $sourceFile;
  182. unset($sourceFile);
  183. }
  184. }
  185. /*
  186. ------------------------------------------------------------------------------
  187. Code to change the comment
  188. ------------------------------------------------------------------------------
  189. Step 2. React on POST data
  190. (Step 1 see below)
  191. */
  192. if (isset($_POST['newComment']))
  193. {
  194. //to try to fix the path if it is wrong
  195. $commentPath = str_replace("//", "/", $_POST['commentPath']);
  196. $newComment = trim($_POST['newComment']); // remove spaces
  197. $newTitle = trim($_POST['newTitle']); // remove spaces
  198. // Check if there is already a record for this file in the DB
  199. $result = mysql_query ("SELECT * FROM $dbTable WHERE path='".$commentPath."'");
  200. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  201. {
  202. $attribute['path' ] = $row['path' ];
  203. $attribute['comment' ] = $row['title' ];
  204. }
  205. //Determine the correct query to the DB
  206. //new code always keeps document in database
  207. $query = "UPDATE $dbTable SET comment='".$newComment."', title='".$newTitle."' WHERE path='".$commentPath."'";
  208. mysql_query($query);
  209. //this is an UPDATE page... we shouldn't be creating new documents here.
  210. /*
  211. if (mysql_affected_rows() == 0)
  212. {
  213. mysql_query("INSERT INTO $dbTable SET path='".$commentPath."', title='".$newTitle."', comment='".$newComment."'");
  214. }
  215. */
  216. $oldComment = $newComment;
  217. $oldTitle = $newTitle;
  218. $comments_updated = get_lang('ComMod');
  219. $info_message = get_lang('fileModified');
  220. }
  221. /*
  222. ------------------------------------------------------------------------------
  223. Code to change the name
  224. ------------------------------------------------------------------------------
  225. Step 2. react on POST data - change the name
  226. (Step 1 see below)
  227. */
  228. if (isset($_POST['renameTo']))
  229. {
  230. $info_message = change_name($baseWorkDir, $_GET['sourceFile'], $_POST['renameTo'], $dir, $doc);
  231. //assume name change was successful
  232. }
  233. /*
  234. ------------------------------------------------------------------------------
  235. Code to change the comment
  236. ------------------------------------------------------------------------------
  237. Step 1. Create dialog box.
  238. */
  239. /* Search the old comment */ // RH: metadata: added 'id,'
  240. $result = mysql_query ("SELECT id,comment,title FROM $dbTable WHERE path='$dir$doc'");
  241. $message = "<i>Debug info</i><br>directory = $dir<br>";
  242. $message .= "document = $file_name<br>";
  243. $message .= "comments file = " . $file . "<br>";
  244. //Display::display_normal_message($message);
  245. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  246. {
  247. $oldComment = $row['comment'];
  248. $oldTitle = $row['title'];
  249. $docId = $row['id']; // RH: metadata
  250. }
  251. /*
  252. ------------------------------------------------------------------------------
  253. WYSIWYG HTML EDITOR - Program Logic
  254. ------------------------------------------------------------------------------
  255. */
  256. if($is_allowedToEdit)
  257. {
  258. if($_POST['formSent']==1)
  259. {
  260. if(isset($_POST['renameTo']))
  261. {
  262. $_POST['filename']=$_POST['renameTo'];
  263. $extension=explode('.',$_POST['filename']);
  264. $extension=$extension[sizeof($extension)-1];
  265. $_POST['filename']=str_replace('.'.$extension,'',$_POST['filename']);
  266. }
  267. $filename=stripslashes($_POST['filename']);
  268. $texte=trim(str_replace(array("\r","\n"),"",stripslashes($_POST['texte'])));
  269. if(!strstr($texte,'/css/frames.css'))
  270. {
  271. $texte=str_replace('</title></head>','</title><link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>',$texte);
  272. }
  273. // RH commented: $filename=replace_dangerous_char($filename,'strict');
  274. if($_POST['extension'] != 'htm' && $_POST['extension'] != 'html')
  275. {
  276. $extension='html';
  277. }
  278. else
  279. {
  280. $extension = $_POST['extension'];
  281. }
  282. $file=$dir.$filename.'.'.$extension;
  283. if(empty($texte))
  284. {
  285. $msgError=get_lang('NoText');
  286. }
  287. elseif(empty($filename))
  288. {
  289. $msgError=get_lang('NoFileName');
  290. }
  291. else
  292. {
  293. if($fp=@fopen($filepath.$filename.'.'.$extension,'w'))
  294. {
  295. $texte = text_filter($texte);
  296. //echo('file path: '.$filepath.$filename.'.'.$extension);
  297. $path_to_remove=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  298. $texte=str_replace($path_to_remove,'./',$texte);
  299. $texte=str_replace('mp3player.swf?son='.urlencode($path_to_remove),'mp3player.swf?son=.%2F',$texte);
  300. //echo('texte: '.$texte);
  301. //echo (fputs($fp,$texte))?'FPUTS OK':'FPUTS NIET OK';
  302. fputs($fp,$texte);
  303. fclose($fp);
  304. if(!is_dir($filepath.'css'))
  305. {
  306. mkdir($filepath.'css',0777);
  307. $doc_id=add_document($_course,$dir.'css','folder',0,'css');
  308. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_uid);
  309. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_uid);
  310. }
  311. if(!is_file($filepath.'css/frames.css'))
  312. {
  313. copy(api_get_path(SYS_CODE_PATH).'css/frames.css',$filepath.'css/frames.css');
  314. $doc_id=add_document($_course,$dir.'css/frames.css','file',filesize($filepath.'css/frames.css'),'frames.css');
  315. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_uid);
  316. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_uid);
  317. }
  318. // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  319. $document_id = DocumentManager::get_document_id($_course,$file);
  320. if($document_id)
  321. {
  322. $file_size = filesize($filepath.$filename.'.'.$extension);
  323. update_existing_document($_course, $document_id,$file_size);
  324. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentUpdated', $_uid);
  325. //update parent folders
  326. item_property_update_on_folder($_course,$dir,$_uid);
  327. header('Location: document.php?curdirpath='.urlencode($_GET['curdirpath']).$req_gid);
  328. exit();
  329. }
  330. else
  331. {
  332. //$msgError=get_lang('Impossible');
  333. }
  334. }
  335. else
  336. {
  337. $msgError=get_lang('Impossible');
  338. }
  339. }
  340. }
  341. }
  342. if(file_exists($filepath.$doc))
  343. {
  344. $extension=explode('.',$doc);
  345. $extension=$extension[sizeof($extension)-1];
  346. $filename=str_replace('.'.$extension,'',$doc);
  347. $extension=strtolower($extension);
  348. if(!in_array($extension,array('html','htm')))
  349. {
  350. $extension=$filename=$texte='';
  351. }
  352. else
  353. {
  354. $texte=file($filepath.$doc);
  355. $texte=implode('',$texte);
  356. $path_to_append=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  357. $texte=str_replace('="./','="'.$path_to_append,$texte);
  358. $texte=str_replace('mp3player.swf?son=.%2F','mp3player.swf?son='.urlencode($path_to_append),$texte);
  359. }
  360. }
  361. /*
  362. ==============================================================================
  363. MAIN EDIT_DOCUMENT CODE
  364. - react on input
  365. - display user interface
  366. ==============================================================================
  367. */
  368. Display::display_header($nameTools,"Doc");
  369. api_display_tool_title(get_lang("EditDocument") . ": $file_name");
  370. if(isset($msgError))
  371. {
  372. Display::display_normal_message($msgError); //main API
  373. }
  374. if( isset($info_message))
  375. {
  376. Display::display_normal_message($info_message); //main API
  377. }
  378. $action = $_SERVER['PHP_SELF'].'?sourceFile='.urlencode($file_name).'&amp;curdirpath='.urlencode($_GET['curdirpath']).'&amp;file='.urlencode($_GET['file']).'&amp;doc='.urlencode($doc);
  379. $form = new FormValidator('formEdit','post',$action);
  380. $form->addElement('hidden','filename');
  381. $form->addElement('hidden','extension');
  382. $form->addElement('hidden','file_path');
  383. $form->addElement('hidden','commentPath');
  384. $form->add_textfield('renameTo',get_lang('FileName'));
  385. if($use_document_title)
  386. {
  387. $form->add_textfield('newTitle',get_lang('Title'));
  388. $defaults['newTitle'] = $oldTitle;
  389. }
  390. if($extension == "htm" || $extension == "html")
  391. {
  392. $form->addElement('hidden','formSent');
  393. $defaults['formSent'] = 1;
  394. $form->add_html_editor('texte',get_lang('Content'),false,true);
  395. $defaults['texte'] = $texte;
  396. }
  397. if(!$group_document)
  398. {
  399. $metadata_link = '<a href="../metadata/index.php?eid='.urlencode('Document.'.$docId).'">'.get_lang('AddMetadata').'</a>';
  400. $form->addElement('static',null,get_lang('Metadata'),$metadata_link);
  401. }
  402. $form->addElement('textarea','newComment',get_lang('Comment'),'rows="3" style="width:300px;"');
  403. $form->addElement('submit','submit',get_lang('Ok'));
  404. $defaults['filename'] = $filename;
  405. $defaults['extension'] = $extension;
  406. $defaults['file_path'] = $_GET['file'];
  407. $defaults['commentPath'] = $file;
  408. $defaults['renameTo'] = $file_name;
  409. $defaults['newComment'] = $oldComment;
  410. $form->setDefaults($defaults);
  411. $form->display();
  412. /*
  413. ==============================================================================
  414. DOKEOS FOOTER
  415. ==============================================================================
  416. */
  417. Display::display_footer();
  418. ?>