general_util.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 &copy; 1999-2006 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. * @param string $xhtml_end The XHTML-compliant close tag syntax to
  22. * use (optional; default "/")
  23. *
  24. * @return string The full text of the stylesheet link.
  25. *
  26. */
  27. function create_css_link($uri, $name='', $alt=TRUE, $mtype='screen', $xhtml_end='/') {
  28. // FIXME: Add closing / to link and meta elements only after
  29. // switching to xhtml 1.0 Transitional.
  30. // It is not compatible with html 4.01 Transitional
  31. $xhtml_end='';
  32. if (empty($uri)) {
  33. return '';
  34. }
  35. // set to lower case to avoid errors
  36. //
  37. sqGetGlobalVar('HTTP_USER_AGENT', $browser_user_agent, SQ_SERVER);
  38. $browser_user_agent = strtolower($browser_user_agent);
  39. if (stristr($browser_user_agent, "msie 4")) {
  40. $browser = 'msie4';
  41. $dom_browser = false;
  42. $is_IE = true;
  43. } elseif (stristr($browser_user_agent, "msie")
  44. && stristr($browser_user_agent, 'opera') === FALSE) {
  45. $browser = 'msie';
  46. $dom_browser = true;
  47. $is_IE = true;
  48. }
  49. if ((strpos($uri, '-ie')!== false) and !$is_IE) {
  50. //not IE, so don't render this sheet
  51. return;
  52. }
  53. if ( strpos($uri, 'print') !== false )
  54. $mtype = 'print';
  55. $href = 'href="'.$uri.'" ';
  56. $media = 'media="'.$mtype.'" ';
  57. if ( empty($name) ) {
  58. $title = '';
  59. $rel = 'rel="stylesheet" ';
  60. } else {
  61. $title = 'title="'.$name.'" ';
  62. $rel = 'rel="'.( $alt ? 'alternate ' : '' ).'stylesheet" ';
  63. }
  64. return '<link '.$media.$title.$rel.'type="text/css" '.$href." $xhtml_end>\n";
  65. }
  66. /**
  67. * Checks for an image icon and returns a complete image tag or a text
  68. * string with the text icon based on what is found and user prefs.
  69. *
  70. * @param string $icon_theme_path User's chosen icon set
  71. * @param string $icon_name File name of the desired icon
  72. * @param string $text_icon Text-based icon to display if desired
  73. * @param string $alt_text Optional. Text for alt/title attribute of image
  74. * @param integer $w Optional. Width of requested image.
  75. * @param integer $h Optional. Height of requested image.
  76. *
  77. * @return string $icon String containing icon that can be echo'ed
  78. *
  79. * @author Steve Brown
  80. * @since 1.5.2
  81. *
  82. */
  83. function getIcon($icon_theme_path, $icon_name, $text_icon, $alt_text='', $w=NULL, $h=NULL) {
  84. $icon = '';
  85. if (is_null($icon_theme_path)) {
  86. $icon = $text_icon;
  87. } else {
  88. $icon_path = getIconPath($icon_theme_path, $icon_name);
  89. // If we found an icon, build an img tag to display it. If we didn't
  90. // find an image, we will revert back to the text icon.
  91. if (!is_null($icon_path)) {
  92. $icon = create_image($icon_path, $alt_text, $w, $h, '', '', '',
  93. '', $alt_text, '', '', '', $text_icon);
  94. } else {
  95. $icon = $text_icon;
  96. }
  97. }
  98. return $icon;
  99. }
  100. /**
  101. * Gets the path to the specified icon or returns NULL if the image is not
  102. * found. This has been separated from getIcon to allow the path to be fetched
  103. * for use w/ third party packages, e.g. dTree.
  104. *
  105. * @param string $icon_theme_path User's chosen icon set
  106. * @param string $icon_name File name of the desired icon
  107. *
  108. * @return string $icon String containing path to icon that can be used in
  109. * an IMG tag, or NULL if the image is not found.
  110. *
  111. * @author Steve Brown
  112. * @since 1.5.2
  113. *
  114. */
  115. function getIconPath ($icon_theme_path, $icon_name) {
  116. global $fallback_icon_theme_path;
  117. if (is_null($icon_theme_path))
  118. return NULL;
  119. // Desired icon exists in the current theme?
  120. if (is_file($icon_theme_path . $icon_name)) {
  121. return $icon_theme_path . $icon_name;
  122. // Icon not found, check for the admin-specified fallback
  123. } elseif (!is_null($fallback_icon_theme_path) && is_file($fallback_icon_theme_path . $icon_name)) {
  124. return $fallback_icon_theme_path . $icon_name;
  125. // Icon not found, return the SQM default icon
  126. } elseif (is_file(SM_PATH . 'images/themes/default/'.$icon_name)) {
  127. return SM_PATH . 'images/themes/default/'.$icon_name;
  128. }
  129. return NULL;
  130. }
  131. /**
  132. * Display error messages for use in footer.tpl
  133. *
  134. * @author Steve Brown
  135. * @since 1.5.2
  136. **/
  137. function displayErrors () {
  138. global $oErrorHandler;
  139. if ($oErrorHandler) {
  140. $oErrorHandler->displayErrors();
  141. }
  142. }
  143. /**
  144. * Make the internal show_readable_size() function available to templates.
  145. //FIXME: I think this is needless since there is no reason templates cannot just call directly to show_readable_size
  146. *
  147. * @param int size to be converted to human-readable
  148. * @return string human-readable form
  149. * @since 1.5.2
  150. **/
  151. function humanReadableSize ($size) {
  152. return show_readable_size($size);
  153. }