edit_document.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php // $Id: edit_document.php 14776 2008-04-08 06:55:16Z 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 LIKE BINARY '".$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 LIKE BINARY '".$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. /** TODO check if this code is still used **/
  247. /* Search the old comment */ // RH: metadata: added 'id,'
  248. $result = mysql_query ("SELECT id,comment,title FROM $dbTable WHERE path LIKE BINARY '$dir$doc'");
  249. $message = "<i>Debug info</i><br>directory = $dir<br>";
  250. $message .= "document = $file_name<br>";
  251. $message .= "comments file = " . $file . "<br>";
  252. //Display::display_normal_message($message);
  253. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  254. {
  255. $oldComment = $row['comment'];
  256. $oldTitle = $row['title'];
  257. $docId = $row['id']; // RH: metadata
  258. }
  259. /*
  260. ------------------------------------------------------------------------------
  261. WYSIWYG HTML EDITOR - Program Logic
  262. ------------------------------------------------------------------------------
  263. */
  264. if($is_allowedToEdit)
  265. {
  266. if($_POST['formSent']==1)
  267. {
  268. if(isset($_POST['renameTo']))
  269. {
  270. $_POST['filename']=disable_dangerous_file($_POST['renameTo']);
  271. $extension=explode('.',$_POST['filename']);
  272. $extension=$extension[sizeof($extension)-1];
  273. $_POST['filename']=str_replace('.'.$extension,'',$_POST['filename']);
  274. }
  275. $filename=stripslashes($_POST['filename']);
  276. $texte=trim(str_replace(array("\r","\n"),"",stripslashes($_POST['texte'])));
  277. if(!strstr($texte,'/css/frames.css'))
  278. {
  279. $texte=str_replace('</title></head>','</title><link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>',$texte);
  280. }
  281. // RH commented: $filename=replace_dangerous_char($filename,'strict');
  282. if($_POST['extension'] != 'htm' && $_POST['extension'] != 'html')
  283. {
  284. $extension='html';
  285. }
  286. else
  287. {
  288. $extension = $_POST['extension'];
  289. }
  290. $file=$dir.$filename.'.'.$extension;
  291. if(empty($texte))
  292. {
  293. $msgError=get_lang('NoText');
  294. }
  295. elseif(empty($filename))
  296. {
  297. $msgError=get_lang('NoFileName');
  298. }
  299. else
  300. {
  301. if($fp=@fopen($filepath.$filename.'.'.$extension,'w'))
  302. {
  303. $texte = text_filter($texte);
  304. //echo('file path: '.$filepath.$filename.'.'.$extension);
  305. //if flv player, change absolute paht temporarely to prevent from erasing it in the following lines
  306. $texte = str_replace('flv=h','flv=h|',$texte);
  307. $texte = str_replace('flv=/','flv=/|',$texte);
  308. $path_to_remove=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  309. $texte=str_replace($path_to_remove,'./',$texte);
  310. $texte=str_replace('mp3player.swf?son='.urlencode($path_to_remove),'mp3player.swf?son=.%2F',$texte);
  311. // for flv player : change back the url to absolute
  312. $texte = str_replace('flv=h|','flv=h',$texte);
  313. $texte = str_replace('flv=/|','flv=/',$texte);
  314. //echo('texte: '.$texte);
  315. //echo (fputs($fp,$texte))?'FPUTS OK':'FPUTS NIET OK';
  316. fputs($fp,$texte);
  317. fclose($fp);
  318. $perm = api_get_setting('permissions_for_new_directories');
  319. $perm = octdec(!empty($perm)?$perm:'0770');
  320. if(!is_dir($filepath.'css'))
  321. {
  322. mkdir($filepath.'css',$perm);
  323. $doc_id=add_document($_course,$dir.'css','folder',0,'css');
  324. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id']);
  325. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  326. }
  327. if(!is_file($filepath.'css/frames.css'))
  328. {
  329. copy(api_get_path(SYS_CODE_PATH).'css/frames.css',$filepath.'css/frames.css');
  330. $doc_id=add_document($_course,$dir.'css/frames.css','file',filesize($filepath.'css/frames.css'),'frames.css');
  331. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id']);
  332. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id']);
  333. }
  334. // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  335. $document_id = DocumentManager::get_document_id($_course,$file);
  336. if($document_id)
  337. {
  338. $file_size = filesize($filepath.$filename.'.'.$extension);
  339. update_existing_document($_course, $document_id,$file_size);
  340. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentUpdated', $_user['user_id']);
  341. //update parent folders
  342. item_property_update_on_folder($_course,$dir,$_user['user_id']);
  343. header('Location: document.php?curdirpath='.urlencode($_GET['curdirpath']).$req_gid);
  344. exit();
  345. }
  346. else
  347. {
  348. //$msgError=get_lang('Impossible');
  349. }
  350. }
  351. else
  352. {
  353. $msgError=get_lang('Impossible');
  354. }
  355. }
  356. }
  357. }
  358. if(file_exists($filepath.$doc))
  359. {
  360. $extension=explode('.',$doc);
  361. $extension=$extension[sizeof($extension)-1];
  362. $filename=str_replace('.'.$extension,'',$doc);
  363. $extension=strtolower($extension);
  364. if(!in_array($extension,array('html','htm')))
  365. {
  366. $extension=$filename=$texte='';
  367. }
  368. else
  369. {
  370. $texte=file($filepath.$doc);
  371. $texte=implode('',$texte);
  372. $path_to_append=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  373. $texte=str_replace('="./','="'.$path_to_append,$texte);
  374. $texte=str_replace('mp3player.swf?son=.%2F','mp3player.swf?son='.urlencode($path_to_append),$texte);
  375. }
  376. }
  377. /*
  378. ==============================================================================
  379. MAIN EDIT_DOCUMENT CODE
  380. - react on input
  381. - display user interface
  382. ==============================================================================
  383. */
  384. Display::display_header($nameTools,"Doc");
  385. api_display_tool_title(get_lang("EditDocument") . ": $file_name");
  386. if(isset($msgError))
  387. {
  388. Display::display_error_message($msgError); //main API
  389. }
  390. if( isset($info_message))
  391. {
  392. Display::display_normal_message($info_message); //main API
  393. }
  394. $action = api_get_self().'?sourceFile='.urlencode($file_name).'&curdirpath='.urlencode($_GET['curdirpath']).'&file='.urlencode($_GET['file']).'&doc='.urlencode($doc);
  395. $form = new FormValidator('formEdit','post',$action);
  396. $form->addElement('hidden','filename');
  397. $form->addElement('hidden','extension');
  398. $form->addElement('hidden','file_path');
  399. $form->addElement('hidden','commentPath');
  400. if($use_document_title)
  401. {
  402. $form->add_textfield('newTitle',get_lang('Title'));
  403. $defaults['newTitle'] = $oldTitle;
  404. }
  405. else
  406. {
  407. $form->addElement('hidden','renameTo');
  408. }
  409. // readonly
  410. $sql = 'SELECT id, readonly FROM '.$dbTable.'
  411. WHERE path LIKE BINARY "'.$dir.$doc.'"';
  412. $rs = api_sql_query($sql, __FILE__, __LINE__);
  413. $readonly = mysql_result($rs,0,'readonly');
  414. $doc_id = mysql_result($rs,0,'id');
  415. // owner
  416. $sql = 'SELECT insert_user_id FROM '.Database::get_course_table(TABLE_ITEM_PROPERTY).'
  417. WHERE tool LIKE "document"
  418. AND ref='.intval($doc_id);
  419. $rs = api_sql_query($sql, __FILE__, __LINE__);
  420. $owner_id = mysql_result($rs,0,'insert_user_id');
  421. if($owner_id != $_user['user_id'])
  422. {
  423. $form->addElement('hidden','readonly');
  424. }
  425. else
  426. {
  427. $renderer = $form->defaultRenderer();
  428. $renderer->setElementTemplate('<div class="row"><div class="label"></div><div class="formw">{element}{label}</div></div>', 'readonly');
  429. $form->addElement('checkbox','readonly',get_lang('ReadOnly'));
  430. }
  431. $defaults['readonly']=$readonly;
  432. if($extension == "htm" || $extension == "html")
  433. {
  434. $form->addElement('hidden','formSent');
  435. $defaults['formSent'] = 1;
  436. $form->addElement('submit','submit',get_lang('Ok'));
  437. $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);
  438. $defaults['texte'] = $texte;
  439. }
  440. if(!$group_document)
  441. {
  442. $metadata_link = '<a href="../metadata/index.php?eid='.urlencode('Document.'.$docId).'">'.get_lang('AddMetadata').'</a>';
  443. $form->addElement('static',null,get_lang('Metadata'),$metadata_link);
  444. }
  445. $form->addElement('textarea','newComment',get_lang('Comment'),'rows="3" style="width:300px;"');
  446. $form->addElement('submit','submit',get_lang('Ok'));
  447. $defaults['filename'] = $filename;
  448. $defaults['extension'] = $extension;
  449. $defaults['file_path'] = $_GET['file'];
  450. $defaults['commentPath'] = $file;
  451. $defaults['renameTo'] = $file_name;
  452. $defaults['newComment'] = $oldComment;
  453. $form->setDefaults($defaults);
  454. $form->display();
  455. /*
  456. ==============================================================================
  457. DOKEOS FOOTER
  458. ==============================================================================
  459. */
  460. Display::display_footer();
  461. ?>