Add RFC 2231 support. Thanks to Piotr Pawlow. (#2501379)
[squirrelmail.git] / plugins / sent_subfolders / setup.php
CommitLineData
a3439b27 1<?php
2
3/**
4 * setup.php -- Sent Subfolders Setup File
5 *
598294a7 6 * This is a standard SquirrelMail 1.2 API for plugins.
a3439b27 7 *
4b5049de 8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
4f51df66 10 * @version $Id$
ea5f4b8e 11 * @package plugins
12 * @subpackage sent_subfolders
a3439b27 13 */
14
91e0dccc 15/**
ea5f4b8e 16 */
a3439b27 17define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
18define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
19define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
20define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
21define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
22
bb58f260 23/**
598294a7 24 * Adds plugin to SquirrelMail's hooks
bb58f260 25 */
a3439b27 26function squirrelmail_plugin_init_sent_subfolders() {
27 /* Standard initialization API. */
28 global $squirrelmail_plugin_hooks;
29
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';
34
35 /* The hooks to automatically update sent subfolders. */
36 $squirrelmail_plugin_hooks
37 ['left_main_before']['sent_subfolders'] =
38 'sent_subfolders_update_sentfolder';
39
40 $squirrelmail_plugin_hooks
41 ['compose_send']['sent_subfolders'] =
42 'sent_subfolders_update_sentfolder';
43
44 /* The hook to load the sent subfolders prefs. */
45 $squirrelmail_plugin_hooks
46 ['loading_prefs']['sent_subfolders'] =
47 'sent_subfolders_load_prefs';
48
49 /* The hooks to handle sent subfolders options. */
50 $squirrelmail_plugin_hooks
51 ['optpage_loadhook_folder']['sent_subfolders'] =
52 'sent_subfolders_optpage_loadhook_folders';
bb58f260 53
54 /* mark base sent folder as special mailbox */
55 $squirrelmail_plugin_hooks
91e0dccc 56 ['special_mailbox']['sent_subfolders'] =
57 'sent_subfolders_special_mailbox';
a3439b27 58}
59
60function sent_subfolders_check_handleAsSent() {
b6b31355 61 global $handleAsSent_result, $sent_subfolders_base,
04f6008a 62 $use_sent_subfolders;
b587ac51 63
53445d9f 64 // FIXME: hardcoded folder
a3439b27 65 $sent_subfolders_base = 'INBOX.Sent';
66 $args = func_get_arg(0);
b587ac51 67 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
a3439b27 68
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));
74
75 /* Perform the comparison. */
76 $handleAsSent_result =
77 ( $handleAsSent_result
d79e01f5 78 || ($base_str == $mbox_str)
79 || ($sent_subfolders_base == $args[1])
a3439b27 80 );
81 }
82}
83
bb58f260 84/**
85 * Loads sent_subfolders settings
86 */
a3439b27 87function sent_subfolders_load_prefs() {
88cb1b4d 88 global $use_sent_subfolders, $data_dir, $username,
89 $sent_subfolders_setting, $sent_subfolders_base;
a3439b27 90
91 $use_sent_subfolders = getPref
92 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
d79e01f5 93
a3439b27 94 $sent_subfolders_setting = getPref
95 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
96
97 $sent_subfolders_base = getPref
98 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
99}
100
bb58f260 101/**
102 * Adds sent_subfolders options in folder preferences
103 */
a3439b27 104function sent_subfolders_optpage_loadhook_folders() {
2128bbc6 105 global $username, $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
a3439b27 106
107 /* Get some imap data we need later. */
045ec1a1 108 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
a3439b27 109 $boxes = sqimap_mailbox_list($imapConnection);
110 sqimap_logout($imapConnection);
111
112 /* Load the Sent Subfolder Options into an array. */
113 $optgrp = _("Sent Subfolders Options");
114 $optvals = array();
115
116 $optvals[] = array(
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"),
d79e01f5 122 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
123 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
124 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
a3439b27 125 'save' => 'save_option_sent_subfolders_setting'
126 );
127
2589ba9a 128 $filtered_folders=array_filter($boxes, "filter_folders");
129 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
a3439b27 130
131 $optvals[] = array(
132 'name' => 'sent_subfolders_base',
133 'caption' => _("Base Sent Folder"),
2589ba9a 134 'type' => SMOPT_TYPE_FLDRLIST,
a3439b27 135 'refresh' => SMOPT_REFRESH_FOLDERLIST,
99ecf044 136 'posvals' => $sent_subfolders_base_values,
137 'folder_filter' => 'noinferiors'
a3439b27 138 );
139
367fb0ca 140 if ($show_contain_subfolders_option) {
141 $optvals[] = array(
142 'name' => 'sent_subfolders_warning',
143 'caption' => _("Warning"),
144 'type' => SMOPT_TYPE_COMMENT,
145 'comment' => _("There are some restrictions in Sent Subfolder options.")
146 );
147 }
148
a3439b27 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;
152}
153
2589ba9a 154/**
155 * Defines folder filtering rules
156 *
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
91e0dccc 160 * @access private
2589ba9a 161 */
162function filter_folders($fldr) {
163 return strtolower($fldr['unformatted'])!='inbox';
164}
165
bb58f260 166/**
167 * Saves sent_subfolder_options
168 */
a3439b27 169function save_option_sent_subfolders_setting($option) {
170 global $data_dir, $username, $use_sent_subfolders;
171
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);
175 } else {
176 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
177 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
178 }
179
180 /* Now just save the option as normal. */
181 save_option($option);
182}
183
bb58f260 184/**
185 * Update sent_subfolders settings
186 *
91e0dccc 187 * function updates default sent folder value and
bb58f260 188 * creates required imap folders
189 */
a3439b27 190function sent_subfolders_update_sentfolder() {
2128bbc6 191 global $sent_folder, $username;
a3439b27 192 global $sent_subfolders_base, $sent_subfolders_setting;
fb0bcfdb 193 global $data_dir, $imapServerAddress, $imapPort;
ce68b76b 194 global $use_sent_subfolders, $move_to_sent;
d79e01f5 195
b587ac51 196 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
91e0dccc 197
1c52ba77 198 if ($use_sent_subfolders || $move_to_sent) {
a3439b27 199 $year = date('Y');
200 $month = date('m');
201 $quarter = sent_subfolder_getQuarter($month);
202
99ecf044 203 /**
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.
208 */
91e0dccc 209
a3439b27 210 switch ($sent_subfolders_setting) {
1c52ba77 211 case SMPREF_SENT_SUBFOLDERS_YEARLY:
212 $level = 1;
6d6bb41d 213 $sent_subfolder = $sent_subfolders_base . $delimiter
1c52ba77 214 . $year;
1c52ba77 215 break;
216 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
217 $level = 2;
6d6bb41d 218 $sent_subfolder = $sent_subfolders_base . $delimiter
1c52ba77 219 . $year
220 . $delimiter . $quarter;
6d6bb41d 221 $year_folder = $sent_subfolders_base . $delimiter
a3add160 222 . $year;
1c52ba77 223 break;
224 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
225 $level = 2;
6d6bb41d 226 $sent_subfolder = $sent_subfolders_base . $delimiter
1c52ba77 227 . $year
228 . $delimiter . $month;
6d6bb41d 229 $year_folder = $sent_subfolders_base. $delimiter . $year;
1c52ba77 230 break;
231 case SMPREF_SENT_SUBFOLDERS_DISABLED:
232 default:
233 $level = 0;
234 $sent_subfolder = $sent_folder;
235 $year_folder = $sent_folder;
a3439b27 236 }
237
238 /* If this folder is NOT the current sent folder, update stuff. */
239 if ($sent_subfolder != $sent_folder) {
a3439b27 240 /* Auto-create folders, if they do not yet exist. */
367fb0ca 241 if ($sent_subfolder != 'none') {
a3439b27 242 /* Create the imap connection. */
045ec1a1 243 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
99ecf044 244
367fb0ca 245 $boxes = false;
246 /**
247 * If sent_subfolder can't store messages (noselect) ||
248 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
249 * subfolder_base can't store subfolders (noinferiors), setup is broken
250 */
251 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
252 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
253 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
1b858d86 254 error_box(_("Sent Subfolders plugin is misconfigured."));
367fb0ca 255 } else {
256 if ($level==2) {
257 /* Auto-create the year folder, if it does not yet exist. */
258 if (!sqimap_mailbox_exists($ic, $year_folder)) {
259 sqimap_mailbox_create($ic, $year_folder, 'noselect');
260 // TODO: safety check for imap servers that can't create subfolders
261
262 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
263 sqimap_subscribe($ic, $year_folder);
264 }
99ecf044 265 }
d79e01f5 266
367fb0ca 267 /* Auto-create the subfolder, if it does not yet exist. */
268 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
269 sqimap_mailbox_create($ic, $sent_subfolder, '');
270 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
271 sqimap_subscribe($ic, $sent_subfolder);
272 }
273 /* Update sent_folder setting. */
53445d9f 274 //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
275 //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
367fb0ca 276 $sent_folder = $sent_subfolder;
277 $move_to_sent = SMPREF_ON;
a3439b27 278 }
a3439b27 279 /* Close the imap connection. */
280 sqimap_logout($ic);
281 }
99ecf044 282
a3439b27 283 }
284 }
285}
286
bb58f260 287/**
288 * Sets quarter subfolder names
289 *
290 * @param string $month numeric month
291 * @return string quarter name (Q + number)
292 */
a3439b27 293function sent_subfolder_getQuarter($month) {
294 switch ($month) {
295 case '01':
296 case '02':
d79e01f5 297 case '03':
298 $result = '1';
299 break;
a3439b27 300 case '04':
301 case '05':
d79e01f5 302 case '06':
303 $result = '2';
304 break;
a3439b27 305 case '07':
306 case '08':
d79e01f5 307 case '09':
308 $result = '3';
309 break;
a3439b27 310 case '10':
311 case '11':
d79e01f5 312 case '12':
313 $result = '4';
314 break;
315 default:
316 $result = 'ERR';
a3439b27 317 }
318
319 /* Return the current quarter. */
320 return ('Q' . $result);
321}
322
bb58f260 323/**
324 * detects if mailbox is part of sent_subfolders
325 *
326 * @param string $mb imap folder name
327 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
328 */
329function sent_subfolders_special_mailbox($mb) {
6d6bb41d 330 global $data_dir, $username;
331
332 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
bb58f260 333
334 $use_sent_subfolders = getPref
335 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
336 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
337
367fb0ca 338 /**
339 * If sent_subfolders are used and mailbox is equal to subfolder base
340 * or mailbox matches subfolder base + delimiter.
341 */
91e0dccc 342 if ($use_sent_subfolders == SMPREF_ON &&
367fb0ca 343 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
91e0dccc 344 return 1;
bb58f260 345 }
346 return 0;
347}