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