A start for a new change_password master plugin. This is not finished
[squirrelmail.git] / plugins / sent_subfolders / setup.php
... / ...
CommitLineData
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 * @package plugins
13 * @subpackage sent_subfolders
14 */
15
16/**
17 *
18 */
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() {
55 global $handleAsSent_result, $sent_subfolders_base,
56 $use_sent_subfolders;
57
58 $sent_subfolders_base = 'INBOX.Sent';
59 $args = func_get_arg(0);
60 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
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
71 || ($base_str == $mbox_str)
72 || ($sent_subfolders_base == $args[1])
73 );
74 }
75}
76
77function sent_subfolders_load_prefs() {
78 global $use_sent_subfolders, $data_dir, $username,
79 $sent_subfolders_setting, $sent_subfolders_base;
80
81 $use_sent_subfolders = getPref
82 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
83
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() {
92 global $optpage_data, $imapServerAddress, $imapPort;
93
94 sqgetGlobalVar('username', $username, SQ_SESSION);
95 sqgetGlobalVar('key', $key, SQ_COOKIE);
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
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() {
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 sqgetGlobalVar('username', $username, SQ_SESSION);
163 sqgetGlobalVar('key', $key, SQ_COOKIE);
164 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
165
166 if ($use_sent_subfolders || $move_to_sent) {
167 $year = date('Y');
168 $month = date('m');
169 $quarter = sent_subfolder_getQuarter($month);
170
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 */
177/*
178 if( $imap_server_type == 'uw' ) {
179 $cnd_delimiter = '';
180 } else {
181 $cnd_delimiter = $delimiter;
182 }
183*/
184 $cnd_delimiter = $delimiter;
185
186 switch ($sent_subfolders_setting) {
187 case SMPREF_SENT_SUBFOLDERS_YEARLY:
188 $level = 1;
189 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
190 . $year;
191 break;
192 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
193 $level = 2;
194 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
195 . $year
196 . $delimiter . $quarter;
197 $year_folder = $sent_subfolders_base
198 . $year;
199 break;
200 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
201 $level = 2;
202 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
203 . $year
204 . $delimiter . $month;
205 $year_folder = $sent_subfolders_base . $year;
206 break;
207 case SMPREF_SENT_SUBFOLDERS_DISABLED:
208 default:
209 $level = 0;
210 $sent_subfolder = $sent_folder;
211 $year_folder = $sent_folder;
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. */
217
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);
228
229 /* Auto-create the year folder, if it does not yet exist. */
230 if (!sqimap_mailbox_exists($ic, $year_folder)) {
231 sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
232 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
233 sqimap_subscribe($ic, $year_folder);
234 }
235
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, '');
239 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
240 sqimap_subscribe($ic, $sent_subfolder);
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':
254 case '03':
255 $result = '1';
256 break;
257 case '04':
258 case '05':
259 case '06':
260 $result = '2';
261 break;
262 case '07':
263 case '08':
264 case '09':
265 $result = '3';
266 break;
267 case '10':
268 case '11':
269 case '12':
270 $result = '4';
271 break;
272 default:
273 $result = 'ERR';
274 }
275
276 /* Return the current quarter. */
277 return ('Q' . $result);
278}
279
280?>