somehow url vars with &amp are not picked up by PHP
[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 // preg_replace('/&amp;/','&',$url);
92 switch (true) {
93 case (preg_match($pat_a[0],$url,$regs)):
94 $k = $regs[1];
95 $v = $regs[2];
96 break;
97 case (preg_match($pat_a[1],$url,$regs)):
98 $k = $regs[1];
99 $v = $regs[2];
100 break;
101 case (preg_match($pat_a[2],$url,$regs)):
102 $k = $regs[1];
103 $v = $regs[2];
104 break;
105 case (preg_match($pat_a[3],$url,$regs)):
106 $k = $regs[1];
107 $v = $regs[2];
108 break;
109 default:
110 if ($val) {
111 if (strpos($url,'?')) {
112 $url .= "&$var=$val";
113 } else {
114 $url .= "?$var=$val";
115 }
116 }
117 break;
118 }
119
120 if ($k) {
121 if ($val) {
122 $rpl = "$k=$val";
123 // $rpl = preg_replace('/&/','&amp;',$rpl);
124 } else {
125 $rpl = '';
126 }
127 $pat = "/$k=$v/";
128 $url = preg_replace($pat,$rpl,$url);
129 }
130 return $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 ?>