fileDisplay.lib.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 chamilo.library
  8. */
  9. /* FILE DISPLAY FUNCTIONS */
  10. /**
  11. * Define the image to display for each file extension.
  12. * This needs an existing image repository to work.
  13. *
  14. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  15. * @param - $file_name (string) - Name of a file
  16. * @return - The gif image to chose
  17. */
  18. function choose_image($file_name)
  19. {
  20. static $type, $image;
  21. /* TABLES INITIALISATION */
  22. if (!$type || !$image)
  23. {
  24. $type['word' ] = array('doc', 'dot', 'rtf', 'mcw', 'wps', 'psw', 'docm', 'docx', 'dotm', 'dotx');
  25. $type['web' ] = array('htm', 'html', 'htx', 'xml', 'xsl', 'php', 'xhtml');
  26. $type['image' ] = array('gif', 'jpg', 'png', 'bmp', 'jpeg', 'tif', 'tiff');
  27. $type['image_vect'] = array('svg','svgz');
  28. $type['audio' ] = array('wav', 'mid', 'mp2', 'mp3', 'midi', 'sib', 'amr', 'kar', 'oga','au','wma');
  29. $type['video' ] = array('mp4', 'mov', 'rm', 'pls', 'mpg', 'mpeg', 'm2v', 'm4v', 'flv', 'f4v', 'avi', 'wmv', 'asf', '3gp','ogv','ogg','ogx','webm');
  30. $type['excel' ] = array('xls', 'xlt', 'xls', 'xlt', 'pxl', 'xlsx', 'xlsm', 'xlam', 'xlsb', 'xltm', 'xltx');
  31. $type['compressed'] = array('zip', 'tar', 'rar', 'gz');
  32. $type['code' ] = array('js', 'cpp', 'c', 'java', 'phps', 'jsp', 'asp', 'aspx', 'cfm');
  33. $type['acrobat' ] = array('pdf');
  34. $type['powerpoint'] = array('ppt', 'pps', 'pptm', 'pptx', 'potm', 'potx', 'ppam', 'ppsm', 'ppsx');
  35. $type['flash' ] = array('fla', 'swf');
  36. $type['text' ] = array('txt','log');
  37. $type['oo_writer' ] = array('odt', 'ott', 'sxw', 'stw');
  38. $type['oo_calc' ] = array('ods', 'ots', 'sxc', 'stc');
  39. $type['oo_impress'] = array('odp', 'otp', 'sxi', 'sti');
  40. $type['oo_draw' ] = array('odg', 'otg', 'sxd', 'std');
  41. $type['epub' ] = array('epub');
  42. $type['java' ] = array('class','jar');
  43. $type['freemind' ] = array('mm');
  44. $image['word' ] = 'word.gif';
  45. $image['web' ] = 'file_html.gif';
  46. $image['image' ] = 'file_image.gif';
  47. $image['image_vect'] = 'file_svg.png';
  48. $image['audio' ] = 'file_sound.gif';
  49. $image['video' ] = 'film.gif';
  50. $image['excel' ] = 'excel.gif';
  51. $image['compressed'] = 'file_zip.gif';
  52. $image['code' ] = 'icons/22/mime_code.png';
  53. $image['acrobat' ] = 'file_pdf.gif';
  54. $image['powerpoint'] = 'powerpoint.gif';
  55. $image['flash' ] = 'file_flash.gif';
  56. $image['text' ] = 'icons/22/mime_text.png';
  57. $image['oo_writer' ] = 'file_oo_writer.gif';
  58. $image['oo_calc' ] = 'file_oo_calc.gif';
  59. $image['oo_impress'] = 'file_oo_impress.gif';
  60. $image['oo_draw' ] = 'file_oo_draw.gif';
  61. $image['epub' ] = 'file_epub.gif';
  62. $image['java' ] = 'file_java.png';
  63. $image['freemind' ] = 'file_freemind.png';
  64. }
  65. /* FUNCTION CORE */
  66. $extension = array();
  67. if (!is_array($file_name)) {
  68. if (preg_match('/\.([[:alnum:]]+)(\?|$)/', $file_name, $extension)) {
  69. $extension[1] = strtolower($extension[1]);
  70. foreach ($type as $generic_type => $extension_list)
  71. {
  72. if (in_array($extension[1], $extension_list))
  73. {
  74. return $image[$generic_type];
  75. }
  76. }
  77. }
  78. }
  79. return 'defaut.gif';
  80. }
  81. /**
  82. * Transform a UNIX time stamp in human readable format date.
  83. *
  84. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  85. * @param - $date - UNIX time stamp
  86. * @return - A human readable representation of the UNIX date
  87. */
  88. function format_date($date)
  89. {
  90. return date('d.m.Y', $date);
  91. }
  92. /**
  93. * Transform the file path to a URL.
  94. *
  95. * @param - $file_path (string) - Relative local path of the file on the hard disk
  96. * @return - Relative url
  97. */
  98. function format_url($file_path)
  99. {
  100. $path_component = explode('/', $file_path);
  101. $path_component = array_map('rawurlencode', $path_component);
  102. return implode('/', $path_component);
  103. }
  104. /**
  105. * Get the most recent time the content of a folder was changed.
  106. *
  107. * @param - $dir_name (string) - Path of the dir on the hard disk
  108. * @param - $do_recursive (bool) - Traverse all folders in the folder?
  109. * @return - Time the content of the folder was changed
  110. */
  111. function recent_modified_file_time($dir_name, $do_recursive = true)
  112. {
  113. $dir = dir($dir_name);
  114. $last_modified = 0;
  115. $return = 0;
  116. if (is_dir($dir)) {
  117. while(($entry = $dir->read()) !== false)
  118. {
  119. if ($entry != '.' && $entry != '..')
  120. continue;
  121. if (!is_dir($dir_name.'/'.$entry))
  122. $current_modified = filemtime($dir_name.'/'.$entry);
  123. elseif ($do_recursive)
  124. $current_modified = recent_modified_file_time($dir_name.'/'.$entry, true);
  125. if ($current_modified > $last_modified)
  126. $last_modified = $current_modified;
  127. }
  128. $dir->close();
  129. //prevents returning 0 (for empty directories)
  130. $return = ($last_modified == 0) ? filemtime($dir_name) : $last_modified;
  131. }
  132. return $return;
  133. }
  134. /**
  135. * Get the total size of a directory.
  136. *
  137. * @param - $dir_name (string) - Path of the dir on the hard disk
  138. * @return - Total size in bytes
  139. */
  140. function folder_size($dir_name)
  141. {
  142. $size = 0;
  143. if ($dir_handle = opendir($dir_name)) {
  144. while (($entry = readdir($dir_handle)) !== false) {
  145. if ($entry == '.' || $entry == '..') {
  146. continue;
  147. }
  148. if (is_dir($dir_name.'/'.$entry)) {
  149. $size += folder_size($dir_name.'/'.$entry);
  150. } else {
  151. $size += filesize($dir_name.'/'.$entry);
  152. }
  153. }
  154. closedir($dir_handle);
  155. }
  156. return $size;
  157. }
  158. /**
  159. * Calculates the total size of a directory by adding the sizes (that
  160. * are stored in the database) of all files & folders in this directory.
  161. *
  162. * @param string $path
  163. * @param boolean $can_see_invisible
  164. * @return int Total size
  165. */
  166. function get_total_folder_size($path, $can_see_invisible = false)
  167. {
  168. $table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  169. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  170. $tool_document = TOOL_DOCUMENT;
  171. $course_id = api_get_course_int_id();
  172. $session_id = api_get_session_id();
  173. $session_condition = api_get_session_condition(
  174. $session_id,
  175. true,
  176. true,
  177. 'props.session_id'
  178. );
  179. $visibility_rule = ' props.visibility ' . ($can_see_invisible ? '<> 2' : '= 1');
  180. $sql = "SELECT SUM(table1.size) FROM (
  181. SELECT size
  182. FROM $table_itemproperty AS props, $table_document AS docs
  183. WHERE
  184. docs.c_id = $course_id AND
  185. docs.id = props.ref AND
  186. docs.path LIKE '$path/%' AND
  187. props.c_id = $course_id AND
  188. props.tool = '$tool_document' AND
  189. $visibility_rule
  190. $session_condition
  191. GROUP BY ref
  192. ) as table1";
  193. $result = Database::query($sql);
  194. if ($result && Database::num_rows($result) != 0) {
  195. $row = Database::fetch_row($result);
  196. return $row[0] == null ? 0 : $row[0];
  197. } else {
  198. return 0;
  199. }
  200. }