Moving template util files
[squirrelmail.git] / functions / html.php
CommitLineData
9cd67a0a 1<?php
2
3/**
8008456a 4 * html.php
9cd67a0a 5 *
9cd67a0a 6 * The idea is to inlcude here some functions to make easier
7 * the right to left implementation by "functionize" some
8 * html outputs.
9 *
47ccfad4 10 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
792f911e 14 * @since 1.3.0
9cd67a0a 15 */
16
d6c32258 17/**
792f911e 18 * Generates html tags
d6c32258 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
792f911e 26 * @since 1.3.0
d6c32258 27 */
28function html_tag( $tag, // Tag to output
d68323ff 29 $val = '', // Value between tags
8008456a 30 $align = '', // Alignment
31 $bgcolor = '', // Back color
32 $xtra = '' ) { // Extra options
892b98c9 33
792f911e 34 GLOBAL $languages, $squirrelmail_language;
892b98c9 35
792f911e 36 $align = strtolower( $align );
37 $bgc = '';
38 $tag = strtolower( $tag );
892b98c9 39
792f911e 40 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
41 $dir = $languages[$squirrelmail_language]['DIR'];
42 } else {
43 $dir = 'ltr';
44 }
892b98c9 45
792f911e 46 if ( $dir == 'ltr' ) {
47 $rgt = 'right';
48 $lft = 'left';
49 } else {
50 $rgt = 'left';
51 $lft = 'right';
52 }
892b98c9 53
792f911e 54 if ( $bgcolor <> '' ) {
55 $bgc = " bgcolor=\"$bgcolor\"";
56 }
892b98c9 57
792f911e 58 switch ( $align ) {
59 case '':
60 $alg = '';
61 break;
62 case 'right':
63 $alg = " align=\"$rgt\"";
64 break;
65 case 'left':
66 $alg = " align=\"$lft\"";
67 break;
68 default:
69 $alg = " align=\"$align\"";
70 break;
71 }
892b98c9 72
792f911e 73 $ret = "<$tag";
892b98c9 74
792f911e 75 if ( $dir <> 'ltr' ) {
76 $ret .= " dir=\"$dir\"";
77 }
78 $ret .= $bgc . $alg;
892b98c9 79
792f911e 80 if ( $xtra <> '' ) {
81 $ret .= " $xtra";
82 }
892b98c9 83
792f911e 84 if ( $val <> '' ) {
85 $ret .= ">$val</$tag>\n";
86 } else {
87 $ret .= '>'. "\n";
94ac35c6 88 }
89
792f911e 90 return( $ret );
91}
892b98c9 92
792f911e 93/**
94 * handy function to set url vars
95 *
96 * especially useful when $url = $PHP_SELF
97 * @param string $url url that must be modified
98 * @param string $var variable name
99 * @param string $val variable value
100 * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
101 * @return string $url modified url
102 * @since 1.3.0
103 */
104function set_url_var($url, $var, $val=0, $link=true) {
105 $k = '';
106 $pat_a = array (
107 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
108 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
109 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
110 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
111 );
4f113ce5 112 $url = str_replace('&amp;','&',$url);
792f911e 113
114 // FIXME: why switch is used instead of if () or one preg_match()
115 switch (true) {
116 case (preg_match($pat_a[0],$url,$regs)):
117 $k = $regs[1];
118 $v = $regs[2];
119 break;
120 case (preg_match($pat_a[1],$url,$regs)):
121 $k = $regs[1];
122 $v = $regs[2];
123 break;
124 case (preg_match($pat_a[2],$url,$regs)):
125 $k = $regs[1];
126 $v = $regs[2];
127 break;
128 case (preg_match($pat_a[3],$url,$regs)):
129 $k = $regs[1];
130 $v = $regs[2];
131 break;
132 default:
133 if ($val) {
134 if (strpos($url,'?')) {
135 $url .= "&$var=$val";
892b98c9 136 } else {
792f911e 137 $url .= "?$var=$val";
892b98c9 138 }
792f911e 139 }
140 break;
141 }
142
143 if ($k) {
144 if ($val) {
145 $rpl = "$k=$val";
792f911e 146 } else {
147 $rpl = '';
148 }
149 if( substr($v,-1)=='&' ) {
150 $rpl .= '&';
02725e0b 151 }
792f911e 152 $pat = "/$k=$v/";
153 $url = preg_replace($pat,$rpl,$url);
892b98c9 154 }
4f113ce5 155 if ($link) {
156 $url = str_replace('&','&amp;',$url);
157 }
792f911e 158 return $url;
159}