document html functions
[squirrelmail.git] / functions / html.php
1 <?php
2
3 /**
4 * html.php
5 *
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 *
10 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package squirrelmail
14 * @since 1.3.0
15 */
16
17 /**
18 * Generates html tags
19 *
20 * @param string $tag Tag to output
21 * @param string $val Value between tags
22 * @param string $align Alignment (left, center, etc)
23 * @param string $bgcolor Back color in hexadecimal
24 * @param string $xtra Extra options
25 * @return string HTML ready for output
26 * @since 1.3.0
27 */
28 function html_tag( $tag, // Tag to output
29 $val = '', // Value between tags
30 $align = '', // Alignment
31 $bgcolor = '', // Back color
32 $xtra = '' ) { // Extra options
33
34 GLOBAL $languages, $squirrelmail_language;
35
36 $align = strtolower( $align );
37 $bgc = '';
38 $tag = strtolower( $tag );
39
40 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
41 $dir = $languages[$squirrelmail_language]['DIR'];
42 } else {
43 $dir = 'ltr';
44 }
45
46 if ( $dir == 'ltr' ) {
47 $rgt = 'right';
48 $lft = 'left';
49 } else {
50 $rgt = 'left';
51 $lft = 'right';
52 }
53
54 if ( $bgcolor <> '' ) {
55 $bgc = " bgcolor=\"$bgcolor\"";
56 }
57
58 switch ( $align ) {
59 case '':
60 $alg = '';
61 break;
62 case 'right':
63 $alg = " align=\"$rgt\"";
64 break;
65 case 'left':
66 $alg = " align=\"$lft\"";
67 break;
68 default:
69 $alg = " align=\"$align\"";
70 break;
71 }
72
73 $ret = "<$tag";
74
75 if ( $dir <> 'ltr' ) {
76 $ret .= " dir=\"$dir\"";
77 }
78 $ret .= $bgc . $alg;
79
80 if ( $xtra <> '' ) {
81 $ret .= " $xtra";
82 }
83
84 if ( $val <> '' ) {
85 $ret .= ">$val</$tag>\n";
86 } else {
87 $ret .= '>'. "\n";
88 }
89
90 return( $ret );
91 }
92
93 /**
94 * handy function to set url vars
95 *
96 * especially useful when $url = $PHP_SELF
97 * @param string $url url that must be modified
98 * @param string $var variable name
99 * @param string $val variable value
100 * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
101 * @return string $url modified url
102 * @since 1.3.0
103 */
104 function set_url_var($url, $var, $val=0, $link=true) {
105 $k = '';
106 $pat_a = array (
107 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
108 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
109 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
110 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
111 );
112 preg_replace('/&amp;/','&',$url);
113
114 // FIXME: why switch is used instead of if () or one preg_match()
115 switch (true) {
116 case (preg_match($pat_a[0],$url,$regs)):
117 $k = $regs[1];
118 $v = $regs[2];
119 break;
120 case (preg_match($pat_a[1],$url,$regs)):
121 $k = $regs[1];
122 $v = $regs[2];
123 break;
124 case (preg_match($pat_a[2],$url,$regs)):
125 $k = $regs[1];
126 $v = $regs[2];
127 break;
128 case (preg_match($pat_a[3],$url,$regs)):
129 $k = $regs[1];
130 $v = $regs[2];
131 break;
132 default:
133 if ($val) {
134 if (strpos($url,'?')) {
135 $url .= "&$var=$val";
136 } else {
137 $url .= "?$var=$val";
138 }
139 }
140 break;
141 }
142
143 if ($k) {
144 if ($val) {
145 $rpl = "$k=$val";
146 if ($link) {
147 $rpl = preg_replace('/&/','&amp;',$rpl);
148 }
149 } else {
150 $rpl = '';
151 }
152 if( substr($v,-1)=='&' ) {
153 $rpl .= '&';
154 }
155 $pat = "/$k=$v/";
156 $url = preg_replace($pat,$rpl,$url);
157 }
158 return $url;
159 }
160
161 /**
162 * Temporary test function to process template vars with formatting.
163 * I use it for viewing the message_header (view_header.php) with
164 * a sort of template.
165 * @param mixed $var
166 * @param mixed $format_ar
167 * @since 1.3.0
168 * @todo if function is temporary, then why it is used.
169 * @deprecated
170 */
171 function echo_template_var($var, $format_ar = array() ) {
172 $frm_last = count($format_ar) -1;
173
174 if (isset($format_ar[0])) echo $format_ar[0];
175 $i = 1;
176
177 switch (true) {
178 case (is_string($var)):
179 echo $var;
180 break;
181 case (is_array($var)):
182 $frm_a = array_slice($format_ar,1,$frm_last-1);
183 foreach ($var as $a_el) {
184 if (is_array($a_el)) {
185 echo_template_var($a_el,$frm_a);
186 } else {
187 echo $a_el;
188 if (isset($format_ar[$i])) {
189 echo $format_ar[$i];
190 }
191 $i++;
192 }
193 }
194 break;
195 default:
196 break;
197 }
198 if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
199 echo $format_ar[$frm_last];
200 }
201 }
202 ?>