dafc1035dc9dd93e6e87eb2d2c3e7191bfa191f0
[squirrelmail.git] / plugins / sent_subfolders / setup.php
1 <?php
2
3 /**
4 * setup.php -- Sent Subfolders Setup File
5 *
6 * Copyright (c) 1999-2003 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() {
50 global $handleAsSent_result, $sent_subfolders_base,
51 $use_sent_subfolders;
52
53 $sent_subfolders_base = 'INBOX.Sent';
54 $args = func_get_arg(0);
55 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
56
57 /* Only check the folder string if we have been passed a mailbox. */
58 if ($use_sent_subfolders && (count($args) > 1)) {
59 /* Chop up the folder strings as needed. */
60 $base_str = $sent_subfolders_base . $delimiter;
61 $mbox_str = substr($args[1], 0, strlen($base_str));
62
63 /* Perform the comparison. */
64 $handleAsSent_result =
65 ( $handleAsSent_result
66 || ($base_str == $mbox_str)
67 || ($sent_subfolders_base == $args[1])
68 );
69 }
70 }
71
72 function sent_subfolders_load_prefs() {
73 global $use_sent_subfolders, $data_dir, $username,
74 $sent_subfolders_setting, $sent_subfolders_base;
75
76 $use_sent_subfolders = getPref
77 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
78
79 $sent_subfolders_setting = getPref
80 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
81
82 $sent_subfolders_base = getPref
83 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
84 }
85
86 function sent_subfolders_optpage_loadhook_folders() {
87 global $optpage_data, $imapServerAddress, $imapPort;
88
89 sqgetGlobalVar('username', $username, SQ_SESSION);
90 sqgetGlobalVar('key', $key, SQ_COOKIE);
91
92 /* Get some imap data we need later. */
93 $imapConnection =
94 sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
95 $boxes = sqimap_mailbox_list($imapConnection);
96 sqimap_logout($imapConnection);
97
98 /* Load the Sent Subfolder Options into an array. */
99 $optgrp = _("Sent Subfolders Options");
100 $optvals = array();
101
102 $optvals[] = array(
103 'name' => 'sent_subfolders_setting',
104 'caption' => _("Use Sent Subfolders"),
105 'type' => SMOPT_TYPE_STRLIST,
106 'refresh' => SMOPT_REFRESH_FOLDERLIST,
107 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
108 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
109 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
110 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
111 'save' => 'save_option_sent_subfolders_setting'
112 );
113
114 $sent_subfolders_base_values = array();
115 foreach ($boxes as $folder) {
116 if (strtolower($folder['unformatted']) != 'inbox') {
117 $real_value = $folder['unformatted-dm'];
118 $disp_value = str_replace(' ', '&nbsp;', $folder['formatted']);
119 $sent_subfolders_base_values[$real_value] = $disp_value;
120 }
121 }
122
123 $optvals[] = array(
124 'name' => 'sent_subfolders_base',
125 'caption' => _("Base Sent Folder"),
126 'type' => SMOPT_TYPE_STRLIST,
127 'refresh' => SMOPT_REFRESH_FOLDERLIST,
128 'posvals' => $sent_subfolders_base_values
129 );
130
131 /* Add our option data to the global array. */
132 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
133 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
134 }
135
136 function save_option_sent_subfolders_setting($option) {
137 global $data_dir, $username, $use_sent_subfolders;
138
139 /* Set use_sent_subfolders as either on or off. */
140 if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
141 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
142 } else {
143 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
144 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
145 }
146
147 /* Now just save the option as normal. */
148 save_option($option);
149 }
150
151 function sent_subfolders_update_sentfolder() {
152 global $sent_folder, $auto_create_special, $auto_create_done;
153 global $sent_subfolders_base, $sent_subfolders_setting;
154 global $data_dir, $imapServerAddress, $imapPort;
155 global $use_sent_subfolders, $move_to_sent, $imap_server_type;
156
157 sqgetGlobalVar('username', $username, SQ_SESSION);
158 sqgetGlobalVar('key', $key, SQ_COOKIE);
159 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
160
161 if ($use_sent_subfolders || $move_to_sent) {
162 $year = date('Y');
163 $month = date('m');
164 $quarter = sent_subfolder_getQuarter($month);
165
166 /*
167 Regarding the structure we've got three main possibilities.
168 One sent holder. level 0.
169 Multiple year holders with messages in it. level 1.
170 Multiple year folders with holders in it. level 2.
171 */
172 /*
173 if( $imap_server_type == 'uw' ) {
174 $cnd_delimiter = '';
175 } else {
176 $cnd_delimiter = $delimiter;
177 }
178 */
179 $cnd_delimiter = $delimiter;
180
181 switch ($sent_subfolders_setting) {
182 case SMPREF_SENT_SUBFOLDERS_YEARLY:
183 $level = 1;
184 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
185 . $year;
186 break;
187 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
188 $level = 2;
189 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
190 . $year
191 . $delimiter . $quarter;
192 $year_folder = $sent_subfolders_base
193 . $year;
194 break;
195 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
196 $level = 2;
197 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
198 . $year
199 . $delimiter . $month;
200 $year_folder = $sent_subfolders_base . $year;
201 break;
202 case SMPREF_SENT_SUBFOLDERS_DISABLED:
203 default:
204 $level = 0;
205 $sent_subfolder = $sent_folder;
206 $year_folder = $sent_folder;
207 }
208
209 /* If this folder is NOT the current sent folder, update stuff. */
210 if ($sent_subfolder != $sent_folder) {
211 /* First, update the sent folder. */
212
213 setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
214 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
215 $sent_folder = $sent_subfolder;
216 $move_to_sent = SMPREF_ON;
217
218 /* Auto-create folders, if they do not yet exist. */
219 if ($sent_folder != 'none') {
220 /* Create the imap connection. */
221 $ic = sqimap_login
222 ($username, $key, $imapServerAddress, $imapPort, 10);
223
224 /* Auto-create the year folder, if it does not yet exist. */
225 if (!sqimap_mailbox_exists($ic, $year_folder)) {
226 sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
227 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
228 sqimap_subscribe($ic, $year_folder);
229 }
230
231 /* Auto-create the subfolder, if it does not yet exist. */
232 if (!sqimap_mailbox_exists($ic, $sent_folder)) {
233 sqimap_mailbox_create($ic, $sent_folder, '');
234 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
235 sqimap_subscribe($ic, $sent_subfolder);
236 }
237
238 /* Close the imap connection. */
239 sqimap_logout($ic);
240 }
241 }
242 }
243 }
244
245 function sent_subfolder_getQuarter($month) {
246 switch ($month) {
247 case '01':
248 case '02':
249 case '03':
250 $result = '1';
251 break;
252 case '04':
253 case '05':
254 case '06':
255 $result = '2';
256 break;
257 case '07':
258 case '08':
259 case '09':
260 $result = '3';
261 break;
262 case '10':
263 case '11':
264 case '12':
265 $result = '4';
266 break;
267 default:
268 $result = 'ERR';
269 }
270
271 /* Return the current quarter. */
272 return ('Q' . $result);
273 }
274
275 ?>