fileDisplay.lib.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * @todo move this file to DocumentManager
  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 string $file_name (string) - Name of a file
  16. * @return string 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. $type['word'] = array(
  24. 'doc',
  25. 'dot',
  26. 'rtf',
  27. 'mcw',
  28. 'wps',
  29. 'psw',
  30. 'docm',
  31. 'docx',
  32. 'dotm',
  33. 'dotx',
  34. );
  35. $type['web'] = array(
  36. 'htm',
  37. 'html',
  38. 'htx',
  39. 'xml',
  40. 'xsl',
  41. 'php',
  42. 'xhtml',
  43. );
  44. $type['image'] = array(
  45. 'gif',
  46. 'jpg',
  47. 'png',
  48. 'bmp',
  49. 'jpeg',
  50. 'tif',
  51. 'tiff',
  52. );
  53. $type['image_vect'] = array('svg', 'svgz');
  54. $type['audio'] = array(
  55. 'wav',
  56. 'mid',
  57. 'mp2',
  58. 'mp3',
  59. 'midi',
  60. 'sib',
  61. 'amr',
  62. 'kar',
  63. 'oga',
  64. 'au',
  65. 'wma',
  66. );
  67. $type['video'] = array(
  68. 'mp4',
  69. 'mov',
  70. 'rm',
  71. 'pls',
  72. 'mpg',
  73. 'mpeg',
  74. 'm2v',
  75. 'm4v',
  76. 'flv',
  77. 'f4v',
  78. 'avi',
  79. 'wmv',
  80. 'asf',
  81. '3gp',
  82. 'ogv',
  83. 'ogg',
  84. 'ogx',
  85. 'webm',
  86. );
  87. $type['excel'] = array(
  88. 'xls',
  89. 'xlt',
  90. 'xls',
  91. 'xlt',
  92. 'pxl',
  93. 'xlsx',
  94. 'xlsm',
  95. 'xlam',
  96. 'xlsb',
  97. 'xltm',
  98. 'xltx',
  99. );
  100. $type['compressed'] = array('zip', 'tar', 'rar', 'gz');
  101. $type['code'] = array(
  102. 'js',
  103. 'cpp',
  104. 'c',
  105. 'java',
  106. 'phps',
  107. 'jsp',
  108. 'asp',
  109. 'aspx',
  110. 'cfm',
  111. );
  112. $type['acrobat'] = array('pdf');
  113. $type['powerpoint'] = array(
  114. 'ppt',
  115. 'pps',
  116. 'pptm',
  117. 'pptx',
  118. 'potm',
  119. 'potx',
  120. 'ppam',
  121. 'ppsm',
  122. 'ppsx',
  123. );
  124. $type['flash'] = array('fla', 'swf');
  125. $type['text'] = array('txt', 'log');
  126. $type['oo_writer'] = array('odt', 'ott', 'sxw', 'stw');
  127. $type['oo_calc'] = array('ods', 'ots', 'sxc', 'stc');
  128. $type['oo_impress'] = array('odp', 'otp', 'sxi', 'sti');
  129. $type['oo_draw'] = array('odg', 'otg', 'sxd', 'std');
  130. $type['epub'] = array('epub');
  131. $type['java'] = array('class', 'jar');
  132. $type['freemind'] = array('mm');
  133. $image['word'] = 'word.gif';
  134. $image['web'] = 'file_html.gif';
  135. $image['image'] = 'file_image.gif';
  136. $image['image_vect'] = 'file_svg.png';
  137. $image['audio'] = 'file_sound.gif';
  138. $image['video'] = 'film.gif';
  139. $image['excel'] = 'excel.gif';
  140. $image['compressed'] = 'file_zip.gif';
  141. $image['code'] = 'icons/22/mime_code.png';
  142. $image['acrobat'] = 'file_pdf.gif';
  143. $image['powerpoint'] = 'powerpoint.gif';
  144. $image['flash'] = 'file_flash.gif';
  145. $image['text'] = 'icons/22/mime_text.png';
  146. $image['oo_writer'] = 'file_oo_writer.gif';
  147. $image['oo_calc'] = 'file_oo_calc.gif';
  148. $image['oo_impress'] = 'file_oo_impress.gif';
  149. $image['oo_draw'] = 'file_oo_draw.gif';
  150. $image['epub'] = 'file_epub.gif';
  151. $image['java'] = 'file_java.png';
  152. $image['freemind'] = 'file_freemind.png';
  153. }
  154. $extension = array();
  155. if (!is_array($file_name)) {
  156. if (preg_match('/\.([[:alnum:]]+)(\?|$)/', $file_name, $extension)) {
  157. $extension[1] = strtolower($extension[1]);
  158. foreach ($type as $generic_type => $extension_list) {
  159. if (in_array($extension[1], $extension_list)) {
  160. return $image[$generic_type];
  161. }
  162. }
  163. }
  164. }
  165. return 'defaut.gif';
  166. }
  167. /**
  168. * Get the icon to display for a folder by its path
  169. * @param string $folderPath
  170. * @return string
  171. */
  172. function chooseFolderIcon($folderPath)
  173. {
  174. if ($folderPath == '/shared_folder') {
  175. return 'folder_users.gif';
  176. }
  177. if (strstr($folderPath, 'shared_folder_session_')) {
  178. return 'folder_users.gif';
  179. }
  180. switch ($folderPath) {
  181. case '/audio':
  182. return 'folder_audio.gif';
  183. case '/flash':
  184. return 'folder_flash.gif';
  185. case '/images':
  186. return 'folder_images.gif';
  187. case '/video':
  188. return 'folder_video.gif';
  189. case '/images/gallery':
  190. return 'folder_gallery.gif';
  191. case '/chat_files':
  192. return 'folder_chat.gif';
  193. case '/learning_path':
  194. return 'folder_learningpath.gif';
  195. }
  196. return 'folder_document.gif';
  197. }
  198. /**
  199. * Transform a UNIX time stamp in human readable format date.
  200. *
  201. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  202. * @param int $date - UNIX time stamp
  203. * @return string A human readable representation of the UNIX date
  204. */
  205. function format_date($date)
  206. {
  207. return date('d.m.Y', $date);
  208. }
  209. /**
  210. * Transform the file path to a URL.
  211. *
  212. * @param string $file_path (string) - Relative local path of the file on the hard disk
  213. * @return string Relative url
  214. */
  215. function format_url($file_path)
  216. {
  217. $path_component = explode('/', $file_path);
  218. $path_component = array_map('rawurlencode', $path_component);
  219. return implode('/', $path_component);
  220. }
  221. /**
  222. * Get the total size of a directory.
  223. *
  224. * @param string $dir_name (string) - Path of the dir on the hard disk
  225. * @return int Total size in bytes
  226. */
  227. function folder_size($dir_name)
  228. {
  229. $size = 0;
  230. if ($dir_handle = opendir($dir_name)) {
  231. while (($entry = readdir($dir_handle)) !== false) {
  232. if ($entry == '.' || $entry == '..') {
  233. continue;
  234. }
  235. if (is_dir($dir_name.'/'.$entry)) {
  236. $size += folder_size($dir_name.'/'.$entry);
  237. } else {
  238. $size += filesize($dir_name.'/'.$entry);
  239. }
  240. }
  241. closedir($dir_handle);
  242. }
  243. return $size;
  244. }
  245. /**
  246. * Calculates the total size of a directory by adding the sizes (that
  247. * are stored in the database) of all files & folders in this directory.
  248. *
  249. * @param string $path
  250. * @param boolean $can_see_invisible
  251. * @return int Total size
  252. */
  253. function get_total_folder_size($path, $can_see_invisible = false)
  254. {
  255. $table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  256. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  257. $tool_document = TOOL_DOCUMENT;
  258. $course_id = api_get_course_int_id();
  259. $session_id = api_get_session_id();
  260. $session_condition = api_get_session_condition(
  261. $session_id,
  262. true,
  263. true,
  264. 'props.session_id'
  265. );
  266. $visibility_rule = ' props.visibility '.($can_see_invisible ? '<> 2' : '= 1');
  267. $sql = "SELECT SUM(table1.size) FROM (
  268. SELECT props.ref, size
  269. FROM $table_itemproperty AS props
  270. INNER JOIN $table_document AS docs
  271. ON (docs.id = props.ref AND docs.c_id = props.c_id)
  272. WHERE
  273. docs.c_id = $course_id AND
  274. docs.path LIKE '$path/%' AND
  275. props.c_id = $course_id AND
  276. props.tool = '$tool_document' AND
  277. $visibility_rule
  278. $session_condition
  279. GROUP BY ref
  280. ) as table1";
  281. $result = Database::query($sql);
  282. if ($result && Database::num_rows($result) != 0) {
  283. $row = Database::fetch_row($result);
  284. return $row[0] == null ? 0 : $row[0];
  285. } else {
  286. return 0;
  287. }
  288. }