cleanup some plugins to make use of sqgetGlobalVar
[squirrelmail.git] / plugins / sent_subfolders / setup.php
CommitLineData
a3439b27 1<?php
2
3/**
4 * setup.php -- Sent Subfolders Setup File
5 *
76911253 6 * Copyright (c) 1999-2003 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$
12 */
13
14define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
15define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
16define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
17define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
18define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
19
20function 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
49function sent_subfolders_check_handleAsSent() {
b6b31355 50 global $handleAsSent_result, $sent_subfolders_base,
04f6008a 51 $use_sent_subfolders;
b587ac51 52
a3439b27 53 $sent_subfolders_base = 'INBOX.Sent';
54 $args = func_get_arg(0);
b587ac51 55 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
a3439b27 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
d79e01f5 66 || ($base_str == $mbox_str)
67 || ($sent_subfolders_base == $args[1])
a3439b27 68 );
69 }
70}
71
72function sent_subfolders_load_prefs() {
88cb1b4d 73 global $use_sent_subfolders, $data_dir, $username,
74 $sent_subfolders_setting, $sent_subfolders_base;
a3439b27 75
76 $use_sent_subfolders = getPref
77 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
d79e01f5 78
a3439b27 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
86function sent_subfolders_optpage_loadhook_folders() {
04f6008a 87 global $optpage_data, $imapServerAddress, $imapPort;
88
b587ac51 89 sqgetGlobalVar('username', $username, SQ_SESSION);
90 sqgetGlobalVar('key', $key, SQ_COOKIE);
a3439b27 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"),
d79e01f5 108 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
109 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
110 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
a3439b27 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
136function 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
151function sent_subfolders_update_sentfolder() {
04f6008a 152 global $sent_folder, $auto_create_special, $auto_create_done;
a3439b27 153 global $sent_subfolders_base, $sent_subfolders_setting;
04f6008a 154 global $data_dir, $imapServerAddress, $imapPort;
64727b20 155 global $use_sent_subfolders, $move_to_sent, $imap_server_type;
d79e01f5 156
b587ac51 157 sqgetGlobalVar('username', $username, SQ_SESSION);
158 sqgetGlobalVar('key', $key, SQ_COOKIE);
159 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
160
1c52ba77 161 if ($use_sent_subfolders || $move_to_sent) {
a3439b27 162 $year = date('Y');
163 $month = date('m');
164 $quarter = sent_subfolder_getQuarter($month);
165
1c52ba77 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 */
4965e012 172/*
72b9aff9 173 if( $imap_server_type == 'uw' ) {
174 $cnd_delimiter = '';
175 } else {
176 $cnd_delimiter = $delimiter;
177 }
4965e012 178*/
179 $cnd_delimiter = $delimiter;
72b9aff9 180
a3439b27 181 switch ($sent_subfolders_setting) {
1c52ba77 182 case SMPREF_SENT_SUBFOLDERS_YEARLY:
183 $level = 1;
72b9aff9 184 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 185 . $year;
1c52ba77 186 break;
187 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
188 $level = 2;
72b9aff9 189 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 190 . $year
191 . $delimiter . $quarter;
192 $year_folder = $sent_subfolders_base
a3add160 193 . $year;
1c52ba77 194 break;
195 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
196 $level = 2;
72b9aff9 197 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 198 . $year
199 . $delimiter . $month;
a3add160 200 $year_folder = $sent_subfolders_base . $year;
1c52ba77 201 break;
202 case SMPREF_SENT_SUBFOLDERS_DISABLED:
203 default:
204 $level = 0;
205 $sent_subfolder = $sent_folder;
206 $year_folder = $sent_folder;
a3439b27 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. */
1c52ba77 212
a3439b27 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);
d79e01f5 223
a3439b27 224 /* Auto-create the year folder, if it does not yet exist. */
225 if (!sqimap_mailbox_exists($ic, $year_folder)) {
1c52ba77 226 sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
a3439b27 227 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
228 sqimap_subscribe($ic, $year_folder);
229 }
d79e01f5 230
a3439b27 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, '');
1c52ba77 234 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
235 sqimap_subscribe($ic, $sent_subfolder);
a3439b27 236 }
237
238 /* Close the imap connection. */
239 sqimap_logout($ic);
240 }
241 }
242 }
243}
244
245function sent_subfolder_getQuarter($month) {
246 switch ($month) {
247 case '01':
248 case '02':
d79e01f5 249 case '03':
250 $result = '1';
251 break;
a3439b27 252 case '04':
253 case '05':
d79e01f5 254 case '06':
255 $result = '2';
256 break;
a3439b27 257 case '07':
258 case '08':
d79e01f5 259 case '09':
260 $result = '3';
261 break;
a3439b27 262 case '10':
263 case '11':
d79e01f5 264 case '12':
265 $result = '4';
266 break;
267 default:
268 $result = 'ERR';
a3439b27 269 }
270
271 /* Return the current quarter. */
272 return ('Q' . $result);
273}
274
275?>