a3439b27 |
1 | <?php |
2 | |
3 | /** |
4 | * setup.php -- Sent Subfolders Setup File |
5 | * |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This is a standard Squirrelmail-1.2 API for plugins. |
10 | * |
11 | * $Id$ |
12 | */ |
13 | |
14 | define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0); |
15 | define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1); |
16 | define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2); |
17 | define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3); |
18 | define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS'); |
19 | |
20 | function squirrelmail_plugin_init_sent_subfolders() { |
21 | /* Standard initialization API. */ |
22 | global $squirrelmail_plugin_hooks; |
23 | |
24 | /* The hooks to make the sent subfolders display correctly. */ |
25 | $squirrelmail_plugin_hooks |
26 | ['check_handleAsSent_result']['sent_subfolders'] = |
27 | 'sent_subfolders_check_handleAsSent'; |
28 | |
29 | /* The hooks to automatically update sent subfolders. */ |
30 | $squirrelmail_plugin_hooks |
31 | ['left_main_before']['sent_subfolders'] = |
32 | 'sent_subfolders_update_sentfolder'; |
33 | |
34 | $squirrelmail_plugin_hooks |
35 | ['compose_send']['sent_subfolders'] = |
36 | 'sent_subfolders_update_sentfolder'; |
37 | |
38 | /* The hook to load the sent subfolders prefs. */ |
39 | $squirrelmail_plugin_hooks |
40 | ['loading_prefs']['sent_subfolders'] = |
41 | 'sent_subfolders_load_prefs'; |
42 | |
43 | /* The hooks to handle sent subfolders options. */ |
44 | $squirrelmail_plugin_hooks |
45 | ['optpage_loadhook_folder']['sent_subfolders'] = |
46 | 'sent_subfolders_optpage_loadhook_folders'; |
47 | } |
48 | |
49 | function sent_subfolders_check_handleAsSent() { |
b6b31355 |
50 | global $handleAsSent_result, $sent_subfolders_base, |
04f6008a |
51 | $use_sent_subfolders; |
fb694681 |
52 | if ( !check_php_version(4,1) ) { |
04f6008a |
53 | global $_SESSION; |
54 | } |
a3439b27 |
55 | $sent_subfolders_base = 'INBOX.Sent'; |
56 | $args = func_get_arg(0); |
04f6008a |
57 | $delimiter = $_SESSION['delimiter']; |
a3439b27 |
58 | |
59 | /* Only check the folder string if we have been passed a mailbox. */ |
60 | if ($use_sent_subfolders && (count($args) > 1)) { |
61 | /* Chop up the folder strings as needed. */ |
62 | $base_str = $sent_subfolders_base . $delimiter; |
63 | $mbox_str = substr($args[1], 0, strlen($base_str)); |
64 | |
65 | /* Perform the comparison. */ |
66 | $handleAsSent_result = |
67 | ( $handleAsSent_result |
d79e01f5 |
68 | || ($base_str == $mbox_str) |
69 | || ($sent_subfolders_base == $args[1]) |
a3439b27 |
70 | ); |
71 | } |
72 | } |
73 | |
74 | function sent_subfolders_load_prefs() { |
88cb1b4d |
75 | global $use_sent_subfolders, $data_dir, $username, |
76 | $sent_subfolders_setting, $sent_subfolders_base; |
a3439b27 |
77 | |
78 | $use_sent_subfolders = getPref |
79 | ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF); |
d79e01f5 |
80 | |
a3439b27 |
81 | $sent_subfolders_setting = getPref |
82 | ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED); |
83 | |
84 | $sent_subfolders_base = getPref |
85 | ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE); |
86 | } |
87 | |
88 | function sent_subfolders_optpage_loadhook_folders() { |
04f6008a |
89 | global $optpage_data, $imapServerAddress, $imapPort; |
90 | |
fb694681 |
91 | if ( !check_php_version(4,1) ) { |
04f6008a |
92 | global $_SESSION, $_COOKIE; |
93 | } |
94 | $username = $_SESSION['username']; |
95 | $key = $_COOKIE['key']; |
a3439b27 |
96 | |
97 | /* Get some imap data we need later. */ |
98 | $imapConnection = |
99 | sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
100 | $boxes = sqimap_mailbox_list($imapConnection); |
101 | sqimap_logout($imapConnection); |
102 | |
103 | /* Load the Sent Subfolder Options into an array. */ |
104 | $optgrp = _("Sent Subfolders Options"); |
105 | $optvals = array(); |
106 | |
107 | $optvals[] = array( |
108 | 'name' => 'sent_subfolders_setting', |
109 | 'caption' => _("Use Sent Subfolders"), |
110 | 'type' => SMOPT_TYPE_STRLIST, |
111 | 'refresh' => SMOPT_REFRESH_FOLDERLIST, |
112 | 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"), |
d79e01f5 |
113 | SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"), |
114 | SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"), |
115 | SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")), |
a3439b27 |
116 | 'save' => 'save_option_sent_subfolders_setting' |
117 | ); |
118 | |
119 | $sent_subfolders_base_values = array(); |
120 | foreach ($boxes as $folder) { |
121 | if (strtolower($folder['unformatted']) != 'inbox') { |
122 | $real_value = $folder['unformatted-dm']; |
123 | $disp_value = str_replace(' ', ' ', $folder['formatted']); |
124 | $sent_subfolders_base_values[$real_value] = $disp_value; |
125 | } |
126 | } |
127 | |
128 | $optvals[] = array( |
129 | 'name' => 'sent_subfolders_base', |
130 | 'caption' => _("Base Sent Folder"), |
131 | 'type' => SMOPT_TYPE_STRLIST, |
132 | 'refresh' => SMOPT_REFRESH_FOLDERLIST, |
133 | 'posvals' => $sent_subfolders_base_values |
134 | ); |
135 | |
136 | /* Add our option data to the global array. */ |
137 | $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp; |
138 | $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals; |
139 | } |
140 | |
141 | function save_option_sent_subfolders_setting($option) { |
142 | global $data_dir, $username, $use_sent_subfolders; |
143 | |
144 | /* Set use_sent_subfolders as either on or off. */ |
145 | if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) { |
146 | setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF); |
147 | } else { |
148 | setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON); |
149 | setPref($data_dir, $username, 'move_to_sent', SMPREF_ON); |
150 | } |
151 | |
152 | /* Now just save the option as normal. */ |
153 | save_option($option); |
154 | } |
155 | |
156 | function sent_subfolders_update_sentfolder() { |
04f6008a |
157 | global $sent_folder, $auto_create_special, $auto_create_done; |
a3439b27 |
158 | global $sent_subfolders_base, $sent_subfolders_setting; |
04f6008a |
159 | global $data_dir, $imapServerAddress, $imapPort; |
64727b20 |
160 | global $use_sent_subfolders, $move_to_sent, $imap_server_type; |
d79e01f5 |
161 | |
fb694681 |
162 | if ( !check_php_version(4,1) ) { |
04f6008a |
163 | global $_SESSION, $_COOKIE; |
164 | } |
165 | $username = $_SESSION['username']; |
166 | $key = $_COOKIE['key']; |
167 | $delimiter = $_SESSION['delimiter']; |
168 | |
1c52ba77 |
169 | if ($use_sent_subfolders || $move_to_sent) { |
a3439b27 |
170 | $year = date('Y'); |
171 | $month = date('m'); |
172 | $quarter = sent_subfolder_getQuarter($month); |
173 | |
1c52ba77 |
174 | /* |
175 | Regarding the structure we've got three main possibilities. |
176 | One sent holder. level 0. |
177 | Multiple year holders with messages in it. level 1. |
178 | Multiple year folders with holders in it. level 2. |
179 | */ |
4965e012 |
180 | /* |
72b9aff9 |
181 | if( $imap_server_type == 'uw' ) { |
182 | $cnd_delimiter = ''; |
183 | } else { |
184 | $cnd_delimiter = $delimiter; |
185 | } |
4965e012 |
186 | */ |
187 | $cnd_delimiter = $delimiter; |
72b9aff9 |
188 | |
a3439b27 |
189 | switch ($sent_subfolders_setting) { |
1c52ba77 |
190 | case SMPREF_SENT_SUBFOLDERS_YEARLY: |
191 | $level = 1; |
72b9aff9 |
192 | $sent_subfolder = $sent_subfolders_base . $cnd_delimiter |
1c52ba77 |
193 | . $year; |
1c52ba77 |
194 | break; |
195 | case SMPREF_SENT_SUBFOLDERS_QUARTERLY: |
196 | $level = 2; |
72b9aff9 |
197 | $sent_subfolder = $sent_subfolders_base . $cnd_delimiter |
1c52ba77 |
198 | . $year |
199 | . $delimiter . $quarter; |
200 | $year_folder = $sent_subfolders_base |
a3add160 |
201 | . $year; |
1c52ba77 |
202 | break; |
203 | case SMPREF_SENT_SUBFOLDERS_MONTHLY: |
204 | $level = 2; |
72b9aff9 |
205 | $sent_subfolder = $sent_subfolders_base . $cnd_delimiter |
1c52ba77 |
206 | . $year |
207 | . $delimiter . $month; |
a3add160 |
208 | $year_folder = $sent_subfolders_base . $year; |
1c52ba77 |
209 | break; |
210 | case SMPREF_SENT_SUBFOLDERS_DISABLED: |
211 | default: |
212 | $level = 0; |
213 | $sent_subfolder = $sent_folder; |
214 | $year_folder = $sent_folder; |
a3439b27 |
215 | } |
216 | |
217 | /* If this folder is NOT the current sent folder, update stuff. */ |
218 | if ($sent_subfolder != $sent_folder) { |
219 | /* First, update the sent folder. */ |
1c52ba77 |
220 | |
a3439b27 |
221 | setPref($data_dir, $username, 'sent_folder', $sent_subfolder); |
222 | setPref($data_dir, $username, 'move_to_sent', SMPREF_ON); |
223 | $sent_folder = $sent_subfolder; |
224 | $move_to_sent = SMPREF_ON; |
225 | |
226 | /* Auto-create folders, if they do not yet exist. */ |
227 | if ($sent_folder != 'none') { |
228 | /* Create the imap connection. */ |
229 | $ic = sqimap_login |
230 | ($username, $key, $imapServerAddress, $imapPort, 10); |
d79e01f5 |
231 | |
a3439b27 |
232 | /* Auto-create the year folder, if it does not yet exist. */ |
233 | if (!sqimap_mailbox_exists($ic, $year_folder)) { |
1c52ba77 |
234 | sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect'); |
a3439b27 |
235 | } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) { |
236 | sqimap_subscribe($ic, $year_folder); |
237 | } |
d79e01f5 |
238 | |
a3439b27 |
239 | /* Auto-create the subfolder, if it does not yet exist. */ |
240 | if (!sqimap_mailbox_exists($ic, $sent_folder)) { |
241 | sqimap_mailbox_create($ic, $sent_folder, ''); |
1c52ba77 |
242 | } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) { |
243 | sqimap_subscribe($ic, $sent_subfolder); |
a3439b27 |
244 | } |
245 | |
246 | /* Close the imap connection. */ |
247 | sqimap_logout($ic); |
248 | } |
249 | } |
250 | } |
251 | } |
252 | |
253 | function sent_subfolder_getQuarter($month) { |
254 | switch ($month) { |
255 | case '01': |
256 | case '02': |
d79e01f5 |
257 | case '03': |
258 | $result = '1'; |
259 | break; |
a3439b27 |
260 | case '04': |
261 | case '05': |
d79e01f5 |
262 | case '06': |
263 | $result = '2'; |
264 | break; |
a3439b27 |
265 | case '07': |
266 | case '08': |
d79e01f5 |
267 | case '09': |
268 | $result = '3'; |
269 | break; |
a3439b27 |
270 | case '10': |
271 | case '11': |
d79e01f5 |
272 | case '12': |
273 | $result = '4'; |
274 | break; |
275 | default: |
276 | $result = 'ERR'; |
a3439b27 |
277 | } |
278 | |
279 | /* Return the current quarter. */ |
280 | return ('Q' . $result); |
281 | } |
282 | |
283 | ?> |