4 * setup.php -- Sent Subfolders Setup File
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This is a standard Squirrelmail-1.2 API for plugins.
13 * @subpackage sent_subfolders
19 define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
20 define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
21 define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
22 define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
23 define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
25 function squirrelmail_plugin_init_sent_subfolders() {
26 /* Standard initialization API. */
27 global $squirrelmail_plugin_hooks;
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';
34 /* The hooks to automatically update sent subfolders. */
35 $squirrelmail_plugin_hooks
36 ['left_main_before']['sent_subfolders'] =
37 'sent_subfolders_update_sentfolder';
39 $squirrelmail_plugin_hooks
40 ['compose_send']['sent_subfolders'] =
41 'sent_subfolders_update_sentfolder';
43 /* The hook to load the sent subfolders prefs. */
44 $squirrelmail_plugin_hooks
45 ['loading_prefs']['sent_subfolders'] =
46 'sent_subfolders_load_prefs';
48 /* The hooks to handle sent subfolders options. */
49 $squirrelmail_plugin_hooks
50 ['optpage_loadhook_folder']['sent_subfolders'] =
51 'sent_subfolders_optpage_loadhook_folders';
54 function sent_subfolders_check_handleAsSent() {
55 global $handleAsSent_result, $sent_subfolders_base,
58 $sent_subfolders_base = 'INBOX.Sent';
59 $args = func_get_arg(0);
60 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
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));
68 /* Perform the comparison. */
69 $handleAsSent_result =
70 ( $handleAsSent_result
71 ||
($base_str == $mbox_str)
72 ||
($sent_subfolders_base == $args[1])
77 function sent_subfolders_load_prefs() {
78 global $use_sent_subfolders, $data_dir, $username,
79 $sent_subfolders_setting, $sent_subfolders_base;
81 $use_sent_subfolders = getPref
82 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
84 $sent_subfolders_setting = getPref
85 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED
);
87 $sent_subfolders_base = getPref
88 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE
);
91 function sent_subfolders_optpage_loadhook_folders() {
92 global $optpage_data, $imapServerAddress, $imapPort;
94 sqgetGlobalVar('username', $username, SQ_SESSION
);
95 sqgetGlobalVar('key', $key, SQ_COOKIE
);
97 /* Get some imap data we need later. */
99 sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
100 $boxes = sqimap_mailbox_list($imapConnection);
101 sqimap_logout($imapConnection);
103 /* Load the Sent Subfolder Options into an array. */
104 $optgrp = _("Sent Subfolders Options");
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'
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(' ', ' ', $folder['formatted']);
124 $sent_subfolders_base_values[$real_value] = $disp_value;
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
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;
141 function save_option_sent_subfolders_setting($option) {
142 global $data_dir, $username, $use_sent_subfolders;
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
);
148 setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON
);
149 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON
);
152 /* Now just save the option as normal. */
153 save_option($option);
156 function 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;
162 sqgetGlobalVar('username', $username, SQ_SESSION
);
163 sqgetGlobalVar('key', $key, SQ_COOKIE
);
164 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
166 if ($use_sent_subfolders ||
$move_to_sent) {
169 $quarter = sent_subfolder_getQuarter($month);
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.
178 if( $imap_server_type == 'uw' ) {
181 $cnd_delimiter = $delimiter;
184 $cnd_delimiter = $delimiter;
186 switch ($sent_subfolders_setting) {
187 case SMPREF_SENT_SUBFOLDERS_YEARLY
:
189 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
192 case SMPREF_SENT_SUBFOLDERS_QUARTERLY
:
194 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
196 . $delimiter . $quarter;
197 $year_folder = $sent_subfolders_base
200 case SMPREF_SENT_SUBFOLDERS_MONTHLY
:
202 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
204 . $delimiter . $month;
205 $year_folder = $sent_subfolders_base . $year;
207 case SMPREF_SENT_SUBFOLDERS_DISABLED
:
210 $sent_subfolder = $sent_folder;
211 $year_folder = $sent_folder;
214 /* If this folder is NOT the current sent folder, update stuff. */
215 if ($sent_subfolder != $sent_folder) {
216 /* First, update the sent folder. */
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
;
223 /* Auto-create folders, if they do not yet exist. */
224 if ($sent_folder != 'none') {
225 /* Create the imap connection. */
227 ($username, $key, $imapServerAddress, $imapPort, 10);
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);
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);
243 /* Close the imap connection. */
250 function sent_subfolder_getQuarter($month) {
276 /* Return the current quarter. */
277 return ('Q' . $result);