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