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