Index: pontomail.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/pontomail/pontomail.module,v
retrieving revision 1.27
diff -u -r1.27 pontomail.module
--- pontomail.module	4 Sep 2006 04:22:28 -0000	1.27
+++ pontomail.module	6 Sep 2006 14:15:49 -0000
@@ -64,41 +64,64 @@
     }
 
 
-  // Grap the list of accounts
-  if((arg(1) == 'viewmailbox' || arg(0) == 'pontomail') && $access_mailbox) {
-  $accounts = db_query('SELECT * FROM {pontomail_servers} WHERE uid = %d AND enabled = 1', $user->uid);
-  while($row = db_fetch_object($accounts)) {
-    $items[] = array('path' => 'pontomail/viewmailbox/accounts/' . $row->sid,
-                    'title' => $row->server_name);
-    // List Subscribed Mail Boxes for this Account
-    $imap = _pontomail_get_imap_obj($row->sid, "*", FALSE); // Set the redirect to false 
-    if($imap->open()) { 
-      $subs = $imap->get_subscriptions();
-      foreach($subs as $key => $folder) {
-        $folder_name = $imap->get_folder_name($folder->name);
-        $folder_status = $imap->folder_status($folder_name);
-        if($folder_status->rescent > 0) {
-          $title = "$folder_name ($folder_status->unseen)";
-        }
-        elseif($folder_status->unseen > 0) {
-          $title = "$folder_name ($folder_status->unseen)";
+    // Grap the list of accounts
+    if((arg(1) == 'viewmailbox' || arg(0) == 'pontomail') && $access_mailbox) {
+      if(variable_get('pontomail_use_predefined_server', '') && !$_SESSION['mail_auth_session']['error']) {
+        // List Subscribed Mail Boxes for this Account
+        $imap = _pontomail_get_imap_obj('-1', "*", FALSE); // Set the redirect to false 
+        if($imap->open()) { 
+          $items[] = array('path' => 'pontomail/viewmailbox/accounts/' . '-1',
+                          'title' => variable_get('pontomail_server_name', ''));
+          foreach(_pontomail_get_menu_folders($imap, -1) as $value) array_push($items, $value);
         }
-        else {
-          $title = $folder_name;
+	else {
+         $_SESSION['mail_auth_session']['error'] = 1;
+	}
+      }  
+
+      if(user_access('setup email')) {
+        $accounts = db_query('SELECT * FROM {pontomail_servers} WHERE uid = %d AND enabled = 1', $user->uid);
+        while($row = db_fetch_object($accounts)) {
+          $items[] = array('path' => 'pontomail/viewmailbox/accounts/' . $row->sid,
+                          'title' => $row->server_name);
+          // List Subscribed Mail Boxes for this Account
+          $imap = _pontomail_get_imap_obj($row->sid, "*", FALSE); // Set the redirect to false 
+          if($imap->open()) { 
+            foreach(_pontomail_get_menu_folders($imap, $row->sid) as $value) array_push($items, $value);
+          }
         }
-        $items[] = array('path' => 'pontomail/viewmailbox/accounts/' . $row->sid . '/' . $folder_name,
-                          'title' => $title);
-      }
+      }	
     }
   }
-
-  }
-
-  }
   
   return $items;
 } // end function pontomail_menu
 
+/**
+ * Returns account foder list for the menu
+ * $imap connection
+ * $id account id
+ * @items array of account folders
+ */
+function _pontomail_get_menu_folders ($imap, $id) {
+  $subs = $imap->get_subscriptions();
+  foreach($subs as $key => $folder) {
+    $folder_name = $imap->get_folder_name($folder->name);
+    $folder_status = $imap->folder_status($folder_name);
+    if($folder_status->rescent > 0) {
+      $title = "$folder_name ($folder_status->unseen)";
+    }
+    elseif($folder_status->unseen > 0) {
+      $title = "$folder_name ($folder_status->unseen)";
+    }
+    else {
+      $title = $folder_name;
+    }
+    $items[] = array('path' => 'pontomail/viewmailbox/accounts/' . $id . '/' . $folder_name,
+                      'title' => $title);
+  }
+  return $items;
+}
 
 /**
  * Help section
@@ -125,6 +148,37 @@
   return array('setup email', 'administer pontomail');
 } // funtion pontomail_perm
 
+
+/**
+ * determine the action when user account actions are performed.
+ * this hook allows the module to react when operations are performed on user accounts
+ * @param op the operation from the URL
+ * @param edit the form data
+ * @param user the user object	
+ * @category the category
+ */
+
+function pontomail_user($op, &$edit, &$user, $category = NULL) {
+
+  // if the user is logging in;
+  if ($op == 'login') {
+    if (variable_get('pontomail_use_predefined_server', '')) {
+      // get the password;
+      $_SESSION['mail_auth_session']['error'] = '';
+      $_SESSION['mail_auth_session']['password'] = $edit['pass'];
+    }
+  }
+
+  // if the user is changing his settings
+  else if ($op == 'update') {
+    if (variable_get('pontomail_use_predefined_server', '')) {
+      // get the password;
+      $_SESSION['mail_auth_session']['error'] = '';
+      $_SESSION['mail_auth_session']['password'] = $edit['pass'];
+    }
+  }
+}
+
 /**
  * Generate block content for the pontomail block.
  * @param op the operation from the URL
@@ -143,33 +197,26 @@
       case 0:
         $block['subject'] = 'Mail View';
         //$block['content'] = 'Mail Content';
-        $imap = new IMAPService();
-        $accounts = db_query('SELECT * FROM {pontomail_servers} WHERE uid = %d AND enabled = 1', $user->uid);
-        while($row = db_fetch_object($accounts)) {
-          $items[] = array('path' => 'pontomail/viewmailbox/accounts/' . $row->sid, 'title' => $row->server_name);
-	  if((arg(1) == 'viewmailbox' || arg(0) == 'pontomail') && arg(3) == $row->sid) {
-            $block['content'] .= '<li class="expanded">'. l($row->server_name, 'pontomail/viewmailbox/accounts/'. $row->sid) .'</li>'."\n";
-            // List Subscribed Mail Boxes for this Account
-            $imap->connect($row->server_address, $row->server_port, $row->user, $row->password, "", (array)($row));
-	    $block['content'] .= '<ul>'."\n";
-            if($imap->open()) {
-              $subs = $imap->get_subscriptions();
-              foreach ($subs as $key => $folder) {
-	        $folders[] = $imap->get_folder_name($folder->name);
-	      }
-              sort($folders);
-              $curbox = 0;
-	      while ($curbox < count($folders)) {
-                $block['content'] .= _pontomail_compute_folder_children($row->sid, $imap, $folders, $subs[0]->delimiter, $curbox);
-              }
-            }
-            $block['content'] .= '</ul>'."\n";
-          }
-	  else {
-            $block['content'] .= '<li class="collapsed">'. l($row->server_name, 'pontomail/viewmailbox/accounts/'. $row->sid) .'</li>'."\n";
-          }
-        }
-	$block['content'] = "\n".'<ul class="menu">'."\n". $block['content'] .'</ul>'."\n";
+        if(variable_get('pontomail_use_predefined_server', '') && !$_SESSION['mail_auth_session']['error']) {
+          $row['sid'] = -1;
+          $row['server_name'] = variable_get('pontomail_server_name', '');
+          $row['server_address'] = variable_get('pontomail_server_address', '');
+          $row['server_port'] = variable_get('pontomail_server_port', '');
+          $row['secure'] = variable_get('pontomail_server_secure', '');
+          $row['con_ssl'] = variable_get('pontomail_server_con_ssl', '');
+          $row['validate_cert'] = variable_get('pontomail_server_validate_cert', '');
+          $row['no_tls'] = variable_get('pontomail_server_no_tls', '');
+          $row['user'] =  $user->name;
+          $row['password'] =  $_SESSION['mail_auth_session']['password'];
+          $block['content'] .= _pontomail_get_block_folders($row);
+	}
+        if(user_access('setup email')) {
+          $accounts = db_query('SELECT * FROM {pontomail_servers} WHERE uid = %d AND enabled = 1', $user->uid);
+          while($row = db_fetch_object($accounts)) {
+	    $block['content'] .= _pontomail_get_block_folders((array)($row));
+          }  
+	}
+	if($block['content']) $block['content'] = "\n".'<ul class="menu">'."\n". $block['content'] .'</ul>'."\n";
         break;
       case 1:
         $block['subject'] = '2Mail View';
@@ -181,6 +228,39 @@
 } // end function pontomail_block
 
 /**
+ * Prints mail block folder list.
+ * $row account settings
+ * $return folder list HTML
+ */
+function _pontomail_get_block_folders ($row) {
+  global $user;
+  $str = '';
+  if((arg(1) == 'viewmailbox' || arg(0) == 'pontomail') && arg(3) == $row['sid']) {
+    $imap = new IMAPService();
+    $str .= '<li class="expanded">'. l($row['server_name'], 'pontomail/viewmailbox/accounts/'. $row['sid']) .'</li>'."\n";
+    // List Subscribed Mail Boxes for this Account
+    $imap->connect($row['server_address'], $row['server_port'], $user->name, $_SESSION['mail_auth_session']['password'], "", $row);
+    $str .= '<ul>'."\n";
+    if($imap->open()) {
+      $subs = $imap->get_subscriptions();
+      foreach ($subs as $key => $folder) {
+        $folders[] = $imap->get_folder_name($folder->name);
+      }
+      sort($folders);
+      $curbox = 0;
+      while ($curbox < count($folders)) {
+        $str .= _pontomail_compute_folder_children($row['sid'], $imap, $folders, $subs[0]->delimiter, $curbox);
+      }
+    }
+    $str .= '</ul>'."\n";
+  }
+  else {
+    $str .= '<li class="collapsed">'. l($row['server_name'], 'pontomail/viewmailbox/accounts/'. $row['sid']) .'</li>'."\n";
+  }
+  return $str;
+}
+
+/**
  * Recurent function generates folder list.
  * $account imap account number
  * $imap imap connection
@@ -343,7 +423,62 @@
                                         '#default_value' => variable_get('pontomail_user_disk_quota', ''),
                                         '#description' => t('This is the size limit that you want to limit each user too.<br/>Note: 0 is unlimited.'));
   
-  // Debuggins Options
+  // Mail Server Options
+  $form['pontomail_server_options'] = array('#type' => 'fieldset',
+    '#title' => t('Mail Server Options'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#description' => t('These options will let you specify the predefined mail server or allow users to edit mail server settings'));
+  $form['pontomail_server_options']['pontomail_use_predefined_server'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use predefined mail server'),
+    '#default_value' => variable_get('pontomail_use_predefined_server', ''),
+    '#description' => t('Whwn using predefined imap mail server, fill in the settings below.')
+  );
+  $form['pontomail_server_options']['pontomail_server_name'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Predefined Server Name'),
+    '#default_value' => variable_get('pontomail_server_name', ''),
+    '#description' => t('If you use predefined imap server, user\'s drupal and mail server username and password should be the same.')
+    ); 
+  $form['pontomail_server_options']['pontomail_server_address'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Predefined Server Address'),
+    '#default_value' => variable_get('pontomail_server_address', ''),
+    '#description' => t('DNS name or IP address.')
+    ); 
+  $form['pontomail_server_options']['pontomail_server_port'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Connection Port'),
+    '#default_value' => '143',
+    '#description' => t('The default IMAP port is 143 if one is not specified.')
+    ); 
+  $form['pontomail_server_options']['pontomail_server_secure'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Secure Connection'),
+    '#default_value' => variable_get('pontomail_server_secure', ''),
+    '#description' => t('Check to make a secure connection to your IMAP Server.')
+    );
+  $form['pontomail_server_options']['pontomail_server_con_ssl'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Encrypt session using SSL'),
+    '#default_value' => variable_get('pontomail_server_con_ssl', ''),
+    '#description' => t('')
+    );
+  $form['pontomail_server_options']['pontomail_server_validate_cert'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Validate Certificates'),
+    '#default_value' => variable_get('pontomail_server_validate_cert', ''),
+    '#description' => t('When using a secure connection, do not validate the certificate. It is best to leaves this off initially.')
+    );
+  $form['pontomail_server_options']['pontomail_server_no_tls'] = array(
+    '#type' => 'checkbox', 
+    '#title' => t('No TLS'),
+    '#default_value' => variable_get('pontomail_server_no_tls', ''),
+    '#description' => t('TLS Description')
+    );
+
+// Debuggins Options
   $form['pontomail_debug_options'] = array('#type' => 'fieldset',
     '#title' => t('Debugging Options'),
     '#collapsible' => TRUE,
@@ -1347,6 +1482,20 @@
   $imap = new IMAPService();
   if($sid == "")
     return $imap;
+  if($sid == "-1") {
+    $account['server_port'] = variable_get('pontomail_server_port', '');
+    $account['secure'] = variable_get('pontomail_server_secure', '');
+    $account['con_ssl'] = variable_get('pontomail_server_con_ssl', '');
+    $account['validate_cert'] = variable_get('pontomail_server_validate_cert', '');
+    $account['no_tls'] = variable_get('pontomail_server_no_tls', '');
+    $account['user'] =  $user->name;
+    $account['password'] =  $_SESSION['mail_auth_session']['password'];
+    $ip = gethostbyname(variable_get('pontomail_server_address', ''));
+    $imap->connect($ip, $account['server_port'], $account['user'], $account['password'], $folder, $account);
+    if($imap->open) {
+      return $imap;
+    }
+  }
   $account = db_fetch_array(db_query('SELECT * FROM {pontomail_servers} WHERE sid = %d AND uid = %d', $sid, $user->uid));
   if($account['enabled'] == 0) {
     return $imap;
