4 * setup.php -- Sent Subfolders Setup File
6 * This is a standard SquirrelMail 1.2 API for plugins.
8 * @copyright © 1999-2007 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;
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', SMPREF_NONE
);
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;
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', SMPREF_NONE
);
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'
95 if ($show_contain_subfolders_option) {
97 'name' => 'sent_subfolders_warning',
98 'caption' => _("Warning"),
99 'type' => SMOPT_TYPE_COMMENT
,
100 'comment' => _("There are some restrictions in Sent Subfolder options.")
104 /* Add our option data to the global array. */
105 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS
] = $optgrp;
106 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS
] = $optvals;
110 * Defines folder filtering rules
112 * Callback function that should exclude some folders from folder listing.
113 * @param array $fldr list of folders. See sqimap_mailbox_list
114 * @return boolean returns true, if folder has to included in folder listing
117 function filter_folders($fldr) {
118 return strtolower($fldr['unformatted'])!='inbox';
122 * Saves sent_subfolder_options
124 function save_option_sent_subfolders_setting($option) {
125 global $data_dir, $username;
127 /* Set use_sent_subfolders as either on or off. */
128 if ($option->new_value
== SMPREF_SENT_SUBFOLDERS_DISABLED
) {
129 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
131 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON
);
132 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON
);
135 /* Now just save the option as normal. */
136 save_option($option);
140 * Update sent_subfolders settings
142 * function updates default sent folder value and
143 * creates required imap folders
145 function sent_subfolders_update_sentfolder_do() {
146 global $sent_folder, $username,
147 $data_dir, $imapServerAddress, $imapPort,
150 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
152 $use_sent_subfolders = getPref($data_dir, $username,
153 'use_sent_subfolders', SMPREF_OFF
);
154 $sent_subfolders_setting = getPref($data_dir, $username,
155 'sent_subfolders_setting',
156 SMPREF_SENT_SUBFOLDERS_DISABLED
);
157 $sent_subfolders_base = getPref($data_dir, $username,
158 'sent_subfolders_base', SMPREF_NONE
);
160 if ($use_sent_subfolders ||
$move_to_sent) {
163 $quarter = sent_subfolder_getQuarter($month);
166 * Regarding the structure we've got three main possibilities.
167 * One sent holder. level 0.
168 * Multiple year holders with messages in it. level 1.
169 * Multiple year folders with holders in it. level 2.
172 switch ($sent_subfolders_setting) {
173 case SMPREF_SENT_SUBFOLDERS_YEARLY
:
175 $sent_subfolder = $sent_subfolders_base . $delimiter
178 case SMPREF_SENT_SUBFOLDERS_QUARTERLY
:
180 $sent_subfolder = $sent_subfolders_base . $delimiter
182 . $delimiter . $quarter;
183 $year_folder = $sent_subfolders_base . $delimiter
186 case SMPREF_SENT_SUBFOLDERS_MONTHLY
:
188 $sent_subfolder = $sent_subfolders_base . $delimiter
190 . $delimiter . $month;
191 $year_folder = $sent_subfolders_base. $delimiter . $year;
193 case SMPREF_SENT_SUBFOLDERS_DISABLED
:
196 $sent_subfolder = $sent_folder;
197 $year_folder = $sent_folder;
200 /* If this folder is NOT the current sent folder, update stuff. */
201 if ($sent_subfolder != $sent_folder) {
202 /* Auto-create folders, if they do not yet exist. */
203 if ($sent_subfolder != 'none') {
204 /* Create the imap connection. */
205 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
209 * If sent_subfolder can't store messages (noselect) ||
210 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
211 * subfolder_base can't store subfolders (noinferiors), setup is broken
213 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
214 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
215 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
216 error_box(_("Sent Subfolders plugin is misconfigured."));
219 /* Auto-create the year folder, if it does not yet exist. */
220 if (!sqimap_mailbox_exists($ic, $year_folder)) {
221 sqimap_mailbox_create($ic, $year_folder, 'noselect');
222 // TODO: safety check for imap servers that can't create subfolders
224 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
225 sqimap_subscribe($ic, $year_folder);
229 /* Auto-create the subfolder, if it does not yet exist. */
230 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
231 sqimap_mailbox_create($ic, $sent_subfolder, '');
232 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
233 sqimap_subscribe($ic, $sent_subfolder);
235 /* Update sent_folder setting. */
236 //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
237 //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
238 $sent_folder = $sent_subfolder;
239 $move_to_sent = SMPREF_ON
;
241 /* Close the imap connection. */
250 * Sets quarter subfolder names
252 * @param string $month numeric month
253 * @return string quarter name (Q + number)
255 function sent_subfolder_getQuarter($month) {
281 /* Return the current quarter. */
282 return ('Q' . $result);
286 * detects if mailbox is part of sent_subfolders
288 * @param string $mb imap folder name
289 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
291 function sent_subfolders_special_mailbox_do($mb) {
292 global $data_dir, $username;
294 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
296 $use_sent_subfolders = getPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
298 //FIXME: why "na" as the default? Shouldn't this be SMPREF_NONE as it is elsewhere in this file?
299 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
302 * If sent_subfolders are used and mailbox is equal to subfolder base
303 * or mailbox matches subfolder base + delimiter.
305 if ($use_sent_subfolders == SMPREF_ON
&&
306 ($mb == $sent_subfolders_base ||
stristr($mb,$sent_subfolders_base . $delimiter) ) ) {