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 * Various support functions, useful or useless.
10 *
11 * @version $Id: support.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 * Delete element from array.
20 */
21function array_del($array, $place) {
22    $newarray = array();
23    $n=0;
24    for ($i=0; $i<sizeof($array); $i++)
25        if ($i!=$place)
26            $newarray[$n++] = $array[$i];
27    return $newarray;
28}
29
30
31/**
32 * Swap values of two elements in array.
33 */
34function array_swapval ($array, $i, $j) {
35    $temp[$i] = $array[$j];
36    $temp[$j] = $array[$i];
37
38    $array[$i] = $temp[$i];
39    $array[$j] = $temp[$j];
40
41    return $array;
42}
43
44/**
45 * This plugin's error display function.
46 */
47function print_errormsg($errormsg) {
48    include_once(SM_PATH . 'functions/display_messages.php');
49    global $color;
50    error_box ($errormsg, $color);
51    exit;
52}
53
54/**
55 * Create a new folder: wrapper function for avelsieve.
56 *
57 * @param string $foldername
58 * @param string $subfolder
59 * @param string $created_mailbox_name
60 * @param array $errmsg Array of error messages, in which to append an error
61 *   message if it occurs.
62 * @return boolean True upon success, otherwise false.
63 * @todo Check for folders that already exist
64 */
65function avelsieve_create_folder($foldername, $subfolder = '', &$created_mailbox_name, &$errmsg) {
66    /* Copy & paste magic (aka kludge) */
67    global $mailboxlist, $delimiter, $username, $imapServerAddress, $imapPort, $imapConnection;
68
69    sqgetGlobalVar('key', $key, SQ_COOKIE);
70    sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
71
72    if(!isset($delimiter) && isset($_SESSION['delimiter'])) {
73        $delimiter = $_SESSION['delimiter'];
74    } else { /* Just in case... */
75        if(!isset($imapConnection)) {
76            $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
77        }
78        $delimiter = sqimap_get_delimiter($imapConnection);
79        $_SESSION['delimiter'] = $delimiter;
80    }
81
82    if(isset($foldername) && trim($foldername) != '' ) {
83        $foldername = imap_utf7_encode_local(trim($foldername));
84    } else {
85        $errmsg[] = _("You have not defined the name for the new folder.") .
86                ' ' . _("Please try again.");
87        return false;
88    }
89
90    if(empty($subfolder)) {
91        $subfolder = "INBOX";
92    }
93
94    if (strpos($foldername, "\"") || strpos($foldername, "\\") ||
95    strpos($foldername, "'") || strpos($foldername, "$delimiter")) {
96        $errmsg[] = _("Illegal folder name.  Please select a different name");
97        return false;
98    }
99
100    if (isset($contain_subs) && $contain_subs ) {
101        $foldername = "$foldername$delimiter";
102    }
103
104    // $folder_prefix = "INBOX";
105    $folder_prefix = '';
106
107    if (!empty($folder_prefix) && (substr($folder_prefix, -1) != $delimiter)) {
108        $folder_prefix = $folder_prefix . $delimiter;
109    }
110    if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)){
111        $subfolder_orig = $subfolder;
112        $subfolder = $folder_prefix . $subfolder;
113    } else {
114        $subfolder_orig = $subfolder;
115    }
116    if (trim($subfolder_orig) == '') {
117        $mailbox = $folder_prefix.$foldername;
118    } else {
119        $mailbox = $subfolder.$delimiter.$foldername;
120    }
121    /*    if (strtolower($type) == 'noselect') {
122            $mailbox = $mailbox.$delimiter;
123        }
124    */
125    /* Actually create the folder. */
126
127    if(!isset($imapConnection)) {
128        $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
129    }
130
131    /* Here we could do some more error checking to see if the
132     * folder already exists. If it exists, the creation will not
133     * do anything ANW, so it works well as it is. It can be made
134     * better, e.g. by printing a notice "Note that the folder you
135     * wanted to create already exists". */
136
137    // $boxes = sqimap_mailbox_list($imapConnection);
138
139    /* Instead of using sqimap_mailbox_create(), I use sqimap_run_command so
140     * that I will put 'false' in the error handling. */
141
142    $response = '';
143    $message = '';
144
145    $read_ary = sqimap_run_command($imapConnection, "CREATE \"$mailbox\"", false, $response, $message);
146       sqimap_subscribe ($imapConnection, $mailbox);
147
148    if(strtolower($response) != 'ok') {
149        $errmsg[] = $message;
150        return false;
151    }
152    $created_mailbox_name = $mailbox;
153    return true;
154}
155
156/**
157 * Print mailbox select widget.
158 *
159 * @param string $selectname name for the select HTML variable
160 * @param string $selectedmbox which mailbox to be selected in the form
161 * @param boolean $sub
162 */
163function mailboxlist($selectname, $selectedmbox, $sub = false) {
164
165    global $boxes_append, $boxes_admin, $imap_server_type,
166    $default_sub_of_inbox;
167
168        if(isset($boxes_admin) && $sub) {
169            $boxes = $boxes_admin;
170        } elseif(isset($boxes_append)) {
171            $boxes = $boxes_append;
172        } else {
173            global $boxes;
174        }
175
176        if (count($boxes)) {
177            $mailboxlist = '<select name="'.$selectname.'" onclick="checkOther(\'5\');" >';
178
179            if($sub) {
180            if ($default_sub_of_inbox == false ) {
181                $mailboxlist = $mailboxlist."\n".'<option selected value="">[ '._("None")." ] </option>\n";
182            }
183            }
184
185            for ($i = 0; $i < count($boxes); $i++) {
186                    $box = $boxes[$i]['unformatted-dm'];
187                    $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['formatted']);
188                    //$box2 = str_replace(' ', '&nbsp;', $boxes[$i]['formatted']);
189
190                    if (strtolower($imap_server_type) != 'courier' || strtolower($box) != 'inbox.trash') {
191                        $mailboxlist .= '<option value="'.htmlspecialchars($box).'"';
192                if($selectedmbox == $box) {
193                    $mailboxlist .= ' selected="SELECTED"';
194                }
195                $mailboxlist .= '>'.$box2."</option>\n";
196                    }
197            }
198            $mailboxlist .= "</select>\n";
199
200        } else {
201            $mailboxlist = "No folders found.";
202        }
203        return $mailboxlist;
204}
205
206/**
207 * Get user's email addresses (from all identities). They are to be used in the
208 * vacation ":address" field.
209 * @return string A string with comma-separated email addresses
210 */
211function get_user_addresses() {
212    $idents = get_identities();
213    foreach($idents as $identity) {
214        $emailaddresses[] = $identity['email_address'];
215    }
216    return implode(",", $emailaddresses);
217
218    /* Rest of the code in this function is probably obsolete */
219
220    global $data_dir, $username, $ldapuserdatamode;
221    $default_emailaddress = getPref($data_dir, $username, 'email_address');
222
223    if ($ldapuserdatamode) {
224        /* Get user's email addresses from LDAP Prefs Backend plugin's cache */
225        $addressarray[] = $default_emailaddress;
226
227        if (isset($_SESSION['alternateemails'])) {
228            $alternateemails = $_SESSION['alternateemails'];
229            for ($i=0; $i<sizeof($alternateemails); $i++) {
230                $addressarray[] = $alternateemails[$i];
231            }
232            $emailaddresses = implode(",", $addressarray);
233        } else {
234            $emailaddresses = $default_emailaddress;
235        }
236
237    } else {
238        /* Normal Mode; get email address from user's prefs and from
239         * user's possible identities. */
240
241        $emailaddresses = $default_emailaddress;
242
243        $idents = getPref($data_dir, $username, 'identities');
244        if ($idents != '' && $idents > 1) {
245            for ($i = 1; $i < $idents; $i ++) {
246                $cur_email_address = getPref($data_dir, $username, 'email_address' . $i);
247                $cur_email_address = strtolower(trim($cur_email_address));
248                $emailaddresses = $emailaddresses . ',' . $cur_email_address;
249            }
250        }
251    }
252    return $emailaddresses;
253}
254
255/**
256 * Escape only double quotes and backslashes, as required by SIEVE RFC. For the
257 * reverse procedure, PHP function stripslashes() will do.
258 *
259 * @param string $script
260 * @return string
261 */
262function avelsieve_addslashes($string) {
263    /* 1) quoted string
264     * 2) str_replace
265     * 3) sieve.lib.php
266     * 4) .....
267     */
268    $temp =  str_replace("\\", "\\\\\\\\\\\\\\\\", $string);
269    return str_replace('"', "\\\\\\\\\"", $temp);
270}
271
272