fileDisplay.lib.php 8.8 KB

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