4 * setup.php -- Sent Subfolders Setup File
6 * This is a standard SquirrelMail 1.2 API for plugins.
8 * @copyright © 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @subpackage sent_subfolders
17 define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
18 define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
19 define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
20 define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
21 define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
24 * Adds plugin to SquirrelMail's hooks
26 function squirrelmail_plugin_init_sent_subfolders() {
27 /* Standard initialization API. */
28 global $squirrelmail_plugin_hooks;
30 /* The hooks to make the sent subfolders display correctly. */
31 $squirrelmail_plugin_hooks
32 ['check_handleAsSent_result']['sent_subfolders'] =
33 'sent_subfolders_check_handleAsSent';
35 /* The hooks to automatically update sent subfolders. */
36 $squirrelmail_plugin_hooks
37 ['left_main_before']['sent_subfolders'] =
38 'sent_subfolders_update_sentfolder';
40 $squirrelmail_plugin_hooks
41 ['compose_send']['sent_subfolders'] =
42 'sent_subfolders_update_sentfolder';
44 /* The hook to load the sent subfolders prefs. */
45 $squirrelmail_plugin_hooks
46 ['loading_prefs']['sent_subfolders'] =
47 'sent_subfolders_load_prefs';
49 /* The hooks to handle sent subfolders options. */
50 $squirrelmail_plugin_hooks
51 ['optpage_loadhook_folder']['sent_subfolders'] =
52 'sent_subfolders_optpage_loadhook_folders';
54 /* mark base sent folder as special mailbox */
55 $squirrelmail_plugin_hooks
56 ['special_mailbox']['sent_subfolders'] =
57 'sent_subfolders_special_mailbox';
60 function sent_subfolders_check_handleAsSent() {
61 global $handleAsSent_result, $sent_subfolders_base,
64 // FIXME: hardcoded folder
65 $sent_subfolders_base = 'INBOX.Sent';
66 $args = func_get_arg(0);
67 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
69 /* Only check the folder string if we have been passed a mailbox. */
70 if ($use_sent_subfolders && (count($args) > 1)) {
71 /* Chop up the folder strings as needed. */
72 $base_str = $sent_subfolders_base . $delimiter;
73 $mbox_str = substr($args[1], 0, strlen($base_str));
75 /* Perform the comparison. */
76 $handleAsSent_result =
77 ( $handleAsSent_result
78 ||
($base_str == $mbox_str)
79 ||
($sent_subfolders_base == $args[1])
85 * Loads sent_subfolders settings
87 function sent_subfolders_load_prefs() {
88 global $use_sent_subfolders, $data_dir, $username,
89 $sent_subfolders_setting, $sent_subfolders_base;
91 $use_sent_subfolders = getPref
92 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
94 $sent_subfolders_setting = getPref
95 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED
);
97 $sent_subfolders_base = getPref
98 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE
);
102 * Adds sent_subfolders options in folder preferences
104 function sent_subfolders_optpage_loadhook_folders() {
105 global $username, $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
107 /* Get some imap data we need later. */
108 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
109 $boxes = sqimap_mailbox_list($imapConnection);
110 sqimap_logout($imapConnection);
112 /* Load the Sent Subfolder Options into an array. */
113 $optgrp = _("Sent Subfolders Options");
117 'name' => 'sent_subfolders_setting',
118 'caption' => _("Use Sent Subfolders"),
119 'type' => SMOPT_TYPE_STRLIST
,
120 'refresh' => SMOPT_REFRESH_FOLDERLIST
,
121 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED
=> _("Disabled"),
122 SMPREF_SENT_SUBFOLDERS_MONTHLY
=> _("Monthly"),
123 SMPREF_SENT_SUBFOLDERS_QUARTERLY
=> _("Quarterly"),
124 SMPREF_SENT_SUBFOLDERS_YEARLY
=> _("Yearly")),
125 'save' => 'save_option_sent_subfolders_setting'
128 $filtered_folders=array_filter($boxes, "filter_folders");
129 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
132 'name' => 'sent_subfolders_base',
133 'caption' => _("Base Sent Folder"),
134 'type' => SMOPT_TYPE_FLDRLIST
,
135 'refresh' => SMOPT_REFRESH_FOLDERLIST
,
136 'posvals' => $sent_subfolders_base_values,
137 'folder_filter' => 'noinferiors'
140 if ($show_contain_subfolders_option) {
142 'name' => 'sent_subfolders_warning',
143 'caption' => _("Warning"),
144 'type' => SMOPT_TYPE_COMMENT
,
145 'comment' => _("There are some restrictions in Sent Subfolder options.")
149 /* Add our option data to the global array. */
150 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS
] = $optgrp;
151 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS
] = $optvals;
155 * Defines folder filtering rules
157 * Callback function that should exclude some folders from folder listing.
158 * @param array $fldr list of folders. See sqimap_mailbox_list
159 * @return boolean returns true, if folder has to included in folder listing
162 function filter_folders($fldr) {
163 return strtolower($fldr['unformatted'])!='inbox';
167 * Saves sent_subfolder_options
169 function save_option_sent_subfolders_setting($option) {
170 global $data_dir, $username, $use_sent_subfolders;
172 /* Set use_sent_subfolders as either on or off. */
173 if ($option->new_value
== SMPREF_SENT_SUBFOLDERS_DISABLED
) {
174 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
176 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON
);
177 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON
);
180 /* Now just save the option as normal. */
181 save_option($option);
185 * Update sent_subfolders settings
187 * function updates default sent folder value and
188 * creates required imap folders
190 function sent_subfolders_update_sentfolder() {
191 global $sent_folder, $username;
192 global $sent_subfolders_base, $sent_subfolders_setting;
193 global $data_dir, $imapServerAddress, $imapPort, $color;
194 global $use_sent_subfolders, $move_to_sent;
196 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
198 if ($use_sent_subfolders ||
$move_to_sent) {
201 $quarter = sent_subfolder_getQuarter($month);
204 * Regarding the structure we've got three main possibilities.
205 * One sent holder. level 0.
206 * Multiple year holders with messages in it. level 1.
207 * Multiple year folders with holders in it. level 2.
209 $cnd_delimiter = $delimiter;
211 switch ($sent_subfolders_setting) {
212 case SMPREF_SENT_SUBFOLDERS_YEARLY
:
214 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
217 case SMPREF_SENT_SUBFOLDERS_QUARTERLY
:
219 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
221 . $delimiter . $quarter;
222 $year_folder = $sent_subfolders_base . $cnd_delimiter
225 case SMPREF_SENT_SUBFOLDERS_MONTHLY
:
227 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
229 . $delimiter . $month;
230 $year_folder = $sent_subfolders_base. $cnd_delimiter . $year;
232 case SMPREF_SENT_SUBFOLDERS_DISABLED
:
235 $sent_subfolder = $sent_folder;
236 $year_folder = $sent_folder;
239 /* If this folder is NOT the current sent folder, update stuff. */
240 if ($sent_subfolder != $sent_folder) {
241 /* Auto-create folders, if they do not yet exist. */
242 if ($sent_subfolder != 'none') {
243 /* Create the imap connection. */
244 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
248 * If sent_subfolder can't store messages (noselect) ||
249 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
250 * subfolder_base can't store subfolders (noinferiors), setup is broken
252 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
253 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
254 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
255 error_box(_("Sent Subfolders plugin is misconfigured."));
258 /* Auto-create the year folder, if it does not yet exist. */
259 if (!sqimap_mailbox_exists($ic, $year_folder)) {
260 sqimap_mailbox_create($ic, $year_folder, 'noselect');
261 // TODO: safety check for imap servers that can't create subfolders
263 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
264 sqimap_subscribe($ic, $year_folder);
268 /* Auto-create the subfolder, if it does not yet exist. */
269 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
270 sqimap_mailbox_create($ic, $sent_subfolder, '');
271 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
272 sqimap_subscribe($ic, $sent_subfolder);
274 /* Update sent_folder setting. */
275 //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
276 //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
277 $sent_folder = $sent_subfolder;
278 $move_to_sent = SMPREF_ON
;
280 /* Close the imap connection. */
289 * Sets quarter subfolder names
291 * @param string $month numeric month
292 * @return string quarter name (Q + number)
294 function sent_subfolder_getQuarter($month) {
320 /* Return the current quarter. */
321 return ('Q' . $result);
325 * detects if mailbox is part of sent_subfolders
327 * @param string $mb imap folder name
328 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
330 function sent_subfolders_special_mailbox($mb) {
331 global $data_dir, $username, $delimiter;
333 $use_sent_subfolders = getPref
334 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
335 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
338 * If sent_subfolders are used and mailbox is equal to subfolder base
339 * or mailbox matches subfolder base + delimiter.
341 if ($use_sent_subfolders == SMPREF_ON
&&
342 ($mb == $sent_subfolders_base ||
stristr($mb,$sent_subfolders_base . $delimiter) ) ) {