Translate special folders doesn't mean translate any folder
[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
afa916e9 15define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
16define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
17define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
18define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
19define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
20
32a7b3e0 21function sent_subfolders_check_handleAsSent_do($mailbox) {
afa916e9 22
77800601 23 global $handleAsSent_result, $data_dir, $username;
afa916e9 24
25 // don't need to bother if it's already special
26 if ($handleAsSent_result) return;
0e21ce8f 27
0e21ce8f 28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
29
afa916e9 30 $use_sent_subfolders = getPref($data_dir, $username,
31 'use_sent_subfolders', SMPREF_OFF);
32 $sent_subfolders_base = getPref($data_dir, $username,
33 'sent_subfolders_base', SMPREF_NONE);
34
0e21ce8f 35 /* Only check the folder string if we have been passed a mailbox. */
32a7b3e0 36 if ($use_sent_subfolders && !empty($mailbox)) {
0e21ce8f 37 /* Chop up the folder strings as needed. */
38 $base_str = $sent_subfolders_base . $delimiter;
32a7b3e0 39 $mbox_str = substr($mailbox, 0, strlen($base_str));
0e21ce8f 40
41 /* Perform the comparison. */
afa916e9 42 $handleAsSent_result = ( ($base_str == $mbox_str)
43 || ($sent_subfolders_base == $mailbox) );
0e21ce8f 44 }
45}
46
47/**
0e21ce8f 48 * Adds sent_subfolders options in folder preferences
49 */
50function sent_subfolders_optpage_loadhook_folders_do() {
afa916e9 51
77800601 52 global $data_dir, $username, $optpage_data, $imapServerAddress,
53 $imapPort, $show_contain_subfolders_option;
0e21ce8f 54
55 /* Get some imap data we need later. */
56 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
57 $boxes = sqimap_mailbox_list($imapConnection);
58 sqimap_logout($imapConnection);
59
60 /* Load the Sent Subfolder Options into an array. */
61 $optgrp = _("Sent Subfolders Options");
62 $optvals = array();
63
afa916e9 64 global $sent_subfolders_setting;
65 $sent_subfolders_setting = getPref($data_dir, $username,
66 'sent_subfolders_setting',
67 SMPREF_SENT_SUBFOLDERS_DISABLED);
0e21ce8f 68 $optvals[] = array(
69 'name' => 'sent_subfolders_setting',
70 'caption' => _("Use Sent Subfolders"),
71 'type' => SMOPT_TYPE_STRLIST,
72 'refresh' => SMOPT_REFRESH_FOLDERLIST,
73 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
74 SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
75 SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
76 SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
77 'save' => 'save_option_sent_subfolders_setting'
78 );
79
80 $filtered_folders=array_filter($boxes, "filter_folders");
81 $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
82
afa916e9 83 global $sent_subfolders_base;
84 $sent_subfolders_base = getPref($data_dir, $username,
85 'sent_subfolders_base', SMPREF_NONE);
0e21ce8f 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) {
afa916e9 125 global $data_dir, $username;
0e21ce8f 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() {
afa916e9 146 global $sent_folder, $username,
147 $data_dir, $imapServerAddress, $imapPort,
148 $move_to_sent;
0e21ce8f 149
150 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
151
afa916e9 152 $use_sent_subfolders = getPref($data_dir, $username,
153 'use_sent_subfolders', SMPREF_OFF);
154 $sent_subfolders_setting = getPref($data_dir, $username,
155 'sent_subfolders_setting',
156 SMPREF_SENT_SUBFOLDERS_DISABLED);
157 $sent_subfolders_base = getPref($data_dir, $username,
158 'sent_subfolders_base', SMPREF_NONE);
159
0e21ce8f 160 if ($use_sent_subfolders || $move_to_sent) {
161 $year = date('Y');
162 $month = date('m');
163 $quarter = sent_subfolder_getQuarter($month);
164
165 /**
166 * Regarding the structure we've got three main possibilities.
167 * One sent holder. level 0.
168 * Multiple year holders with messages in it. level 1.
169 * Multiple year folders with holders in it. level 2.
170 */
171
172 switch ($sent_subfolders_setting) {
173 case SMPREF_SENT_SUBFOLDERS_YEARLY:
174 $level = 1;
175 $sent_subfolder = $sent_subfolders_base . $delimiter
176 . $year;
177 break;
178 case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
179 $level = 2;
180 $sent_subfolder = $sent_subfolders_base . $delimiter
181 . $year
182 . $delimiter . $quarter;
183 $year_folder = $sent_subfolders_base . $delimiter
184 . $year;
185 break;
186 case SMPREF_SENT_SUBFOLDERS_MONTHLY:
187 $level = 2;
188 $sent_subfolder = $sent_subfolders_base . $delimiter
189 . $year
190 . $delimiter . $month;
191 $year_folder = $sent_subfolders_base. $delimiter . $year;
192 break;
193 case SMPREF_SENT_SUBFOLDERS_DISABLED:
194 default:
195 $level = 0;
196 $sent_subfolder = $sent_folder;
197 $year_folder = $sent_folder;
198 }
199
200 /* If this folder is NOT the current sent folder, update stuff. */
201 if ($sent_subfolder != $sent_folder) {
202 /* Auto-create folders, if they do not yet exist. */
203 if ($sent_subfolder != 'none') {
204 /* Create the imap connection. */
205 $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
206
207 $boxes = false;
208 /**
209 * If sent_subfolder can't store messages (noselect) ||
210 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
211 * subfolder_base can't store subfolders (noinferiors), setup is broken
212 */
213 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
214 ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
215 sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
216 error_box(_("Sent Subfolders plugin is misconfigured."));
217 } else {
218 if ($level==2) {
219 /* Auto-create the year folder, if it does not yet exist. */
220 if (!sqimap_mailbox_exists($ic, $year_folder)) {
221 sqimap_mailbox_create($ic, $year_folder, 'noselect');
222 // TODO: safety check for imap servers that can't create subfolders
223
224 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
225 sqimap_subscribe($ic, $year_folder);
226 }
227 }
228
229 /* Auto-create the subfolder, if it does not yet exist. */
230 if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
231 sqimap_mailbox_create($ic, $sent_subfolder, '');
232 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
233 sqimap_subscribe($ic, $sent_subfolder);
234 }
235 /* Update sent_folder setting. */
236 //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
237 //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
238 $sent_folder = $sent_subfolder;
239 $move_to_sent = SMPREF_ON;
240 }
241 /* Close the imap connection. */
242 sqimap_logout($ic);
243 }
244
245 }
246 }
247}
248
249/**
250 * Sets quarter subfolder names
251 *
252 * @param string $month numeric month
253 * @return string quarter name (Q + number)
254 */
255function sent_subfolder_getQuarter($month) {
256 switch ($month) {
257 case '01':
258 case '02':
259 case '03':
260 $result = '1';
261 break;
262 case '04':
263 case '05':
264 case '06':
265 $result = '2';
266 break;
267 case '07':
268 case '08':
269 case '09':
270 $result = '3';
271 break;
272 case '10':
273 case '11':
274 case '12':
275 $result = '4';
276 break;
277 default:
278 $result = 'ERR';
279 }
280
281 /* Return the current quarter. */
282 return ('Q' . $result);
283}
284
285/**
286 * detects if mailbox is part of sent_subfolders
287 *
288 * @param string $mb imap folder name
289 * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
290 */
291function sent_subfolders_special_mailbox_do($mb) {
292 global $data_dir, $username;
293
294 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
295
afa916e9 296 $use_sent_subfolders = getPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
297
298//FIXME: why "na" as the default? Shouldn't this be SMPREF_NONE as it is elsewhere in this file?
0e21ce8f 299 $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
300
301 /**
302 * If sent_subfolders are used and mailbox is equal to subfolder base
303 * or mailbox matches subfolder base + delimiter.
304 */
305 if ($use_sent_subfolders == SMPREF_ON &&
306 ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
307 return 1;
308 }
309 return 0;
310}