fileDisplay.lib.php 7.1 KB

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