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