1<?php
2/**
3 * User-friendly interface to SIEVE server-side mail filtering.
4 * Plugin for Squirrelmail 1.4+
5 *
6 * Licensed under the GNU GPL. For full terms see the file COPYING that came
7 * with the Squirrelmail distribution.
8 *
9 * HTML Functions
10 *
11 * @version $Id: html_main.inc.php 1020 2009-05-13 14:10:13Z avel $
12 * @author Alexandros Vellis <avel@users.sourceforge.net>
13 * @copyright 2004-2007 The SquirrelMail Project Team, Alexandros Vellis
14 * @package plugins
15 * @subpackage avelsieve
16 */
17
18/**
19 * Global HTML Output functions. These contain functions for starting/ending
20 * sections, helper functions for determining and printing HTML snippets,
21 * as well as header/footer thingies.
22 */
23class avelsieve_html {
24    /**
25     * @var int Level of Javascript support
26     */
27    var $js = 0;
28
29    /**
30     * @param boolean Flag for image usage
31     */
32    var $useimages = true;
33
34    /**
35     * Constructor function will initialize some variables, depending on the
36     * environment.
37     */
38    function avelsieve_html() {
39        global $plugins, $javascript_on, $useimages;
40
41        if($javascript_on) {
42            $this->js++;
43            if(in_array('javascript_libs', $plugins)) {
44                $this->js++;
45            }
46        }
47        $this->useimages = $useimages;
48        $this->baseuri = sqm_baseuri();
49        $this->imageuri = $this->baseuri . 'plugins/avelsieve/images/';
50        $this->iconuri = $this->baseuri . 'plugins/avelsieve/images/icons/';
51    }
52
53    /**
54     * Page Header
55     */
56    function header($customtitle = '') {
57        $out = '<h1>'._("Server-Side Mail Filtering");
58        if($customtitle) {
59            $out .= ' - '.$customtitle;
60        }
61        $out .= '</h1>';
62        return $out;
63    }
64
65    function my_header() {
66        global $color;
67        return '<br><table width="100%"><tr><td bgcolor="'.$color[0].'">'.
68            '<center><b>' . _("Server-Side Mail Filtering"). '</b></center>'.
69            '</td></tr></table>';
70    }
71
72    /**
73     * Squirrelmail-style table header
74     * @return string
75     */
76    function table_header($customtitle) {
77        global $color;
78        $out = "\n<!-- Table header --><br/>".
79        '<table bgcolor="'.$color[0].'" width="95%" align="center" cellpadding="2" cellspacing="0" border="0">
80        <tr><td align="center">
81            <strong>'.  _("Server-Side Mail Filtering") .
82            ( !empty($customtitle) ? ' - '.$customtitle : '' ) . '</strong>
83            <table width="100%" border="0" cellpadding="5" cellspacing="0">
84            <tr><td bgcolor="'.$color[4].'" align="center">
85        ';
86        return $out;
87    }
88
89    /**
90     * Squirrelmail-style table footer
91     * @return string
92     */
93    function table_footer() {
94        return '</td></tr></table>'.
95            '</td></tr></table>';
96    }
97
98    /**
99     * All sections table start
100     * @return string
101     */
102    function all_sections_start() {
103        return '<!-- All sect. start --><table width="95%" align="center" cellpadding="4" cellspacing="0" border="0">';
104    }
105
106    /**
107     * All sections table end
108     * @return string
109     */
110    function all_sections_end() {
111        return '</table><!-- All sect. end -->';
112    }
113
114    /**
115     * Table 'section' start
116     * @return string
117     */
118    function section_start($title = '') {
119        global $color;
120        if(empty($title)) {
121            return "\n<!-- Section start -->".
122                '<tr><td bgcolor="'.$color[0].'" align="left">';
123        } else {
124            return "\n<!-- Section start -->".
125                '<tr><td bgcolor="'.$color[9].'" align="center">'.
126                '<strong>'.$title.'</strong></td></tr>'.
127                '<tr><td bgcolor="'.$color[0].'" align="left">';
128        }
129    }
130
131    /**
132     * Table 'section' end
133     * @return string
134     */
135    function section_end() {
136        global $color;
137        $out = "</td></tr>\n".
138            "<tr><td bgcolor=\"$color[4]\">&nbsp;</td></tr>\n";
139        return $out;
140    }
141
142    /**
143      * Generic Listbox widget
144      *
145      * @param $selected_header Selected header
146      * @param $n option number
147      */
148    function generic_listbox($name, $options, $selected_option = '') {
149        $out = '<select name="'.$name.'">';
150        foreach($options as $o => $desc) {
151            if ($selected_option==$o) {
152                $out .= '<option value="'.htmlspecialchars($o).'" selected="SELECTED">'.htmlspecialchars($desc).'</option>';
153            } else {
154                $out .= '<option value="'.htmlspecialchars($o).'">'.htmlspecialchars($desc).'</option>';
155            }
156        }
157        $out .= '</select>';
158        return $out;
159    }
160
161    /**
162     * Explicitly set Error Messages that might have occured from an external
163     * source.
164     *
165     * Inside the classes themselves, I use
166     * $this->errmsg[] = 'Message' ...
167     *
168     * @param array $array
169     * @return void
170     */
171    function set_errmsg($array) {
172        $this->errmsg = array_merge($this->errmsg, $array);
173    }
174
175    /**
176     * Access the messages queue from session variables, and return what should
177     * be displayed in the end to the user.
178     *
179     * @todo Implement a proper message queue.
180     * @return array
181     */
182    function retrieve_avelsieve_messages() {
183        global $color;
184        $out = '';
185        $msgs = array();
186
187        if(isset($_SESSION['comm'])) {
188            $comm = $_SESSION['comm'];
189
190            $out .= '<p style="background-color: '.$color[16].'; color:'.$color[8].'; text-align: center;">'.
191                ($this->useimages == true? '<img src="'.$this->iconuri.'tick.png" alt="(OK)" /> ' : '');
192
193            if (isset($comm['raw'])) {
194                $out .= $comm['raw'];
195
196            } elseif(isset($comm['new'])) {
197                $out .= _("Successfully added new rule.");
198
199            } elseif (isset($comm['edited'])) {
200                $out .= sprintf( _("Successfully updated rule #%s"), $comm['edited']+1);
201
202            } elseif (isset($comm['deleted'])) {
203                if(is_array($comm['deleted'])) {
204                    $deletedRulesNumbers = '';
205                    for ($i=0; $i<sizeof($comm['deleted']); $i++ ) {
206                        $deletedRulesNumbers .= $comm['deleted'][$i] +1;
207                        if($i != (sizeof($comm['deleted']) -1) ) {
208                            $deletedRulesNumbers .= ", ";
209                        }
210                    }
211                    $out .= sprintf( _("Successfully deleted rules #%s"), $deletedRulesNumbers);
212                } else {
213                    $out .= sprintf( _("Successfully deleted rule #%s"), $comm['deleted']+1);
214                }
215            }
216
217            $out .= '</p>';
218        }
219        return $out;
220    }
221
222    /**
223     * Remove messages from session.
224     * @return void
225     */
226    function clear_avelsieve_messages() {
227        if(isset($_SESSION['comm'])) {
228            session_unregister('comm');
229        }
230    }
231
232    /**
233     * Small helper function, that returns the appropriate javascript snippet
234     * for "toggle" links.
235     *
236     * @param string $divname ID of the DIV/SPAN
237     * @param int $js Force Javascript level. for instance, sometimes we don't
238     *   want scriptaculous-style effects, so we set js=1.
239     * @param boolean $imageChange Change the image "$divname_img" to
240     *   closed/open triangle
241     * @return string
242     */
243    function js_toggle_display($divname, $js = '', $imageChange = '') {
244        if($this->js == 0) {
245            $js = 0;
246        } elseif(empty($js)) {
247            $js = $this->js;
248        }
249        if(empty($imageChange)) $imageChange = $this->useimages;
250
251        if($js == 2) {
252            /* Scriptaculous */
253            if($imageChange) {
254                return 'AVELSIEVE.edit.toggleShowDivWithImg(\''.$divname.'\', 1);';
255            } else {
256                return 'Effect.toggle(\''.$divname.'\', \'slide\');';
257            }
258
259        } elseif($js == 1) {
260            /* Simple javascript */
261            if($imageChange) {
262                return 'AVELSIEVE.edit.toggleShowDivWithImg(\''.$divname.'\');';
263            } else {
264                return 'AVELSIEVE.edit.toggleShowDiv(\''.$divname.'\');';
265            }
266        }
267    }
268
269    /**
270     * Print formatted error message(s), if they exist.
271     *
272     * @return string
273     */
274    function print_errmsg() {
275        $out = '';
276        if(!empty($this->errmsg)) {
277            global $color;
278            $out .= $this->section_start( _("Error Encountered:") ).
279                '<div style="text-align:center; color:'.$color[2].';">';
280
281            if(is_array($this->errmsg)) {
282                $out .= '<ul>';
283                foreach($this->errmsg as $msg) {
284                    $out .= '<li>'.$msg.'</li>';
285                }
286                $out .= '</ul>';
287            } else {
288                $out .= '<p>'.$this->errmsg .'</p>';
289            }
290            $out .= '<p>'. _("You must correct the above errors before continuing."). '</p>';
291            $out .= '</div>' .     $this->section_end();
292        }
293        return $out;
294    }
295
296    /**
297     * Helper function: Find if the option $optname is on, in the current rule.
298     * (Checks if $this->rule[$optname] evaluates to true).
299     *
300     * @param string $optname
301     * @return boolean
302     */
303    function determineState($optname) {
304        if(!isset($this->rule[$optname])) {
305            return false;
306        }
307        if($this->rule[$optname] == 0) {
308            return false;
309        }
310        if($this->rule[$optname] || $this->rule[$optname] == 'on' || $this->rule[$optname] == true) {
311            return true;
312        }
313        return false;
314    }
315
316    /**
317     * Returns the checked state for a checkbox that corresponds to option
318     * $optname. (Helper function).
319     *
320     * @param string $optname
321     * @return string
322     */
323    function stateCheckbox($optname) {
324        if($this->determineState($optname) === true) {
325            return 'checked="" ';
326        } else {
327            return '';
328        }
329    }
330
331    /**
332     * Returns the visibility state for a div that corresponds to more options
333     * under the "top" option $optname. (Helper function).
334     *
335     * @param string $optname
336     * @return string
337     */
338    function stateVisibility($optname) {
339        if($this->determineState($optname) === true) {
340            return '';
341        } else {
342            return 'style="display:none;" ';
343        }
344    }
345}
346
347