general_util.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * general_util.php
  4. *
  5. * This file is intended to contain helper functions for template sets
  6. * that would like to use them.
  7. *
  8. * @copyright 1999-2025 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /**
  14. * Create stylesheet links that will work for multiple browsers
  15. *
  16. * @param string $uri The URI to the linked stylesheet.
  17. * @param string $name The title of the stylesheet (optional; default empty).
  18. * @param boolean $alt Whether or not this is an alternate
  19. * stylesheet (optional; default TRUE).
  20. * @param string $mtype The target media display type (optional; default "screen").
  21. *
  22. * @return string The full text of the stylesheet link.
  23. *
  24. */
  25. function create_css_link($uri, $name='', $alt=TRUE, $mtype='screen') {
  26. // FIXME: Add closing / to link and meta elements only after
  27. // switching to xhtml 1.0 Transitional.
  28. // It is not compatible with html 4.01 Transitional
  29. if (empty($uri)) {
  30. return '';
  31. }
  32. sqGetGlobalVar('HTTP_USER_AGENT', $browser_user_agent, SQ_SERVER);
  33. if (!empty($browser_user_agent)) {
  34. if (stristr($browser_user_agent, "msie 4")) {
  35. $browser = 'msie4';
  36. $dom_browser = false;
  37. $is_IE = true;
  38. } elseif (stristr($browser_user_agent, "msie")
  39. && stristr($browser_user_agent, 'opera') === FALSE) {
  40. $browser = 'msie';
  41. $dom_browser = true;
  42. $is_IE = true;
  43. }
  44. }
  45. if ((strpos($uri, '-ie')!== false) and !$is_IE) {
  46. //not IE, so don't render this sheet
  47. return;
  48. }
  49. if ( strpos($uri, 'print') !== false )
  50. $mtype = 'print';
  51. $href = 'href="'.$uri.'" ';
  52. $media = 'media="'.$mtype.'" ';
  53. if ( empty($name) ) {
  54. $title = '';
  55. $rel = 'rel="stylesheet" ';
  56. } else {
  57. $title = 'title="'.$name.'" ';
  58. $rel = 'rel="'.( $alt ? 'alternate ' : '' ).'stylesheet" ';
  59. }
  60. return '<link '.$media.$title.$rel.'type="text/css" '.$href." />\n";
  61. }
  62. /**
  63. * Checks for an image icon and returns a complete image tag or a text
  64. * string with the text icon based on what is found and user prefs.
  65. *
  66. * @param string $icon_theme_path User's chosen icon set
  67. * @param string $icon_name File name of the desired icon
  68. * @param string $text_icon Text-based icon to display if desired
  69. * @param string $alt_text Text for alt/title attribute of image
  70. * @param integer $w Optional. Width of requested image.
  71. * @param integer $h Optional. Height of requested image.
  72. *
  73. * @return string $icon String containing icon that can be echo'ed
  74. *
  75. * @author Steve Brown
  76. * @since 1.5.2
  77. */
  78. function getIcon($icon_theme_path, $icon_name, $text_icon, $alt_text, $w=NULL, $h=NULL) {
  79. $icon = '';
  80. if (is_null($icon_theme_path)) {
  81. $icon = $text_icon;
  82. } else {
  83. $icon_path = getIconPath($icon_theme_path, $icon_name);
  84. // If we found an icon, build an img tag to display it. If we didn't
  85. // find an image, we will revert back to the text icon.
  86. if (!is_null($icon_path)) {
  87. $icon = create_image($icon_path, $alt_text, $w, $h, '', '', '',
  88. '', $alt_text, '', '', '', $text_icon);
  89. } else {
  90. $icon = $text_icon;
  91. }
  92. }
  93. return $icon;
  94. }
  95. /**
  96. * Gets the path to the specified icon or returns NULL if the image is not
  97. * found. This has been separated from getIcon to allow the path to be fetched
  98. * for use w/ third party packages, e.g. dTree.
  99. *
  100. * @param string $icon_theme_path User's chosen icon set
  101. * @param string $icon_name File name of the desired icon
  102. *
  103. * @return string $icon String containing path to icon that can be used in
  104. * an IMG tag, or NULL if the image is not found.
  105. *
  106. * @author Steve Brown
  107. * @since 1.5.2
  108. *
  109. */
  110. function getIconPath ($icon_theme_path, $icon_name) {
  111. global $fallback_icon_theme_path;
  112. if (is_null($icon_theme_path))
  113. return NULL;
  114. // Desired icon exists in the current theme?
  115. //FIXME: this assumes a URI path will be valid when used as a filesystem path - this will fail for some systems -- OTOH, if $icon_theme_path is meant as an internal filesystem path to the user's chosen theme directory, then the assumption that is wrong here is that this internal filesystem path is always correct for use as part of a URI. This really should be mapped to/from an internal path to a URI path -- or can we guarantee that the two are always the same?
  116. if (is_file($icon_theme_path . $icon_name)) {
  117. return $icon_theme_path . $icon_name;
  118. // Icon not found, check for the admin-specified fallback
  119. //FIXME: same problem here as above
  120. } elseif (!is_null($fallback_icon_theme_path) && is_file($fallback_icon_theme_path . $icon_name)) {
  121. return $fallback_icon_theme_path . $icon_name;
  122. // Icon not found, return the SQM default icon
  123. //FIXME: same problem here -- SM_PATH is *NOT* intended for use in URIs
  124. } elseif (is_file(SM_PATH . 'images/themes/default/'.$icon_name)) {
  125. return SM_PATH . 'images/themes/default/'.$icon_name;
  126. }
  127. return NULL;
  128. }
  129. /**
  130. * Display error messages for use in footer.tpl
  131. *
  132. * @author Steve Brown
  133. * @since 1.5.2
  134. **/
  135. function displayErrors () {
  136. global $oErrorHandler;
  137. if ($oErrorHandler) {
  138. $oErrorHandler->displayErrors();
  139. }
  140. }
  141. /**
  142. * Make the internal show_readable_size() function available to templates.
  143. //FIXME: I think this is needless since there is no reason templates cannot just call directly to show_readable_size
  144. *
  145. * @param int size to be converted to human-readable
  146. * @param int filesize_divisor the divisor we'll use (OPTIONAL; default 1024)
  147. * @return string human-readable form
  148. * @since 1.5.2
  149. **/
  150. function humanReadableSize ($size, $filesize_divisor=1024) {
  151. return show_readable_size($size, $filesize_divisor);
  152. }