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

Source for file document.inc.php

Documentation is available at document.inc.php

  1. <?php // $Id: document.inc.php 15525 2008-06-09 06:24:20Z yannoo $
  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) various contributors
  10.  
  11.     For a full list of contributors, see "credits.txt".
  12.     The full license can be read in "license.txt".
  13.  
  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.  
  19.     See the GNU General Public License for more details.
  20.  
  21.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  22.     Mail: info@dokeos.com
  23. ==============================================================================
  24. */
  25.  
  26. /*
  27. ==============================================================================
  28.         EXTRA FUNCTIONS FOR DOCUMENT.PHP/UPLOAD.PHP
  29. ==============================================================================
  30. /////////////////////////////////////////////////
  31. //--> leave these here or move them elsewhere? //
  32. /////////////////////////////////////////////////
  33. */
  34.  
  35.  
  36. /**
  37.  * Builds the form thats enables the user to
  38.  * select a directory to browse/upload in
  39.  *
  40.  * @param array     An array containing the folders we want to be able to select
  41.  * @param string    The current folder (path inside of the "document" directory, including the prefix "/")
  42.  * @param string    Group directory, if empty, prevents documents to be uploaded (because group documents cannot be uploaded in root)
  43.  * @param    boolean    Whether to change the renderer (this will add a template <span> to the QuickForm object displaying the form)
  44.  * @return string html form
  45.  */
  46. function build_directory_selector($folders,$curdirpath,$group_dir='',$changeRenderer=false)
  47. {
  48.     $folder_titles array();
  49.     if(get_setting('use_document_title'== 'true')
  50.     {
  51.         if (is_array($folders))
  52.         {
  53.             $escaped_folders array();
  54.             foreach($folders as $key=>$val){$escaped_folders[$keyDatabase::escape_string($val);}
  55.             $folder_sql implode("','",$escaped_folders);
  56.             $doc_table Database::get_course_table(TABLE_DOCUMENT);
  57.             $sql "SELECT * FROM $doc_table WHERE filetype='folder' AND path IN ('".$folder_sql."')";
  58.             $res api_sql_query($sql,__FILE__,__LINE__);
  59.             $folder_titles array();
  60.             while($obj Database::fetch_object($res))
  61.             {
  62.                 $folder_titles[$obj->path$obj->title;    
  63.             }
  64.         }
  65.     }
  66.     else
  67.     {
  68.         foreach($folders as $folder)
  69.         {
  70.             $folder_titles[$folderbasename($folder);    
  71.         }    
  72.     }
  73.     
  74.     require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  75.     $form new FormValidator('selector','POST',api_get_self());
  76.     
  77.     $parent_select $form->addElement('select''curdirpath'get_lang('CurrentDirectory'),'','onchange="javascript:document.selector.submit()"');
  78.     
  79.     if($changeRenderer==true){
  80.         $renderer $form->defaultRenderer();
  81.         $renderer->setElementTemplate('<span>{label} : {element}</span> ','curdirpath');
  82.     }
  83.     
  84.     //group documents cannot be uploaded in the root
  85.     if($group_dir=='')
  86.     {
  87.         $parent_select -> addOption(get_lang('HomeDirectory'),'/');
  88.         if(is_array($folders))
  89.         {
  90.             foreach ($folders as $folder)
  91.             {
  92.                 $selected ($curdirpath==$folder)?' selected="selected"':'';
  93.                 $path_parts explode('/',$folder);
  94.                 $label str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$folder_titles[$folder];
  95.                 $parent_select -> addOption($label,$folder);
  96.                 if($selected!=''$parent_select->setSelected($folder);
  97.             }
  98.         }
  99.     }
  100.     else
  101.     {
  102.         foreach ($folders as $folder)
  103.         {
  104.             $selected ($curdirpath==$folder)?' selected="selected"':'';
  105.             $label $folder_titles[$folder];
  106.             if$folder == $group_dir)
  107.             {
  108.                 $label '/ ('.get_lang('HomeDirectory').')';
  109.             }
  110.             else
  111.             {
  112.                 $path_parts explode('/',str_replace($group_dir,'',$folder));
  113.                 $label str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$label;            
  114.             }
  115.             $parent_select -> addOption($label,$folder);
  116.             if($selected!=''$parent_select->setSelected($folder);
  117.         }
  118.     }
  119.     
  120.     $form=$form->toHtml();
  121.  
  122.     return $form;
  123. }
  124.  
  125.  
  126. {
  127.     $message "<a href=\"quota.php?".api_get_cidreq()."\">".get_lang("ShowCourseQuotaUse")."</a>";
  128.     echo     /*"<div id=\"smallmessagebox\">"
  129.             .*/ "<p>" $message "</p>"
  130.             /*. "</div>"*/;
  131. }
  132.  
  133. /**
  134.  * Create a html hyperlink depending on if it's a folder or a file
  135.  *
  136.  * @param string $www 
  137.  * @param string $title 
  138.  * @param string $path 
  139.  * @param string $filetype (file/folder)
  140.  * @param int $visibility (1/0)
  141.  * @return string url
  142.  */
  143. function create_document_link($www,$title,$path,$filetype,$size,$visibility)
  144. {
  145.     global $dbl_click_id;
  146.     if(isset($_SESSION['_gid']))
  147.     {
  148.         $req_gid '&amp;gidReq='.$_SESSION['_gid'];
  149.     }
  150.     else 
  151.     {
  152.         $req_gid '';
  153.     }
  154.     $url_path urlencode($path);
  155.     //add class="invisible" on invisible files
  156.     $visibility_class($visibility==0)?' class="invisible"':'';
  157.     //build download link (icon)
  158.     $forcedownload_link=($filetype=='folder')?api_get_self().'?'.api_get_cidreq().'&action=downloadfolder&amp;path='.$url_path.$req_gid:api_get_self().'?'.api_get_cidreq().'&amp;action=download&amp;id='.$url_path.$req_gid;
  159.     //folder download or file download?
  160.     $forcedownload_icon=($filetype=='folder')?'folder_zip.gif':'filesave.gif';
  161.     //prevent multiple clicks on zipped folder download
  162.     $prevent_multiple_click =($filetype=='folder')?" onclick=\"javascript:if(typeof clic_$dbl_click_id == 'undefined' || clic_$dbl_click_id == false) { clic_$dbl_click_id=true; window.setTimeout('clic_".($dbl_click_id++)."=false;',10000); } else { return false; }\"":'';
  163.     $target='_top';
  164.     if($filetype=='file'{
  165.         //check the extension
  166.         $ext=explode('.',$path);
  167.         $ext=strtolower($ext[sizeof($ext)-1]);
  168.         //"htmlfiles" are shown in a frameset
  169.         if($ext == 'htm' || $ext == 'html' || $ext == 'gif' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'png')
  170.         {
  171.             $url "showinframes.php?".api_get_cidreq()."&amp;file=".$url_path.$req_gid;
  172.         }
  173.         else 
  174.         {
  175.             //url-encode for problematic characters (we may not call them dangerous characters...)
  176.             $path str_replace('%2F''/',$url_path).'?'.api_get_cidreq();
  177.             $url=$www.$path;
  178.         }
  179.         //files that we want opened in a new window
  180.         if($ext=='txt'//add here
  181.         {
  182.             $target='_blank';
  183.         }
  184.     }
  185.     else 
  186.     {
  187.         $url=api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$url_path.$req_gid;
  188.     }
  189.     //the little download icon
  190.     $force_download_html ($size==0)?'':'<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'><img width="16" height="16" src="'.api_get_path(WEB_CODE_PATH).'img/'.$forcedownload_icon.'" alt="" /></a>';
  191.     
  192.     $tooltip_title str_replace('?cidReq='.$_GET['cidReq'],'',basename($path));
  193.     return '<a href="'.$url.'" title="'.$tooltip_title.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.$title.'</a>'.$force_download_html;
  194. }
  195.  
  196. /**
  197.  * Builds an img html tag for the filetype
  198.  *
  199.  * @param string $type (file/folder)
  200.  * @param string $path 
  201.  * @return string img html tag
  202.  */
  203. function build_document_icon_tag($type,$path)
  204. {
  205.     $icon='folder_document.gif';
  206.     if($type=='file')
  207.     {
  208.         $icon=choose_image(basename($path));
  209.     }
  210.     return '<img src="'.api_get_path(WEB_CODE_PATH).'img/'.$icon.'" border="0" hspace="5" align="middle" alt="" />';
  211. }
  212.  
  213. /**
  214.  * Creates the row of edit icons for a file/folder
  215.  *
  216.  * @param string $curdirpath current path (cfr open folder)
  217.  * @param string $type (file/folder)
  218.  * @param string $path dbase path of file/folder
  219.  * @param int $visibility (1/0)
  220.  * @param int $id dbase id of the document
  221.  * @return string html img tags with hyperlinks
  222.  */
  223. function build_edit_icons($curdirpath,$type,$path,$visibility,$id,$is_template,$is_read_only=0)
  224. {
  225.     if(isset($_SESSION['_gid']))
  226.     {
  227.         $req_gid '&amp;gidReq='.$_SESSION['_gid'];
  228.     }
  229.     else 
  230.     {
  231.         $req_gid '';
  232.     }
  233.     //build URL-parameters for table-sorting
  234.     $sort_params array();
  235.     ifisset($_GET['column']))
  236.     {
  237.         $sort_params['column='.$_GET['column'];
  238.     }
  239.     ifisset($_GET['page_nr']))
  240.     {
  241.         $sort_params['page_nr='.$_GET['page_nr'];
  242.     }
  243.     ifisset($_GET['per_page']))
  244.     {
  245.         $sort_params['per_page='.$_GET['per_page'];
  246.     }
  247.     ifisset($_GET['direction']))
  248.     {
  249.         $sort_params['direction='.$_GET['direction'];
  250.     }    
  251.     $sort_params implode('&amp;',$sort_params);
  252.     $visibility_icon ($visibility==0)?'invisible':'visible';
  253.     $visibility_command ($visibility==0)?'set_visible':'set_invisible';
  254.     $curdirpath urlencode($curdirpath);
  255.     
  256.     $modify_icons '';
  257.         
  258.     if ($is_read_only)
  259.     {
  260.         $modify_icons '<img src="../img/edit_na.gif" border="0" title="'.get_lang('Modify').'" alt="" />';        
  261.         $modify_icons .= '&nbsp;<img src="../img/delete_na.gif" border="0" title="'.get_lang('Delete').'" alt="" />';
  262.         $modify_icons .= '&nbsp;<img src="../img/deplacer_fichier_na.gif" border="0" title="'.get_lang('Move').'" alt="" />';
  263.         $modify_icons .= '&nbsp;<img src="../img/'.$visibility_icon.'_na.gif" border="0" title="'.get_lang('Visible').'" alt="" />';
  264.     }
  265.     else
  266.     {
  267.         $modify_icons '<a href="edit_document.php?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;file='.urlencode($path).$req_gid.'"><img src="../img/edit.gif" border="0" title="'.get_lang('Modify').'" alt="" /></a>';
  268.         $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;delete='.urlencode($path).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename($path).'\');"><img src="../img/delete.gif" border="0" title="'.get_lang('Delete').'" alt="" /></a>';
  269.         $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;move='.urlencode($path).$req_gid.'"><img src="../img/deplacer_fichier.gif" border="0" title="'.get_lang('Move').'" alt="" /></a>';
  270.         $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;'.$visibility_command.'='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/'.$visibility_icon.'.gif" border="0" title="'.get_lang('Visible').'" alt="" /></a>';
  271.     }    
  272.     
  273.     if($type == 'file' && pathinfo($path,PATHINFO_EXTENSION)=='html')
  274.     {
  275.         if($is_template==0)
  276.         {
  277.             $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;add_as_template='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/wizard_small.gif" border="0" title="'.get_lang('AddAsTemplate').'" alt="'.get_lang('AddAsTemplate').'" /></a>';
  278.         }
  279.         else{
  280.             $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;remove_as_template='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/wizard_gray_small.gif" border="0" title="'.get_lang('RemoveAsTemplate').'" alt=""'.get_lang('RemoveAsTemplate').'" /></a>';
  281.         }
  282.     }    
  283.     return $modify_icons;
  284. }
  285.  
  286.  
  287. function build_move_to_selector($folders,$curdirpath,$move_file,$group_dir='')
  288. {
  289.     $form '<form name="move_to" action="'.api_get_self().'" method="post">'."\n";
  290.     $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />'."\n";
  291.     $form .= get_lang('MoveTo').' <select name="move_to">'."\n";
  292.     
  293.     //group documents cannot be uploaded in the root
  294.     if($group_dir==''
  295.     {
  296.         if($curdirpath!='/')
  297.         {
  298.             $form .= '<option value="/">/ ('.get_lang('HomeDirectory').')</option>';
  299.         }
  300.         if(is_array($folders))
  301.         {
  302.             foreach ($folders AS $folder)
  303.             {    
  304.                 //you cannot move a file to:
  305.                 //1. current directory
  306.                 //2. inside the folder you want to move
  307.                 //3. inside a subfolder of the folder you want to move
  308.                 if(($curdirpath!=$folder&& ($folder!=$move_file&& (substr($folder,0,strlen($move_file)+1!= $move_file.'/'))
  309.                 {
  310.                     $path_displayed $folder;
  311.                     
  312.                     // if document title is used, we have to display titles instead of real paths...
  313.                     if(api_get_setting('use_document_title'))
  314.                     {
  315.                         $path_displayed get_titles_of_path($folder);
  316.                     }
  317.                     $form .= '<option value="'.$folder.'">'.$path_displayed.'</option>'."\n";
  318.                 }
  319.             }
  320.         }
  321.     }
  322.     else
  323.     {
  324.         foreach ($folders AS $folder)
  325.         {    
  326.             if(($curdirpath!=$folder&& ($folder!=$move_file&& (substr($folder,0,strlen($move_file)+1!= $move_file.'/'))//cannot copy dir into his own subdir
  327.             {
  328.                 if(api_get_setting('use_document_title'))
  329.                 {
  330.                     $path_displayed get_titles_of_path($folder);
  331.                 }
  332.                 
  333.                 $display_folder substr($path_displayed,strlen($group_dir));
  334.                 $display_folder ($display_folder == '')?'/ ('.get_lang('HomeDirectory').')':$display_folder;
  335.                                 
  336.                 $form .= '<option value="'.$folder.'">'.$display_folder.'</option>'."\n";
  337.             }
  338.         }
  339.     }
  340.  
  341.     $form .= '</select>'."\n";
  342.     $form .= '<input type="submit" name="move_file_submit" value="'.get_lang('Ok').'" />'."\n";
  343.     $form .= '</form>';
  344.  
  345.     return $form;
  346. }
  347.  
  348.  
  349. /**
  350.  * get the path translated with title of docs and folders
  351.  * @param string the real path
  352.  * @return the path which should be displayed
  353.  */
  354. function get_titles_of_path($path)
  355. {
  356.     global $tmp_folders_titles;
  357.  
  358.     $nb_slashes substr_count($path,'/');
  359.     $tmp_path '';
  360.     $current_slash_pos 0;
  361.     $path_displayed '';
  362.     for($i=0$i<$nb_slashes$i++)
  363.     // foreach folders of the path, retrieve title.
  364.     
  365.         $current_slash_pos strpos($path,'/',$current_slash_pos+1);
  366.         $tmp_path substr($path,strpos($path,'/',0),$current_slash_pos);
  367.         
  368.         if(empty($tmp_path)) // if empty, then we are in the final part of the path
  369.             $tmp_path $path;
  370.             
  371.         if(!empty($tmp_folders_titles[$tmp_path])) // if this path has soon been stored here we don't need a new query
  372.         {
  373.             $path_displayed .= $tmp_folders_titles[$tmp_path];
  374.         }
  375.         else
  376.         {
  377.             $sql 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).' WHERE path LIKE BINARY "'.$tmp_path.'"';
  378.             $rs api_sql_query($sql,__FILE__,__LINE__);
  379.             $tmp_title '/'.Database::result($rs,0,0);
  380.             $path_displayed .= $tmp_title;
  381.             $tmp_folders_titles[$tmp_path$tmp_title;
  382.         }
  383.     }
  384.     return $path_displayed;
  385. }
  386. ?>

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