1<?php
2
3/**
4 * html.php
5 *
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 *
10 * @copyright 1999-2021 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id: html.php 14885 2021-02-05 19:19:32Z pdontthink $
13 * @package squirrelmail
14 * @since 1.3.0
15 */
16
17/**
18 * Generate html tags
19 *
20 * NOTE!  If $val is non-empty, a closing tag will be added after $val
21 *        Otherwise, NO closing tag is provided!
22 *
23 * @param string $tag Tag to output
24 * @param string $val Value between tags
25 * @param string $align Alignment (left, center, etc)
26 * @param string $bgcolor Back color in hexadecimal
27 * @param string $xtra Extra options
28 * @return string HTML ready for output
29 */
30function html_tag( $tag,                // Tag to output
31                       $val = '',           // Value between tags
32                       $align = '',         // Alignment
33                       $bgcolor = '',       // Back color
34                       $xtra = '' ) {       // Extra options
35
36        GLOBAL $languages, $squirrelmail_language;
37
38        $align = strtolower( $align );
39        $bgc = '';
40        $tag = strtolower( $tag );
41
42        if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
43            $dir = $languages[$squirrelmail_language]['DIR'];
44        } else {
45            $dir = 'ltr';
46        }
47
48        if ( $dir == 'ltr' ) {
49            $rgt = 'right';
50            $lft = 'left';
51        } else {
52            $rgt = 'left';
53            $lft = 'right';
54        }
55
56        if ( $bgcolor <> '' ) {
57            $bgc = " bgcolor=\"$bgcolor\"";
58        }
59
60        switch ( $align ) {
61            case '':
62                $alg = '';
63                break;
64            case 'right':
65                $alg = " align=\"$rgt\"";
66                break;
67            case 'left':
68                $alg = " align=\"$lft\"";
69                break;
70            default:
71                $alg = " align=\"$align\"";
72                break;
73        }
74
75        $ret = "<$tag";
76
77        if ( $dir <> 'ltr' ) {
78            $ret .= " dir=\"$dir\"";
79        }
80        $ret .= $bgc . $alg;
81
82        if ( $xtra <> '' ) {
83            $ret .= " $xtra";
84        }
85
86        if ( $val <> '' ) {
87            $ret .= ">$val</$tag>\n";
88        } else {
89            $ret .= '>' . "\n";
90        }
91
92        return( $ret );
93    }
94
95/**
96 * This function is used to add, modify or delete GET variables in a URL.
97 * It is especially useful when $url = $PHP_SELF
98 *
99 * Set $val to NULL to remove $var from $url.
100 * To ensure compatibility with older versions, use $val='0' to set $var to 0.
101 *
102 * @param string $url url that must be modified
103 * @param string $var GET variable name
104 * @param string $val variable value (CANNOT be an array)
105 * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
106 * @param boolean $treat_as_array When TRUE, if $var is an array (it occurs one
107 *                                or more times with square brackets after it,
108 *                                e.g. "var[1]"), the whole array will be removed
109 *                                (when $val is NULL) or the given value will be
110 *                                added to the next array slot (@since 1.4.23/1.5.2)
111 *
112 * @return string $url modified url
113 *
114 * @since 1.3.0
115 *
116 */
117function set_url_var($url, $var, $val=null, $link=true, $treat_as_array=false) {
118    $url = str_replace('&amp;','&',$url);
119
120    if (strpos($url, '?') === false) {
121        $url .= '?';
122    }
123
124    list($uri, $params) = explode('?', $url, 2);
125
126    $newpar = array();
127    $params = explode('&', $params);
128    $array_names = array();
129
130    foreach ($params as $p) {
131        if (trim($p)) {
132            $p = explode('=', $p);
133            $newpar[$p[0]] = (isset($p[1]) ? $p[1] : '');
134            if ($treat_as_array && preg_match('/(.*)\[(\d+)]$/', $p[0], $matches)) {
135               if (!isset($array_names[$matches[1]])) $array_names[$matches[1]] = array();
136               $array_names[$matches[1]][$matches[2]] = $p[1];
137            }
138        }
139    }
140
141    if (is_null($val)) {
142        if ($treat_as_array && !empty($array_names[$var])) {
143            foreach ($array_names[$var] as $key => $ignore)
144                unset($newpar[$var . '[' . $key . ']']);
145        } else {
146            unset($newpar[$var]);
147        }
148    } else {
149        if ($treat_as_array && !empty($array_names[$var])) {
150            $max_key = 0;
151            foreach ($array_names[$var] as $key => $ignore)
152                if ($key >= $max_key) $max_key = $key + 1;
153            $newpar[$var . '[' . $max_key . ']'] = $val;
154        } else {
155            $newpar[$var] = $val;
156        }
157    }
158
159    if (!count($newpar)) {
160        return $uri;
161    }
162
163    $url = $uri . '?';
164    foreach ($newpar as $name => $value) {
165        $url .= "$name=$value&";
166    }
167
168    $url = substr($url, 0, -1);
169    if ($link) {
170        $url = str_replace('&','&amp;',$url);
171    }
172
173    return $url;
174}
175
176    /* Temporary test function to proces template vars with formatting.
177     * I use it for viewing the message_header (view_header.php) with
178     * a sort of template.
179     */
180    function echo_template_var($var, $format_ar = array() ) {
181        $frm_last = count($format_ar) -1;
182
183        if (isset($format_ar[0])) echo $format_ar[0];
184            $i = 1;
185
186        switch (true) {
187            case (is_string($var)):
188                echo $var;
189                break;
190            case (is_array($var)):
191                $frm_a = array_slice($format_ar,1,$frm_last-1);
192                foreach ($var as $a_el) {
193                    if (is_array($a_el)) {
194                        echo_template_var($a_el,$frm_a);
195                    } else {
196                        echo $a_el;
197                        if (isset($format_ar[$i])) {
198                            echo $format_ar[$i];
199                        }
200                        $i++;
201                    }
202                }
203                break;
204            default:
205                break;
206        }
207        if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
208            echo $format_ar[$frm_last];
209        }
210    }
211