Replacing tabs with spaces, trimming white space at EOL and newline at EOF
[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 = '';
892b98c9 96 $pat_a = array (
02725e0b 97 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
892b98c9 98 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
99 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
100 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
101 );
91e0dccc 102 preg_replace('/&amp;/','&',$url);
892b98c9 103 switch (true) {
104 case (preg_match($pat_a[0],$url,$regs)):
105 $k = $regs[1];
106 $v = $regs[2];
107 break;
108 case (preg_match($pat_a[1],$url,$regs)):
109 $k = $regs[1];
110 $v = $regs[2];
111 break;
112 case (preg_match($pat_a[2],$url,$regs)):
113 $k = $regs[1];
114 $v = $regs[2];
115 break;
116 case (preg_match($pat_a[3],$url,$regs)):
117 $k = $regs[1];
118 $v = $regs[2];
119 break;
120 default:
121 if ($val) {
122 if (strpos($url,'?')) {
6c359ab4 123 $url .= "&$var=$val";
892b98c9 124 } else {
125 $url .= "?$var=$val";
126 }
127 }
128 break;
129 }
130
131 if ($k) {
132 if ($val) {
133 $rpl = "$k=$val";
91e0dccc 134 if ($link) {
135 $rpl = preg_replace('/&/','&amp;',$rpl);
136 }
892b98c9 137 } else {
138 $rpl = '';
139 }
140 $pat = "/$k=$v/";
141 $url = preg_replace($pat,$rpl,$url);
02725e0b 142 }
c9aa0b45 143 return $url;
892b98c9 144 }
02725e0b 145
6330a306 146 /* Temporary test function to proces template vars with formatting.
892b98c9 147 * I use it for viewing the message_header (view_header.php) with
6330a306 148 * a sort of template.
892b98c9 149 */
6330a306 150 function echo_template_var($var, $format_ar = array() ) {
892b98c9 151 $frm_last = count($format_ar) -1;
152
62f7daa5 153 if (isset($format_ar[0])) echo $format_ar[0];
892b98c9 154 $i = 1;
6330a306 155
892b98c9 156 switch (true) {
157 case (is_string($var)):
158 echo $var;
159 break;
160 case (is_array($var)):
161 $frm_a = array_slice($format_ar,1,$frm_last-1);
162 foreach ($var as $a_el) {
163 if (is_array($a_el)) {
164 echo_template_var($a_el,$frm_a);
165 } else {
166 echo $a_el;
167 if (isset($format_ar[$i])) {
168 echo $format_ar[$i];
169 }
170 $i++;
171 }
172 }
173 break;
174 default:
175 break;
176 }
177 if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
178 echo $format_ar[$frm_last];
179 }
180 }
62f7daa5 181?>