3df9b3182397bc0eef5d3557c6dec8efe56f6197
[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 function sent_subfolders_check_handleAsSent_do($mailbox) {
16 global $handleAsSent_result, $sent_subfolders_base,
17 $use_sent_subfolders;
18
19 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
20
21 /* Only check the folder string if we have been passed a mailbox. */
22 if ($use_sent_subfolders && !empty($mailbox)) {
23 /* Chop up the folder strings as needed. */
24 $base_str = $sent_subfolders_base . $delimiter;
25 $mbox_str = substr($mailbox, 0, strlen($base_str));
26
27 /* Perform the comparison. */
28 $handleAsSent_result =
29 ( $handleAsSent_result
30 || ($base_str == $mbox_str)
31 || ($sent_subfolders_base == $mailbox)
32 );
33 }
34 }
35
36 /**
37 * Loads sent_subfolders settings
38 */
39 function sent_subfolders_load_prefs_do() {
40 global $use_sent_subfolders, $data_dir, $username,
41 $sent_subfolders_setting, $sent_subfolders_base;
42
43 $use_sent_subfolders = getPref
44 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
45
46 $sent_subfolders_setting = getPref
47 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
48
49 $sent_subfolders_base = getPref
50 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
51 }
52
53 /**
54 * Adds sent_subfolders options in folder preferences
55 */
56 function sent_subfolders_optpage_loadhook_folders_do() {
57 global $username, $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
58
59 /* Get some imap data we need later. */
60 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
61 $boxes = sqimap_mailbox_list($imapConnection);
62 sqimap_logout($imapConnection);
63
64 /* Load the Sent Subfolder Options into an array. */
65 $optgrp = _("Sent Subfolders Options");
66 $optvals = array();
67
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 $optvals[] = array(
84 'name' => 'sent_subfolders_base',
85 'caption' => _("Base Sent Folder"),
86 'type' => SMOPT_TYPE_FLDRLIST,
87 'refresh' => SMOPT_REFRESH_FOLDERLIST,
88 'posvals' => $sent_subfolders_base_values,
89 'folder_filter' => 'noinferiors'
90 );
91
92 if ($show_contain_subfolders_option) {
93 $optvals[] = array(
94 'name' => 'sent_subfolders_warning',
95 'caption' => _("Warning"),
96 'type' => SMOPT_TYPE_COMMENT,
97 'comment' => _("There are some restrictions in Sent Subfolder options.")
98 );
99 }
100
101 /* Add our option data to the global array. */
102 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
103 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
104 }
105
106 /**
107 * Defines folder filtering rules
108 *
109 * Callback function that should exclude some folders from folder listing.
110 * @param array $fldr list of folders. See sqimap_mailbox_list
111 * @return boolean returns true, if folder has to included in folder listing
112 * @access private
113 */
114 function filter_folders($fldr) {
115 return strtolower($fldr['unformatted'])!='inbox';
116 }
117
118 /**
119 * Saves sent_subfolder_options
120 */
121 function save_option_sent_subfolders_setting($option) {
122 global $data_dir, $username, $use_sent_subfolders;
123
124 /* Set use_sent_subfolders as either on or off. */
125 if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
126 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
127 } else {
128 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
129 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
130 }
131
132 /* Now just save the option as normal. */
133 save_option($option);
134 }
135
136 /**
137 * Update sent_subfolders settings
138 *
139 * function updates default sent folder value and
140 * creates required imap folders
141 */
142 function sent_subfolders_update_sentfolder_do() {
143 global $sent_folder, $username;
144 global $sent_subfolders_base, $sent_subfolders_setting;
145 global $data_dir, $imapServerAddress, $imapPort;
146 global $use_sent_subfolders, $move_to_sent;
147
148 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
149
150 if ($use_sent_subfolders || $move_to_sent) {
151 $year = date('Y');
152 $month = date('m');
153 $quarter = sent_subfolder_getQuarter($month);
154
155 /**
156 * Regarding the structure we've got three main possibilities.
157 * One sent holder. level 0.
158 * Multiple year holders with messages in it. level 1.
159 * Multiple year folders with holders in it. level 2.
160 */
161
162 switch ($sent_subfolders_setting) {
163 case SMPREF_SENT_SUBFOLDERS_YEARLY:
164 $level = 1;
165 $sent_subfolder = $sent_subfolders_base . $delimiter
166 . $year;
167 break;
168 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
169 $level = 2;
170 $sent_subfolder = $sent_subfolders_base . $delimiter
171 . $year
172 . $delimiter . $quarter;
173 $year_folder = $sent_subfolders_base . $delimiter
174 . $year;
175 break;
176 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
177 $level = 2;
178 $sent_subfolder = $sent_subfolders_base . $delimiter
179 . $year
180 . $delimiter . $month;
181 $year_folder = $sent_subfolders_base. $delimiter . $year;
182 break;
183 case SMPREF_SENT_SUBFOLDERS_DISABLED:
184 default:
185 $level = 0;
186 $sent_subfolder = $sent_folder;
187 $year_folder = $sent_folder;
188 }
189
190 /* If this folder is NOT the current sent folder, update stuff. */
191 if ($sent_subfolder != $sent_folder) {
192 /* Auto-create folders, if they do not yet exist. */
193 if ($sent_subfolder != 'none') {
194 /* Create the imap connection. */
195 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
196
197 $boxes = false;
198 /**
199 * If sent_subfolder can't store messages (noselect) ||
200 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
201 * subfolder_base can't store subfolders (noinferiors), setup is broken
202 */
203 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
204 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
205 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
206 error_box(_("Sent Subfolders plugin is misconfigured."));
207 } else {
208 if ($level==2) {
209 /* Auto-create the year folder, if it does not yet exist. */
210 if (!sqimap_mailbox_exists($ic, $year_folder)) {
211 sqimap_mailbox_create($ic, $year_folder, 'noselect');
212 // TODO: safety check for imap servers that can't create subfolders
213
214 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
215 sqimap_subscribe($ic, $year_folder);
216 }
217 }
218
219 /* Auto-create the subfolder, if it does not yet exist. */
220 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
221 sqimap_mailbox_create($ic, $sent_subfolder, '');
222 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
223 sqimap_subscribe($ic, $sent_subfolder);
224 }
225 /* Update sent_folder setting. */
226 //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
227 //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
228 $sent_folder = $sent_subfolder;
229 $move_to_sent = SMPREF_ON;
230 }
231 /* Close the imap connection. */
232 sqimap_logout($ic);
233 }
234
235 }
236 }
237 }
238
239 /**
240 * Sets quarter subfolder names
241 *
242 * @param string $month numeric month
243 * @return string quarter name (Q + number)
244 */
245 function sent_subfolder_getQuarter($month) {
246 switch ($month) {
247 case '01':
248 case '02':
249 case '03':
250 $result = '1';
251 break;
252 case '04':
253 case '05':
254 case '06':
255 $result = '2';
256 break;
257 case '07':
258 case '08':
259 case '09':
260 $result = '3';
261 break;
262 case '10':
263 case '11':
264 case '12':
265 $result = '4';
266 break;
267 default:
268 $result = 'ERR';
269 }
270
271 /* Return the current quarter. */
272 return ('Q' . $result);
273 }
274
275 /**
276 * detects if mailbox is part of sent_subfolders
277 *
278 * @param string $mb imap folder name
279 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
280 */
281 function sent_subfolders_special_mailbox_do($mb) {
282 global $data_dir, $username;
283
284 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
285
286 $use_sent_subfolders = getPref
287 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
288 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
289
290 /**
291 * If sent_subfolders are used and mailbox is equal to subfolder base
292 * or mailbox matches subfolder base + delimiter.
293 */
294 if ($use_sent_subfolders == SMPREF_ON &&
295 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
296 return 1;
297 }
298 return 0;
299 }