Fix the save message button, using new functionality marc introduced yesterday.
[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 if ( !check_php_version(4,1) ) {
53 global $_SESSION;
54 }
55 $sent_subfolders_base = 'INBOX.Sent';
56 $args = func_get_arg(0);
57 $delimiter = $_SESSION['delimiter'];
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
68 || ($base_str == $mbox_str)
69 || ($sent_subfolders_base == $args[1])
70 );
71 }
72 }
73
74 function sent_subfolders_load_prefs() {
75 global $use_sent_subfolders, $data_dir, $username,
76 $sent_subfolders_setting, $sent_subfolders_base;
77
78 $use_sent_subfolders = getPref
79 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
80
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() {
89 global $optpage_data, $imapServerAddress, $imapPort;
90
91 if ( !check_php_version(4,1) ) {
92 global $_SESSION, $_COOKIE;
93 }
94 $username = $_SESSION['username'];
95 $key = $_COOKIE['key'];
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"),
113 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
114 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
115 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
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(' ', '&nbsp;', $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() {
157 global $sent_folder, $auto_create_special, $auto_create_done;
158 global $sent_subfolders_base, $sent_subfolders_setting;
159 global $data_dir, $imapServerAddress, $imapPort;
160 global $use_sent_subfolders, $move_to_sent, $imap_server_type;
161
162 if ( !check_php_version(4,1) ) {
163 global $_SESSION, $_COOKIE;
164 }
165 $username = $_SESSION['username'];
166 $key = $_COOKIE['key'];
167 $delimiter = $_SESSION['delimiter'];
168
169 if ($use_sent_subfolders || $move_to_sent) {
170 $year = date('Y');
171 $month = date('m');
172 $quarter = sent_subfolder_getQuarter($month);
173
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 */
180 /*
181 if( $imap_server_type == 'uw' ) {
182 $cnd_delimiter = '';
183 } else {
184 $cnd_delimiter = $delimiter;
185 }
186 */
187 $cnd_delimiter = $delimiter;
188
189 switch ($sent_subfolders_setting) {
190 case SMPREF_SENT_SUBFOLDERS_YEARLY:
191 $level = 1;
192 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
193 . $year;
194 break;
195 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
196 $level = 2;
197 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
198 . $year
199 . $delimiter . $quarter;
200 $year_folder = $sent_subfolders_base
201 . $year;
202 break;
203 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
204 $level = 2;
205 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
206 . $year
207 . $delimiter . $month;
208 $year_folder = $sent_subfolders_base . $year;
209 break;
210 case SMPREF_SENT_SUBFOLDERS_DISABLED:
211 default:
212 $level = 0;
213 $sent_subfolder = $sent_folder;
214 $year_folder = $sent_folder;
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. */
220
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);
231
232 /* Auto-create the year folder, if it does not yet exist. */
233 if (!sqimap_mailbox_exists($ic, $year_folder)) {
234 sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
235 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
236 sqimap_subscribe($ic, $year_folder);
237 }
238
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, '');
242 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
243 sqimap_subscribe($ic, $sent_subfolder);
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':
257 case '03':
258 $result = '1';
259 break;
260 case '04':
261 case '05':
262 case '06':
263 $result = '2';
264 break;
265 case '07':
266 case '08':
267 case '09':
268 $result = '3';
269 break;
270 case '10':
271 case '11':
272 case '12':
273 $result = '4';
274 break;
275 default:
276 $result = 'ERR';
277 }
278
279 /* Return the current quarter. */
280 return ('Q' . $result);
281 }
282
283 ?>