adding sqimap functions to check status of folder. Code reuse.
[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 *
4b4abf93 8 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
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
a3439b27 64 $sent_subfolders_base = 'INBOX.Sent';
65 $args = func_get_arg(0);
b587ac51 66 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
a3439b27 67
68 /* Only check the folder string if we have been passed a mailbox. */
69 if ($use_sent_subfolders && (count($args) > 1)) {
70 /* Chop up the folder strings as needed. */
71 $base_str = $sent_subfolders_base . $delimiter;
72 $mbox_str = substr($args[1], 0, strlen($base_str));
73
74 /* Perform the comparison. */
75 $handleAsSent_result =
76 ( $handleAsSent_result
d79e01f5 77 || ($base_str == $mbox_str)
78 || ($sent_subfolders_base == $args[1])
a3439b27 79 );
80 }
81}
82
bb58f260 83/**
84 * Loads sent_subfolders settings
85 */
a3439b27 86function sent_subfolders_load_prefs() {
88cb1b4d 87 global $use_sent_subfolders, $data_dir, $username,
88 $sent_subfolders_setting, $sent_subfolders_base;
a3439b27 89
90 $use_sent_subfolders = getPref
91 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
d79e01f5 92
a3439b27 93 $sent_subfolders_setting = getPref
94 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
95
96 $sent_subfolders_base = getPref
97 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
98}
99
bb58f260 100/**
101 * Adds sent_subfolders options in folder preferences
102 */
a3439b27 103function sent_subfolders_optpage_loadhook_folders() {
367fb0ca 104 global $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
04f6008a 105
b587ac51 106 sqgetGlobalVar('username', $username, SQ_SESSION);
107 sqgetGlobalVar('key', $key, SQ_COOKIE);
a3439b27 108
109 /* Get some imap data we need later. */
110 $imapConnection =
111 sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
112 $boxes = sqimap_mailbox_list($imapConnection);
113 sqimap_logout($imapConnection);
114
115 /* Load the Sent Subfolder Options into an array. */
116 $optgrp = _("Sent Subfolders Options");
117 $optvals = array();
118
119 $optvals[] = array(
120 'name' => 'sent_subfolders_setting',
121 'caption' => _("Use Sent Subfolders"),
122 'type' => SMOPT_TYPE_STRLIST,
123 'refresh' => SMOPT_REFRESH_FOLDERLIST,
124 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
d79e01f5 125 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
126 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
127 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
a3439b27 128 'save' => 'save_option_sent_subfolders_setting'
129 );
130
2589ba9a 131 $filtered_folders=array_filter($boxes, "filter_folders");
132 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
a3439b27 133
134 $optvals[] = array(
135 'name' => 'sent_subfolders_base',
136 'caption' => _("Base Sent Folder"),
2589ba9a 137 'type' => SMOPT_TYPE_FLDRLIST,
a3439b27 138 'refresh' => SMOPT_REFRESH_FOLDERLIST,
99ecf044 139 'posvals' => $sent_subfolders_base_values,
140 'folder_filter' => 'noinferiors'
a3439b27 141 );
142
367fb0ca 143 if ($show_contain_subfolders_option) {
144 $optvals[] = array(
145 'name' => 'sent_subfolders_warning',
146 'caption' => _("Warning"),
147 'type' => SMOPT_TYPE_COMMENT,
148 'comment' => _("There are some restrictions in Sent Subfolder options.")
149 );
150 }
151
a3439b27 152 /* Add our option data to the global array. */
153 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
154 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
155}
156
2589ba9a 157/**
158 * Defines folder filtering rules
159 *
160 * Callback function that should exclude some folders from folder listing.
161 * @param array $fldr list of folders. See sqimap_mailbox_list
162 * @return boolean returns true, if folder has to included in folder listing
91e0dccc 163 * @access private
2589ba9a 164 */
165function filter_folders($fldr) {
166 return strtolower($fldr['unformatted'])!='inbox';
167}
168
bb58f260 169/**
170 * Saves sent_subfolder_options
171 */
a3439b27 172function save_option_sent_subfolders_setting($option) {
173 global $data_dir, $username, $use_sent_subfolders;
174
175 /* Set use_sent_subfolders as either on or off. */
176 if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
177 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
178 } else {
179 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
180 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
181 }
182
183 /* Now just save the option as normal. */
184 save_option($option);
185}
186
bb58f260 187/**
188 * Update sent_subfolders settings
189 *
91e0dccc 190 * function updates default sent folder value and
bb58f260 191 * creates required imap folders
192 */
a3439b27 193function sent_subfolders_update_sentfolder() {
ce68b76b 194 global $sent_folder;
a3439b27 195 global $sent_subfolders_base, $sent_subfolders_setting;
367fb0ca 196 global $data_dir, $imapServerAddress, $imapPort, $color;
ce68b76b 197 global $use_sent_subfolders, $move_to_sent;
d79e01f5 198
b587ac51 199 sqgetGlobalVar('username', $username, SQ_SESSION);
200 sqgetGlobalVar('key', $key, SQ_COOKIE);
201 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
91e0dccc 202
1c52ba77 203 if ($use_sent_subfolders || $move_to_sent) {
a3439b27 204 $year = date('Y');
205 $month = date('m');
206 $quarter = sent_subfolder_getQuarter($month);
207
99ecf044 208 /**
209 * Regarding the structure we've got three main possibilities.
210 * One sent holder. level 0.
211 * Multiple year holders with messages in it. level 1.
212 * Multiple year folders with holders in it. level 2.
213 */
4965e012 214 $cnd_delimiter = $delimiter;
91e0dccc 215
a3439b27 216 switch ($sent_subfolders_setting) {
1c52ba77 217 case SMPREF_SENT_SUBFOLDERS_YEARLY:
218 $level = 1;
72b9aff9 219 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 220 . $year;
1c52ba77 221 break;
222 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
223 $level = 2;
91e0dccc 224 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 225 . $year
226 . $delimiter . $quarter;
99ecf044 227 $year_folder = $sent_subfolders_base . $cnd_delimiter
a3add160 228 . $year;
1c52ba77 229 break;
230 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
231 $level = 2;
72b9aff9 232 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 233 . $year
234 . $delimiter . $month;
99ecf044 235 $year_folder = $sent_subfolders_base. $cnd_delimiter . $year;
1c52ba77 236 break;
237 case SMPREF_SENT_SUBFOLDERS_DISABLED:
238 default:
239 $level = 0;
240 $sent_subfolder = $sent_folder;
241 $year_folder = $sent_folder;
a3439b27 242 }
243
244 /* If this folder is NOT the current sent folder, update stuff. */
245 if ($sent_subfolder != $sent_folder) {
a3439b27 246 /* Auto-create folders, if they do not yet exist. */
367fb0ca 247 if ($sent_subfolder != 'none') {
a3439b27 248 /* Create the imap connection. */
99ecf044 249 $ic = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
250
367fb0ca 251 $boxes = false;
252 /**
253 * If sent_subfolder can't store messages (noselect) ||
254 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
255 * subfolder_base can't store subfolders (noinferiors), setup is broken
256 */
257 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
258 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
259 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
260 error_box(_("Sent Subfolders plugin is misconfigured."),$color);
261 } else {
262 if ($level==2) {
263 /* Auto-create the year folder, if it does not yet exist. */
264 if (!sqimap_mailbox_exists($ic, $year_folder)) {
265 sqimap_mailbox_create($ic, $year_folder, 'noselect');
266 // TODO: safety check for imap servers that can't create subfolders
267
268 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
269 sqimap_subscribe($ic, $year_folder);
270 }
99ecf044 271 }
d79e01f5 272
367fb0ca 273 /* Auto-create the subfolder, if it does not yet exist. */
274 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
275 sqimap_mailbox_create($ic, $sent_subfolder, '');
276 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
277 sqimap_subscribe($ic, $sent_subfolder);
278 }
279 /* Update sent_folder setting. */
280 setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
281 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
282 $sent_folder = $sent_subfolder;
283 $move_to_sent = SMPREF_ON;
a3439b27 284 }
a3439b27 285 /* Close the imap connection. */
286 sqimap_logout($ic);
287 }
99ecf044 288
a3439b27 289 }
290 }
291}
292
bb58f260 293/**
294 * Sets quarter subfolder names
295 *
296 * @param string $month numeric month
297 * @return string quarter name (Q + number)
298 */
a3439b27 299function sent_subfolder_getQuarter($month) {
300 switch ($month) {
301 case '01':
302 case '02':
d79e01f5 303 case '03':
304 $result = '1';
305 break;
a3439b27 306 case '04':
307 case '05':
d79e01f5 308 case '06':
309 $result = '2';
310 break;
a3439b27 311 case '07':
312 case '08':
d79e01f5 313 case '09':
314 $result = '3';
315 break;
a3439b27 316 case '10':
317 case '11':
d79e01f5 318 case '12':
319 $result = '4';
320 break;
321 default:
322 $result = 'ERR';
a3439b27 323 }
324
325 /* Return the current quarter. */
326 return ('Q' . $result);
327}
328
bb58f260 329/**
330 * detects if mailbox is part of sent_subfolders
331 *
332 * @param string $mb imap folder name
333 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
334 */
335function sent_subfolders_special_mailbox($mb) {
367fb0ca 336 global $data_dir, $username, $delimiter;
bb58f260 337
338 $use_sent_subfolders = getPref
339 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
340 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
341
367fb0ca 342 /**
343 * If sent_subfolders are used and mailbox is equal to subfolder base
344 * or mailbox matches subfolder base + delimiter.
345 */
91e0dccc 346 if ($use_sent_subfolders == SMPREF_ON &&
367fb0ca 347 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
91e0dccc 348 return 1;
bb58f260 349 }
350 return 0;
351}
91e0dccc 352?>