PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/smarty_tiki/modifier.iconify.php

https://gitlab.com/ElvisAns/tiki
PHP | 159 lines | 130 code | 4 blank | 25 comment | 28 complexity | 69f32e22b63d6528e829e72fa049eab7 MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /**
  8. * Smarty plugin
  9. * -------------------------------------------------------------
  10. * Type: modifier
  11. * Name: iconify
  12. * Purpose: Returns a filetype icon or file type
  13. * -------------------------------------------------------------
  14. *
  15. * @param $string File name with extension
  16. * @param null $filetype File type
  17. * @param null $fileId File id when using file galleries
  18. * @param int $size Icon size
  19. * @param string $return icon or filtype
  20. * @return null|string
  21. * @throws Exception
  22. * @throws SmartyException
  23. */
  24. function smarty_modifier_iconify($string, $filetype = null, $fileId = null, $size = 1, $return = 'icon')
  25. {
  26. $smarty = TikiLib::lib('smarty');
  27. global $prefs;
  28. $smarty->loadPlugin('smarty_function_icon');
  29. $icon = '';
  30. $ext = strtolower(substr($string, strrpos($string, '.') + 1));
  31. if ($fileId && substr($filetype, 0, 6) == 'image/') {
  32. // Special handling for file gallery images,
  33. // display thumbnail
  34. $smarty->loadPlugin('smarty_modifier_sefurl');
  35. $smarty->loadPlugin('smarty_modifier_escape');
  36. $icon = smarty_modifier_sefurl($fileId, 'thumbnail');
  37. $icon = smarty_modifier_escape($icon);
  38. $width = 16 * $size;
  39. return "<img src=\"$icon\" width=\"$width\"/>";
  40. } else {
  41. include_once('lib/mime/mimetypes.php');
  42. global $mimetypes;
  43. $mimes = array_keys($mimetypes, $filetype);
  44. if ($prefs['theme_iconset'] === 'legacy') {
  45. if (file_exists("img/icons/mime/$ext.png")) {
  46. $icon = $ext;
  47. } elseif (file_exists('img/icons/mime/' . substr($ext, 0, 3) . '.png')) {
  48. $icon = substr($ext, 0, 3);
  49. } else {
  50. foreach ($mimes as $m) {
  51. if (file_exists("img/icons/mime/$m.png")) {
  52. $icon = $m;
  53. }
  54. }
  55. if (empty($icon)) {
  56. $icon = 'default';
  57. }
  58. }
  59. if ($return === 'filetype') {
  60. return $m;
  61. } elseif ($return === 'icon') {
  62. return smarty_function_icon(
  63. [
  64. '_id' => 'img/icons/mime/' . $icon . '.png',
  65. 'alt' => ( $filetype === null ? $icon : $filetype ),
  66. 'class' => '',
  67. 'size' => $size
  68. ],
  69. $smarty->getEmptyInternalTemplate()
  70. );
  71. }
  72. //iconsets introduced with Tiki14
  73. } else {
  74. if (! empty($filetype)) {
  75. $type = $filetype;
  76. } elseif (! empty($mimetypes[$ext])) {
  77. $type = $mimetypes[$ext];
  78. } else {
  79. $type = 'file';
  80. }
  81. switch ($type) {
  82. case $type === 'application/msword'
  83. || $type === 'application/vnd.ms-word'
  84. || strpos($type, 'application/vnd.openxmlformats-officedocument.wordprocessingml') === 0:
  85. $iconname = 'word';
  86. break;
  87. case $type === 'application/pdf':
  88. $iconname = 'pdf';
  89. break;
  90. case $type === 'application/vnd.ms-excel'
  91. || $type === 'application/ms-excel'
  92. || $type === 'application/msexcel'
  93. || strpos($type, 'application/vnd.openxmlformats-officedocument.spreadsheetml') === 0
  94. || $type === 'application/vnd.oasis.opendocument.spreadsheet':
  95. $iconname = 'excel';
  96. break;
  97. case $type === 'application/vnd.ms-powerpoint'
  98. || $type === 'application/ms-powerpoint'
  99. || $type === 'application/mspowerpoint'
  100. || strpos($type, 'application/vnd.openxmlformats-officedocument.presentationml') === 0:
  101. $iconname = 'powerpoint';
  102. break;
  103. case strpos($type, 'audio/') === 0:
  104. $iconname = 'audio';
  105. break;
  106. case strpos($type, 'image/') === 0:
  107. $iconname = 'image';
  108. break;
  109. case strpos($type, 'text/') === 0:
  110. switch ($ext) {
  111. case 'c':
  112. case 'cpp':
  113. case 'css':
  114. case 'htm':
  115. case 'html':
  116. case 'java':
  117. case 'js':
  118. case 'less':
  119. case 'php':
  120. case 'py':
  121. case 'scss':
  122. $iconname = 'code_file';
  123. break;
  124. case 'eml':
  125. $iconname = 'envelope';
  126. break;
  127. default:
  128. $iconname = 'textfile';
  129. }
  130. break;
  131. case $type === 'application/vnd.oasis.opendocument.text':
  132. $iconname = 'textfile';
  133. break;
  134. case strpos($type, 'video/') === 0:
  135. $iconname = 'video';
  136. break;
  137. case strpos($type, 'application/') === 0 && strpos($type, 'zip') !== false:
  138. $iconname = 'zip';
  139. break;
  140. case strpos($type, 'application/') === 0 &&
  141. (strpos($type, 'mail') !== false || $type === 'application/CDFV2-corrupt'): // outlook mail
  142. $iconname = 'envelope';
  143. break;
  144. default:
  145. $iconname = 'file';
  146. break;
  147. }
  148. if ($return === 'filetype') {
  149. return $type;
  150. } else {
  151. return smarty_function_icon(['name' => $iconname, 'size' => $size], $smarty->getEmptyInternalTemplate());
  152. }
  153. }
  154. }
  155. }