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