All this document.write stuff is overkill when we already have checkForJavascript...
[squirrelmail.git] / plugins / sent_subfolders / setup.php
index d4aa59f4d26b9448627b478de6619aa38ab1100e..5ea8f17281fbba6338ec470b455203669625dab1 100644 (file)
@@ -3,18 +3,16 @@
 /**
  * setup.php -- Sent Subfolders Setup File
  *
- * Copyright (c) 1999-2004 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
+ * This is a standard SquirrelMail 1.2 API for plugins.
  *
- * This is a standard Squirrelmail-1.2 API for plugins.
- *
- * $Id$
+ * @copyright © 1999-2007 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);
@@ -23,7 +21,7 @@ define('SMPREF_SENT_SUBFOLDERS_MONTHLY',   3);
 define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
 
 /**
- * Adds plugin to squirrelmail hooks
+ * Adds plugin to SquirrelMail's hooks
  */
 function squirrelmail_plugin_init_sent_subfolders() {
     /* Standard initialization API. */
@@ -63,6 +61,7 @@ function sent_subfolders_check_handleAsSent() {
     global $handleAsSent_result, $sent_subfolders_base,
            $use_sent_subfolders;
 
+    // FIXME: hardcoded folder
     $sent_subfolders_base = 'INBOX.Sent';
     $args = func_get_arg(0);
     sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
@@ -103,14 +102,10 @@ function sent_subfolders_load_prefs() {
  * Adds sent_subfolders options in folder preferences
  */
 function sent_subfolders_optpage_loadhook_folders() {
-    global $optpage_data, $imapServerAddress, $imapPort;
-
-    sqgetGlobalVar('username', $username, SQ_SESSION);
-    sqgetGlobalVar('key', $key, SQ_COOKIE);
+    global $username, $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
 
     /* Get some imap data we need later. */
-    $imapConnection =
-        sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
+    $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
     $boxes = sqimap_mailbox_list($imapConnection);
     sqimap_logout($imapConnection);
 
@@ -138,9 +133,19 @@ function sent_subfolders_optpage_loadhook_folders() {
         'caption' => _("Base Sent Folder"),
         'type'    => SMOPT_TYPE_FLDRLIST,
         'refresh' => SMOPT_REFRESH_FOLDERLIST,
-        'posvals' => $sent_subfolders_base_values
+        'posvals' => $sent_subfolders_base_values,
+        'folder_filter' => 'noinferiors'
     );
 
+    if ($show_contain_subfolders_option) {
+        $optvals[] = array(
+            'name' => 'sent_subfolders_warning',
+            'caption' => _("Warning"),
+            'type' => SMOPT_TYPE_COMMENT,
+            'comment' => _("There are some restrictions in Sent Subfolder options.")
+            );
+    }
+
     /* Add our option data to the global array. */
     $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
     $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
@@ -183,13 +188,11 @@ function save_option_sent_subfolders_setting($option) {
  * creates required imap folders
  */
 function sent_subfolders_update_sentfolder() {
-    global $sent_folder;
+    global $sent_folder, $username;
     global $sent_subfolders_base, $sent_subfolders_setting;
-    global $data_dir, $imapServerAddress, $imapPort;
+    global $data_dir, $imapServerAddress, $imapPort, $color;
     global $use_sent_subfolders, $move_to_sent;
 
-    sqgetGlobalVar('username', $username, SQ_SESSION);
-    sqgetGlobalVar('key', $key, SQ_COOKIE);
     sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
 
     if ($use_sent_subfolders || $move_to_sent) {
@@ -197,19 +200,12 @@ function sent_subfolders_update_sentfolder() {
         $month = date('m');
         $quarter = sent_subfolder_getQuarter($month);
 
-        /*
-            Regarding the structure we've got three main possibilities.
-            One sent holder. level 0.
-            Multiple year holders with messages in it. level 1.
-            Multiple year folders with holders in it. level 2.
-        */
-/*
-        if( $imap_server_type == 'uw' ) {
-            $cnd_delimiter = '';
-        } else {
-            $cnd_delimiter = $delimiter;
-        }
-*/
+        /**
+         * Regarding the structure we've got three main possibilities.
+         * One sent holder. level 0.
+         * Multiple year holders with messages in it. level 1.
+         * Multiple year folders with holders in it. level 2.
+         */
         $cnd_delimiter = $delimiter;
 
         switch ($sent_subfolders_setting) {
@@ -223,7 +219,7 @@ function sent_subfolders_update_sentfolder() {
             $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
                             . $year
                             . $delimiter . $quarter;
-            $year_folder = $sent_subfolders_base
+            $year_folder = $sent_subfolders_base . $cnd_delimiter
                             . $year;
             break;
         case SMPREF_SENT_SUBFOLDERS_MONTHLY:
@@ -231,7 +227,7 @@ function sent_subfolders_update_sentfolder() {
             $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
                             . $year
                             . $delimiter . $month;
-            $year_folder = $sent_subfolders_base . $year;
+            $year_folder = $sent_subfolders_base. $cnd_delimiter . $year;
             break;
         case SMPREF_SENT_SUBFOLDERS_DISABLED:
         default:
@@ -242,36 +238,49 @@ function sent_subfolders_update_sentfolder() {
 
         /* If this folder is NOT the current sent folder, update stuff. */
         if ($sent_subfolder != $sent_folder) {
-            /* First, update the sent folder. */
-
-            setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
-            setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
-            $sent_folder = $sent_subfolder;
-            $move_to_sent = SMPREF_ON;
-
             /* Auto-create folders, if they do not yet exist. */
-            if ($sent_folder != 'none') {
+            if ($sent_subfolder != 'none') {
                 /* Create the imap connection. */
-                $ic = sqimap_login
-                ($username, $key, $imapServerAddress, $imapPort, 10);
-
-                /* Auto-create the year folder, if it does not yet exist. */
-                if (!sqimap_mailbox_exists($ic, $year_folder)) {
-                    sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
-                } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
-                    sqimap_subscribe($ic, $year_folder);
-                }
-
-                /* Auto-create the subfolder, if it does not yet exist. */
-                if (!sqimap_mailbox_exists($ic, $sent_folder)) {
-                    sqimap_mailbox_create($ic, $sent_folder, '');
-                } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
-                    sqimap_subscribe($ic, $sent_subfolder);
+                $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
+
+                $boxes = false;
+                /**
+                 * If sent_subfolder can't store messages (noselect) ||
+                 * year_folder can't store subfolders (noinferiors) in level=2 setup ||
+                 * subfolder_base can't store subfolders (noinferiors), setup is broken
+                 */
+                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."));
+                } else {
+                    if ($level==2) {
+                        /* Auto-create the year folder, if it does not yet exist. */
+                        if (!sqimap_mailbox_exists($ic, $year_folder)) {
+                            sqimap_mailbox_create($ic, $year_folder, 'noselect');
+                            // TODO: safety check for imap servers that can't create subfolders
+
+                        } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
+                            sqimap_subscribe($ic, $year_folder);
+                        }
+                    }
+
+                    /* Auto-create the subfolder, if it does not yet exist. */
+                    if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
+                        sqimap_mailbox_create($ic, $sent_subfolder, '');
+                    } 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);
+                    $sent_folder = $sent_subfolder;
+                    $move_to_sent = SMPREF_ON;
                 }
-
                 /* Close the imap connection. */
                 sqimap_logout($ic);
             }
+
         }
     }
 }
@@ -319,16 +328,19 @@ 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($mb) {
-    global $data_dir, $username;
+    global $data_dir, $username, $delimiter;
 
     $use_sent_subfolders = getPref
         ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
     $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
 
+    /**
+     * If sent_subfolders are used and mailbox is equal to subfolder base 
+     * or mailbox matches subfolder base + delimiter.
+     */
     if ($use_sent_subfolders == SMPREF_ON &&
-    ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base) ) ) {
+    ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
         return 1;
     }
     return 0;
 }
-?>
\ No newline at end of file