fileDisplay.lib.php 7.4 KB

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