general_util.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * @return string $icon String containing icon that can be echo'ed
  77. * @author Steve Brown
  78. * @since 1.5.2
  79. */
  80. function getIcon($icon_theme_path, $icon_name, $text_icon, $alt_text='', $w=NULL, $h=NULL) {
  81. $icon = '';
  82. if (is_null($icon_theme_path)) {
  83. $icon = $text_icon;
  84. } else {
  85. $icon_path = getIconPath($icon_theme_path, $icon_name);
  86. // If we found an icon, build an img tag to display it. If we didn't
  87. // find an image, we will revert back to the text icon.
  88. if (!is_null($icon_path)) {
  89. global $oTemplate;
  90. $oTemplate->assign('src', $icon_path);
  91. $oTemplate->assign('alt', $alt_text);
  92. $oTemplate->assign('title', $alt_text);
  93. $oTemplate->assign('width', $w);
  94. $oTemplate->assign('height', $h);
  95. // blank other attributes because the template
  96. // object might already contain values due to
  97. // having been used to show another image before
  98. // this one
  99. //
  100. $oTemplate->assign('onclick', '');
  101. $oTemplate->assign('align', '');
  102. $oTemplate->assign('border', '');
  103. $oTemplate->assign('hspace', '');
  104. $oTemplate->assign('vspace', '');
  105. $icon = $oTemplate->fetch('image.tpl');
  106. } else {
  107. $icon = $text_icon;
  108. }
  109. }
  110. return $icon;
  111. }
  112. /**
  113. * Gets the path to the specified icon or returns NULL if the image is not
  114. * found. This has been separated from getIcon to allow the path to be fetched
  115. * for use w/ third party packages, e.g. dTree.
  116. *
  117. * @param string $icon_theme_path User's chosen icon set
  118. * @param string $icon_name File name of the desired icon
  119. * @return string $icon String containing path to icon that can be used in
  120. * an IMG tag, or NULL if the image is not found.
  121. * @author Steve Brown
  122. * @since 1.5.2
  123. */
  124. function getIconPath ($icon_theme_path, $icon_name) {
  125. global $fallback_icon_theme_path;
  126. if (is_null($icon_theme_path))
  127. return NULL;
  128. // Desired icon exists in the current theme?
  129. if (is_file($icon_theme_path . $icon_name)) {
  130. return $icon_theme_path . $icon_name;
  131. // Icon not found, check for the admin-specified fallback
  132. } elseif (!is_null($fallback_icon_theme_path) && is_file($fallback_icon_theme_path . $icon_name)) {
  133. return $fallback_icon_theme_path . $icon_name;
  134. // Icon not found, return the SQM default icon
  135. } elseif (is_file(SM_PATH . 'images/themes/default/'.$icon_name)) {
  136. return SM_PATH . 'images/themes/default/'.$icon_name;
  137. }
  138. return NULL;
  139. }
  140. /**
  141. * Display error messages for use in footer.tpl
  142. *
  143. * @author Steve Brown
  144. * @since 1.5.2
  145. **/
  146. function displayErrors () {
  147. global $oErrorHandler;
  148. if ($oErrorHandler) {
  149. $oErrorHandler->displayErrors();
  150. }
  151. }
  152. /**
  153. * Make the internal show_readable_size() function available to templates.
  154. //FIXME: I think this is needless since there is no reason templates cannot just call directly to show_readable_size
  155. *
  156. * @param int size to be converted to human-readable
  157. * @return string human-readable form
  158. * @since 1.5.2
  159. **/
  160. function humanReadableSize ($size) {
  161. return show_readable_size($size);
  162. }