4 * setup.php -- Sent Subfolders Setup File
6 * Copyright (c) 1999-2002 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.
14 define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
15 define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
16 define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
17 define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
18 define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
20 function squirrelmail_plugin_init_sent_subfolders() {
21 /* Standard initialization API. */
22 global $squirrelmail_plugin_hooks;
24 /* The hooks to make the sent subfolders display correctly. */
25 $squirrelmail_plugin_hooks
26 ['check_handleAsSent_result']['sent_subfolders'] =
27 'sent_subfolders_check_handleAsSent';
29 /* The hooks to automatically update sent subfolders. */
30 $squirrelmail_plugin_hooks
31 ['left_main_before']['sent_subfolders'] =
32 'sent_subfolders_update_sentfolder';
34 $squirrelmail_plugin_hooks
35 ['compose_send']['sent_subfolders'] =
36 'sent_subfolders_update_sentfolder';
38 /* The hook to load the sent subfolders prefs. */
39 $squirrelmail_plugin_hooks
40 ['loading_prefs']['sent_subfolders'] =
41 'sent_subfolders_load_prefs';
43 /* The hooks to handle sent subfolders options. */
44 $squirrelmail_plugin_hooks
45 ['optpage_loadhook_folder']['sent_subfolders'] =
46 'sent_subfolders_optpage_loadhook_folders';
49 function sent_subfolders_check_handleAsSent() {
50 global $handleAsSent_result, $sent_subfolders_base,
52 if ( !check_php_version(4,1) ) {
55 $sent_subfolders_base = 'INBOX.Sent';
56 $args = func_get_arg(0);
57 $delimiter = $_SESSION['delimiter'];
59 /* Only check the folder string if we have been passed a mailbox. */
60 if ($use_sent_subfolders && (count($args) > 1)) {
61 /* Chop up the folder strings as needed. */
62 $base_str = $sent_subfolders_base . $delimiter;
63 $mbox_str = substr($args[1], 0, strlen($base_str));
65 /* Perform the comparison. */
66 $handleAsSent_result =
67 ( $handleAsSent_result
68 ||
($base_str == $mbox_str)
69 ||
($sent_subfolders_base == $args[1])
74 function sent_subfolders_load_prefs() {
75 global $use_sent_subfolders, $data_dir, $username,
76 $sent_subfolders_setting, $sent_subfolders_base;
78 $use_sent_subfolders = getPref
79 ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF
);
81 $sent_subfolders_setting = getPref
82 ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED
);
84 $sent_subfolders_base = getPref
85 ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE
);
88 function sent_subfolders_optpage_loadhook_folders() {
89 global $optpage_data, $imapServerAddress, $imapPort;
91 if ( !check_php_version(4,1) ) {
92 global $_SESSION, $_COOKIE;
94 $username = $_SESSION['username'];
95 $key = $_COOKIE['key'];
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 if ( !check_php_version(4,1) ) {
163 global $_SESSION, $_COOKIE;
165 $username = $_SESSION['username'];
166 $key = $_COOKIE['key'];
167 $delimiter = $_SESSION['delimiter'];
169 if ($use_sent_subfolders ||
$move_to_sent) {
172 $quarter = sent_subfolder_getQuarter($month);
175 Regarding the structure we've got three main possibilities.
176 One sent holder. level 0.
177 Multiple year holders with messages in it. level 1.
178 Multiple year folders with holders in it. level 2.
181 if( $imap_server_type == 'uw' ) {
184 $cnd_delimiter = $delimiter;
187 $cnd_delimiter = $delimiter;
189 switch ($sent_subfolders_setting) {
190 case SMPREF_SENT_SUBFOLDERS_YEARLY
:
192 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
195 case SMPREF_SENT_SUBFOLDERS_QUARTERLY
:
197 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
199 . $delimiter . $quarter;
200 $year_folder = $sent_subfolders_base
203 case SMPREF_SENT_SUBFOLDERS_MONTHLY
:
205 $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
207 . $delimiter . $month;
208 $year_folder = $sent_subfolders_base . $year;
210 case SMPREF_SENT_SUBFOLDERS_DISABLED
:
213 $sent_subfolder = $sent_folder;
214 $year_folder = $sent_folder;
217 /* If this folder is NOT the current sent folder, update stuff. */
218 if ($sent_subfolder != $sent_folder) {
219 /* First, update the sent folder. */
221 setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
222 setPref($data_dir, $username, 'move_to_sent', SMPREF_ON
);
223 $sent_folder = $sent_subfolder;
224 $move_to_sent = SMPREF_ON
;
226 /* Auto-create folders, if they do not yet exist. */
227 if ($sent_folder != 'none') {
228 /* Create the imap connection. */
230 ($username, $key, $imapServerAddress, $imapPort, 10);
232 /* Auto-create the year folder, if it does not yet exist. */
233 if (!sqimap_mailbox_exists($ic, $year_folder)) {
234 sqimap_mailbox_create($ic, $year_folder, ($level==1)?
'':'noselect');
235 } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
236 sqimap_subscribe($ic, $year_folder);
239 /* Auto-create the subfolder, if it does not yet exist. */
240 if (!sqimap_mailbox_exists($ic, $sent_folder)) {
241 sqimap_mailbox_create($ic, $sent_folder, '');
242 } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
243 sqimap_subscribe($ic, $sent_subfolder);
246 /* Close the imap connection. */
253 function sent_subfolder_getQuarter($month) {
279 /* Return the current quarter. */
280 return ('Q' . $result);