Refactor image template code
[squirrelmail.git] / functions / html.php
CommitLineData
9cd67a0a 1<?php
2
3/**
8008456a 4 * html.php
9cd67a0a 5 *
9cd67a0a 6 * The idea is to inlcude here some functions to make easier
7 * the right to left implementation by "functionize" some
8 * html outputs.
9 *
47ccfad4 10 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
792f911e 14 * @since 1.3.0
9cd67a0a 15 */
16
769a819d 17
18/**
19 * Generates a hyperlink
20 *
21 * @param string $uri The target link location
22 * @param string $text The link text
23 * @param string $target The location where the link should
24 * be opened (OPTIONAL; default not used)
25 * @param string $onclick The onClick JavaScript handler (OPTIONAL;
26 * default not used)
27 * @param string $class The CSS class name (OPTIONAL; default
28 * not used)
29 * @param string $id The ID name (OPTIONAL; default not used)
30 *
31 * @return string The desired hyperlink tag.
32 *
33 * @since 1.5.2
34 *
35 */
36function create_hyperlink($uri, $text, $target='', $onclick='', $class='', $id='') {
37
38 global $oTemplate;
39
40 $oTemplate->assign('uri', $uri);
41 $oTemplate->assign('text', $text);
42 $oTemplate->assign('target', $target);
43 $oTemplate->assign('onclick', $onclick);
44 $oTemplate->assign('class', $class);
45 $oTemplate->assign('id', $id);
46
47 return $oTemplate->fetch('hyperlink.tpl');
48
49}
50
51
d6c32258 52/**
0173ad29 53 * Generates an image tag
54 *
55 * @param string $src The image source path
56 * @param string $alt Alternate link text (OPTIONAL; default
57 * not used)
58 * @param string $width The width the image should be shown in
59 * (OPTIONAL; default not used)
60 * @param string $height The height the image should be shown in
61 * (OPTIONAL; default not used)
62 * @param string $border The image's border attribute value
63 * (OPTIONAL; default not used)
64 * @param string $class The CSS class name (OPTIONAL; default
65 * not used)
66 * @param string $id The ID name (OPTIONAL; default not used)
67 * @param string $onclick The onClick JavaScript handler (OPTIONAL;
68 * default not used)
69 * @param string $title The image's title attribute value
70 * (OPTIONAL; default not used)
71 * @param string $align The image's alignment attribute value
72 * (OPTIONAL; default not used)
73 * @param string $hspace The image's hspace attribute value
74 * (OPTIONAL; default not used)
75 * @param string $vspace The image's vspace attribute value
76 * (OPTIONAL; default not used)
77 *
78 * @return string The desired hyperlink tag.
79 *
80 * @since 1.5.2
81 *
82 */
83function create_image($src, $alt='', $width='', $height='',
84 $border='', $class='', $id='', $onclick='',
85 $title='', $align='', $hspace='', $vspace='') {
86
87 global $oTemplate;
88
89 $oTemplate->assign('src', $src);
90 $oTemplate->assign('alt', $alt);
91 $oTemplate->assign('width', $width);
92 $oTemplate->assign('height', $height);
93 $oTemplate->assign('border', $border);
94 $oTemplate->assign('class', $class);
95 $oTemplate->assign('id', $id);
96 $oTemplate->assign('onclick', $onclick);
97 $oTemplate->assign('title', $title);
98 $oTemplate->assign('align', $align);
99 $oTemplate->assign('hspace', $hspace);
100 $oTemplate->assign('vspace', $vspace);
101
102 return $oTemplate->fetch('image.tpl');
103
104}
105
106
107/**
792f911e 108 * Generates html tags
d6c32258 109 *
110 * @param string $tag Tag to output
111 * @param string $val Value between tags
112 * @param string $align Alignment (left, center, etc)
113 * @param string $bgcolor Back color in hexadecimal
114 * @param string $xtra Extra options
115 * @return string HTML ready for output
792f911e 116 * @since 1.3.0
d6c32258 117 */
118function html_tag( $tag, // Tag to output
d68323ff 119 $val = '', // Value between tags
8008456a 120 $align = '', // Alignment
121 $bgcolor = '', // Back color
122 $xtra = '' ) { // Extra options
892b98c9 123
792f911e 124 GLOBAL $languages, $squirrelmail_language;
892b98c9 125
792f911e 126 $align = strtolower( $align );
127 $bgc = '';
128 $tag = strtolower( $tag );
892b98c9 129
792f911e 130 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
131 $dir = $languages[$squirrelmail_language]['DIR'];
132 } else {
133 $dir = 'ltr';
134 }
892b98c9 135
792f911e 136 if ( $dir == 'ltr' ) {
137 $rgt = 'right';
138 $lft = 'left';
139 } else {
140 $rgt = 'left';
141 $lft = 'right';
142 }
892b98c9 143
792f911e 144 if ( $bgcolor <> '' ) {
145 $bgc = " bgcolor=\"$bgcolor\"";
146 }
892b98c9 147
792f911e 148 switch ( $align ) {
149 case '':
150 $alg = '';
151 break;
152 case 'right':
153 $alg = " align=\"$rgt\"";
154 break;
155 case 'left':
156 $alg = " align=\"$lft\"";
157 break;
158 default:
159 $alg = " align=\"$align\"";
160 break;
161 }
892b98c9 162
792f911e 163 $ret = "<$tag";
892b98c9 164
792f911e 165 if ( $dir <> 'ltr' ) {
166 $ret .= " dir=\"$dir\"";
167 }
168 $ret .= $bgc . $alg;
892b98c9 169
792f911e 170 if ( $xtra <> '' ) {
171 $ret .= " $xtra";
172 }
892b98c9 173
792f911e 174 if ( $val <> '' ) {
175 $ret .= ">$val</$tag>\n";
176 } else {
177 $ret .= '>'. "\n";
94ac35c6 178 }
179
792f911e 180 return( $ret );
181}
892b98c9 182
769a819d 183
792f911e 184/**
185 * handy function to set url vars
186 *
187 * especially useful when $url = $PHP_SELF
188 * @param string $url url that must be modified
189 * @param string $var variable name
190 * @param string $val variable value
191 * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
192 * @return string $url modified url
193 * @since 1.3.0
194 */
195function set_url_var($url, $var, $val=0, $link=true) {
196 $k = '';
197 $pat_a = array (
198 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
199 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
200 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
201 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
202 );
4f113ce5 203 $url = str_replace('&amp;','&',$url);
792f911e 204
205 // FIXME: why switch is used instead of if () or one preg_match()
206 switch (true) {
207 case (preg_match($pat_a[0],$url,$regs)):
208 $k = $regs[1];
209 $v = $regs[2];
210 break;
211 case (preg_match($pat_a[1],$url,$regs)):
212 $k = $regs[1];
213 $v = $regs[2];
214 break;
215 case (preg_match($pat_a[2],$url,$regs)):
216 $k = $regs[1];
217 $v = $regs[2];
218 break;
219 case (preg_match($pat_a[3],$url,$regs)):
220 $k = $regs[1];
221 $v = $regs[2];
222 break;
223 default:
224 if ($val) {
225 if (strpos($url,'?')) {
226 $url .= "&$var=$val";
892b98c9 227 } else {
792f911e 228 $url .= "?$var=$val";
892b98c9 229 }
792f911e 230 }
231 break;
232 }
233
234 if ($k) {
235 if ($val) {
236 $rpl = "$k=$val";
792f911e 237 } else {
238 $rpl = '';
239 }
240 if( substr($v,-1)=='&' ) {
241 $rpl .= '&';
02725e0b 242 }
792f911e 243 $pat = "/$k=$v/";
244 $url = preg_replace($pat,$rpl,$url);
892b98c9 245 }
4f113ce5 246 if ($link) {
247 $url = str_replace('&','&amp;',$url);
248 }
792f911e 249 return $url;
250}
769a819d 251
252