Doc fix
[squirrelmail.git] / functions / imap_general.php
index 8020433cbc1de0eb9811e1ecbec2eac54a795108..2ed19901b0d70c9a1f22e1b704f09ba791d6af9d 100755 (executable)
@@ -748,15 +748,20 @@ function sqimap_create_stream($server,$port,$tls=0) {
 
 /**
  * Logs the user into the IMAP server.  If $hide is set, no error messages
- * will be displayed.  This function returns the IMAP connection handle.
+ * will be displayed (if set to 1, just exits, if set to 2, returns FALSE).  
+ * This function returns the IMAP connection handle.
  * @param string $username user name
  * @param string $password password encrypted with onetimepad. Since 1.5.2
  *  function can use internal password functions, if parameter is set to
  *  boolean false.
  * @param string $imap_server_address address of imap server
  * @param integer $imap_port port of imap server
- * @param boolean $hide controls display connection errors
- * @return stream
+ * @param int $hide controls display connection errors:
+ *                  0 = do not hide
+ *                  1 = show no errors (just exit)
+ *                  2 = show no errors (return FALSE)
+ * @return mixed The IMAP connection stream, or FALSE if $hide is set to 2 
+ *               and the connection fails.
  */
 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
     global $color, $squirrelmail_language, $onetimepad, $use_imap_tls,
@@ -774,10 +779,10 @@ function sqimap_login ($username, $password, $imap_server_address, $imap_port, $
          * exist, they will override the current ones.
          * This is useful if we want to use different SASL authentication mechanism
          * and/or different TLS settings for proxy logins. */
-               global $authz_imap_auth_mech, $authz_use_imap_tls, $authz_imapPort_tls; 
+        global $authz_imap_auth_mech, $authz_use_imap_tls, $authz_imapPort_tls; 
         $imap_auth_mech = !empty($authz_imap_auth_mech) ? strtolower($authz_imap_auth_mech) : $imap_auth_mech;
-               $use_imap_tls = !empty($authz_use_imap_tls)? $authz_use_imap_tls : $use_imap_tls;
-               $imap_port = !empty($authz_use_imap_tls)? $authz_imapPort_tls : $imap_port;
+        $use_imap_tls = !empty($authz_use_imap_tls)? $authz_use_imap_tls : $use_imap_tls;
+        $imap_port = !empty($authz_use_imap_tls)? $authz_imapPort_tls : $imap_port;
 
         if($imap_auth_mech == 'login' || $imap_auth_mech == 'cram-md5') {
             logout_error("Misconfigured Plugin (authz or equivalent):<br/>".
@@ -936,6 +941,7 @@ function sqimap_login ($username, $password, $imap_server_address, $imap_port, $
                 exit;
             }
         } else {
+            if ($hide == 2) return FALSE;
             exit;
         }
     }
@@ -1041,6 +1047,8 @@ function sqimap_get_delimiter ($imap_stream = false) {
              * OS: According to rfc2342 response from NAMESPACE command is:
              * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
              * OS: We want to lookup all personal NAMESPACES...
+             * 
+             * TODO: remove this in favour of the information from sqimap_get_namespace()
              */
             $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
             if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
@@ -1069,6 +1077,71 @@ function sqimap_get_delimiter ($imap_stream = false) {
     return $sqimap_delimiter;
 }
 
+/**
+ * Retrieves the namespaces from the IMAP server.
+ * NAMESPACE is an IMAP extension defined in RFC 2342.
+ *
+ * @param stream $imap_stream
+ * @return array
+ */
+function sqimap_get_namespace($imap_stream) {
+    $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
+    return sqimap_parse_namespace($read[0]);
+}
+    
+/**
+ * Parses a NAMESPACE response and returns an array with the available
+ * personal, users and shared namespaces.
+ *
+ * @param string $input
+ * @return array The returned array has the following format:
+ * <pre>
+ * array(
+ *   'personal' => array(
+ *       0 => array('prefix'=>'INBOX.','delimiter' =>'.'),
+ *       1 => ...
+ *    ),
+ *    'users' => array(..
+ *    ),
+ *    'shared' => array( ..
+ *    )
+ * )
+ * </pre>
+ * Note that if a namespace is not defined in the server, then the corresponding
+ * array will be empty.
+ */
+function sqimap_parse_namespace(&$input) {
+    $ns_strings = array(1=>'personal', 2=>'users', 3=>'shared');
+    $namespace = array();
+
+    if(ereg('NAMESPACE (\(\(.*\)\)|NIL) (\(\(.*\)\)|NIL) (\(\(.*\)\)|NIL)', $input, $regs) !== false) {
+        for($i=1; $i<=3; $i++) {
+            if($regs[$i] == 'NIL') {
+                $namespace[$ns_strings[$i]] = array();
+            } else {
+                // Pop-out the first ( and last ) for easier parsing
+                $ns = substr($regs[$i], 1, sizeof($regs[$i])-2);
+                if($c = preg_match_all('/\((?:(.*?)\s*?)\)/', $ns, $regs2)) {
+                    $namespace[$ns_strings[$i]] = array();
+                    for($j=0; $j<sizeof($regs2[1]); $j++) {
+                        preg_match('/"(.*)"\s+("(.*)"|NIL)/', $regs2[1][$j], $regs3);
+                        $namespace[$ns_strings[$i]][$j]['prefix'] = $regs3[1];
+                        if($regs3[2] == 'NIL') {
+                            $namespace[$ns_strings[$i]][$j]['delimiter'] = null;
+                        } else {
+                            // $regs[3] is $regs[2] without the quotes
+                            $namespace[$ns_strings[$i]][$j]['delimiter'] = $regs3[3];
+                        }
+                        unset($regs3);
+                    }
+                }
+                unset($ns);
+            }
+        }
+    }
+    return($namespace);
+}
+
 /**
  * This encodes a mailbox name for use in IMAP commands.
  * @param string $what the mailbox to encode