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

Source for file fileDisplay.lib.php

Documentation is available at fileDisplay.lib.php

  1. <?php
  2. /*
  3. vim: set expandtab tabstop=4 shiftwidth=4:
  4. ============================================================================== 
  5.     Dokeos - elearning and course management software
  6.     
  7.     Copyright (c) 2004 Dokeos S.A.
  8.     Copyright (c) 2003 Ghent University (UGent)
  9.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  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: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  22. ============================================================================== 
  23. */
  24. /**
  25. ============================================================================== 
  26. *    This is the file display library for Dokeos.
  27. *    Include/require it in your code to use its functionality.
  28. *
  29. *    @package dokeos.library
  30. ============================================================================== 
  31. */
  32.  
  33.  
  34. /*
  35. ============================================================================== 
  36.         GENERIC FUNCTIONS : FOR OLDER PHP VERSIONS
  37. ============================================================================== 
  38. */
  39. if function_exists('array_search') )
  40. {
  41.     /**
  42.      * Searches haystack for needle and returns the key
  43.      * if it is found in the array, FALSE otherwise.
  44.      *
  45.      * Natively implemented in PHP since 4.0.5 version.
  46.      * This function is intended for previous version.
  47.      *
  48.      * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  49.      * @param  needle (mixed)
  50.      * @param  haystack (array)
  51.      * @return array key or FALSE
  52.      *
  53.      * @see    - http://www.php.net/array_search
  54.      */
  55.     function array_search($needle$haystack)
  56.     {
  57.         while (list($key$valeach($haystack))
  58.             if ($val == $needle)
  59.                 return $key;
  60.         return false;
  61.     }
  62. }
  63.  
  64. /*
  65. ============================================================================== 
  66.         FILE DISPLAY FUNCTIONS
  67. ============================================================================== 
  68. */ 
  69. /**
  70.  * Define the image to display for each file extension.
  71.  * This needs an existing image repository to work.
  72.  *
  73.  * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  74.  * @param  $file_name (string) - Name of a file
  75.  * @return The gif image to chose
  76.  */
  77. function choose_image($file_name)
  78. {
  79.     static $type$image;
  80.  
  81.     /* TABLES INITIALISATION */
  82.     if (!$type || !$image)
  83.     {
  84.         $type['word'      array('doc''dot',  'rtf''mcw',  'wps');
  85.         $type['web'       array('htm''html''htx''xml',  'xsl',  'php');
  86.         $type['image'     array('gif''jpg',  'png''bmp',  'jpeg');
  87.         $type['audio'     array('wav''mid',  'mp2''mp3',  'midi''sib');
  88.         $type['video'     array('mp4''mov',  'rm',  'pls',  'mpg',  'mpeg''au');
  89.         $type['excel'     array('xls''xlt',  'xls''xlt');
  90.         $type['compressed'array('zip''tar',  'rar''gz');
  91.         $type['code'      array('js',  'cpp',  'c',   'java''phps');
  92.         $type['acrobat'   array('pdf');
  93.         $type['powerpoint'array('ppt');
  94.         $type['flash'     array('fla''swf');
  95.         $type['text'      array('txt','log');
  96.         $type['oo_writer' array('odt','sxw');
  97.         $type['oo_calc'   array('ods','sxc');
  98.         $type['oo_impress'array('odp','sxi');
  99.         $type['oo_draw'   array('odg','sxd');
  100.  
  101.         $image['word'      'word.gif';
  102.         $image['web'       'file_html.gif';
  103.         $image['image'     'file_image.gif';
  104.         $image['audio'     'file_sound.gif';
  105.         $image['video'     'film.gif';
  106.         $image['excel'     'excel.gif';
  107.         $image['compressed''file_zip.gif';
  108.         $image['code'      'file_txt.gif';
  109.         $image['acrobat'   'file_pdf.gif';
  110.         $image['powerpoint''powerpoint.gif';
  111.         $image['flash'     'file_flash.gif';
  112.         $image['text'      'file_txt.gif';
  113.         $image['oo_writer' 'word.gif';
  114.         $image['oo_calc'   'excel.gif';
  115.         $image['oo_impress''powerpoint.gif';
  116.         $image['oo_draw'   'file_oo_draw.gif';
  117.     }
  118.  
  119.     /* FUNCTION CORE */
  120.     $extension array();
  121.     if (ereg('\.([[:alnum:]]+)$'$file_name$extension))
  122.     {
  123.         $extension[1strtolower($extension[1]);
  124.  
  125.         foreach ($type as $generic_type => $extension_list)
  126.         {
  127.             if (in_array($extension[1]$extension_list))
  128.             {
  129.                 return $image[$generic_type];
  130.             }
  131.         }
  132.     }
  133.  
  134.     return 'defaut.gif';
  135. }
  136.  
  137. /**
  138.  * Transform the file size in a human readable format.
  139.  *
  140.  * @param  $file_size (int) - Size of the file in bytes
  141.  * @return A human readable representation of the file size
  142.  */
  143. function format_file_size($file_size)
  144. {
  145.     if($file_size >= 1073741824)
  146.     {
  147.         $file_size round($file_size 1073741824 100100 'G';
  148.     }
  149.     elseif($file_size >= 1048576)
  150.     {
  151.         $file_size round($file_size 1048576 100100 'M';
  152.     }
  153.     elseif($file_size >= 1024)
  154.     {
  155.         $file_size round($file_size 1024 100100 'k';
  156.     }
  157.     else
  158.     {
  159.         $file_size $file_size 'B';
  160.     }
  161.  
  162.     return $file_size;
  163. }
  164.  
  165. /**
  166.  * Transform a UNIX time stamp in human readable format date.
  167.  *
  168.  * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  169.  * @param  $date - UNIX time stamp
  170.  * @return A human readable representation of the UNIX date
  171.  */
  172. function format_date($date)
  173. {
  174.     return date('d.m.Y'$date);
  175. }
  176.  
  177. /**
  178.  * Transform the file path to a URL.
  179.  *
  180.  * @param  $file_path (string) - Relative local path of the file on the hard disk
  181.  * @return Relative url
  182.  */
  183. function format_url($file_path)
  184. {
  185.     $path_component explode('/'$file_path);
  186.  
  187.     $path_component array_map('rawurlencode'$path_component);
  188.  
  189.     return implode('/'$path_component);
  190. }
  191.  
  192. /**
  193.  * Get the most recent time the content of a folder was changed.
  194.  *
  195.  * @param  $dir_name (string)   - Path of the dir on the hard disk
  196.  * @param  $do_recursive (bool) - Traverse all folders in the folder?
  197.  * @return Time the content of the folder was changed
  198.  */
  199. function recent_modified_file_time($dir_name$do_recursive true)
  200. {
  201.     $dir dir($dir_name);
  202.     $last_modified 0;
  203.  
  204.     while(($entry $dir->read()) !== false)
  205.     {
  206.         if ($entry != '.' && $entry != '..')
  207.             continue;
  208.  
  209.         if (!is_dir($dir_name.'/'.$entry))
  210.             $current_modified filemtime($dir_name.'/'.$entry);
  211.         elseif ($do_recursive)
  212.             $current_modified recent_modified_file_time($dir_name.'/'.$entrytrue);
  213.  
  214.         if ($current_modified $last_modified)
  215.             $last_modified $current_modified;
  216.     }
  217.  
  218.     $dir->close();
  219.  
  220.     //prevents returning 0 (for empty directories)
  221.     return ($last_modified == 0filemtime($dir_name$last_modified;
  222. }
  223.  
  224. /**
  225.  * Get the total size of a directory.
  226.  *
  227.  * @param  $dir_name (string) - Path of the dir on the hard disk
  228.  * @return Total size in bytes
  229.  */
  230. function folder_size($dir_name)
  231. {
  232.     $size 0;
  233.  
  234.     if ($dir_handle opendir($dir_name))
  235.     {
  236.         while (($entry readdir($dir_handle)) !== false
  237.         {
  238.             if($entry == '.' || $entry == '..'
  239.                 continue;
  240.  
  241.             if(is_dir($dir_name.'/'.$entry))
  242.                 $size += folder_size($dir_name.'/'.$entry);
  243.             else
  244.                 $size += filesize($dir_name.'/'.$entry);
  245.         }
  246.         
  247.         closedir($dir_handle);
  248.     }
  249.  
  250.     return $size;
  251. }
  252.  
  253. /**
  254.  * Calculates the total size of a directory by adding the sizes (that
  255.  * are stored in the database) of all files & folders in this directory.
  256.  *
  257.  * @param     string  $path 
  258.  * @param     boolean $can_see_invisible 
  259.  * @return     Total size
  260.  */
  261. function get_total_folder_size($path$can_see_invisible false)
  262. {    
  263.     $table_itemproperty Database::get_course_table(TABLE_ITEM_PROPERTY);
  264.     $table_document Database::get_course_table(TABLE_DOCUMENT);
  265.     $tool_document TOOL_DOCUMENT;
  266.     
  267.     $visibility_rule 'props.visibility ' ($can_see_invisible '<> 2' '= 1');
  268.  
  269.     $sql = <<<EOQ
  270. SELECT SUM(size)
  271.     FROM $table_itemproperty AS props, $table_document AS docs
  272.     WHERE docs.id = props.ref
  273.         AND props.tool = '$tool_document'
  274.         AND path LIKE '$path/%'
  275.         AND $visibility_rule
  276. EOQ;
  277.  
  278.     $result api_sql_query($sql,__FILE__,__LINE__);
  279.         
  280.     if($result && mysql_num_rows($result!= 0)
  281.     {    
  282.         $row mysql_fetch_row($result);
  283.         return $row[0== null $row[0];
  284.     }
  285.     else
  286.     {
  287.         return 0;
  288.     
  289. }
  290. ?>

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