4 * setup.php -- Sent Subfolders Setup File
6 * This is a standard SquirrelMail 1.2 API for plugins.
8 * @copyright 1999-2015 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @subpackage sent_subfolders
15 define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
16 define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
17 define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
18 define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
19 define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
21 function sent_subfolders_check_handleAsSent_do($mailbox) {
23 global $handleAsSent_result, $data_dir, $username, $sent_folder;
25 // don't need to bother if it's already special
26 if ($handleAsSent_result) return;
28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
30 $use_sent_subfolders = getPref($data_dir, $username,
31 'use_sent_subfolders', SMPREF_OFF
);
32 $sent_subfolders_base = getPref($data_dir, $username,
33 'sent_subfolders_base', $sent_folder);
35 /* Only check the folder string if we have been passed a mailbox. */
36 if ($use_sent_subfolders && !empty($mailbox)) {
37 /* Chop up the folder strings as needed. */
38 $base_str = $sent_subfolders_base . $delimiter;
39 $mbox_str = substr($mailbox, 0, strlen($base_str));
41 /* Perform the comparison. */
42 $handleAsSent_result = ( ($base_str == $mbox_str)
43 ||
($sent_subfolders_base == $mailbox) );
48 * Adds sent_subfolders options in folder preferences
50 function sent_subfolders_optpage_loadhook_folders_do() {
52 global $data_dir, $username, $optpage_data, $imapServerAddress,
53 $imapPort, $imap_stream_options, $show_contain_subfolders_option, $sent_folder;
55 /* Get some imap data we need later. */
56 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0, $imap_stream_options);
57 $boxes = sqimap_mailbox_list($imapConnection);
58 sqimap_logout($imapConnection);
60 /* Load the Sent Subfolder Options into an array. */
61 $optgrp = _("Sent Subfolders Options");
64 global $sent_subfolders_setting;
65 $sent_subfolders_setting = getPref($data_dir, $username,
66 'sent_subfolders_setting',
67 SMPREF_SENT_SUBFOLDERS_DISABLED
);
69 'name' => 'sent_subfolders_setting',
70 'caption' => _("Use Sent Subfolders"),
71 'type' => SMOPT_TYPE_STRLIST
,
72 'refresh' => SMOPT_REFRESH_FOLDERLIST
,
73 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED
=> _("Disabled"),
74 SMPREF_SENT_SUBFOLDERS_MONTHLY
=> _("Monthly"),
75 SMPREF_SENT_SUBFOLDERS_QUARTERLY
=> _("Quarterly"),
76 SMPREF_SENT_SUBFOLDERS_YEARLY
=> _("Yearly")),
77 'save' => 'save_option_sent_subfolders_setting'
80 $filtered_folders=array_filter($boxes, "filter_folders");
81 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
83 global $sent_subfolders_base;
84 $sent_subfolders_base = getPref($data_dir, $username,
85 'sent_subfolders_base', $sent_folder);
87 'name' => 'sent_subfolders_base',
88 'caption' => _("Base Sent Folder"),
89 'type' => SMOPT_TYPE_FLDRLIST
,
90 'refresh' => SMOPT_REFRESH_FOLDERLIST
,
91 'posvals' => $sent_subfolders_base_values,
92 'folder_filter' => 'noinferiors',
93 'save' => 'save_option_sent_subfolders_base'
96 if ($show_contain_subfolders_option) {
98 'name' => 'sent_subfolders_warning',
99 'caption' => _("Warning"),
100 'type' => SMOPT_TYPE_COMMENT
,
101 'comment' => _("There are some restrictions in Sent Subfolder options.")
105 /* Add our option data to the global array. */
106 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS
] = $optgrp;
107 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS
] = $optvals;
111 * Defines folder filtering rules
113 * Callback function that should exclude some folders from folder listing.
114 * @param array $fldr list of folders. See sqimap_mailbox_list
115 * @return boolean returns true, if folder has to included in folder listing
118 function filter_folders($fldr) {
119 return strtolower($fldr['unformatted'])!='inbox';
123 * Saves sent_subfolder_options
125 function save_option_sent_subfolders_setting($option) {
126 global $data_dir, $username;
128 /* Set use_sent_subfolders as either on or off. */
129 if ($option->new_value
== SMPREF_SENT_SUBFOLDERS_DISABLED
) {
130 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
132 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON
);
133 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON
);
134 $check_sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', '');
135 if ($check_sent_subfolders_base === '') {
136 setPref($data_dir, $username, 'sent_subfolders_base', $sent_subfolders_base);
140 /* Now just save the option as normal. */
141 save_option($option);
145 * Update the folder settings/auto-create new subfolder
147 function save_option_sent_subfolders_base($option) {
148 // first save the option as normal
149 save_option($option);
151 // now update folder settings and auto-create first subfolder if needed
152 sent_subfolders_update_sentfolder_do();
156 * Update sent_subfolders settings
158 * function updates default sent folder value and
159 * creates required imap folders
161 function sent_subfolders_update_sentfolder_do() {
162 global $sent_folder, $username,
163 $data_dir, $imapServerAddress, $imapPort,
164 $imap_stream_options, $move_to_sent;
166 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
168 $use_sent_subfolders = getPref($data_dir, $username,
169 'use_sent_subfolders', SMPREF_OFF
);
170 $sent_subfolders_setting = getPref($data_dir, $username,
171 'sent_subfolders_setting',
172 SMPREF_SENT_SUBFOLDERS_DISABLED
);
173 $sent_subfolders_base = getPref($data_dir, $username,
174 'sent_subfolders_base', $sent_folder);
176 if ($use_sent_subfolders ||
$move_to_sent) {
179 $quarter = sent_subfolder_getQuarter($month);
182 * Regarding the structure we've got three main possibilities.
183 * One sent holder. level 0.
184 * Multiple year holders with messages in it. level 1.
185 * Multiple year folders with holders in it. level 2.
188 switch ($sent_subfolders_setting) {
189 case SMPREF_SENT_SUBFOLDERS_YEARLY
:
191 $sent_subfolder = $sent_subfolders_base . $delimiter
194 case SMPREF_SENT_SUBFOLDERS_QUARTERLY
:
196 $sent_subfolder = $sent_subfolders_base . $delimiter
198 . $delimiter . $quarter;
199 $year_folder = $sent_subfolders_base . $delimiter
202 case SMPREF_SENT_SUBFOLDERS_MONTHLY
:
204 $sent_subfolder = $sent_subfolders_base . $delimiter
206 . $delimiter . $month;
207 $year_folder = $sent_subfolders_base. $delimiter . $year;
209 case SMPREF_SENT_SUBFOLDERS_DISABLED
:
212 $sent_subfolder = $sent_folder;
213 $year_folder = $sent_folder;
216 /* If this folder is NOT the current sent folder, update stuff. */
217 if ($sent_subfolder != $sent_folder) {
218 /* Auto-create folders, if they do not yet exist. */
219 if ($sent_subfolder != 'none') {
220 /* Create the imap connection. */
221 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10, $imap_stream_options);
225 * If sent_subfolder can't store messages (noselect) ||
226 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
227 * subfolder_base can't store subfolders (noinferiors), setup is broken
229 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
230 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
231 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
232 error_box(_("Sent subfolders options are misconfigured."));
235 /* Auto-create the year folder, if it does not yet exist. */
236 if (!sqimap_mailbox_exists($ic, $year_folder)) {
237 sqimap_mailbox_create($ic, $year_folder, 'noselect');
238 // TODO: safety check for imap servers that can't create subfolders
240 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
241 sqimap_subscribe($ic, $year_folder);
245 /* Auto-create the subfolder, if it does not yet exist. */
246 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
247 sqimap_mailbox_create($ic, $sent_subfolder, '');
248 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
249 sqimap_subscribe($ic, $sent_subfolder);
251 /* Update sent_folder setting in prefs only if the base
252 subfolders setting is not the same as the normal sent
253 folder... otherwise, it is quite misleading to the user.
254 If the sent folder is the same as the subfolders base, it's
255 OK to leave the sent folder as is.
256 The sent_folder setting itself needs to be the actual
257 subfolder (not the base) for proper functionality */
258 if ($sent_subfolders_base != $sent_folder) {
259 setPref($data_dir, $username, 'sent_folder', $sent_subfolders_base);
260 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON
);
261 setPref($data_dir, $username, 'translate_special_folders', SMPREF_OFF
);
263 $sent_folder = $sent_subfolder;
264 $move_to_sent = SMPREF_ON
;
266 /* Close the imap connection. */
275 * Sets quarter subfolder names
277 * @param string $month numeric month
278 * @return string quarter name (Q + number)
280 function sent_subfolder_getQuarter($month) {
306 /* Return the current quarter. */
307 return ('Q' . $result);
311 * detects if mailbox is part of sent_subfolders
313 * @param string $mb imap folder name
314 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
316 function sent_subfolders_special_mailbox_do($mb) {
317 global $data_dir, $username, $sent_folder;
319 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
321 $use_sent_subfolders = getPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
323 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', $sent_folder);
326 * If sent_subfolders are used and mailbox is equal to subfolder base
327 * or mailbox matches subfolder base + delimiter.
329 if ($use_sent_subfolders == SMPREF_ON
&&
330 ($mb == $sent_subfolders_base ||
stristr($mb,$sent_subfolders_base . $delimiter) ) ) {