4 * setup.php -- Sent Subfolders Setup File
6 * This is a standard SquirrelMail 1.2 API for plugins.
8 * @copyright 1999-2009 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, $show_contain_subfolders_option, $sent_folder;
55 /* Get some imap data we need later. */
56 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
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
);
136 /* Now just save the option as normal. */
137 save_option($option);
141 * Update the folder settings/auto-create new subfolder
143 function save_option_sent_subfolders_base($option) {
144 // first save the option as normal
145 save_option($option);
147 // now update folder settings and auto-create first subfolder if needed
148 sent_subfolders_update_sentfolder_do();
152 * Update sent_subfolders settings
154 * function updates default sent folder value and
155 * creates required imap folders
157 function sent_subfolders_update_sentfolder_do() {
158 global $sent_folder, $username,
159 $data_dir, $imapServerAddress, $imapPort,
162 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
164 $use_sent_subfolders = getPref($data_dir, $username,
165 'use_sent_subfolders', SMPREF_OFF
);
166 $sent_subfolders_setting = getPref($data_dir, $username,
167 'sent_subfolders_setting',
168 SMPREF_SENT_SUBFOLDERS_DISABLED
);
169 $sent_subfolders_base = getPref($data_dir, $username,
170 'sent_subfolders_base', $sent_folder);
172 if ($use_sent_subfolders ||
$move_to_sent) {
175 $quarter = sent_subfolder_getQuarter($month);
178 * Regarding the structure we've got three main possibilities.
179 * One sent holder. level 0.
180 * Multiple year holders with messages in it. level 1.
181 * Multiple year folders with holders in it. level 2.
184 switch ($sent_subfolders_setting) {
185 case SMPREF_SENT_SUBFOLDERS_YEARLY
:
187 $sent_subfolder = $sent_subfolders_base . $delimiter
190 case SMPREF_SENT_SUBFOLDERS_QUARTERLY
:
192 $sent_subfolder = $sent_subfolders_base . $delimiter
194 . $delimiter . $quarter;
195 $year_folder = $sent_subfolders_base . $delimiter
198 case SMPREF_SENT_SUBFOLDERS_MONTHLY
:
200 $sent_subfolder = $sent_subfolders_base . $delimiter
202 . $delimiter . $month;
203 $year_folder = $sent_subfolders_base. $delimiter . $year;
205 case SMPREF_SENT_SUBFOLDERS_DISABLED
:
208 $sent_subfolder = $sent_folder;
209 $year_folder = $sent_folder;
212 /* If this folder is NOT the current sent folder, update stuff. */
213 if ($sent_subfolder != $sent_folder) {
214 /* Auto-create folders, if they do not yet exist. */
215 if ($sent_subfolder != 'none') {
216 /* Create the imap connection. */
217 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
221 * If sent_subfolder can't store messages (noselect) ||
222 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
223 * subfolder_base can't store subfolders (noinferiors), setup is broken
225 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
226 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
227 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
228 error_box(_("Sent subfolders options are misconfigured."));
231 /* Auto-create the year folder, if it does not yet exist. */
232 if (!sqimap_mailbox_exists($ic, $year_folder)) {
233 sqimap_mailbox_create($ic, $year_folder, 'noselect');
234 // TODO: safety check for imap servers that can't create subfolders
236 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
237 sqimap_subscribe($ic, $year_folder);
241 /* Auto-create the subfolder, if it does not yet exist. */
242 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
243 sqimap_mailbox_create($ic, $sent_subfolder, '');
244 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
245 sqimap_subscribe($ic, $sent_subfolder);
247 /* Update sent_folder setting in prefs only if the base
248 subfolders setting is not the same as the normal sent
249 folder... otherwise, it is quite misleading to the user.
250 If the sent folder is the same as the subfolders base, it's
251 OK to leave the sent folder as is.
252 The sent_folder setting itself needs to be the actual
253 subfolder (not the base) for proper functionality */
254 if ($sent_subfolders_base != $sent_folder) {
255 setPref($data_dir, $username, 'sent_folder', $sent_subfolders_base);
256 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON
);
257 setPref($data_dir, $username, 'translate_special_folders', SMPREF_OFF
);
259 $sent_folder = $sent_subfolder;
260 $move_to_sent = SMPREF_ON
;
262 /* Close the imap connection. */
271 * Sets quarter subfolder names
273 * @param string $month numeric month
274 * @return string quarter name (Q + number)
276 function sent_subfolder_getQuarter($month) {
302 /* Return the current quarter. */
303 return ('Q' . $result);
307 * detects if mailbox is part of sent_subfolders
309 * @param string $mb imap folder name
310 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
312 function sent_subfolders_special_mailbox_do($mb) {
313 global $data_dir, $username, $sent_folder;
315 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
317 $use_sent_subfolders = getPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
319 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', $sent_folder);
322 * If sent_subfolders are used and mailbox is equal to subfolder base
323 * or mailbox matches subfolder base + delimiter.
325 if ($use_sent_subfolders == SMPREF_ON
&&
326 ($mb == $sent_subfolders_base ||
stristr($mb,$sent_subfolders_base . $delimiter) ) ) {