246808e1b88c1782aacee14ef632b179c13cd12d
[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 $pat_a = array (
86 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
87 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
88 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
89 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
90 );
91 switch (true) {
92 case (preg_match($pat_a[0],$url,$regs)):
93 $k = $regs[1];
94 $v = $regs[2];
95 break;
96 case (preg_match($pat_a[1],$url,$regs)):
97 $k = $regs[1];
98 $v = $regs[2];
99 break;
100 case (preg_match($pat_a[2],$url,$regs)):
101 $k = $regs[1];
102 $v = $regs[2];
103 break;
104 case (preg_match($pat_a[3],$url,$regs)):
105 $k = $regs[1];
106 $v = $regs[2];
107 break;
108 default:
109 if ($val) {
110 if (strpos($url,'?')) {
111 $url .= "&amp;$var=$val";
112 } else {
113 $url .= "?$var=$val";
114 }
115 }
116 break;
117 }
118
119 if ($k) {
120 if ($val) {
121 $rpl = "$k=$val";
122 $rpl = preg_replace('/&/','&amp;',$rpl);
123 } else {
124 $rpl = '';
125 }
126 $pat = "/$k=$v/";
127 $url = preg_replace($pat,$rpl,$url);
128 }
129 return $url;
130 }
131
132 /* Temporary test function to proces template vars with formatting.
133 * I use it for viewing the message_header (view_header.php) with
134 * a sort of template.
135 */
136 function echo_template_var($var, $format_ar = array() ) {
137 $frm_last = count($format_ar) -1;
138
139 if (isset($format_ar[0])) echo $format_ar[0];
140 $i = 1;
141
142 switch (true) {
143 case (is_string($var)):
144 echo $var;
145 break;
146 case (is_array($var)):
147 $frm_a = array_slice($format_ar,1,$frm_last-1);
148 foreach ($var as $a_el) {
149 if (is_array($a_el)) {
150 echo_template_var($a_el,$frm_a);
151 } else {
152 echo $a_el;
153 if (isset($format_ar[$i])) {
154 echo $format_ar[$i];
155 }
156 $i++;
157 }
158 }
159 break;
160 default:
161 break;
162 }
163 if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
164 echo $format_ar[$frm_last];
165 }
166 }
167 ?>