d49617344cb85cee6cab339d66a93e941069c592
[squirrelmail.git] / functions / html.php
1 <?php
2
3 /**
4 * html.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * The idea is to inlcude here some functions to make easier
10 * the right to left implementation by "functionize" some
11 * html outputs.
12 *
13 * $Id$
14 */
15
16 function html_tag( $tag, // Tag to output
17 $val = '', // Value between tags (if empty only start tag is issued)
18 $align = '', // Alignment
19 $bgcolor = '', // Back color
20 $xtra = '' ) { // Extra options
21
22 GLOBAL $languages, $squirrelmail_language;
23
24 $align = strtolower( $align );
25 $bgc = '';
26 $tag = strtoupper( $tag );
27
28 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
29 $dir = $languages[$squirrelmail_language]['DIR'];
30 } else {
31 $dir = 'ltr';
32 }
33
34 if ( $dir == 'ltr' ) {
35 $rgt = 'right';
36 $lft = 'left';
37 } else {
38 $rgt = 'left';
39 $lft = 'right';
40 }
41
42 if ( $bgcolor <> '' ) {
43 $bgc = " BGCOLOR=\"$bgcolor\"";
44 }
45
46 switch ( $align ) {
47 case '':
48 $alg = '';
49 break;
50 case 'right':
51 $alg = " ALIGN=\"$rgt\"";
52 break;
53 case 'left':
54 $alg = " ALIGN=\"$lft\"";
55 break;
56 default:
57 $alg = " ALIGN=\"$align\"";
58 break;
59 }
60
61 $ret = "<$tag";
62
63 if ( $dir <> 'ltr' ) {
64 $ret .= " DIR=\"$dir\"";
65 }
66 $ret .= "$bgc$alg";
67
68 if ( $xtra <> '' ) {
69 $ret .= " $xtra";
70 }
71 $ret .= '>';
72
73 if ( $val <> '' ) {
74 $ret .= "$val</$tag>";
75 }
76
77 return( $ret );
78 }
79
80 /* handy function to set url vars */
81 /* especially usefull when $url = $PHP_SELF */
82 function set_url_var($url, $var, $val=0) {
83 $k = '';
84 $ret = '';
85 $url = trim(preg_replace('/&amp;/','&',$url));
86
87 $pat_a = array (
88 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
89 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
90 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
91 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
92 );
93 switch (true) {
94 case (preg_match($pat_a[0],$url,$regs)):
95 $k = $regs[1];
96 $v = $regs[2];
97 break;
98 case (preg_match($pat_a[1],$url,$regs)):
99 $k = $regs[1];
100 $v = $regs[2];
101 break;
102 case (preg_match($pat_a[2],$url,$regs)):
103 $k = $regs[1];
104 $v = $regs[2];
105 break;
106 case (preg_match($pat_a[3],$url,$regs)):
107 $k = $regs[1];
108 $v = $regs[2];
109 break;
110 default:
111 if ($val) {
112 if (strpos($url,'?')) {
113 $url .= "&$var=$val";
114 } else {
115 $url .= "?$var=$val";
116 }
117 }
118 break;
119 }
120
121 if ($k) {
122 if ($val) {
123 $rpl = "$k=$val";
124 } else {
125 $rpl = '';
126 }
127 $pat = "/$k=$v/";
128 $url = preg_replace($pat,$rpl,$url);
129 }
130 return preg_replace('/&/','&amp;',$url);
131 }
132
133 /* Temporary test function to proces template vars with formatting.
134 * I use it for viewing the message_header (view_header.php) with
135 * a sort of template.
136 */
137 function echo_template_var($var, $format_ar = array() ) {
138 $frm_last = count($format_ar) -1;
139
140 if (isset($format_ar[0])) echo $format_ar[0];
141 $i = 1;
142
143 switch (true) {
144 case (is_string($var)):
145 echo $var;
146 break;
147 case (is_array($var)):
148 $frm_a = array_slice($format_ar,1,$frm_last-1);
149 foreach ($var as $a_el) {
150 if (is_array($a_el)) {
151 echo_template_var($a_el,$frm_a);
152 } else {
153 echo $a_el;
154 if (isset($format_ar[$i])) {
155 echo $format_ar[$i];
156 }
157 $i++;
158 }
159 }
160 break;
161 default:
162 break;
163 }
164 if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
165 echo $format_ar[$frm_last];
166 }
167 }
168 ?>