Forgot to add file before last commit...
[squirrelmail.git] / plugins / sent_subfolders / functions.php
CommitLineData
0e21ce8f 1<?php
2
3/**
4 * setup.php -- Sent Subfolders Setup File
5 *
6 * This is a standard SquirrelMail 1.2 API for plugins.
7 *
8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package plugins
12 * @subpackage sent_subfolders
13 */
14
15function sent_subfolders_check_handleAsSent_do() {
16 global $handleAsSent_result, $sent_subfolders_base,
17 $use_sent_subfolders;
18
19 // FIXME: hardcoded folder
20 $sent_subfolders_base = 'INBOX.Sent';
21 $args = func_get_arg(0);
22 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
23
24 /* Only check the folder string if we have been passed a mailbox. */
25 if ($use_sent_subfolders && (count($args) > 1)) {
26 /* Chop up the folder strings as needed. */
27 $base_str = $sent_subfolders_base . $delimiter;
28 $mbox_str = substr($args[1], 0, strlen($base_str));
29
30 /* Perform the comparison. */
31 $handleAsSent_result =
32 ( $handleAsSent_result
33 || ($base_str == $mbox_str)
34 || ($sent_subfolders_base == $args[1])
35 );
36 }
37}
38
39/**
40 * Loads sent_subfolders settings
41 */
42function sent_subfolders_load_prefs_do() {
43 global $use_sent_subfolders, $data_dir, $username,
44 $sent_subfolders_setting, $sent_subfolders_base;
45
46 $use_sent_subfolders = getPref
47 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
48
49 $sent_subfolders_setting = getPref
50 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
51
52 $sent_subfolders_base = getPref
53 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
54}
55
56/**
57 * Adds sent_subfolders options in folder preferences
58 */
59function sent_subfolders_optpage_loadhook_folders_do() {
60 global $username, $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
61
62 /* Get some imap data we need later. */
63 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
64 $boxes = sqimap_mailbox_list($imapConnection);
65 sqimap_logout($imapConnection);
66
67 /* Load the Sent Subfolder Options into an array. */
68 $optgrp = _("Sent Subfolders Options");
69 $optvals = array();
70
71 $optvals[] = array(
72 'name' => 'sent_subfolders_setting',
73 'caption' => _("Use Sent Subfolders"),
74 'type' => SMOPT_TYPE_STRLIST,
75 'refresh' => SMOPT_REFRESH_FOLDERLIST,
76 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
77 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
78 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
79 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
80 'save' => 'save_option_sent_subfolders_setting'
81 );
82
83 $filtered_folders=array_filter($boxes, "filter_folders");
84 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
85
86 $optvals[] = array(
87 'name' => 'sent_subfolders_base',
88 'caption' => _("Base Sent Folder"),
89 'type' => SMOPT_TYPE_FLDRLIST,
90 'refresh' => SMOPT_REFRESH_FOLDERLIST,
91 'posvals' => $sent_subfolders_base_values,
92 'folder_filter' => 'noinferiors'
93 );
94
95 if ($show_contain_subfolders_option) {
96 $optvals[] = array(
97 'name' => 'sent_subfolders_warning',
98 'caption' => _("Warning"),
99 'type' => SMOPT_TYPE_COMMENT,
100 'comment' => _("There are some restrictions in Sent Subfolder options.")
101 );
102 }
103
104 /* Add our option data to the global array. */
105 $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
106 $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
107}
108
109/**
110 * Defines folder filtering rules
111 *
112 * Callback function that should exclude some folders from folder listing.
113 * @param array $fldr list of folders. See sqimap_mailbox_list
114 * @return boolean returns true, if folder has to included in folder listing
115 * @access private
116 */
117function filter_folders($fldr) {
118 return strtolower($fldr['unformatted'])!='inbox';
119}
120
121/**
122 * Saves sent_subfolder_options
123 */
124function save_option_sent_subfolders_setting($option) {
125 global $data_dir, $username, $use_sent_subfolders;
126
127 /* Set use_sent_subfolders as either on or off. */
128 if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
129 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
130 } else {
131 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
132 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
133 }
134
135 /* Now just save the option as normal. */
136 save_option($option);
137}
138
139/**
140 * Update sent_subfolders settings
141 *
142 * function updates default sent folder value and
143 * creates required imap folders
144 */
145function sent_subfolders_update_sentfolder_do() {
146 global $sent_folder, $username;
147 global $sent_subfolders_base, $sent_subfolders_setting;
148 global $data_dir, $imapServerAddress, $imapPort;
149 global $use_sent_subfolders, $move_to_sent;
150
151 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
152
153 if ($use_sent_subfolders || $move_to_sent) {
154 $year = date('Y');
155 $month = date('m');
156 $quarter = sent_subfolder_getQuarter($month);
157
158 /**
159 * Regarding the structure we've got three main possibilities.
160 * One sent holder. level 0.
161 * Multiple year holders with messages in it. level 1.
162 * Multiple year folders with holders in it. level 2.
163 */
164
165 switch ($sent_subfolders_setting) {
166 case SMPREF_SENT_SUBFOLDERS_YEARLY:
167 $level = 1;
168 $sent_subfolder = $sent_subfolders_base . $delimiter
169 . $year;
170 break;
171 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
172 $level = 2;
173 $sent_subfolder = $sent_subfolders_base . $delimiter
174 . $year
175 . $delimiter . $quarter;
176 $year_folder = $sent_subfolders_base . $delimiter
177 . $year;
178 break;
179 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
180 $level = 2;
181 $sent_subfolder = $sent_subfolders_base . $delimiter
182 . $year
183 . $delimiter . $month;
184 $year_folder = $sent_subfolders_base. $delimiter . $year;
185 break;
186 case SMPREF_SENT_SUBFOLDERS_DISABLED:
187 default:
188 $level = 0;
189 $sent_subfolder = $sent_folder;
190 $year_folder = $sent_folder;
191 }
192
193 /* If this folder is NOT the current sent folder, update stuff. */
194 if ($sent_subfolder != $sent_folder) {
195 /* Auto-create folders, if they do not yet exist. */
196 if ($sent_subfolder != 'none') {
197 /* Create the imap connection. */
198 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
199
200 $boxes = false;
201 /**
202 * If sent_subfolder can't store messages (noselect) ||
203 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
204 * subfolder_base can't store subfolders (noinferiors), setup is broken
205 */
206 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
207 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
208 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
209 error_box(_("Sent Subfolders plugin is misconfigured."));
210 } else {
211 if ($level==2) {
212 /* Auto-create the year folder, if it does not yet exist. */
213 if (!sqimap_mailbox_exists($ic, $year_folder)) {
214 sqimap_mailbox_create($ic, $year_folder, 'noselect');
215 // TODO: safety check for imap servers that can't create subfolders
216
217 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
218 sqimap_subscribe($ic, $year_folder);
219 }
220 }
221
222 /* Auto-create the subfolder, if it does not yet exist. */
223 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
224 sqimap_mailbox_create($ic, $sent_subfolder, '');
225 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
226 sqimap_subscribe($ic, $sent_subfolder);
227 }
228 /* Update sent_folder setting. */
229 //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
230 //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
231 $sent_folder = $sent_subfolder;
232 $move_to_sent = SMPREF_ON;
233 }
234 /* Close the imap connection. */
235 sqimap_logout($ic);
236 }
237
238 }
239 }
240}
241
242/**
243 * Sets quarter subfolder names
244 *
245 * @param string $month numeric month
246 * @return string quarter name (Q + number)
247 */
248function sent_subfolder_getQuarter($month) {
249 switch ($month) {
250 case '01':
251 case '02':
252 case '03':
253 $result = '1';
254 break;
255 case '04':
256 case '05':
257 case '06':
258 $result = '2';
259 break;
260 case '07':
261 case '08':
262 case '09':
263 $result = '3';
264 break;
265 case '10':
266 case '11':
267 case '12':
268 $result = '4';
269 break;
270 default:
271 $result = 'ERR';
272 }
273
274 /* Return the current quarter. */
275 return ('Q' . $result);
276}
277
278/**
279 * detects if mailbox is part of sent_subfolders
280 *
281 * @param string $mb imap folder name
282 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
283 */
284function sent_subfolders_special_mailbox_do($mb) {
285 global $data_dir, $username;
286
287 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
288
289 $use_sent_subfolders = getPref
290 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
291 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
292
293 /**
294 * If sent_subfolders are used and mailbox is equal to subfolder base
295 * or mailbox matches subfolder base + delimiter.
296 */
297 if ($use_sent_subfolders == SMPREF_ON &&
298 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
299 return 1;
300 }
301 return 0;
302}