edit_document.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php // $Id: edit_document.php 13916 2007-12-04 15:06:22Z elixir_inter $
  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. // name of the language file that needs to be included
  48. $language_file = 'document';
  49. /*
  50. ------------------------------------------------------------------------------
  51. Included libraries
  52. ------------------------------------------------------------------------------
  53. */
  54. include('../inc/global.inc.php');
  55. $htmlHeadXtra[] = '
  56. <script type="text/javascript">
  57. function launch_templates(){
  58. window.frames[0].FCKToolbarItems.GetItem("Templates").Click();
  59. }
  60. </script>';
  61. $_SESSION['whereami'] = 'document/create';
  62. $this_section=SECTION_COURSES;
  63. include(api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  64. include(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  65. include(api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  66. include(api_get_path(LIBRARY_PATH).'document.lib.php');
  67. require_once(api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  68. $fck_attribute['Width'] = '100%';
  69. $fck_attribute['Height'] = '950';
  70. $fck_attribute['ToolbarSet'] = 'Full';
  71. $fck_attribute['Config']['FullPage'] = true;
  72. /*
  73. ------------------------------------------------------------------------------
  74. Constants & Variables
  75. ------------------------------------------------------------------------------
  76. */
  77. $file = $_GET['file'];
  78. //echo('file: '.$file.'<br>');
  79. $doc=basename($file);
  80. //echo('doc: '.$doc.'<br>');
  81. $dir=$_GET['curdirpath'];
  82. //echo('dir: '.$dir.'<br>');
  83. $file_name = $doc;
  84. //echo('file_name: '.$file_name.'<br>');
  85. $baseServDir = api_get_path(SYS_COURSE_PATH);
  86. $baseServUrl = $_configuration['url_append']."/";
  87. $courseDir = $_course['path']."/document";
  88. $baseWorkDir = $baseServDir.$courseDir;
  89. $group_document = false;
  90. $use_document_title = (get_setting('use_document_title')=='true')?true:false;
  91. $noPHP_SELF=true;
  92. /*
  93. ------------------------------------------------------------------------------
  94. Other init code
  95. ------------------------------------------------------------------------------
  96. */
  97. /* please do not modify this dirname formatting */
  98. if(strstr($dir,'..'))
  99. {
  100. $dir='/';
  101. }
  102. if($dir[0] == '.')
  103. {
  104. $dir=substr($dir,1);
  105. }
  106. if($dir[0] != '/')
  107. {
  108. $dir='/'.$dir;
  109. }
  110. if($dir[strlen($dir)-1] != '/')
  111. {
  112. $dir.='/';
  113. }
  114. $filepath=api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  115. if(!is_dir($filepath))
  116. {
  117. $filepath=api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  118. $dir='/';
  119. }
  120. /**************************************************/
  121. $nameTools = get_lang('EditDocument');
  122. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  123. if(isset($_SESSION['_gid']) && $_SESSION['_gid']!='')
  124. {
  125. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  126. $interbreadcrumb[]= array ("url"=>"../group/group_space.php?gidReq=".$_SESSION['_gid'], "name"=> get_lang('GroupSpace'));
  127. $group_document = true;
  128. $noPHP_SELF=true;
  129. }
  130. $interbreadcrumb[]=array("url"=>"./document.php?curdirpath=".urlencode($_GET['curdirpath']).$req_gid, "name"=> get_lang('Documents'));
  131. $is_allowedToEdit = is_allowed_to_edit() || $_SESSION['group_member_with_upload_rights'];
  132. if(!$is_allowedToEdit)
  133. {
  134. api_not_allowed(true);
  135. }
  136. event_access_tool(TOOL_DOCUMENT);
  137. /*
  138. ==============================================================================
  139. MAIN TOOL CODE
  140. ==============================================================================
  141. */
  142. /*
  143. ------------------------------------------------------------------------------
  144. General functions
  145. ------------------------------------------------------------------------------
  146. */
  147. /*
  148. ------------------------------------------------------------------------------
  149. Workhorse functions
  150. These do the actual work that is expected from of this tool, other functions
  151. are only there to support these ones.
  152. ------------------------------------------------------------------------------
  153. */
  154. /**
  155. This function changes the name of a certain file.
  156. It needs no global variables, it takes all info from parameters.
  157. It returns nothing.
  158. */
  159. function change_name($baseWorkDir, $sourceFile, $renameTo, $dir, $doc)
  160. {
  161. $file_name_for_change = $baseWorkDir.$dir.$sourceFile;
  162. //api_display_debug_info("call my_rename: params $file_name_for_change, $renameTo");
  163. $renameTo = disable_dangerous_file($renameTo); //avoid renaming to .htaccess file
  164. $renameTo = my_rename($file_name_for_change, stripslashes($renameTo)); //fileManage API
  165. if ($renameTo)
  166. {
  167. if (isset($dir) && $dir != "")
  168. {
  169. $sourceFile = $dir.$sourceFile;
  170. $new_full_file_name = dirname($sourceFile)."/".$renameTo;
  171. }
  172. else
  173. {
  174. $sourceFile = "/".$sourceFile;
  175. $new_full_file_name = "/".$renameTo;
  176. }
  177. update_db_info("update", $sourceFile, $new_full_file_name); //fileManage API
  178. $name_changed = get_lang("ElRen");
  179. $info_message = get_lang('fileModified');
  180. $GLOBALS['file_name'] = $renameTo;
  181. $GLOBALS['doc'] = $renameTo;
  182. return $info_message;
  183. }
  184. else
  185. {
  186. $dialogBox = get_lang('FileExists');
  187. /* return to step 1 */
  188. $rename = $sourceFile;
  189. unset($sourceFile);
  190. }
  191. }
  192. /*
  193. ------------------------------------------------------------------------------
  194. Code to change the comment
  195. ------------------------------------------------------------------------------
  196. Step 2. React on POST data
  197. (Step 1 see below)
  198. */
  199. if (isset($_POST['newComment']))
  200. {
  201. //to try to fix the path if it is wrong
  202. $commentPath = str_replace("//", "/", $_POST['commentPath']);
  203. $newComment = trim($_POST['newComment']); // remove spaces
  204. $newTitle = trim($_POST['newTitle']); // remove spaces
  205. // Check if there is already a record for this file in the DB
  206. $result = mysql_query ("SELECT * FROM $dbTable WHERE path='".$commentPath."'");
  207. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  208. {
  209. $attribute['path' ] = $row['path' ];
  210. $attribute['comment' ] = $row['title' ];
  211. }
  212. //Determine the correct query to the DB
  213. //new code always keeps document in database
  214. $query = "UPDATE $dbTable SET comment='".$newComment."', title='".$newTitle."' WHERE path='".$commentPath."'";
  215. mysql_query($query);
  216. //this is an UPDATE page... we shouldn't be creating new documents here.
  217. /*
  218. if (mysql_affected_rows() == 0)
  219. {
  220. mysql_query("INSERT INTO $dbTable SET path='".$commentPath."', title='".$newTitle."', comment='".$newComment."'");
  221. }
  222. */
  223. $oldComment = $newComment;
  224. $oldTitle = $newTitle;
  225. $comments_updated = get_lang('ComMod');
  226. $info_message = get_lang('fileModified');
  227. }
  228. /*
  229. ------------------------------------------------------------------------------
  230. Code to change the name
  231. ------------------------------------------------------------------------------
  232. Step 2. react on POST data - change the name
  233. (Step 1 see below)
  234. */
  235. if (isset($_POST['renameTo']))
  236. {
  237. $info_message = change_name($baseWorkDir, $_GET['sourceFile'], $_POST['renameTo'], $dir, $doc);
  238. //assume name change was successful
  239. }
  240. /*
  241. ------------------------------------------------------------------------------
  242. Code to change the comment
  243. ------------------------------------------------------------------------------
  244. Step 1. Create dialog box.
  245. */
  246. /* Search the old comment */ // RH: metadata: added 'id,'
  247. $result = mysql_query ("SELECT id,comment,title FROM $dbTable WHERE path='$dir$doc'");
  248. $message = "<i>Debug info</i><br>directory = $dir<br>";
  249. $message .= "document = $file_name<br>";
  250. $message .= "comments file = " . $file . "<br>";
  251. //Display::display_normal_message($message);
  252. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  253. {
  254. $oldComment = $row['comment'];
  255. $oldTitle = $row['title'];
  256. $docId = $row['id']; // RH: metadata
  257. }
  258. /*
  259. ------------------------------------------------------------------------------
  260. WYSIWYG HTML EDITOR - Program Logic
  261. ------------------------------------------------------------------------------
  262. */
  263. if($is_allowedToEdit)
  264. {
  265. if($_POST['formSent']==1)
  266. {
  267. if(isset($_POST['renameTo']))
  268. {
  269. $_POST['filename']=disable_dangerous_file($_POST['renameTo']);
  270. $extension=explode('.',$_POST['filename']);
  271. $extension=$extension[sizeof($extension)-1];
  272. $_POST['filename']=str_replace('.'.$extension,'',$_POST['filename']);
  273. }
  274. $filename=stripslashes($_POST['filename']);
  275. $texte=trim(str_replace(array("\r","\n"),"",stripslashes($_POST['texte'])));
  276. if(!strstr($texte,'/css/frames.css'))
  277. {
  278. $texte=str_replace('</title></head>','</title><link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>',$texte);
  279. }
  280. // RH commented: $filename=replace_dangerous_char($filename,'strict');
  281. if($_POST['extension'] != 'htm' && $_POST['extension'] != 'html')
  282. {
  283. $extension='html';
  284. }
  285. else
  286. {
  287. $extension = $_POST['extension'];
  288. }
  289. $file=$dir.$filename.'.'.$extension;
  290. if(empty($texte))
  291. {
  292. $msgError=get_lang('NoText');
  293. }
  294. elseif(empty($filename))
  295. {
  296. $msgError=get_lang('NoFileName');
  297. }
  298. else
  299. {
  300. if($fp=@fopen($filepath.$filename.'.'.$extension,'w'))
  301. {
  302. $texte = text_filter($texte);
  303. //echo('file path: '.$filepath.$filename.'.'.$extension);
  304. //if flv player, change absolute paht temporarely to prevent from erasing it in the following lines
  305. $texte = str_replace('flv=h','flv=h|',$texte);
  306. $texte = str_replace('flv=/','flv=/|',$texte);
  307. $path_to_remove=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  308. $texte=str_replace($path_to_remove,'./',$texte);
  309. $texte=str_replace('mp3player.swf?son='.urlencode($path_to_remove),'mp3player.swf?son=.%2F',$texte);
  310. // for flv player : change back the url to absolute
  311. $texte = str_replace('flv=h|','flv=h',$texte);
  312. $texte = str_replace('flv=/|','flv=/',$texte);
  313. //echo('texte: '.$texte);
  314. //echo (fputs($fp,$texte))?'FPUTS OK':'FPUTS NIET OK';
  315. fputs($fp,$texte);
  316. fclose($fp);
  317. $perm = api_get_setting('permissions_for_new_directories');
  318. $perm = octdec(!empty($perm)?$perm:'0770');
  319. if(!is_dir($filepath.'css'))
  320. {
  321. mkdir($filepath.'css',$perm);
  322. $doc_id=add_document($_course,$dir.'css','folder',0,'css');
  323. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id']);
  324. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  325. }
  326. if(!is_file($filepath.'css/frames.css'))
  327. {
  328. copy(api_get_path(SYS_CODE_PATH).'css/frames.css',$filepath.'css/frames.css');
  329. $doc_id=add_document($_course,$dir.'css/frames.css','file',filesize($filepath.'css/frames.css'),'frames.css');
  330. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id']);
  331. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  332. }
  333. // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  334. $document_id = DocumentManager::get_document_id($_course,$file);
  335. if($document_id)
  336. {
  337. $file_size = filesize($filepath.$filename.'.'.$extension);
  338. update_existing_document($_course, $document_id,$file_size);
  339. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentUpdated', $_user['user_id']);
  340. //update parent folders
  341. item_property_update_on_folder($_course,$dir,$_user['user_id']);
  342. header('Location: document.php?curdirpath='.urlencode($_GET['curdirpath']).$req_gid);
  343. exit();
  344. }
  345. else
  346. {
  347. //$msgError=get_lang('Impossible');
  348. }
  349. }
  350. else
  351. {
  352. $msgError=get_lang('Impossible');
  353. }
  354. }
  355. }
  356. }
  357. if(file_exists($filepath.$doc))
  358. {
  359. $extension=explode('.',$doc);
  360. $extension=$extension[sizeof($extension)-1];
  361. $filename=str_replace('.'.$extension,'',$doc);
  362. $extension=strtolower($extension);
  363. if(!in_array($extension,array('html','htm')))
  364. {
  365. $extension=$filename=$texte='';
  366. }
  367. else
  368. {
  369. $texte=file($filepath.$doc);
  370. $texte=implode('',$texte);
  371. $path_to_append=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  372. $texte=str_replace('="./','="'.$path_to_append,$texte);
  373. $texte=str_replace('mp3player.swf?son=.%2F','mp3player.swf?son='.urlencode($path_to_append),$texte);
  374. }
  375. }
  376. /*
  377. ==============================================================================
  378. MAIN EDIT_DOCUMENT CODE
  379. - react on input
  380. - display user interface
  381. ==============================================================================
  382. */
  383. Display::display_header($nameTools,"Doc");
  384. api_display_tool_title(get_lang("EditDocument") . ": $file_name");
  385. if(isset($msgError))
  386. {
  387. Display::display_error_message($msgError); //main API
  388. }
  389. if( isset($info_message))
  390. {
  391. Display::display_normal_message($info_message); //main API
  392. }
  393. $action = api_get_self().'?sourceFile='.urlencode($file_name).'&curdirpath='.urlencode($_GET['curdirpath']).'&file='.urlencode($_GET['file']).'&doc='.urlencode($doc);
  394. $form = new FormValidator('formEdit','post',$action);
  395. $form->addElement('hidden','filename');
  396. $form->addElement('hidden','extension');
  397. $form->addElement('hidden','file_path');
  398. $form->addElement('hidden','commentPath');
  399. if($use_document_title)
  400. {
  401. $form->add_textfield('newTitle',get_lang('Title'));
  402. $defaults['newTitle'] = $oldTitle;
  403. }
  404. else
  405. {
  406. $form->addElement('hidden','renameTo');
  407. }
  408. if($extension == "htm" || $extension == "html")
  409. {
  410. $form->addElement('hidden','formSent');
  411. $defaults['formSent'] = 1;
  412. $form->addElement('submit','submit',get_lang('Ok'));
  413. $form->add_html_editor('texte','<a style="cursor:pointer" onclick="launch_templates()"><img src="'.api_get_path(WEB_IMG_PATH).'templates.gif" /></a>',false,true);
  414. $defaults['texte'] = $texte;
  415. }
  416. if(!$group_document)
  417. {
  418. $metadata_link = '<a href="../metadata/index.php?eid='.urlencode('Document.'.$docId).'">'.get_lang('AddMetadata').'</a>';
  419. $form->addElement('static',null,get_lang('Metadata'),$metadata_link);
  420. }
  421. $form->addElement('textarea','newComment',get_lang('Comment'),'rows="3" style="width:300px;"');
  422. $form->addElement('submit','submit',get_lang('Ok'));
  423. $defaults['filename'] = $filename;
  424. $defaults['extension'] = $extension;
  425. $defaults['file_path'] = $_GET['file'];
  426. $defaults['commentPath'] = $file;
  427. $defaults['renameTo'] = $file_name;
  428. $defaults['newComment'] = $oldComment;
  429. $form->setDefaults($defaults);
  430. $form->display();
  431. /*
  432. ==============================================================================
  433. DOKEOS FOOTER
  434. ==============================================================================
  435. */
  436. Display::display_footer();
  437. ?>