Happy New Year
[squirrelmail.git] / plugins / sent_subfolders / functions.php
index 3df9b3182397bc0eef5d3557c6dec8efe56f6197..cdf2879bd7bdac476c2772c95b46549f99a64d8d 100644 (file)
@@ -5,19 +5,33 @@
  *
  * This is a standard SquirrelMail 1.2 API for plugins.
  *
- * @copyright © 1999-2007 The SquirrelMail Project Team
+ * @copyright 1999-2020 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package plugins
  * @subpackage sent_subfolders
  */
 
+define('SMPREF_SENT_SUBFOLDERS_DISABLED',  0);
+define('SMPREF_SENT_SUBFOLDERS_YEARLY',    1);
+define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
+define('SMPREF_SENT_SUBFOLDERS_MONTHLY',   3);
+define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
+
 function sent_subfolders_check_handleAsSent_do($mailbox) {
-    global $handleAsSent_result, $sent_subfolders_base,
-           $use_sent_subfolders;
+
+    global $handleAsSent_result, $data_dir, $username, $sent_folder;
+
+    // don't need to bother if it's already special
+    if ($handleAsSent_result) return;
 
     sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
 
+    $use_sent_subfolders = getPref($data_dir, $username,
+                                   'use_sent_subfolders', SMPREF_OFF);
+    $sent_subfolders_base = getPref($data_dir, $username,
+                                    'sent_subfolders_base', $sent_folder);
+
     /* Only check the folder string if we have been passed a mailbox. */
     if ($use_sent_subfolders && !empty($mailbox)) {
         /* Chop up the folder strings as needed. */
@@ -25,39 +39,21 @@ function sent_subfolders_check_handleAsSent_do($mailbox) {
         $mbox_str = substr($mailbox, 0, strlen($base_str));
 
         /* Perform the comparison. */
-        $handleAsSent_result =
-            ( $handleAsSent_result
-            || ($base_str == $mbox_str)
-            || ($sent_subfolders_base == $mailbox)
-            );
+        $handleAsSent_result = ( ($base_str == $mbox_str)
+                              || ($sent_subfolders_base == $mailbox) );
     }
 }
 
-/**
- * Loads sent_subfolders settings
- */
-function sent_subfolders_load_prefs_do() {
-    global $use_sent_subfolders, $data_dir, $username,
-           $sent_subfolders_setting, $sent_subfolders_base;
-
-    $use_sent_subfolders = getPref
-    ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
-
-    $sent_subfolders_setting = getPref
-    ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
-
-    $sent_subfolders_base = getPref
-    ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
-}
-
 /**
  * Adds sent_subfolders options in folder preferences
  */
 function sent_subfolders_optpage_loadhook_folders_do() {
-    global $username, $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
+
+    global $data_dir, $username, $optpage_data, $imapServerAddress,
+           $imapPort, $imap_stream_options, $show_contain_subfolders_option, $sent_folder;
 
     /* Get some imap data we need later. */
-    $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
+    $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0, $imap_stream_options);
     $boxes = sqimap_mailbox_list($imapConnection);
     sqimap_logout($imapConnection);
 
@@ -65,6 +61,10 @@ function sent_subfolders_optpage_loadhook_folders_do() {
     $optgrp = _("Sent Subfolders Options");
     $optvals = array();
 
+    global $sent_subfolders_setting;
+    $sent_subfolders_setting = getPref($data_dir, $username,
+                                       'sent_subfolders_setting',
+                                       SMPREF_SENT_SUBFOLDERS_DISABLED);
     $optvals[] = array(
         'name'    => 'sent_subfolders_setting',
         'caption' => _("Use Sent Subfolders"),
@@ -80,13 +80,17 @@ function sent_subfolders_optpage_loadhook_folders_do() {
     $filtered_folders=array_filter($boxes, "filter_folders");
     $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
 
+    global $sent_subfolders_base;
+    $sent_subfolders_base = getPref($data_dir, $username,
+                                    'sent_subfolders_base', $sent_folder);
     $optvals[] = array(
         'name'    => 'sent_subfolders_base',
         'caption' => _("Base Sent Folder"),
         'type'    => SMOPT_TYPE_FLDRLIST,
         'refresh' => SMOPT_REFRESH_FOLDERLIST,
         'posvals' => $sent_subfolders_base_values,
-        'folder_filter' => 'noinferiors'
+        'folder_filter' => 'noinferiors',
+        'save'    => 'save_option_sent_subfolders_base'
     );
 
     if ($show_contain_subfolders_option) {
@@ -119,7 +123,7 @@ function filter_folders($fldr) {
  * Saves sent_subfolder_options
  */
 function save_option_sent_subfolders_setting($option) {
-    global $data_dir, $username, $use_sent_subfolders;
+    global $data_dir, $username;
 
     /* Set use_sent_subfolders as either on or off. */
     if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
@@ -127,12 +131,27 @@ function save_option_sent_subfolders_setting($option) {
     } else {
         setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
         setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
+        $check_sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', '');
+        if ($check_sent_subfolders_base === '') {
+            setPref($data_dir, $username, 'sent_subfolders_base', $sent_subfolders_base);
+        }
     }
 
     /* Now just save the option as normal. */
     save_option($option);
 }
 
+/**
+ * Update the folder settings/auto-create new subfolder
+ */
+function save_option_sent_subfolders_base($option) {
+    // first save the option as normal
+    save_option($option);
+
+    // now update folder settings and auto-create first subfolder if needed
+    sent_subfolders_update_sentfolder_do();
+}
+
 /**
  * Update sent_subfolders settings
  *
@@ -140,13 +159,20 @@ function save_option_sent_subfolders_setting($option) {
  * creates required imap folders
  */
 function sent_subfolders_update_sentfolder_do() {
-    global $sent_folder, $username;
-    global $sent_subfolders_base, $sent_subfolders_setting;
-    global $data_dir, $imapServerAddress, $imapPort;
-    global $use_sent_subfolders, $move_to_sent;
+    global $sent_folder, $username,
+           $data_dir, $imapServerAddress, $imapPort,
+           $imap_stream_options, $move_to_sent;
 
     sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
 
+    $use_sent_subfolders = getPref($data_dir, $username,
+                                   'use_sent_subfolders', SMPREF_OFF);
+    $sent_subfolders_setting = getPref($data_dir, $username,
+                                       'sent_subfolders_setting',
+                                       SMPREF_SENT_SUBFOLDERS_DISABLED);
+    $sent_subfolders_base = getPref($data_dir, $username,
+                                    'sent_subfolders_base', $sent_folder);
+
     if ($use_sent_subfolders || $move_to_sent) {
         $year = date('Y');
         $month = date('m');
@@ -192,7 +218,7 @@ function sent_subfolders_update_sentfolder_do() {
             /* Auto-create folders, if they do not yet exist. */
             if ($sent_subfolder != 'none') {
                 /* Create the imap connection. */
-                $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
+                $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10, $imap_stream_options);
 
                 $boxes = false;
                 /**
@@ -203,7 +229,7 @@ function sent_subfolders_update_sentfolder_do() {
                 if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
                     ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
                      sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
-                    error_box(_("Sent Subfolders plugin is misconfigured."));
+                    error_box(_("Sent subfolders options are misconfigured."));
                 } else {
                     if ($level==2) {
                         /* Auto-create the year folder, if it does not yet exist. */
@@ -222,9 +248,18 @@ function sent_subfolders_update_sentfolder_do() {
                     } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
                         sqimap_subscribe($ic, $sent_subfolder);
                     }
-                    /* Update sent_folder setting. */
-                    //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
-                    //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
+                    /* Update sent_folder setting in prefs only if the base
+                       subfolders setting is not the same as the normal sent
+                       folder...  otherwise, it is quite misleading to the user.
+                       If the sent folder is the same as the subfolders base, it's
+                       OK to leave the sent folder as is.
+                       The sent_folder setting itself needs to be the actual
+                       subfolder (not the base) for proper functionality */
+                    if ($sent_subfolders_base != $sent_folder) {
+                        setPref($data_dir, $username, 'sent_folder', $sent_subfolders_base);
+                        setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
+                        setPref($data_dir, $username, 'translate_special_folders', SMPREF_OFF);
+                    }
                     $sent_folder = $sent_subfolder;
                     $move_to_sent = SMPREF_ON;
                 }
@@ -279,13 +314,13 @@ function sent_subfolder_getQuarter($month) {
  * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
  */
 function sent_subfolders_special_mailbox_do($mb) {
-    global $data_dir, $username;
+    global $data_dir, $username, $sent_folder;
 
     sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
 
-    $use_sent_subfolders = getPref
-        ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
-    $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
+    $use_sent_subfolders = getPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
+
+    $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', $sent_folder);
 
     /**
      * If sent_subfolders are used and mailbox is equal to subfolder base