1. added non anonymous ldap binding patch. (thanks to Ilyak Kasnacheev <ilyak at...
[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$
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
25function squirrelmail_plugin_init_sent_subfolders() {
26 /* Standard initialization API. */
27 global $squirrelmail_plugin_hooks;
28
29 /* The hooks to make the sent subfolders display correctly. */
30 $squirrelmail_plugin_hooks
31 ['check_handleAsSent_result']['sent_subfolders'] =
32 'sent_subfolders_check_handleAsSent';
33
34 /* The hooks to automatically update sent subfolders. */
35 $squirrelmail_plugin_hooks
36 ['left_main_before']['sent_subfolders'] =
37 'sent_subfolders_update_sentfolder';
38
39 $squirrelmail_plugin_hooks
40 ['compose_send']['sent_subfolders'] =
41 'sent_subfolders_update_sentfolder';
42
43 /* The hook to load the sent subfolders prefs. */
44 $squirrelmail_plugin_hooks
45 ['loading_prefs']['sent_subfolders'] =
46 'sent_subfolders_load_prefs';
47
48 /* The hooks to handle sent subfolders options. */
49 $squirrelmail_plugin_hooks
50 ['optpage_loadhook_folder']['sent_subfolders'] =
51 'sent_subfolders_optpage_loadhook_folders';
52}
53
54function sent_subfolders_check_handleAsSent() {
b6b31355 55 global $handleAsSent_result, $sent_subfolders_base,
04f6008a 56 $use_sent_subfolders;
b587ac51 57
a3439b27 58 $sent_subfolders_base = 'INBOX.Sent';
59 $args = func_get_arg(0);
b587ac51 60 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
a3439b27 61
62 /* Only check the folder string if we have been passed a mailbox. */
63 if ($use_sent_subfolders && (count($args) > 1)) {
64 /* Chop up the folder strings as needed. */
65 $base_str = $sent_subfolders_base . $delimiter;
66 $mbox_str = substr($args[1], 0, strlen($base_str));
67
68 /* Perform the comparison. */
69 $handleAsSent_result =
70 ( $handleAsSent_result
d79e01f5 71 || ($base_str == $mbox_str)
72 || ($sent_subfolders_base == $args[1])
a3439b27 73 );
74 }
75}
76
77function sent_subfolders_load_prefs() {
88cb1b4d 78 global $use_sent_subfolders, $data_dir, $username,
79 $sent_subfolders_setting, $sent_subfolders_base;
a3439b27 80
81 $use_sent_subfolders = getPref
82 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
d79e01f5 83
a3439b27 84 $sent_subfolders_setting = getPref
85 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
86
87 $sent_subfolders_base = getPref
88 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
89}
90
91function sent_subfolders_optpage_loadhook_folders() {
04f6008a 92 global $optpage_data, $imapServerAddress, $imapPort;
93
b587ac51 94 sqgetGlobalVar('username', $username, SQ_SESSION);
95 sqgetGlobalVar('key', $key, SQ_COOKIE);
a3439b27 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"),
d79e01f5 113 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
114 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
115 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
a3439b27 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
141function 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
156function sent_subfolders_update_sentfolder() {
04f6008a 157 global $sent_folder, $auto_create_special, $auto_create_done;
a3439b27 158 global $sent_subfolders_base, $sent_subfolders_setting;
04f6008a 159 global $data_dir, $imapServerAddress, $imapPort;
64727b20 160 global $use_sent_subfolders, $move_to_sent, $imap_server_type;
d79e01f5 161
b587ac51 162 sqgetGlobalVar('username', $username, SQ_SESSION);
163 sqgetGlobalVar('key', $key, SQ_COOKIE);
164 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
165
1c52ba77 166 if ($use_sent_subfolders || $move_to_sent) {
a3439b27 167 $year = date('Y');
168 $month = date('m');
169 $quarter = sent_subfolder_getQuarter($month);
170
1c52ba77 171 /*
172 Regarding the structure we've got three main possibilities.
173 One sent holder. level 0.
174 Multiple year holders with messages in it. level 1.
175 Multiple year folders with holders in it. level 2.
176 */
4965e012 177/*
72b9aff9 178 if( $imap_server_type == 'uw' ) {
179 $cnd_delimiter = '';
180 } else {
181 $cnd_delimiter = $delimiter;
182 }
4965e012 183*/
184 $cnd_delimiter = $delimiter;
72b9aff9 185
a3439b27 186 switch ($sent_subfolders_setting) {
1c52ba77 187 case SMPREF_SENT_SUBFOLDERS_YEARLY:
188 $level = 1;
72b9aff9 189 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 190 . $year;
1c52ba77 191 break;
192 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
193 $level = 2;
72b9aff9 194 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 195 . $year
196 . $delimiter . $quarter;
197 $year_folder = $sent_subfolders_base
a3add160 198 . $year;
1c52ba77 199 break;
200 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
201 $level = 2;
72b9aff9 202 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 203 . $year
204 . $delimiter . $month;
a3add160 205 $year_folder = $sent_subfolders_base . $year;
1c52ba77 206 break;
207 case SMPREF_SENT_SUBFOLDERS_DISABLED:
208 default:
209 $level = 0;
210 $sent_subfolder = $sent_folder;
211 $year_folder = $sent_folder;
a3439b27 212 }
213
214 /* If this folder is NOT the current sent folder, update stuff. */
215 if ($sent_subfolder != $sent_folder) {
216 /* First, update the sent folder. */
1c52ba77 217
a3439b27 218 setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
219 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
220 $sent_folder = $sent_subfolder;
221 $move_to_sent = SMPREF_ON;
222
223 /* Auto-create folders, if they do not yet exist. */
224 if ($sent_folder != 'none') {
225 /* Create the imap connection. */
226 $ic = sqimap_login
227 ($username, $key, $imapServerAddress, $imapPort, 10);
d79e01f5 228
a3439b27 229 /* Auto-create the year folder, if it does not yet exist. */
230 if (!sqimap_mailbox_exists($ic, $year_folder)) {
1c52ba77 231 sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
a3439b27 232 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
233 sqimap_subscribe($ic, $year_folder);
234 }
d79e01f5 235
a3439b27 236 /* Auto-create the subfolder, if it does not yet exist. */
237 if (!sqimap_mailbox_exists($ic, $sent_folder)) {
238 sqimap_mailbox_create($ic, $sent_folder, '');
1c52ba77 239 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
240 sqimap_subscribe($ic, $sent_subfolder);
a3439b27 241 }
242
243 /* Close the imap connection. */
244 sqimap_logout($ic);
245 }
246 }
247 }
248}
249
250function sent_subfolder_getQuarter($month) {
251 switch ($month) {
252 case '01':
253 case '02':
d79e01f5 254 case '03':
255 $result = '1';
256 break;
a3439b27 257 case '04':
258 case '05':
d79e01f5 259 case '06':
260 $result = '2';
261 break;
a3439b27 262 case '07':
263 case '08':
d79e01f5 264 case '09':
265 $result = '3';
266 break;
a3439b27 267 case '10':
268 case '11':
d79e01f5 269 case '12':
270 $result = '4';
271 break;
272 default:
273 $result = 'ERR';
a3439b27 274 }
275
276 /* Return the current quarter. */
277 return ('Q' . $result);
278}
279
280?>