Minor cleanups
[squirrelmail.git] / functions / html.php
CommitLineData
9cd67a0a 1<?php
2
3/**
8008456a 4 * html.php
9cd67a0a 5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
9cd67a0a 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 *
31841a9e 13 * @version $Id$
d6c32258 14 * @package squirrelmail
9cd67a0a 15 */
16
d6c32258 17/**
18 * Generate 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 */
27function html_tag( $tag, // Tag to output
d68323ff 28 $val = '', // Value between tags
8008456a 29 $align = '', // Alignment
30 $bgcolor = '', // Back color
31 $xtra = '' ) { // Extra options
892b98c9 32
33 GLOBAL $languages, $squirrelmail_language;
34
35 $align = strtolower( $align );
36 $bgc = '';
d68323ff 37 $tag = strtolower( $tag );
892b98c9 38
39 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
d68323ff 40 $dir = $languages[$squirrelmail_language]['DIR'];
892b98c9 41 } else {
42 $dir = 'ltr';
43 }
44
45 if ( $dir == 'ltr' ) {
46 $rgt = 'right';
47 $lft = 'left';
48 } else {
49 $rgt = 'left';
50 $lft = 'right';
51 }
52
53 if ( $bgcolor <> '' ) {
62f7daa5 54 $bgc = " bgcolor=\"$bgcolor\"";
892b98c9 55 }
56
57 switch ( $align ) {
58 case '':
59 $alg = '';
60 break;
61 case 'right':
d68323ff 62 $alg = " align=\"$rgt\"";
892b98c9 63 break;
64 case 'left':
d68323ff 65 $alg = " align=\"$lft\"";
892b98c9 66 break;
67 default:
d68323ff 68 $alg = " align=\"$align\"";
892b98c9 69 break;
70 }
71
72 $ret = "<$tag";
73
74 if ( $dir <> 'ltr' ) {
d68323ff 75 $ret .= " dir=\"$dir\"";
892b98c9 76 }
d68323ff 77 $ret .= $bgc . $alg;
892b98c9 78
79 if ( $xtra <> '' ) {
80 $ret .= " $xtra";
81 }
892b98c9 82
83 if ( $val <> '' ) {
d68323ff 84 $ret .= ">$val</$tag>";
85 } else {
1eebcf25 86 $ret .= '>';
62f7daa5 87 }
892b98c9 88
94ac35c6 89 return( $ret );
94ac35c6 90 }
91
02725e0b 92 /* handy function to set url vars */
93 /* especially usefull when $url = $PHP_SELF */
59be1a75 94 function set_url_var($url, $var, $val=0, $link=true) {
892b98c9 95 $k = '';
96 $ret = '';
892b98c9 97 $pat_a = array (
02725e0b 98 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
892b98c9 99 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
100 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
101 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
102 );
62f7daa5 103 preg_replace('/&amp;/','&',$url);
892b98c9 104 switch (true) {
105 case (preg_match($pat_a[0],$url,$regs)):
106 $k = $regs[1];
107 $v = $regs[2];
108 break;
109 case (preg_match($pat_a[1],$url,$regs)):
110 $k = $regs[1];
111 $v = $regs[2];
112 break;
113 case (preg_match($pat_a[2],$url,$regs)):
114 $k = $regs[1];
115 $v = $regs[2];
116 break;
117 case (preg_match($pat_a[3],$url,$regs)):
118 $k = $regs[1];
119 $v = $regs[2];
120 break;
121 default:
122 if ($val) {
123 if (strpos($url,'?')) {
6c359ab4 124 $url .= "&$var=$val";
892b98c9 125 } else {
126 $url .= "?$var=$val";
127 }
128 }
129 break;
130 }
131
132 if ($k) {
133 if ($val) {
134 $rpl = "$k=$val";
59be1a75 135 if ($link) {
136 $rpl = preg_replace('/&/','&amp;',$rpl);
137 }
892b98c9 138 } else {
139 $rpl = '';
140 }
141 $pat = "/$k=$v/";
142 $url = preg_replace($pat,$rpl,$url);
02725e0b 143 }
c9aa0b45 144 return $url;
892b98c9 145 }
02725e0b 146
6330a306 147 /* Temporary test function to proces template vars with formatting.
892b98c9 148 * I use it for viewing the message_header (view_header.php) with
6330a306 149 * a sort of template.
892b98c9 150 */
6330a306 151 function echo_template_var($var, $format_ar = array() ) {
892b98c9 152 $frm_last = count($format_ar) -1;
153
62f7daa5 154 if (isset($format_ar[0])) echo $format_ar[0];
892b98c9 155 $i = 1;
6330a306 156
892b98c9 157 switch (true) {
158 case (is_string($var)):
159 echo $var;
160 break;
161 case (is_array($var)):
162 $frm_a = array_slice($format_ar,1,$frm_last-1);
163 foreach ($var as $a_el) {
164 if (is_array($a_el)) {
165 echo_template_var($a_el,$frm_a);
166 } else {
167 echo $a_el;
168 if (isset($format_ar[$i])) {
169 echo $format_ar[$i];
170 }
171 $i++;
172 }
173 }
174 break;
175 default:
176 break;
177 }
178 if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
179 echo $format_ar[$frm_last];
180 }
181 }
62f7daa5 182?>