edit_document.php 17 KB

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