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

Source for file create_document.php

Documentation is available at create_document.php

  1. <?php
  2. // $Id: create_document.php 15322 2008-05-19 16:31:13Z juliomontoya $
  3. /*
  4. ==============================================================================
  5.     Dokeos - elearning and course management software
  6.  
  7.     Copyright (c) 2004-2008 Dokeos SPRL
  8.     Copyright (c) 2003 Ghent University (UGent)
  9.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  10.     Copyright (c) Olivier Brouckaert
  11.     Copyright (c) Bart Mollet, Hogeschool Gent
  12.  
  13.     For a full list of contributors, see "credits.txt".
  14.     The full license can be read in "license.txt".
  15.  
  16.     This program is free software; you can redistribute it and/or
  17.     modify it under the terms of the GNU General Public License
  18.     as published by the Free Software Foundation; either version 2
  19.     of the License, or (at your option) any later version.
  20.  
  21.     See the GNU General Public License for more details.
  22.  
  23.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  24.     Mail: info@dokeos.com
  25.     
  26. ==============================================================================
  27. */
  28. /**
  29. ==============================================================================
  30. *    This file allows creating new html documents with an online WYSIWYG html380
  31. *    editor.
  32. *
  33. *    @package dokeos.document
  34. ==============================================================================
  35. */
  36.  
  37. /*
  38. ==============================================================================
  39.         INIT SECTION
  40. ==============================================================================
  41. */
  42.  
  43. // name of the language file that needs to be included 
  44. $language_file 'document';
  45. include ('../inc/global.inc.php');
  46. $_SESSION['whereami''document/create';
  47. $this_section SECTION_COURSES;
  48.  
  49. $htmlHeadXtra[]='<script>
  50.     
  51. function InnerDialogLoaded()
  52. {        
  53.     var B=new window.frames[0].FCKToolbarButton(\'Templates\',window.frames[0].FCKLang.Templates);    
  54.     return B.ClickFrame();
  55. };    
  56.  
  57.     var temp=false;
  58.     var temp2=false;
  59.     var use_document_title='.api_get_setting('use_document_title').';
  60.  
  61.     function launch_templates()
  62.     {        
  63.         //document.getElementById(\'frmModel\').style.display="block";                
  64.         //document.getElementById(\'content___Frame\').width=\'70%\';         
  65.         //window.frames[0].FCKToolbarItems.GetItem("Template").Click;
  66.     }
  67.  
  68.     function FCKeditor_OnComplete( editorInstance )
  69.     {
  70.         editorInstance.Events.AttachEvent( \'OnSelectionChange\', check_for_title ) ;
  71.         document.getElementById(\'frmModel\').innerHTML = "<iframe height=950px; width=97%; frameborder=0 src=\''.api_get_path(WEB_LIBRARY_PATH).'fckeditor/editor/fckdialogframe.html \'>";    
  72.     }
  73.  
  74.     function check_for_title()
  75.     {
  76.         if(temp==true){
  77.             // This functions shows that you can interact directly with the editor area
  78.             // DOM. In this way you have the freedom to do anything you want with it.
  79.     
  80.             // Get the editor instance that we want to interact with.
  81.             var oEditor = FCKeditorAPI.GetInstance(\'content\') ;
  82.     
  83.             // Get the Editor Area DOM (Document object).
  84.             var oDOM = oEditor.EditorDocument ;
  85.     
  86.             var iLength ;
  87.             var contentText ;
  88.             var contentTextArray;
  89.             var bestandsnaamNieuw = "";
  90.             var bestandsnaamOud = "";
  91.     
  92.             // The are two diffent ways to get the text (without HTML markups).
  93.             // It is browser specific.
  94.     
  95.             if( document.all )        // If Internet Explorer.
  96.             {
  97.                 contentText = oDOM.body.innerText ;
  98.             }
  99.             else                    // If Gecko.
  100.             {
  101.                 var r = oDOM.createRange() ;
  102.                 r.selectNodeContents( oDOM.body ) ;
  103.                 contentText = r.toString() ;
  104.             }
  105.  
  106.             var index=contentText.indexOf("/*<![CDATA");
  107.             contentText=contentText.substr(0,index);            
  108.  
  109.             // Compose title if there is none
  110.             contentTextArray = contentText.split(\' \') ;
  111.             var x=0;
  112.             for(x=0; (x<5 && x<contentTextArray.length); x++)
  113.             {
  114.                 if(x < 4)
  115.                 {
  116.                     bestandsnaamNieuw += contentTextArray[x] + \' \';
  117.                 }
  118.                 else
  119.                 {
  120.                     bestandsnaamNieuw += contentTextArray[x];
  121.                 }
  122.             }
  123.     
  124.             if(document.getElementById(\'title_edited\').value == "false")
  125.             {
  126.                 document.getElementById(\'filename\').value = bestandsnaamNieuw;
  127.                 if(use_document_title){
  128.                     document.getElementById(\'title\').value = bestandsnaamNieuw;
  129.                 }
  130.             }
  131.         }
  132.         temp=true;
  133.     }
  134.  
  135.     function trim(s)
  136.     {
  137.      while(s.substring(0,1) == \' \') {
  138.       s = s.substring(1,s.length);
  139.      }
  140.      while(s.substring(s.length-1,s.length) == \' \') {
  141.       s = s.substring(0,s.length-1);
  142.      }
  143.      return s;
  144.     }
  145.  
  146.     function check_if_still_empty()
  147.     {
  148.         if(trim(document.getElementById(\'filename\').value) != "")
  149.         {
  150.             document.getElementById(\'title_edited\').value = "true";
  151.         }
  152.     }
  153.  
  154. </script>';
  155.  
  156. include (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  157. include (api_get_path(LIBRARY_PATH).'document.lib.php');
  158. include (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  159. include (api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  160. include (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  161. $nameTools get_lang('CreateDocument');
  162.  
  163. $fck_attribute['Width''100%';
  164. $fck_attribute['Height''950';
  165. $fck_attribute['ToolbarSet''Full';
  166. $fck_attribute['Config']['FullPage'true;
  167.  
  168. /*
  169. -----------------------------------------------------------
  170.     Constants and variables
  171. -----------------------------------------------------------
  172. */
  173. $dir = isset($_GET['dir']$_GET['dir'$_POST['dir']// please do not modify this dirname formatting
  174.  
  175. /*
  176. ==============================================================================
  177.         MAIN CODE
  178. ==============================================================================
  179. */
  180. if (strstr($dir'..'))
  181. {
  182.     $dir '/';
  183. }
  184.  
  185. if ($dir[0== '.')
  186. {
  187.     $dir substr($dir1);
  188. }
  189.  
  190. if ($dir[0!= '/')
  191. {
  192.     $dir '/'.$dir;
  193. }
  194.  
  195. if ($dir[strlen($dir1!= '/')
  196. {
  197.     $dir .= '/';
  198. }
  199.  
  200. $filepath api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$dir;
  201.  
  202. if (!is_dir($filepath))
  203. {
  204.     $filepath api_get_path('SYS_COURSE_PATH').$_course['path'].'/document/';
  205.  
  206.     $dir '/';
  207. }
  208.  
  209. /**************************************************/
  210. $to_group_id 0;
  211.  
  212. if (isset ($_SESSION['_gid']&& $_SESSION['_gid'!= '')
  213. {
  214.     $req_gid '&amp;gidReq='.$_SESSION['_gid'];
  215.     $interbreadcrumb[array ("url" => "../group/group_space.php?gidReq=".$_SESSION['_gid']"name" => get_lang('GroupSpace'));
  216.     $noPHP_SELF true;
  217.     $to_group_id $_SESSION['_gid'];
  218.     $group GroupManager :: get_group_properties($to_group_id);
  219.     $path explode('/'$dir);
  220.     if ('/'.$path[1!= $group['directory'])
  221.     {
  222.         api_not_allowed(true);
  223.     }
  224.     
  225. }
  226. $interbreadcrumb[array ("url" => "./document.php?curdirpath=".urlencode($_GET['dir']).$req_gid"name" => get_lang('Documents'));
  227.  
  228. if (!$is_allowed_in_course)
  229.     api_not_allowed(true);
  230.  
  231. $is_allowedToEdit api_is_allowed_to_edit();
  232. if (!($is_allowedToEdit || $_SESSION['group_member_with_upload_rights']))
  233. {
  234.     api_not_allowed(true);
  235. }
  236. /*
  237. -----------------------------------------------------------
  238.     Header
  239. -----------------------------------------------------------
  240. */
  241.  
  242. $display_dir $dir;
  243. if (isset ($group))
  244. {
  245.     $display_dir explode('/'$dir);
  246.     unset ($display_dir[0]);
  247.     unset ($display_dir[1]);
  248.     $display_dir implode('/'$display_dir);
  249. }
  250.  
  251. // Create a new form
  252. $form new FormValidator('create_document');
  253. // Hidden element with current directory
  254. $form->addElement('hidden''dir');
  255. $default['dir'$dir;
  256. // Filename
  257.  
  258. $form->addElement('hidden','title_edited','false','id="title_edited"');
  259.  
  260. /**
  261.  * Check if a document width the choosen filename allready exists
  262.  */
  263. function document_exists($filename)
  264. {
  265.     global $filepath;
  266.     $filename replace_dangerous_char($filename);
  267.     return !file_exists($filepath.$filename.'.html');
  268. }
  269. // Change the default renderer for the filename-field to display the dir and extension
  270. $renderer $form->defaultRenderer();
  271. //$filename_template = str_replace('{element}', "<tt>$display_dir</tt> {element} <tt>.html</tt>", $renderer->_elementTemplate);
  272. $filename_template str_replace('{element}'"{element}"$renderer->_elementTemplate);
  273. $renderer->setElementTemplate($filename_template'filename');
  274.  
  275. // initialize group array
  276. $group array();
  277.     
  278. // If allowed, add element for document title
  279. if (api_get_setting('use_document_title'== 'true')
  280. {    
  281.     //$group[]= $form->add_textfield('title', get_lang('Title'),true,'class="input_titles" id="title"');
  282.     // replace the     add_textfield with this    
  283.     $group[]=$form->createElement('text','title',get_lang('Title'),'class="input_titles" id="title"');
  284.     //$form->applyFilter('title','trim');        
  285.     //$form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');        
  286. }
  287. else
  288. {        
  289.     //$form->add_textfield('filename', get_lang('FileName'),true,'class="input_titles" id="filename"  onblur="check_if_still_empty()"');
  290.     // replace the     add_textfield with this 
  291.     $group[]=$form->createElement('text','filename',get_lang('FileName'),'class="input_titles" id="filename"  onblur="check_if_still_empty()"');
  292.     //$form->applyFilter('filename','trim');        
  293.     //$form->addRule('filename', get_lang('ThisFieldIsRequired'), 'required');                
  294.     //$form->addRule('filename', get_lang('FileExists'), 'callback', 'document_exists');
  295. }
  296.  
  297. /* Show read-only box only in groups */
  298. if(!empty($_SESSION['_gid']))
  299. {
  300.     //$renderer->setElementTemplate('<div class="row"><div class="label"></div><div class="formw">{element}{label}</div></div>', 'readonly');
  301.     $group[]$form->createElement('checkbox','readonly','',get_lang('ReadOnly'));
  302. }
  303. // add group to the form
  304. $form->addGroup($group'filename_group'get_lang('FileName','&nbsp;&nbsp;&nbsp;'false);
  305. $form->addRule('filename_group'get_lang('ThisFieldIsRequired')'required');
  306.  
  307. if (api_get_setting('use_document_title'== 'true')
  308. {            
  309.     $form->addGroupRule('filename_group'array(
  310.       'title' => array(
  311.         array(get_lang('ThisFieldIsRequired')'required'),
  312.         array(get_lang('FileExists'),'callback''document_exists')
  313.         )
  314.     ));
  315. }
  316. else
  317. {
  318.     $form->addGroupRule('filename_group'array(
  319.       'filename' => array(
  320.         array(get_lang('ThisFieldIsRequired')'required'),
  321.         array(get_lang('FileExists'),'callback''document_exists')
  322.         )
  323.     ));
  324. }
  325.  
  326. $form->addElement('submit''submit'get_lang('Ok'));
  327.  
  328. // HTML-editor
  329. $form->add_html_editor('content',''falsefalse);
  330. // Comment-field
  331.  
  332. //$form->addElement('textarea', 'comment', get_lang('Comment'), array ('rows' => 5, 'cols' => 50));
  333. $form->addElement('submit''submit'get_lang('Ok'));
  334. $form->setDefaults($default);
  335.  
  336. // HTML
  337. $form->addElement('html','<div id="frmModel" style="display:block; height:950px;width:20%; position:absolute; top:135px; left:1px;"></div>');
  338.  
  339. // If form validates -> save the new document
  340. if ($form->validate())
  341. {
  342.     $values $form->exportValues();
  343.     $readonly = isset($values['readonly']0;
  344.     
  345.     $values['title']=addslashes(trim($values['title']));
  346.  
  347.     $clean_val=$values['filename']
  348.  
  349.     $clean_val=replace_dangerous_char(($clean_val));
  350.     $clean_val=disable_dangerous_file($clean_val);
  351.     $clean_val=replace_accents($clean_val);
  352.  
  353.     
  354.     $values['filename']=$clean_val;
  355.     
  356.     if (api_get_setting('use_document_title'!= 'true')
  357.     {
  358.         $values['title'$values['filename'];
  359.     }
  360.     else
  361.     {
  362.         $values['filename'$values['title'];    
  363.     }
  364.     
  365.     $filename replace_accents($values['filename']);
  366.     $texte $values['content'];
  367.     $title $values['filename'];
  368.     $extension 'html';
  369.     if (!strstr($texte'/css/frames.css'))
  370.     {
  371.         $texte str_replace('</head>''<link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>'$texte);
  372.     }
  373.     if ($fp fopen($filepath.$filename.'.'.$extension'w'))
  374.     {
  375.         $texte text_filter($texte);
  376.         
  377.         $content str_replace(api_get_path('WEB_COURSE_PATH')$_configuration['url_append'].'/courses/'$texte);
  378.         
  379.         // replace fake by flv player if needed    (fake is present only in templates)    
  380.         $content str_replace('<img src="'.api_get_path(REL_PATH).'main/inc/lib/fckeditor/editor/css/images/flv.gif?flv=',
  381.                                '<object type="application/x-shockwave-flash" data="'.api_get_path(REL_PATH).'main/inc/lib/flv_player/player_flv_mini.swf" height="240" width="320">
  382.                                       <param name="movie" value="'.api_get_path(REL_PATH).'main/inc/lib/flv_player/player_flv_mini.swf" />
  383.                                       <param name="FlashVars" value="flv=',$content);
  384.         
  385.         $content str_replace('&amp;endflv" alt="" />','&autoplay=1" /></object><style type="text/css">body{}</style>',$content);
  386.  
  387.         $texte str_replace('mp3player.swf?son='.urlencode($path_to_remove)'mp3player.swf?son=.%2F'$texte);
  388.         
  389.         
  390.         fputs($fp$content);
  391.  
  392.         fclose($fp);
  393.         
  394.         $files_perm api_get_setting('permissions_for_new_files');
  395.         $files_perm octdec(!empty($files_perm)?$files_perm:'0770');
  396.         chmod($filepath.$filename.'.'.$extension,$files_perm);
  397.  
  398.         $perm api_get_setting('permissions_for_new_directories');
  399.         $perm octdec(!empty($perm)?$perm:'0770');
  400.         if (!is_dir($filepath.'css'))
  401.         {
  402.             mkdir($filepath.'css');
  403.             chmod($filepath.'css'$perm);
  404.             $doc_id add_document($_course$dir.'css''folder'0'css');
  405.  
  406.             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'FolderCreated'$_user['user_id']);
  407.             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'invisible'$_user['user_id']);
  408.         }
  409.  
  410.         if (!is_file($filepath.'css/frames.css'))
  411.         {
  412.             //make a copy of the current css for the new document            
  413.             
  414.             copy(api_get_path(SYS_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css'$filepath.'css/frames.css');
  415.             
  416.             $doc_id add_document($_course$dir.'css/frames.css''file'filesize($filepath.'css/frames.css')'frames.css');
  417.  
  418.             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'DocumentAdded'$_user['user_id']);
  419.             api_item_property_update($_courseTOOL_DOCUMENT$doc_id'invisible'$_user['user_id']);
  420.         }
  421.  
  422.         $file_size filesize($filepath.$filename.'.'.$extension);
  423.         $save_file_path $dir.$filename.'.'.$extension;
  424.  
  425.         $document_id add_document($_course$save_file_path'file'$file_size$filename,null,$readonly);
  426.         if ($document_id)
  427.         {
  428.             api_item_property_update($_courseTOOL_DOCUMENT$document_id'DocumentAdded'$_user['user_id']$to_group_id);
  429.  
  430.             //update parent folders
  431.             item_property_update_on_folder($_course$_GET['dir']$_user['user_id']);
  432.  
  433.             $new_comment = isset ($_POST['comment']trim($_POST['comment']'';
  434.             $new_title = isset ($_POST['title']trim($_POST['title']'';
  435.             if ($new_comment || $new_title)
  436.             {
  437.                 $TABLE_DOCUMENT Database::get_course_table(TABLE_DOCUMENT);
  438.                 $ct '';
  439.                 if ($new_comment)
  440.                     $ct .= ", comment='$new_comment'";
  441.                 if ($new_title)
  442.                     $ct .= ", title='$new_title'";
  443.                 api_sql_query("UPDATE $TABLE_DOCUMENT SET".substr($ct1)." WHERE id = '$document_id'"__FILE____LINE__);
  444.             }
  445.             $dirsubstr($dir,0,-1);
  446.             header('Location: document.php?curdirpath='.urlencode($dir));
  447.             exit ();
  448.         }
  449.     }
  450.     else
  451.     {
  452.         Display :: display_header($nameTools"Doc");
  453.         //api_display_tool_title($nameTools);
  454.         Display :: display_error_message(get_lang('Impossible'));
  455.         Display :: display_footer();
  456.     }
  457. }
  458. else
  459. {
  460.     Display :: display_header($nameTools"Doc");
  461.     //api_display_tool_title($nameTools);
  462.     $form->display();
  463.     Display :: display_footer();
  464. }
  465. ?>

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