| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * setup.php -- Sent Subfolders Setup File |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 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$ |
| 12 | */ |
| 13 | |
| 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'); |
| 19 | |
| 20 | function squirrelmail_plugin_init_sent_subfolders() { |
| 21 | /* Standard initialization API. */ |
| 22 | global $squirrelmail_plugin_hooks; |
| 23 | |
| 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'; |
| 28 | |
| 29 | /* The hooks to automatically update sent subfolders. */ |
| 30 | $squirrelmail_plugin_hooks |
| 31 | ['left_main_before']['sent_subfolders'] = |
| 32 | 'sent_subfolders_update_sentfolder'; |
| 33 | |
| 34 | $squirrelmail_plugin_hooks |
| 35 | ['compose_send']['sent_subfolders'] = |
| 36 | 'sent_subfolders_update_sentfolder'; |
| 37 | |
| 38 | /* The hook to load the sent subfolders prefs. */ |
| 39 | $squirrelmail_plugin_hooks |
| 40 | ['loading_prefs']['sent_subfolders'] = |
| 41 | 'sent_subfolders_load_prefs'; |
| 42 | |
| 43 | /* The hooks to handle sent subfolders options. */ |
| 44 | $squirrelmail_plugin_hooks |
| 45 | ['optpage_loadhook_folder']['sent_subfolders'] = |
| 46 | 'sent_subfolders_optpage_loadhook_folders'; |
| 47 | } |
| 48 | |
| 49 | function sent_subfolders_check_handleAsSent() { |
| 50 | global $handleAsSent_result, $sent_subfolders_base; |
| 51 | global $use_sent_subfolders; |
| 52 | $sent_subfolders_base = 'INBOX.Sent'; |
| 53 | $args = func_get_arg(0); |
| 54 | |
| 55 | /* Only check the folder string if we have been passed a mailbox. */ |
| 56 | if ($use_sent_subfolders && (count($args) > 1)) { |
| 57 | /* Chop up the folder strings as needed. */ |
| 58 | $base_str = $sent_subfolders_base . $delimiter; |
| 59 | $mbox_str = substr($args[1], 0, strlen($base_str)); |
| 60 | |
| 61 | /* Perform the comparison. */ |
| 62 | $handleAsSent_result = |
| 63 | ( $handleAsSent_result |
| 64 | || ($base_str == $mbox_str) |
| 65 | || ($sent_subfolders_base == $args[1]) |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function sent_subfolders_load_prefs() { |
| 71 | global $use_sent_subfolders; |
| 72 | global $sent_subfolders_setting, $sent_subfolders_base; |
| 73 | |
| 74 | $use_sent_subfolders = getPref |
| 75 | ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF); |
| 76 | |
| 77 | $sent_subfolders_setting = getPref |
| 78 | ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED); |
| 79 | |
| 80 | $sent_subfolders_base = getPref |
| 81 | ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE); |
| 82 | } |
| 83 | |
| 84 | function sent_subfolders_optpage_loadhook_folders() { |
| 85 | global $optpage_data, $username, $key, $imapServerAddress, $imapPort; |
| 86 | |
| 87 | /* Get some imap data we need later. */ |
| 88 | $imapConnection = |
| 89 | sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
| 90 | $boxes = sqimap_mailbox_list($imapConnection); |
| 91 | sqimap_logout($imapConnection); |
| 92 | |
| 93 | /* Load the Sent Subfolder Options into an array. */ |
| 94 | $optgrp = _("Sent Subfolders Options"); |
| 95 | $optvals = array(); |
| 96 | |
| 97 | $optvals[] = array( |
| 98 | 'name' => 'sent_subfolders_setting', |
| 99 | 'caption' => _("Use Sent Subfolders"), |
| 100 | 'type' => SMOPT_TYPE_STRLIST, |
| 101 | 'refresh' => SMOPT_REFRESH_FOLDERLIST, |
| 102 | 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"), |
| 103 | SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"), |
| 104 | SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"), |
| 105 | SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")), |
| 106 | 'save' => 'save_option_sent_subfolders_setting' |
| 107 | ); |
| 108 | |
| 109 | $sent_subfolders_base_values = array(); |
| 110 | foreach ($boxes as $folder) { |
| 111 | if (strtolower($folder['unformatted']) != 'inbox') { |
| 112 | $real_value = $folder['unformatted-dm']; |
| 113 | $disp_value = str_replace(' ', ' ', $folder['formatted']); |
| 114 | $sent_subfolders_base_values[$real_value] = $disp_value; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | $optvals[] = array( |
| 119 | 'name' => 'sent_subfolders_base', |
| 120 | 'caption' => _("Base Sent Folder"), |
| 121 | 'type' => SMOPT_TYPE_STRLIST, |
| 122 | 'refresh' => SMOPT_REFRESH_FOLDERLIST, |
| 123 | 'posvals' => $sent_subfolders_base_values |
| 124 | ); |
| 125 | |
| 126 | /* Add our option data to the global array. */ |
| 127 | $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp; |
| 128 | $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals; |
| 129 | } |
| 130 | |
| 131 | function save_option_sent_subfolders_setting($option) { |
| 132 | global $data_dir, $username, $use_sent_subfolders; |
| 133 | |
| 134 | /* Set use_sent_subfolders as either on or off. */ |
| 135 | if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) { |
| 136 | setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF); |
| 137 | } else { |
| 138 | setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON); |
| 139 | setPref($data_dir, $username, 'move_to_sent', SMPREF_ON); |
| 140 | } |
| 141 | |
| 142 | /* Now just save the option as normal. */ |
| 143 | save_option($option); |
| 144 | } |
| 145 | |
| 146 | function sent_subfolders_update_sentfolder() { |
| 147 | global $sent_folder, $delimiter, $auto_create_special, $auto_create_done; |
| 148 | global $sent_subfolders_base, $sent_subfolders_setting; |
| 149 | global $username, $data_dir, $key, $imapServerAddress, $imapPort; |
| 150 | global $use_sent_subfolders, $move_to_sent; |
| 151 | |
| 152 | if ($use_sent_subfolders && $move_to_sent) { |
| 153 | $year = date('Y'); |
| 154 | $month = date('m'); |
| 155 | $quarter = sent_subfolder_getQuarter($month); |
| 156 | |
| 157 | switch ($sent_subfolders_setting) { |
| 158 | case SMPREF_SENT_SUBFOLDERS_YEARLY: |
| 159 | $sent_subfolder = $sent_subfolders_base |
| 160 | . $delimiter . $year; |
| 161 | $year_folder = $sent_subfolder; |
| 162 | break; |
| 163 | case SMPREF_SENT_SUBFOLDERS_QUARTERLY: |
| 164 | $sent_subfolder = $sent_subfolders_base |
| 165 | . $delimiter . $year |
| 166 | . $delimiter . $quarter; |
| 167 | $year_folder = $sent_subfolders_base |
| 168 | . $delimiter . $year; |
| 169 | break; |
| 170 | case SMPREF_SENT_SUBFOLDERS_MONTHLY: |
| 171 | $sent_subfolder = $sent_subfolders_base |
| 172 | . $delimiter . $year |
| 173 | . $delimiter . $month; |
| 174 | $year_folder = $sent_subfolders_base |
| 175 | . $delimiter . $year; |
| 176 | break; |
| 177 | case SMPREF_SENT_SUBFOLDERS_DISABLED: |
| 178 | default: |
| 179 | $sent_subfolder = $sent_folder; |
| 180 | $year_folder = $sent_folder; |
| 181 | } |
| 182 | |
| 183 | /* If this folder is NOT the current sent folder, update stuff. */ |
| 184 | if ($sent_subfolder != $sent_folder) { |
| 185 | /* First, update the sent folder. */ |
| 186 | setPref($data_dir, $username, 'sent_folder', $sent_subfolder); |
| 187 | setPref($data_dir, $username, 'move_to_sent', SMPREF_ON); |
| 188 | $sent_folder = $sent_subfolder; |
| 189 | $move_to_sent = SMPREF_ON; |
| 190 | |
| 191 | /* Auto-create folders, if they do not yet exist. */ |
| 192 | if ($sent_folder != 'none') { |
| 193 | /* Create the imap connection. */ |
| 194 | $ic = sqimap_login |
| 195 | ($username, $key, $imapServerAddress, $imapPort, 10); |
| 196 | |
| 197 | /* Auto-create the year folder, if it does not yet exist. */ |
| 198 | if (!sqimap_mailbox_exists($ic, $year_folder)) { |
| 199 | sqimap_mailbox_create($ic, $year_folder, ''); |
| 200 | } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) { |
| 201 | sqimap_subscribe($ic, $year_folder); |
| 202 | } |
| 203 | |
| 204 | /* Auto-create the subfolder, if it does not yet exist. */ |
| 205 | if (!sqimap_mailbox_exists($ic, $sent_folder)) { |
| 206 | sqimap_mailbox_create($ic, $sent_folder, ''); |
| 207 | } else if (!sqimap_mailbox_is_subscribed($ic, $sent_folder)) { |
| 208 | sqimap_subscribe($ic, $sent_folder); |
| 209 | } |
| 210 | |
| 211 | /* Close the imap connection. */ |
| 212 | sqimap_logout($ic); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | function sent_subfolder_getQuarter($month) { |
| 219 | switch ($month) { |
| 220 | case '01': |
| 221 | case '02': |
| 222 | case '03': |
| 223 | $result = '1'; |
| 224 | break; |
| 225 | case '04': |
| 226 | case '05': |
| 227 | case '06': |
| 228 | $result = '2'; |
| 229 | break; |
| 230 | case '07': |
| 231 | case '08': |
| 232 | case '09': |
| 233 | $result = '3'; |
| 234 | break; |
| 235 | case '10': |
| 236 | case '11': |
| 237 | case '12': |
| 238 | $result = '4'; |
| 239 | break; |
| 240 | default: |
| 241 | $result = 'ERR'; |
| 242 | } |
| 243 | |
| 244 | /* Return the current quarter. */ |
| 245 | return ('Q' . $result); |
| 246 | } |
| 247 | |
| 248 | ?> |