fix listcommands template for Send form changes
[squirrelmail.git] / plugins / sent_subfolders / functions.php
1 <?php
2
3 /**
4 * setup.php -- Sent Subfolders Setup File
5 *
6 * This is a standard SquirrelMail 1.2 API for plugins.
7 *
8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package plugins
12 * @subpackage sent_subfolders
13 */
14
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');
20
21 function sent_subfolders_check_handleAsSent_do($mailbox) {
22
23 global $handleAsSent_result, $data_dir, $username, $sent_folder;
24
25 // don't need to bother if it's already special
26 if ($handleAsSent_result) return;
27
28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
29
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);
34
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));
40
41 /* Perform the comparison. */
42 $handleAsSent_result = ( ($base_str == $mbox_str)
43 || ($sent_subfolders_base == $mailbox) );
44 }
45 }
46
47 /**
48 * Adds sent_subfolders options in folder preferences
49 */
50 function sent_subfolders_optpage_loadhook_folders_do() {
51
52 global $data_dir, $username, $optpage_data, $imapServerAddress,
53 $imapPort, $show_contain_subfolders_option, $sent_folder;
54
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);
59
60 /* Load the Sent Subfolder Options into an array. */
61 $optgrp = _("Sent Subfolders Options");
62 $optvals = array();
63
64 global $sent_subfolders_setting;
65 $sent_subfolders_setting = getPref($data_dir, $username,
66 'sent_subfolders_setting',
67 SMPREF_SENT_SUBFOLDERS_DISABLED);
68 $optvals[] = array(
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'
78 );
79
80 $filtered_folders=array_filter($boxes, "filter_folders");
81 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
82
83 global $sent_subfolders_base;
84 $sent_subfolders_base = getPref($data_dir, $username,
85 'sent_subfolders_base', $sent_folder);
86 $optvals[] = array(
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'
94 );
95
96 if ($show_contain_subfolders_option) {
97 $optvals[] = array(
98 'name' => 'sent_subfolders_warning',
99 'caption' => _("Warning"),
100 'type' => SMOPT_TYPE_COMMENT,
101 'comment' => _("There are some restrictions in Sent Subfolder options.")
102 );
103 }
104
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;
108 }
109
110 /**
111 * Defines folder filtering rules
112 *
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
116 * @access private
117 */
118 function filter_folders($fldr) {
119 return strtolower($fldr['unformatted'])!='inbox';
120 }
121
122 /**
123 * Saves sent_subfolder_options
124 */
125 function save_option_sent_subfolders_setting($option) {
126 global $data_dir, $username;
127
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);
131 } else {
132 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
133 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
134 }
135
136 /* Now just save the option as normal. */
137 save_option($option);
138 }
139
140 /**
141 * Update the folder settings/auto-create new subfolder
142 */
143 function save_option_sent_subfolders_base($option) {
144 // first save the option as normal
145 save_option($option);
146
147 // now update folder settings and auto-create first subfolder if needed
148 sent_subfolders_update_sentfolder_do();
149 }
150
151 /**
152 * Update sent_subfolders settings
153 *
154 * function updates default sent folder value and
155 * creates required imap folders
156 */
157 function sent_subfolders_update_sentfolder_do() {
158 global $sent_folder, $username,
159 $data_dir, $imapServerAddress, $imapPort,
160 $move_to_sent;
161
162 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
163
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);
171
172 if ($use_sent_subfolders || $move_to_sent) {
173 $year = date('Y');
174 $month = date('m');
175 $quarter = sent_subfolder_getQuarter($month);
176
177 /**
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.
182 */
183
184 switch ($sent_subfolders_setting) {
185 case SMPREF_SENT_SUBFOLDERS_YEARLY:
186 $level = 1;
187 $sent_subfolder = $sent_subfolders_base . $delimiter
188 . $year;
189 break;
190 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
191 $level = 2;
192 $sent_subfolder = $sent_subfolders_base . $delimiter
193 . $year
194 . $delimiter . $quarter;
195 $year_folder = $sent_subfolders_base . $delimiter
196 . $year;
197 break;
198 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
199 $level = 2;
200 $sent_subfolder = $sent_subfolders_base . $delimiter
201 . $year
202 . $delimiter . $month;
203 $year_folder = $sent_subfolders_base. $delimiter . $year;
204 break;
205 case SMPREF_SENT_SUBFOLDERS_DISABLED:
206 default:
207 $level = 0;
208 $sent_subfolder = $sent_folder;
209 $year_folder = $sent_folder;
210 }
211
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);
218
219 $boxes = false;
220 /**
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
224 */
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."));
229 } else {
230 if ($level==2) {
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
235
236 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
237 sqimap_subscribe($ic, $year_folder);
238 }
239 }
240
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);
246 }
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);
258 }
259 $sent_folder = $sent_subfolder;
260 $move_to_sent = SMPREF_ON;
261 }
262 /* Close the imap connection. */
263 sqimap_logout($ic);
264 }
265
266 }
267 }
268 }
269
270 /**
271 * Sets quarter subfolder names
272 *
273 * @param string $month numeric month
274 * @return string quarter name (Q + number)
275 */
276 function sent_subfolder_getQuarter($month) {
277 switch ($month) {
278 case '01':
279 case '02':
280 case '03':
281 $result = '1';
282 break;
283 case '04':
284 case '05':
285 case '06':
286 $result = '2';
287 break;
288 case '07':
289 case '08':
290 case '09':
291 $result = '3';
292 break;
293 case '10':
294 case '11':
295 case '12':
296 $result = '4';
297 break;
298 default:
299 $result = 'ERR';
300 }
301
302 /* Return the current quarter. */
303 return ('Q' . $result);
304 }
305
306 /**
307 * detects if mailbox is part of sent_subfolders
308 *
309 * @param string $mb imap folder name
310 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
311 */
312 function sent_subfolders_special_mailbox_do($mb) {
313 global $data_dir, $username, $sent_folder;
314
315 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
316
317 $use_sent_subfolders = getPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
318
319 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', $sent_folder);
320
321 /**
322 * If sent_subfolders are used and mailbox is equal to subfolder base
323 * or mailbox matches subfolder base + delimiter.
324 */
325 if ($use_sent_subfolders == SMPREF_ON &&
326 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
327 return 1;
328 }
329 return 0;
330 }