fileDisplay.lib.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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', 'psw', 'docm', 'docx', 'dotm', 'dotx');
  76. $type['web' ] = array('htm', 'html', 'htx', 'xml', 'xsl', 'php', 'xhtml');
  77. $type['image' ] = array('gif', 'jpg', 'png', 'bmp', 'jpeg');
  78. $type['audio' ] = array('wav', 'mid', 'mp2', 'mp3', 'midi', 'sib', 'amr', 'kar');
  79. $type['video' ] = array('mp4', 'mov', 'rm', 'pls', 'mpg', 'mpeg', 'au', 'flv', 'avi', 'wmv', 'asf', '3gp');
  80. $type['excel' ] = array('xls', 'xlt', 'xls', 'xlt', 'pxl', 'xlsx', 'xlsm', 'xlam', 'xlsb', 'xltm', 'xltx');
  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', 'pps', 'pptm', 'pptx', 'potm', 'potx', 'ppam', 'ppsm', 'ppsx');
  85. $type['flash' ] = array('fla', 'swf');
  86. $type['text' ] = array('txt','log');
  87. $type['oo_writer' ] = array('odt', 'ott', 'sxw', 'stw');
  88. $type['oo_calc' ] = array('ods', 'ots', 'sxc', 'stc');
  89. $type['oo_impress'] = array('odp', 'otp', 'sxi', 'sti');
  90. $type['oo_draw' ] = array('odg', 'otg', 'sxd', 'std');
  91. $image['word' ] = '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' ] = 'excel.gif';
  97. $image['compressed'] = 'file_zip.gif';
  98. $image['code' ] = 'file_txt.gif';
  99. $image['acrobat' ] = 'file_pdf.gif';
  100. $image['powerpoint'] = 'powerpoint.gif';
  101. $image['flash' ] = 'file_flash.gif';
  102. $image['text' ] = 'file_txt.gif';
  103. //$image['oo_writer' ] = 'word.gif';
  104. //$image['oo_calc' ] = 'excel.gif';
  105. //$image['oo_impress'] = 'powerpoint.gif';
  106. $image['oo_writer' ] = 'file_oo_writer.gif';
  107. $image['oo_calc' ] = 'file_oo_calc.gif';
  108. $image['oo_impress'] = 'file_oo_impress.gif';
  109. $image['oo_draw' ] = 'file_oo_draw.gif';
  110. }
  111. /* FUNCTION CORE */
  112. $extension = array();
  113. if (ereg('\.([[:alnum:]]+)$', $file_name, $extension))
  114. {
  115. $extension[1] = strtolower($extension[1]);
  116. foreach ($type as $generic_type => $extension_list)
  117. {
  118. if (in_array($extension[1], $extension_list))
  119. {
  120. return $image[$generic_type];
  121. }
  122. }
  123. }
  124. return 'defaut.gif';
  125. }
  126. /**
  127. * Transform the file size in a human readable format.
  128. *
  129. * @param int Size of the file in bytes
  130. * @return string A human readable representation of the file size
  131. */
  132. function format_file_size($file_size)
  133. {
  134. if($file_size >= 1073741824)
  135. {
  136. $file_size = round($file_size / 1073741824 * 100) / 100 . 'G';
  137. }
  138. elseif($file_size >= 1048576)
  139. {
  140. $file_size = round($file_size / 1048576 * 100) / 100 . 'M';
  141. }
  142. elseif($file_size >= 1024)
  143. {
  144. $file_size = round($file_size / 1024 * 100) / 100 . 'k';
  145. }
  146. else
  147. {
  148. $file_size = $file_size . 'B';
  149. }
  150. return $file_size;
  151. }
  152. /**
  153. * Transform a UNIX time stamp in human readable format date.
  154. *
  155. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  156. * @param - $date - UNIX time stamp
  157. * @return - A human readable representation of the UNIX date
  158. */
  159. function format_date($date)
  160. {
  161. return date('d.m.Y', $date);
  162. }
  163. /**
  164. * Transform the file path to a URL.
  165. *
  166. * @param - $file_path (string) - Relative local path of the file on the hard disk
  167. * @return - Relative url
  168. */
  169. function format_url($file_path)
  170. {
  171. $path_component = explode('/', $file_path);
  172. $path_component = array_map('rawurlencode', $path_component);
  173. return implode('/', $path_component);
  174. }
  175. /**
  176. * Get the most recent time the content of a folder was changed.
  177. *
  178. * @param - $dir_name (string) - Path of the dir on the hard disk
  179. * @param - $do_recursive (bool) - Traverse all folders in the folder?
  180. * @return - Time the content of the folder was changed
  181. */
  182. function recent_modified_file_time($dir_name, $do_recursive = true)
  183. {
  184. $dir = dir($dir_name);
  185. $last_modified = 0;
  186. while(($entry = $dir->read()) !== false)
  187. {
  188. if ($entry != '.' && $entry != '..')
  189. continue;
  190. if (!is_dir($dir_name.'/'.$entry))
  191. $current_modified = filemtime($dir_name.'/'.$entry);
  192. elseif ($do_recursive)
  193. $current_modified = recent_modified_file_time($dir_name.'/'.$entry, true);
  194. if ($current_modified > $last_modified)
  195. $last_modified = $current_modified;
  196. }
  197. $dir->close();
  198. //prevents returning 0 (for empty directories)
  199. return ($last_modified == 0) ? filemtime($dir_name) : $last_modified;
  200. }
  201. /**
  202. * Get the total size of a directory.
  203. *
  204. * @param - $dir_name (string) - Path of the dir on the hard disk
  205. * @return - Total size in bytes
  206. */
  207. function folder_size($dir_name)
  208. {
  209. $size = 0;
  210. if ($dir_handle = opendir($dir_name))
  211. {
  212. while (($entry = readdir($dir_handle)) !== false)
  213. {
  214. if($entry == '.' || $entry == '..')
  215. continue;
  216. if(is_dir($dir_name.'/'.$entry))
  217. $size += folder_size($dir_name.'/'.$entry);
  218. else
  219. $size += filesize($dir_name.'/'.$entry);
  220. }
  221. closedir($dir_handle);
  222. }
  223. return $size;
  224. }
  225. /**
  226. * Calculates the total size of a directory by adding the sizes (that
  227. * are stored in the database) of all files & folders in this directory.
  228. *
  229. * @param string $path
  230. * @param boolean $can_see_invisible
  231. * @return Total size
  232. */
  233. function get_total_folder_size($path, $can_see_invisible = false)
  234. {
  235. $table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  236. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  237. $tool_document = TOOL_DOCUMENT;
  238. $visibility_rule = 'props.visibility ' . ($can_see_invisible ? '<> 2' : '= 1');
  239. $sql = <<<EOQ
  240. SELECT SUM(size)
  241. FROM $table_itemproperty AS props, $table_document AS docs
  242. WHERE docs.id = props.ref
  243. AND props.tool = '$tool_document'
  244. AND path LIKE '$path/%'
  245. AND $visibility_rule
  246. EOQ;
  247. $result = Database::query($sql,__FILE__,__LINE__);
  248. if($result && mysql_num_rows($result) != 0)
  249. {
  250. $row = mysql_fetch_row($result);
  251. return $row[0] == null ? 0 : $row[0];
  252. }
  253. else
  254. {
  255. return 0;
  256. }
  257. }
  258. ?>