fileDisplay.lib.php 8.0 KB

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