Replacing tabs with spaces
[squirrelmail.git] / plugins / sent_subfolders / setup.php
CommitLineData
a3439b27 1<?php
2
3/**
4 * setup.php -- Sent Subfolders Setup File
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 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
91e0dccc 16/**
17 *
ea5f4b8e 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
bb58f260 25/**
26 * Adds plugin to squirrelmail hooks
27 */
a3439b27 28function squirrelmail_plugin_init_sent_subfolders() {
29 /* Standard initialization API. */
30 global $squirrelmail_plugin_hooks;
31
32 /* The hooks to make the sent subfolders display correctly. */
33 $squirrelmail_plugin_hooks
34 ['check_handleAsSent_result']['sent_subfolders'] =
35 'sent_subfolders_check_handleAsSent';
36
37 /* The hooks to automatically update sent subfolders. */
38 $squirrelmail_plugin_hooks
39 ['left_main_before']['sent_subfolders'] =
40 'sent_subfolders_update_sentfolder';
41
42 $squirrelmail_plugin_hooks
43 ['compose_send']['sent_subfolders'] =
44 'sent_subfolders_update_sentfolder';
45
46 /* The hook to load the sent subfolders prefs. */
47 $squirrelmail_plugin_hooks
48 ['loading_prefs']['sent_subfolders'] =
49 'sent_subfolders_load_prefs';
50
51 /* The hooks to handle sent subfolders options. */
52 $squirrelmail_plugin_hooks
53 ['optpage_loadhook_folder']['sent_subfolders'] =
54 'sent_subfolders_optpage_loadhook_folders';
bb58f260 55
56 /* mark base sent folder as special mailbox */
57 $squirrelmail_plugin_hooks
91e0dccc 58 ['special_mailbox']['sent_subfolders'] =
59 'sent_subfolders_special_mailbox';
a3439b27 60}
61
62function sent_subfolders_check_handleAsSent() {
b6b31355 63 global $handleAsSent_result, $sent_subfolders_base,
04f6008a 64 $use_sent_subfolders;
b587ac51 65
a3439b27 66 $sent_subfolders_base = 'INBOX.Sent';
67 $args = func_get_arg(0);
b587ac51 68 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
a3439b27 69
70 /* Only check the folder string if we have been passed a mailbox. */
71 if ($use_sent_subfolders && (count($args) > 1)) {
72 /* Chop up the folder strings as needed. */
73 $base_str = $sent_subfolders_base . $delimiter;
74 $mbox_str = substr($args[1], 0, strlen($base_str));
75
76 /* Perform the comparison. */
77 $handleAsSent_result =
78 ( $handleAsSent_result
d79e01f5 79 || ($base_str == $mbox_str)
80 || ($sent_subfolders_base == $args[1])
a3439b27 81 );
82 }
83}
84
bb58f260 85/**
86 * Loads sent_subfolders settings
87 */
a3439b27 88function sent_subfolders_load_prefs() {
88cb1b4d 89 global $use_sent_subfolders, $data_dir, $username,
90 $sent_subfolders_setting, $sent_subfolders_base;
a3439b27 91
92 $use_sent_subfolders = getPref
93 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
d79e01f5 94
a3439b27 95 $sent_subfolders_setting = getPref
96 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
97
98 $sent_subfolders_base = getPref
99 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
100}
101
bb58f260 102/**
103 * Adds sent_subfolders options in folder preferences
104 */
a3439b27 105function sent_subfolders_optpage_loadhook_folders() {
04f6008a 106 global $optpage_data, $imapServerAddress, $imapPort;
107
b587ac51 108 sqgetGlobalVar('username', $username, SQ_SESSION);
109 sqgetGlobalVar('key', $key, SQ_COOKIE);
a3439b27 110
111 /* Get some imap data we need later. */
112 $imapConnection =
113 sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
114 $boxes = sqimap_mailbox_list($imapConnection);
115 sqimap_logout($imapConnection);
116
117 /* Load the Sent Subfolder Options into an array. */
118 $optgrp = _("Sent Subfolders Options");
119 $optvals = array();
120
121 $optvals[] = array(
122 'name' => 'sent_subfolders_setting',
123 'caption' => _("Use Sent Subfolders"),
124 'type' => SMOPT_TYPE_STRLIST,
125 'refresh' => SMOPT_REFRESH_FOLDERLIST,
126 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
d79e01f5 127 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
128 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
129 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
a3439b27 130 'save' => 'save_option_sent_subfolders_setting'
131 );
132
2589ba9a 133 $filtered_folders=array_filter($boxes, "filter_folders");
134 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
a3439b27 135
136 $optvals[] = array(
137 'name' => 'sent_subfolders_base',
138 'caption' => _("Base Sent Folder"),
2589ba9a 139 'type' => SMOPT_TYPE_FLDRLIST,
a3439b27 140 'refresh' => SMOPT_REFRESH_FOLDERLIST,
141 'posvals' => $sent_subfolders_base_values
142 );
143
144 /* Add our option data to the global array. */
145 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
146 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
147}
148
bb58f260 149/**
2589ba9a 150 * Defines folder filtering rules
151 *
152 * Callback function that should exclude some folders from folder listing.
153 * @param array $fldr list of folders. See sqimap_mailbox_list
154 * @return boolean returns true, if folder has to included in folder listing
91e0dccc 155 * @access private
2589ba9a 156 */
157function filter_folders($fldr) {
158 return strtolower($fldr['unformatted'])!='inbox';
159}
160
161/**
bb58f260 162 * Saves sent_subfolder_options
163 */
a3439b27 164function save_option_sent_subfolders_setting($option) {
165 global $data_dir, $username, $use_sent_subfolders;
166
167 /* Set use_sent_subfolders as either on or off. */
168 if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
169 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
170 } else {
171 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
172 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
173 }
174
175 /* Now just save the option as normal. */
176 save_option($option);
177}
178
bb58f260 179/**
180 * Update sent_subfolders settings
181 *
91e0dccc 182 * function updates default sent folder value and
bb58f260 183 * creates required imap folders
184 */
a3439b27 185function sent_subfolders_update_sentfolder() {
ce68b76b 186 global $sent_folder;
a3439b27 187 global $sent_subfolders_base, $sent_subfolders_setting;
04f6008a 188 global $data_dir, $imapServerAddress, $imapPort;
ce68b76b 189 global $use_sent_subfolders, $move_to_sent;
d79e01f5 190
b587ac51 191 sqgetGlobalVar('username', $username, SQ_SESSION);
192 sqgetGlobalVar('key', $key, SQ_COOKIE);
193 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
91e0dccc 194
1c52ba77 195 if ($use_sent_subfolders || $move_to_sent) {
a3439b27 196 $year = date('Y');
197 $month = date('m');
198 $quarter = sent_subfolder_getQuarter($month);
199
1c52ba77 200 /*
201 Regarding the structure we've got three main possibilities.
202 One sent holder. level 0.
203 Multiple year holders with messages in it. level 1.
204 Multiple year folders with holders in it. level 2.
205 */
4965e012 206/*
72b9aff9 207 if( $imap_server_type == 'uw' ) {
208 $cnd_delimiter = '';
209 } else {
210 $cnd_delimiter = $delimiter;
211 }
91e0dccc 212*/
4965e012 213 $cnd_delimiter = $delimiter;
91e0dccc 214
a3439b27 215 switch ($sent_subfolders_setting) {
1c52ba77 216 case SMPREF_SENT_SUBFOLDERS_YEARLY:
217 $level = 1;
72b9aff9 218 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 219 . $year;
1c52ba77 220 break;
221 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
222 $level = 2;
91e0dccc 223 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 224 . $year
225 . $delimiter . $quarter;
226 $year_folder = $sent_subfolders_base
a3add160 227 . $year;
1c52ba77 228 break;
229 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
230 $level = 2;
72b9aff9 231 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
1c52ba77 232 . $year
233 . $delimiter . $month;
a3add160 234 $year_folder = $sent_subfolders_base . $year;
1c52ba77 235 break;
236 case SMPREF_SENT_SUBFOLDERS_DISABLED:
237 default:
238 $level = 0;
239 $sent_subfolder = $sent_folder;
240 $year_folder = $sent_folder;
a3439b27 241 }
242
243 /* If this folder is NOT the current sent folder, update stuff. */
244 if ($sent_subfolder != $sent_folder) {
245 /* First, update the sent folder. */
1c52ba77 246
a3439b27 247 setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
248 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
249 $sent_folder = $sent_subfolder;
250 $move_to_sent = SMPREF_ON;
251
252 /* Auto-create folders, if they do not yet exist. */
253 if ($sent_folder != 'none') {
254 /* Create the imap connection. */
255 $ic = sqimap_login
256 ($username, $key, $imapServerAddress, $imapPort, 10);
d79e01f5 257
a3439b27 258 /* Auto-create the year folder, if it does not yet exist. */
259 if (!sqimap_mailbox_exists($ic, $year_folder)) {
1c52ba77 260 sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
a3439b27 261 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
262 sqimap_subscribe($ic, $year_folder);
263 }
d79e01f5 264
a3439b27 265 /* Auto-create the subfolder, if it does not yet exist. */
266 if (!sqimap_mailbox_exists($ic, $sent_folder)) {
267 sqimap_mailbox_create($ic, $sent_folder, '');
1c52ba77 268 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
269 sqimap_subscribe($ic, $sent_subfolder);
a3439b27 270 }
271
272 /* Close the imap connection. */
273 sqimap_logout($ic);
274 }
275 }
276 }
277}
278
bb58f260 279/**
280 * Sets quarter subfolder names
281 *
282 * @param string $month numeric month
283 * @return string quarter name (Q + number)
284 */
a3439b27 285function sent_subfolder_getQuarter($month) {
286 switch ($month) {
287 case '01':
288 case '02':
d79e01f5 289 case '03':
290 $result = '1';
291 break;
a3439b27 292 case '04':
293 case '05':
d79e01f5 294 case '06':
295 $result = '2';
296 break;
a3439b27 297 case '07':
298 case '08':
d79e01f5 299 case '09':
300 $result = '3';
301 break;
a3439b27 302 case '10':
303 case '11':
d79e01f5 304 case '12':
305 $result = '4';
306 break;
307 default:
308 $result = 'ERR';
a3439b27 309 }
310
311 /* Return the current quarter. */
312 return ('Q' . $result);
313}
314
bb58f260 315/**
316 * detects if mailbox is part of sent_subfolders
317 *
318 * @param string $mb imap folder name
319 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
320 */
321function sent_subfolders_special_mailbox($mb) {
322 global $data_dir, $username;
323
324 $use_sent_subfolders = getPref
325 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
326 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
327
91e0dccc 328 if ($use_sent_subfolders == SMPREF_ON &&
bb58f260 329 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base) ) ) {
91e0dccc 330 return 1;
bb58f260 331 }
332 return 0;
333}
91e0dccc 334?>