adding more charsets to sq_mb_list_encodings
[squirrelmail.git] / plugins / sent_subfolders / setup.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-2005 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 /**
16 */
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
23 /**
24 * Adds plugin to SquirrelMail's hooks
25 */
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';
53
54 /* mark base sent folder as special mailbox */
55 $squirrelmail_plugin_hooks
56 ['special_mailbox']['sent_subfolders'] =
57 'sent_subfolders_special_mailbox';
58 }
59
60 function sent_subfolders_check_handleAsSent() {
61 global $handleAsSent_result, $sent_subfolders_base,
62 $use_sent_subfolders;
63
64 $sent_subfolders_base = 'INBOX.Sent';
65 $args = func_get_arg(0);
66 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
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
77 || ($base_str == $mbox_str)
78 || ($sent_subfolders_base == $args[1])
79 );
80 }
81 }
82
83 /**
84 * Loads sent_subfolders settings
85 */
86 function sent_subfolders_load_prefs() {
87 global $use_sent_subfolders, $data_dir, $username,
88 $sent_subfolders_setting, $sent_subfolders_base;
89
90 $use_sent_subfolders = getPref
91 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
92
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
100 /**
101 * Adds sent_subfolders options in folder preferences
102 */
103 function sent_subfolders_optpage_loadhook_folders() {
104 global $optpage_data, $imapServerAddress, $imapPort;
105
106 sqgetGlobalVar('username', $username, SQ_SESSION);
107 sqgetGlobalVar('key', $key, SQ_COOKIE);
108
109 /* Get some imap data we need later. */
110 $imapConnection =
111 sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
112 $boxes = sqimap_mailbox_list($imapConnection);
113 sqimap_logout($imapConnection);
114
115 /* Load the Sent Subfolder Options into an array. */
116 $optgrp = _("Sent Subfolders Options");
117 $optvals = array();
118
119 $optvals[] = array(
120 'name' => 'sent_subfolders_setting',
121 'caption' => _("Use Sent Subfolders"),
122 'type' => SMOPT_TYPE_STRLIST,
123 'refresh' => SMOPT_REFRESH_FOLDERLIST,
124 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
125 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
126 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
127 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
128 'save' => 'save_option_sent_subfolders_setting'
129 );
130
131 $filtered_folders=array_filter($boxes, "filter_folders");
132 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
133
134 $optvals[] = array(
135 'name' => 'sent_subfolders_base',
136 'caption' => _("Base Sent Folder"),
137 'type' => SMOPT_TYPE_FLDRLIST,
138 'refresh' => SMOPT_REFRESH_FOLDERLIST,
139 'posvals' => $sent_subfolders_base_values,
140 'folder_filter' => 'noinferiors'
141 );
142
143 /* Add our option data to the global array. */
144 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
145 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
146 }
147
148 /**
149 * Defines folder filtering rules
150 *
151 * Callback function that should exclude some folders from folder listing.
152 * @param array $fldr list of folders. See sqimap_mailbox_list
153 * @return boolean returns true, if folder has to included in folder listing
154 * @access private
155 */
156 function filter_folders($fldr) {
157 return strtolower($fldr['unformatted'])!='inbox';
158 }
159
160 /**
161 * Saves sent_subfolder_options
162 */
163 function save_option_sent_subfolders_setting($option) {
164 global $data_dir, $username, $use_sent_subfolders;
165
166 /* Set use_sent_subfolders as either on or off. */
167 if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
168 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
169 } else {
170 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
171 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
172 }
173
174 /* Now just save the option as normal. */
175 save_option($option);
176 }
177
178 /**
179 * Update sent_subfolders settings
180 *
181 * function updates default sent folder value and
182 * creates required imap folders
183 */
184 function sent_subfolders_update_sentfolder() {
185 global $sent_folder;
186 global $sent_subfolders_base, $sent_subfolders_setting;
187 global $data_dir, $imapServerAddress, $imapPort;
188 global $use_sent_subfolders, $move_to_sent;
189
190 sqgetGlobalVar('username', $username, SQ_SESSION);
191 sqgetGlobalVar('key', $key, SQ_COOKIE);
192 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
193
194 if ($use_sent_subfolders || $move_to_sent) {
195 $year = date('Y');
196 $month = date('m');
197 $quarter = sent_subfolder_getQuarter($month);
198
199 /**
200 * Regarding the structure we've got three main possibilities.
201 * One sent holder. level 0.
202 * Multiple year holders with messages in it. level 1.
203 * Multiple year folders with holders in it. level 2.
204 */
205 $cnd_delimiter = $delimiter;
206
207 switch ($sent_subfolders_setting) {
208 case SMPREF_SENT_SUBFOLDERS_YEARLY:
209 $level = 1;
210 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
211 . $year;
212 break;
213 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
214 $level = 2;
215 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
216 . $year
217 . $delimiter . $quarter;
218 $year_folder = $sent_subfolders_base . $cnd_delimiter
219 . $year;
220 break;
221 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
222 $level = 2;
223 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
224 . $year
225 . $delimiter . $month;
226 $year_folder = $sent_subfolders_base. $cnd_delimiter . $year;
227 break;
228 case SMPREF_SENT_SUBFOLDERS_DISABLED:
229 default:
230 $level = 0;
231 $sent_subfolder = $sent_folder;
232 $year_folder = $sent_folder;
233 }
234
235 /* If this folder is NOT the current sent folder, update stuff. */
236 if ($sent_subfolder != $sent_folder) {
237 /**
238 * $sent_subfolder should not have \noselect flag or folder should
239 * not exist.
240 * if $level=2, $year_folder should not have \noinferiors or folder
241 * should not exist.
242 *
243 * If some condition fails - sent_subfolders is misconfigured
244 */
245
246 /* Update sent_folder setting. */
247 setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
248 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
249 $sent_folder = $sent_subfolder;
250 $move_to_sent = SMPREF_ON;
251
252 /* Auto-create folders, if they do not yet exist. */
253 if ($sent_folder != 'none') {
254 /* Create the imap connection. */
255 $ic = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
256
257 if ($level==2) {
258 /* Auto-create the year folder, if it does not yet exist. */
259 if (!sqimap_mailbox_exists($ic, $year_folder)) {
260 sqimap_mailbox_create($ic, $year_folder, 'noselect');
261 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
262 sqimap_subscribe($ic, $year_folder);
263 }
264 }
265
266 /* Auto-create the subfolder, if it does not yet exist. */
267 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
268 sqimap_mailbox_create($ic, $sent_subfolder, '');
269 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
270 sqimap_subscribe($ic, $sent_subfolder);
271 }
272
273 /* Close the imap connection. */
274 sqimap_logout($ic);
275 }
276
277 }
278 }
279 }
280
281 /**
282 * Sets quarter subfolder names
283 *
284 * @param string $month numeric month
285 * @return string quarter name (Q + number)
286 */
287 function sent_subfolder_getQuarter($month) {
288 switch ($month) {
289 case '01':
290 case '02':
291 case '03':
292 $result = '1';
293 break;
294 case '04':
295 case '05':
296 case '06':
297 $result = '2';
298 break;
299 case '07':
300 case '08':
301 case '09':
302 $result = '3';
303 break;
304 case '10':
305 case '11':
306 case '12':
307 $result = '4';
308 break;
309 default:
310 $result = 'ERR';
311 }
312
313 /* Return the current quarter. */
314 return ('Q' . $result);
315 }
316
317 /**
318 * detects if mailbox is part of sent_subfolders
319 *
320 * @param string $mb imap folder name
321 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
322 */
323 function sent_subfolders_special_mailbox($mb) {
324 global $data_dir, $username;
325
326 $use_sent_subfolders = getPref
327 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
328 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
329
330 if ($use_sent_subfolders == SMPREF_ON &&
331 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base) ) ) {
332 return 1;
333 }
334 return 0;
335 }
336 ?>