dokeos-document
[ class tree: dokeos-document ] [ index: dokeos-document ] [ all elements ]

Source for file edit_document.php

Documentation is available at edit_document.php

  1. <?php // $Id: edit_document.php 15550 2008-06-11 17:24:32Z juliomontoya $
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.  
  6.     Copyright (c) 2004-2008 Dokeos SPRL
  7.     Copyright (c) 2003 Ghent University (UGent)
  8.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9.     Copyright (c) Olivier Brouckaert
  10.     Copyright (c) Roan Embrechts
  11.     Copyright (c) Rene Haentjens (RH) (update 2004/09/30)
  12.     Copyright (c) Bart Mollet, Hogeschool Gent
  13.  
  14.     For a full list of contributors, see "credits.txt".
  15.     The full license can be read in "license.txt".
  16.  
  17.     This program is free software; you can redistribute it and/or
  18.     modify it under the terms of the GNU General Public License
  19.     as published by the Free Software Foundation; either version 2
  20.     of the License, or (at your option) any later version.
  21.  
  22.     See the GNU General Public License for more details.
  23.  
  24.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  25.     
  26. ==============================================================================
  27. */
  28. /**
  29. ==============================================================================
  30. * This file allows editing documents.
  31. *
  32. * Based on create_document, this file allows
  33. * - edit name
  34. * - edit comments
  35. * - edit metadata (requires a document table entry)
  36. * - edit html content (only for htm/html files)
  37. *
  38. * For all files
  39. * - show editable name field
  40. * - show editable comments field
  41. * Additionally, for html and text files
  42. * - show RTE
  43. *
  44. * Remember, all files and folders must always have an entry in the
  45. * database, regardless of wether they are visible/invisible, have
  46. * comments or not.
  47. *
  48. @package dokeos.document
  49. @todo improve script structure (FormValidator is used to display form, but
  50. *  not for validation at the moment)
  51. ==============================================================================
  52. */
  53. // name of the language file that needs to be included 
  54. $language_file 'document';
  55. /*
  56. ------------------------------------------------------------------------------
  57.     Included libraries
  58. ------------------------------------------------------------------------------
  59. */
  60. include('../inc/global.inc.php');
  61.  
  62. // Template's javascript
  63. $htmlHeadXtra['
  64. <script type="text/javascript">
  65.  
  66. function InnerDialogLoaded()
  67. {    
  68.     var B=new window.frames[0].FCKToolbarButton(\'Templates\',window.frames[0].FCKLang.Templates);    
  69.     return B.ClickFrame();
  70. };    
  71.         
  72. function FCKeditor_OnComplete( editorInstance )
  73. {
  74.     document.getElementById(\'frmModel\').innerHTML = "<iframe height=950px; width=100%; frameborder=0 src=\''.api_get_path(WEB_LIBRARY_PATH).'fckeditor/editor/fckdialogframe.html \'>";    
  75. }
  76.         
  77.  
  78. </script>';
  79.  
  80. $_SESSION['whereami''document/create';
  81. $this_section=SECTION_COURSES;
  82.  
  83. include(api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  84. include(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  85. include(api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  86. include(api_get_path(LIBRARY_PATH).'document.lib.php');
  87.  
  88. include_once(api_get_path(LIBRARY_PATH'groupmanager.lib.php');
  89. require_once(api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  90.  
  91. $fck_attribute['Width''100%';
  92. $fck_attribute['Height''950';
  93. $fck_attribute['ToolbarSet''Full';
  94. $fck_attribute['Config']['FullPage'true;
  95.  
  96.  
  97. /*
  98. ------------------------------------------------------------------------------
  99.     Constants & Variables
  100. ------------------------------------------------------------------------------
  101. */
  102. $file $_GET['file'];
  103. //echo('file: '.$file.'<br>');
  104. $doc=basename($file);
  105. //echo('doc: '.$doc.'<br>');
  106. $dir=$_GET['curdirpath'];
  107. //echo('dir: '.$dir.'<br>');
  108. $file_name $doc;
  109. //echo('file_name: '.$file_name.'<br>');
  110.  
  111. $baseServDir api_get_path(SYS_COURSE_PATH);
  112. $baseServUrl $_configuration['url_append']."/";
  113. $courseDir   $_course['path']."/document";
  114. $baseWorkDir $baseServDir.$courseDir;
  115. $group_document false;
  116.  
  117. $use_document_title (get_setting('use_document_title')=='true')?true:false;
  118. $noPHP_SELF=true;
  119.  
  120. /*
  121. ------------------------------------------------------------------------------
  122.     Other init code
  123. ------------------------------------------------------------------------------
  124. */
  125.  
  126. /* please do not modify this dirname formatting */
  127.  
  128. if(strstr($dir,'..'))
  129. {
  130.     $dir='/';
  131. }
  132.  
  133. if($dir[0== '.')
  134. {
  135.     $dir=substr($dir,1);
  136. }
  137.  
  138. if($dir[0!= '/')
  139. {
  140.     $dir='/'.$dir;
  141. }
  142.  
  143. if($dir[strlen($dir)-1!= '/')
  144. {
  145.     $dir.='/';
  146. }
  147.  
  148. $filepath=api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  149.  
  150. if(!is_dir($filepath))
  151. {
  152.     $filepath=api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  153.     $dir='/';
  154. }
  155.  
  156. /**************************************************/
  157. $nameTools get_lang('EditDocument');
  158.  
  159. if(!empty($_SESSION['_gid']))
  160. {
  161.     $req_gid '&amp;gidReq='.$_SESSION['_gid'];
  162.     $interbreadcrumb[]array ("url"=>"../group/group_space.php?gidReq=".$_SESSION['_gid']"name"=> get_lang('GroupSpace'));
  163.     $group_document true;
  164.     $noPHP_SELF=true;
  165. }
  166.  
  167. $interbreadcrumb[]=array("url"=>"./document.php?curdirpath=".urlencode($_GET['curdirpath']).$req_gid"name"=> get_lang('Documents'));
  168.  
  169. $is_allowedToEdit is_allowed_to_edit(|| $_SESSION['group_member_with_upload_rights'];
  170.  
  171. if(!$is_allowedToEdit)
  172. {
  173.     api_not_allowed(true);
  174. }
  175.  
  176.  
  177. /*
  178. ==============================================================================
  179.        MAIN TOOL CODE
  180. ==============================================================================
  181. */
  182.  
  183. /*
  184. ------------------------------------------------------------------------------
  185.     General functions
  186. ------------------------------------------------------------------------------
  187. */
  188.  
  189.  
  190.  
  191. /*
  192. ------------------------------------------------------------------------------
  193.     Workhorse functions
  194.  
  195.     These do the actual work that is expected from of this tool, other functions
  196.     are only there to support these ones.
  197. ------------------------------------------------------------------------------
  198. */
  199.  
  200. /**
  201.     This function changes the name of a certain file.
  202.     It needs no global variables, it takes all info from parameters.
  203.     It returns nothing.
  204. */
  205. function change_name($baseWorkDir$sourceFile$renameTo$dir$doc)
  206. {
  207.     $file_name_for_change $baseWorkDir.$dir.$sourceFile;
  208.     //api_display_debug_info("call my_rename: params $file_name_for_change, $renameTo");
  209.         $renameTo disable_dangerous_file($renameTo)//avoid renaming to .htaccess file
  210.     $renameTo my_rename($file_name_for_changestripslashes($renameTo))//fileManage API
  211.     
  212.     if ($renameTo)
  213.     {
  214.         if (isset($dir&& $dir != "")
  215.         {
  216.             $sourceFile $dir.$sourceFile;
  217.             $new_full_file_name dirname($sourceFile)."/".$renameTo;
  218.         }
  219.         else
  220.         {
  221.             $sourceFile "/".$sourceFile;
  222.             $new_full_file_name "/".$renameTo;
  223.         }
  224.  
  225.         update_db_info("update"$sourceFile$new_full_file_name)//fileManage API
  226.         $name_changed get_lang("ElRen");
  227.         $info_message get_lang('fileModified');
  228.  
  229.         $GLOBALS['file_name'$renameTo;
  230.         $GLOBALS['doc'$renameTo;
  231.  
  232.         return $info_message;
  233.     }
  234.     else
  235.     {
  236.         $dialogBox get_lang('FileExists');
  237.  
  238.         /* return to step 1 */
  239.         $rename $sourceFile;
  240.         unset($sourceFile);
  241.     }
  242. }
  243.  
  244. /*
  245. ------------------------------------------------------------------------------
  246.     Code to change the comment
  247. ------------------------------------------------------------------------------
  248.     Step 2. React on POST data
  249.     (Step 1 see below)
  250. */
  251. if (isset($_POST['newComment']))
  252. {
  253.     //to try to fix the path if it is wrong
  254.     $commentPath str_replace("//""/"Database::escape_string($_POST['commentPath']));
  255.     $newComment trim(Database::escape_string($_POST['newComment']))// remove spaces
  256.     $newTitle trim(Database::escape_string($_POST['newTitle']))// remove spaces
  257.     // Check if there is already a record for this file in the DB
  258.     $result api_sql_query ("SELECT * FROM $dbTable WHERE path LIKE BINARY '".$commentPath."'",__FILE__,__LINE__);
  259.     while($row Database::fetch_array($result'ASSOC'))
  260.     {
  261.         $attribute['path'      $row['path' ];
  262.         $attribute['comment'   $row['title'];
  263.     }
  264.     //Determine the correct query to the DB
  265.     //new code always keeps document in database
  266.     $query "UPDATE $dbTable 
  267.         SET comment='".$newComment."', title='".$newTitle."' 
  268.         WHERE path 
  269.         LIKE BINARY '".$commentPath."'";
  270.     api_sql_query($query,__FILE__,__LINE__);
  271.     $oldComment $newComment;
  272.     $oldTitle $newTitle;
  273.     $comments_updated get_lang('ComMod');
  274.     $info_message get_lang('fileModified');
  275. }
  276.  
  277. /*
  278. ------------------------------------------------------------------------------
  279.     Code to change the name
  280. ------------------------------------------------------------------------------
  281.     Step 2. react on POST data - change the name
  282.     (Step 1 see below)
  283. */
  284.  
  285. if (isset($_POST['renameTo']))
  286. {
  287.     $info_message change_name($baseWorkDir$_GET['sourceFile']$_POST['renameTo']$dir$doc);
  288.     //assume name change was successful
  289. }
  290.  
  291. /*
  292. ------------------------------------------------------------------------------
  293.     Code to change the comment
  294. ------------------------------------------------------------------------------
  295.     Step 1. Create dialog box.
  296. */
  297.  
  298. /** TODO check if this code is still used **/
  299. /* Search the old comment */  // RH: metadata: added 'id,'
  300. $result api_sql_query("SELECT id,comment,title FROM $dbTable WHERE path LIKE BINARY '$dir$doc'",__FILE__,__LINE__);
  301.  
  302. $message "<i>Debug info</i><br>directory = $dir<br>";
  303. $message .= "document = $file_name<br>";
  304. $message .= "comments file = " $file "<br>";
  305. //Display::display_normal_message($message);
  306.  
  307. while($row Database::fetch_array($result'ASSOC'))
  308. {
  309.     $oldComment $row['comment'];
  310.     $oldTitle $row['title'];
  311.     $docId $row['id'];  // RH: metadata
  312. }
  313.  
  314. /*
  315. ------------------------------------------------------------------------------
  316.     WYSIWYG HTML EDITOR - Program Logic
  317. ------------------------------------------------------------------------------
  318. */
  319.  
  320. if($is_allowedToEdit)
  321. {
  322.     if($_POST['formSent']==1)
  323.     {
  324.         if(isset($_POST['renameTo']))
  325.         {
  326.             $_POST['filename']=disable_dangerous_file($_POST['renameTo']);
  327.  
  328.             $extension=explode('.',$_POST['filename']);
  329.             $extension=$extension[sizeof($extension)-1];
  330.  
  331.             $_POST['filename']=str_replace('.'.$extension,'',$_POST['filename']);
  332.         }
  333.         
  334.         $filename=stripslashes($_POST['filename']);
  335.  
  336.         $texte=trim(str_replace(array("\r","\n"),"",stripslashes($_POST['texte'])));
  337.  
  338.         if(!strstr($texte,'/css/frames.css'))
  339.         {
  340.             $texte=str_replace('</title></head>','</title><link rel="stylesheet" href="../css/frames.css" type="text/css" /></head>',$texte);
  341.         }
  342.  
  343.         // RH commented: $filename=replace_dangerous_char($filename,'strict');
  344.         // What??
  345.         //if($_POST['extension'] != 'htm' && $_POST['extension'] != 'html')
  346.         //{
  347.             //$extension='html';
  348.         //}
  349.         //else
  350.         //{
  351.             $extension $_POST['extension'];
  352.         //}
  353.         
  354.         $file=$dir.$filename.'.'.$extension;
  355.         $read_only_flag=$_POST['readonly'];
  356.         if (!empty($read_only_flag))
  357.         {
  358.             $read_only_flag=1;
  359.         }
  360.         else
  361.         {
  362.             $read_only_flag=0;
  363.         }
  364.         
  365.         $show_edit=$_SESSION['showedit'];
  366.         //unset($_SESSION['showedit']);
  367.         api_session_unregister('showedit');        
  368.         
  369.                         
  370.         if(empty($filename))
  371.         {
  372.             $msgError=get_lang('NoFileName');
  373.         }
  374.         else
  375.         {
  376.             if ($read_only_flag==0)
  377.             {
  378.                 if (!empty($texte))
  379.                 {                    
  380.                     if($fp=@fopen($filepath.$filename.'.'.$extension,'w'))
  381.                     {                         
  382.                         $texte text_filter($texte);
  383.                         //if flv player, change absolute paht temporarely to prevent from erasing it in the following lines
  384.                         $texte str_replace('flv=h','flv=h|',$texte);
  385.                         $texte str_replace('flv=/','flv=/|',$texte);
  386.                         $path_to_remove=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  387.                         $texte=str_replace($path_to_remove,'./',$texte);
  388.                         $texte=str_replace('mp3player.swf?son='.urlencode($path_to_remove),'mp3player.swf?son=.%2F',$texte);
  389.                         // for flv player : change back the url to absolute
  390.                         $texte str_replace('flv=h|','flv=h',$texte);
  391.                         $texte str_replace('flv=/|','flv=/',$texte);
  392.                         fputs($fp,$texte);                        
  393.                         fclose($fp);            
  394.                         $perm api_get_setting('permissions_for_new_directories');
  395.                         $perm octdec(!empty($perm)?$perm:'0770');
  396.                         if(!is_dir($filepath.'css'))
  397.                         {
  398.                             mkdir($filepath.'css',$perm);
  399.                             $doc_id=add_document($_course,$dir.'css','folder',0,'css');
  400.                             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'FolderCreated'$_user['user_id']);
  401.                             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'invisible'$_user['user_id']);
  402.                         }
  403.             
  404.                         if(!is_file($filepath.'css/frames.css'))
  405.                         {
  406.                             copy(api_get_path(SYS_CODE_PATH).'css/frames.css',$filepath.'css/frames.css');
  407.                             $doc_id=add_document($_course,$dir.'css/frames.css','file',filesize($filepath.'css/frames.css'),'frames.css');
  408.                             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'DocumentAdded'$_user['user_id']);
  409.                             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'invisible'$_user['user_id']);
  410.                         }
  411.                                 
  412.                         // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  413.                         $document_id DocumentManager::get_document_id($_course,$file);
  414.                         if($document_id)
  415.                         {    
  416.                             $file_size filesize($filepath.$filename.'.'.$extension);
  417.                             update_existing_document($_course$document_id,$file_size,$read_only_flag);
  418.                             api_item_property_update($_courseTOOL_DOCUMENT$document_id'DocumentUpdated'$_user['user_id']);
  419.                             //update parent folders
  420.                             item_property_update_on_folder($_course,$dir,$_user['user_id']);
  421.                             $dirsubstr($dir,0,-1);
  422.                             header('Location: document.php?curdirpath='.urlencode($dir));
  423.                             exit ();
  424.                         }
  425.                         else
  426.                         {
  427.                         //$msgError=get_lang('Impossible');
  428.                         }                            
  429.                     }
  430.                     else
  431.                     {
  432.                         $msgError=get_lang('Impossible');
  433.                     }                        
  434.                 }
  435.                 else
  436.                 {
  437.                     $file_size filesize($filepath.$filename.'.'.$extension);
  438.                     $document_id DocumentManager::get_document_id($_course,$file);
  439.                     if($document_id)
  440.                     {
  441.                         update_existing_document($_course$document_id,$file_size,$read_only_flag);
  442.                     }                    
  443.                 }                
  444.             }
  445.             else
  446.             {    
  447.                 $filepath.$filename.'.'.$extension;
  448.                 $file_size filesize($filepath.$filename.'.'.$extension);
  449.                 $document_id DocumentManager::get_document_id($_course,$file);
  450.                 
  451.                 if($document_id)
  452.                 {
  453.                     update_existing_document($_course$document_id,$file_size,$read_only_flag);
  454.                 }
  455.                 
  456.             }
  457.         }
  458.     }
  459. }
  460.  
  461.  
  462. //replace relative paths by absolute web paths  (e.g. "./" => "http://www.dokeos.com/courses/ABC/document/")
  463. if(file_exists($filepath.$doc))
  464. {
  465.     $extension=explode('.',$doc);    
  466.     $extension=$extension[sizeof($extension)-1];
  467.     $filename=str_replace('.'.$extension,'',$doc);
  468.     $extension=strtolower($extension);
  469.     
  470.     /*if(!in_array($extension,array('html','htm'))) // that was wrong
  471.     {
  472.         $extension=$filename=$texte='';        
  473.     }*/
  474.     
  475.     if(in_array($extension,array('html','htm')))
  476.     {
  477.         $texte=file($filepath.$doc);
  478.         $texte=implode('',$texte);
  479.         $path_to_append=api_get_path('WEB_COURSE_PATH').$_course['path'].'/document'.$dir;
  480.         $texte=str_replace('="./','="'.$path_to_append,$texte);
  481.         $texte=str_replace('mp3player.swf?son=.%2F','mp3player.swf?son='.urlencode($path_to_append),$texte);
  482.     }    
  483. }
  484.  
  485. /*
  486. ==============================================================================
  487.         - display user interface
  488. ==============================================================================
  489. */
  490. Display::display_header($nameTools,"Doc");
  491. api_display_tool_title(get_lang("EditDocument""$file_name");
  492.  
  493. if(isset($msgError))
  494. {
  495.     Display::display_error_message($msgError)//main API
  496. }
  497.  
  498. ifisset($info_message))
  499. {
  500.     Display::display_normal_message($info_message)//main API
  501. }
  502.  
  503.  
  504. // readonly
  505. $sql 'SELECT id, readonly FROM '.$dbTable.' WHERE path LIKE BINARY "'.$dir.$doc.'"';
  506. $rs api_sql_query($sql__FILE____LINE__);
  507. $readonly Database::result($rs,0,'readonly');
  508. $doc_id Database::result($rs,0,'id');
  509.  
  510. // owner
  511. $sql 'SELECT insert_user_id FROM '.Database::get_course_table(TABLE_ITEM_PROPERTY).'
  512.         WHERE tool LIKE "document"
  513.         AND ref='.intval($doc_id);
  514. $rs api_sql_query($sql__FILE____LINE__);
  515. $owner_id Database::result($rs,0,'insert_user_id');
  516.  
  517.  
  518. if ($owner_id == $_user['user_id'|| api_is_platform_admin(|| api_is_allowed_to_edit(|| GroupManager :: is_user_in_group($_user['user_id'],$_SESSION['_gid'))
  519. {  
  520.     
  521.     $action =  api_get_self().'?sourceFile='.urlencode($file_name).'&curdirpath='.urlencode($_GET['curdirpath']).'&file='.urlencode($_GET['file']).'&doc='.urlencode($doc);
  522.     $form new FormValidator('formEdit','post',$action);
  523.     $form->addElement('hidden','filename');
  524.     $form->addElement('hidden','extension')
  525.     $form->addElement('hidden','file_path');
  526.     $form->addElement('hidden','commentPath');        
  527.     $form->addElement('hidden','showedit');    
  528.     
  529.     if($use_document_title)
  530.     {
  531.         $form->add_textfield('newTitle',get_lang('Title'));
  532.         $defaults['newTitle'$oldTitle;
  533.     }
  534.     else
  535.     {
  536.         $form->addElement('hidden','renameTo');
  537.     }        
  538.  
  539.     $form->addElement('hidden','formSent');
  540.     $defaults['formSent'1;
  541.  
  542.             
  543.     $read_only_flag=$_POST['readonly'];
  544.             
  545.     $defaults['texte'$texte;
  546.             
  547.     if($extension == 'htm' || $extension == 'html')
  548.     {
  549.         if (empty($readonly&& $readonly==0)
  550.         {    
  551.             $form->addElement('submit','submit',get_lang('Ok'));    
  552.             $_SESSION['showedit']=1;
  553.             $form->add_html_editor('texte','',false,true);
  554.         }            
  555.     }
  556.             
  557.     if(!$group_document)
  558.     {
  559.         $metadata_link '<a href="../metadata/index.php?eid='.urlencode('Document.'.$docId).'">'.get_lang('AddMetadata').'</a>';
  560.         $form->addElement('static',null,get_lang('Metadata'),$metadata_link);
  561.     }
  562.             
  563.     $form->addElement('textarea','newComment',get_lang('Comment'),'rows="3" style="width:300px;"');
  564.             
  565.     $renderer $form->defaultRenderer();
  566.     
  567.     if ($owner_id == $_user['user_id'|| api_is_platform_admin())    
  568.     {
  569.         $renderer->setElementTemplate('<div class="row"><div class="label"></div><div class="formw">{element}{label}</div></div>''readonly');
  570.         $checked =&$form->addElement('checkbox','readonly',get_lang('ReadOnly'));            
  571.         if ($readonly==1)
  572.         
  573.             $checked->setChecked(true);
  574.         }
  575.     }        
  576.     $form->addElement('submit','submit',get_lang('Ok'));
  577.             
  578.     $defaults['filename'$filename;
  579.     $defaults['extension'$extension;
  580.     $defaults['file_path'$_GET['file'];
  581.     $defaults['commentPath'$file;
  582.     $defaults['renameTo'$file_name;
  583.     $defaults['newComment'$oldComment;
  584.     
  585.     $form->setDefaults($defaults);
  586.     // show templates
  587.     $form->addElement('html','<div id="frmModel" style="display:block; height:950px;width:20%; position:absolute; top:135px; left:1px;"></div>');            
  588.     $form->display();            
  589.  
  590.     //Display::display_error_message(get_lang('ReadOnlyFile')); //main API
  591.  
  592. }
  593.  
  594. /*
  595. ==============================================================================
  596.        DOKEOS FOOTER
  597. ==============================================================================
  598. */
  599. ?>

Documentation generated on Thu, 12 Jun 2008 13:22:47 -0500 by phpDocumentor 1.4.1