Give back the message object to those who want it
[squirrelmail.git] / plugins / sent_subfolders / functions.php
CommitLineData
0e21ce8f 1<?php
2
3/**
4 * setup.php -- Sent Subfolders Setup File
5 *
6 * This is a standard SquirrelMail 1.2 API for plugins.
7 *
353d074a 8 * @copyright 1999-2018 The SquirrelMail Project Team
0e21ce8f 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package plugins
12 * @subpackage sent_subfolders
13 */
14
afa916e9 15define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
16define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
17define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
18define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
19define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
20
32a7b3e0 21function sent_subfolders_check_handleAsSent_do($mailbox) {
afa916e9 22
e84b2496 23 global $handleAsSent_result, $data_dir, $username, $sent_folder;
afa916e9 24
25 // don't need to bother if it's already special
26 if ($handleAsSent_result) return;
0e21ce8f 27
0e21ce8f 28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
29
afa916e9 30 $use_sent_subfolders = getPref($data_dir, $username,
31 'use_sent_subfolders', SMPREF_OFF);
32 $sent_subfolders_base = getPref($data_dir, $username,
e84b2496 33 'sent_subfolders_base', $sent_folder);
afa916e9 34
0e21ce8f 35 /* Only check the folder string if we have been passed a mailbox. */
32a7b3e0 36 if ($use_sent_subfolders && !empty($mailbox)) {
0e21ce8f 37 /* Chop up the folder strings as needed. */
38 $base_str = $sent_subfolders_base . $delimiter;
32a7b3e0 39 $mbox_str = substr($mailbox, 0, strlen($base_str));
0e21ce8f 40
41 /* Perform the comparison. */
afa916e9 42 $handleAsSent_result = ( ($base_str == $mbox_str)
43 || ($sent_subfolders_base == $mailbox) );
0e21ce8f 44 }
45}
46
0e21ce8f 47/**
48 * Adds sent_subfolders options in folder preferences
49 */
50function sent_subfolders_optpage_loadhook_folders_do() {
afa916e9 51
77800601 52 global $data_dir, $username, $optpage_data, $imapServerAddress,
a9805897 53 $imapPort, $imap_stream_options, $show_contain_subfolders_option, $sent_folder;
0e21ce8f 54
55 /* Get some imap data we need later. */
a9805897 56 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0, $imap_stream_options);
0e21ce8f 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
afa916e9 64 global $sent_subfolders_setting;
65 $sent_subfolders_setting = getPref($data_dir, $username,
66 'sent_subfolders_setting',
67 SMPREF_SENT_SUBFOLDERS_DISABLED);
0e21ce8f 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
afa916e9 83 global $sent_subfolders_base;
84 $sent_subfolders_base = getPref($data_dir, $username,
e84b2496 85 'sent_subfolders_base', $sent_folder);
0e21ce8f 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,
e84b2496 92 'folder_filter' => 'noinferiors',
93 'save' => 'save_option_sent_subfolders_base'
0e21ce8f 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 */
118function filter_folders($fldr) {
119 return strtolower($fldr['unformatted'])!='inbox';
120}
121
122/**
123 * Saves sent_subfolder_options
124 */
125function save_option_sent_subfolders_setting($option) {
afa916e9 126 global $data_dir, $username;
0e21ce8f 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);
16dd8736 134 $check_sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', '');
135 if ($check_sent_subfolders_base === '') {
136 setPref($data_dir, $username, 'sent_subfolders_base', $sent_subfolders_base);
137 }
0e21ce8f 138 }
139
140 /* Now just save the option as normal. */
141 save_option($option);
142}
143
e84b2496 144/**
145 * Update the folder settings/auto-create new subfolder
146 */
147function save_option_sent_subfolders_base($option) {
148 // first save the option as normal
149 save_option($option);
150
151 // now update folder settings and auto-create first subfolder if needed
152 sent_subfolders_update_sentfolder_do();
153}
154
0e21ce8f 155/**
156 * Update sent_subfolders settings
157 *
158 * function updates default sent folder value and
159 * creates required imap folders
160 */
161function sent_subfolders_update_sentfolder_do() {
afa916e9 162 global $sent_folder, $username,
163 $data_dir, $imapServerAddress, $imapPort,
a9805897 164 $imap_stream_options, $move_to_sent;
0e21ce8f 165
166 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
167
afa916e9 168 $use_sent_subfolders = getPref($data_dir, $username,
169 'use_sent_subfolders', SMPREF_OFF);
170 $sent_subfolders_setting = getPref($data_dir, $username,
171 'sent_subfolders_setting',
172 SMPREF_SENT_SUBFOLDERS_DISABLED);
173 $sent_subfolders_base = getPref($data_dir, $username,
e84b2496 174 'sent_subfolders_base', $sent_folder);
afa916e9 175
0e21ce8f 176 if ($use_sent_subfolders || $move_to_sent) {
177 $year = date('Y');
178 $month = date('m');
179 $quarter = sent_subfolder_getQuarter($month);
180
181 /**
182 * Regarding the structure we've got three main possibilities.
183 * One sent holder. level 0.
184 * Multiple year holders with messages in it. level 1.
185 * Multiple year folders with holders in it. level 2.
186 */
187
188 switch ($sent_subfolders_setting) {
189 case SMPREF_SENT_SUBFOLDERS_YEARLY:
190 $level = 1;
191 $sent_subfolder = $sent_subfolders_base . $delimiter
192 . $year;
193 break;
194 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
195 $level = 2;
196 $sent_subfolder = $sent_subfolders_base . $delimiter
197 . $year
198 . $delimiter . $quarter;
199 $year_folder = $sent_subfolders_base . $delimiter
200 . $year;
201 break;
202 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
203 $level = 2;
204 $sent_subfolder = $sent_subfolders_base . $delimiter
205 . $year
206 . $delimiter . $month;
207 $year_folder = $sent_subfolders_base. $delimiter . $year;
208 break;
209 case SMPREF_SENT_SUBFOLDERS_DISABLED:
210 default:
211 $level = 0;
212 $sent_subfolder = $sent_folder;
213 $year_folder = $sent_folder;
214 }
215
216 /* If this folder is NOT the current sent folder, update stuff. */
217 if ($sent_subfolder != $sent_folder) {
218 /* Auto-create folders, if they do not yet exist. */
219 if ($sent_subfolder != 'none') {
220 /* Create the imap connection. */
a9805897 221 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10, $imap_stream_options);
0e21ce8f 222
223 $boxes = false;
224 /**
225 * If sent_subfolder can't store messages (noselect) ||
226 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
227 * subfolder_base can't store subfolders (noinferiors), setup is broken
228 */
229 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
230 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
231 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
e84b2496 232 error_box(_("Sent subfolders options are misconfigured."));
0e21ce8f 233 } else {
234 if ($level==2) {
235 /* Auto-create the year folder, if it does not yet exist. */
236 if (!sqimap_mailbox_exists($ic, $year_folder)) {
237 sqimap_mailbox_create($ic, $year_folder, 'noselect');
238 // TODO: safety check for imap servers that can't create subfolders
239
240 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
241 sqimap_subscribe($ic, $year_folder);
242 }
243 }
244
245 /* Auto-create the subfolder, if it does not yet exist. */
246 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
247 sqimap_mailbox_create($ic, $sent_subfolder, '');
248 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
249 sqimap_subscribe($ic, $sent_subfolder);
250 }
e84b2496 251 /* Update sent_folder setting in prefs only if the base
252 subfolders setting is not the same as the normal sent
253 folder... otherwise, it is quite misleading to the user.
254 If the sent folder is the same as the subfolders base, it's
255 OK to leave the sent folder as is.
256 The sent_folder setting itself needs to be the actual
257 subfolder (not the base) for proper functionality */
258 if ($sent_subfolders_base != $sent_folder) {
259 setPref($data_dir, $username, 'sent_folder', $sent_subfolders_base);
260 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
261 setPref($data_dir, $username, 'translate_special_folders', SMPREF_OFF);
262 }
0e21ce8f 263 $sent_folder = $sent_subfolder;
264 $move_to_sent = SMPREF_ON;
265 }
266 /* Close the imap connection. */
267 sqimap_logout($ic);
268 }
269
270 }
271 }
272}
273
274/**
275 * Sets quarter subfolder names
276 *
277 * @param string $month numeric month
278 * @return string quarter name (Q + number)
279 */
280function sent_subfolder_getQuarter($month) {
281 switch ($month) {
282 case '01':
283 case '02':
284 case '03':
285 $result = '1';
286 break;
287 case '04':
288 case '05':
289 case '06':
290 $result = '2';
291 break;
292 case '07':
293 case '08':
294 case '09':
295 $result = '3';
296 break;
297 case '10':
298 case '11':
299 case '12':
300 $result = '4';
301 break;
302 default:
303 $result = 'ERR';
304 }
305
306 /* Return the current quarter. */
307 return ('Q' . $result);
308}
309
310/**
311 * detects if mailbox is part of sent_subfolders
312 *
313 * @param string $mb imap folder name
314 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
315 */
316function sent_subfolders_special_mailbox_do($mb) {
e84b2496 317 global $data_dir, $username, $sent_folder;
0e21ce8f 318
319 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
320
afa916e9 321 $use_sent_subfolders = getPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
322
e84b2496 323 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', $sent_folder);
0e21ce8f 324
325 /**
326 * If sent_subfolders are used and mailbox is equal to subfolder base
327 * or mailbox matches subfolder base + delimiter.
328 */
329 if ($use_sent_subfolders == SMPREF_ON &&
330 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
331 return 1;
332 }
333 return 0;
334}