Add support for parsing RFC 2384 URLs.
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 3 Mar 2009 20:51:12 +0000 (13:51 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 4 Mar 2009 19:46:46 +0000 (12:46 -0700)
framework/Imap_Client/lib/Horde/Imap/Client/Socket.php
framework/Imap_Client/lib/Horde/Imap/Client/Utils.php
framework/Imap_Client/test/Horde/Imap/test_client.php

index 88ab9de..c6c612c 100644 (file)
@@ -3485,7 +3485,7 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base
 
         case 'REFERRAL':
             // Defined by RFC 2221
-            $this->_temp['referral'] = $this->_utils->parseImapUrl($data);
+            $this->_temp['referral'] = $this->_utils->parseUrl($data);
             break;
 
         case 'UNKNOWN-CTE':
index e010e5c..91f073a 100644 (file)
@@ -167,33 +167,39 @@ class Horde_Imap_Client_Utils
     }
 
     /**
-     * Parse an IMAP URL (RFC 5092).
+     * Parse a POP3 (RFC 2384) or IMAP (RFC 5092) URL.
      *
-     * @param string $url  A IMAP URL string.
+     * @param string $url  A URL string.
      *
      * @return mixed  False if the URL is invalid.  If valid, a URL with the
      *                following fields:
      * <pre>
      * 'auth' - (string) The authentication method to use.
-     * 'port' - (integer) The remote port
      * 'hostspec' - (string) The remote server
+     * 'port' - (integer) The remote port
+     * 'type' - (string) Either 'imap' or 'pop'.
      * 'username' - (string) The username to use on the remote server.
      * </pre>
      */
-    public function parseImapUrl($url)
+    public function parseUrl($url)
     {
         $url = trim($url);
-        if (stripos($url, 'imap://') !== 0) {
+
+        $prefix = strpos($url, '://');
+        $type = substr($url, 0, $prefix);
+
+        if ((strcasecmp($type, 'imap') !== 0) &&
+            (strcasecmp($type, 'pop') !== 0)) {
             return false;
         }
-        $url = substr($url, 7);
+        $url = substr($url, $prefix + 3);
 
-        /* At present, only support imap://<iserver>[/] style URLs. */
+        /* At present, only support type://<iserver>[/] style URLs. */
         if (($pos = strpos($url, '/')) !== false) {
             $url = substr($url, 0, $pos);
         }
 
-        $ret_array = array();
+        $ret_array = array('type' => strtolower($type));
 
         /* Check for username/auth information. */
         if (($pos = strpos($url, '@')) !== false) {
@@ -216,6 +222,10 @@ class Horde_Imap_Client_Utils
             $url = substr($url, 0, $pos);
         }
 
+        if (strpos($url, ' ') !== false) {
+            return false;
+        }
+
         $ret_array['hostspec'] = $url;
 
         return $ret_array;
index 501488c..070dfe6 100644 (file)
@@ -76,7 +76,11 @@ if (empty($argv[2])) {
 
 $imap_utils = new Horde_Imap_Client_Utils();
 if (!empty($argv[3])) {
-    $params = array_merge($params, $imap_utils->parseImapUrl($argv[3]));
+    $parseurl = $imap_utils->parseUrl($argv[3]);
+    if ($parseurl === false) {
+        exit("Bad URL. Exiting.\n");
+    }
+    $params = array_merge($params, $imap_utils->parseUrl($argv[3]));
 }
 
 function exception_handler($exception) {
@@ -861,25 +865,38 @@ foreach ($subject_lines as $val) {
     print "  BASE: \"" . $imap_utils->getBaseSubject($val) . "\"\n\n";
 }
 
-$imap_urls = array(
+$urls = array(
     'NOT A VALID URL',
-    'imap://test.example.com/',
-    'imap://test.example.com:143/',
-    'imap://testuser@test.example.com/',
-    'imap://testuser@test.example.com:143/',
-    'imap://;AUTH=PLAIN@test.example.com/',
-    'imap://;AUTH=PLAIN@test.example.com:143/',
-    'imap://;AUTH=*@test.example.com:143/',
-    'imap://testuser;AUTH=*@test.example.com:143/',
-    'imap://testuser;AUTH=PLAIN@test.example.com:143/'
+    'test.example.com/',
+    'test.example.com:143/',
+    'testuser@test.example.com/',
+    'testuser@test.example.com:143/',
+    ';AUTH=PLAIN@test.example.com/',
+    ';AUTH=PLAIN@test.example.com:143/',
+    ';AUTH=*@test.example.com:143/',
+    'testuser;AUTH=*@test.example.com:143/',
+    'testuser;AUTH=PLAIN@test.example.com:143/'
+);
+
+$url_types = array(
+    'pop' => 'RFC 2384 URL parsing',
+    'imap' => 'RFC 5092 URL parsing'
 );
 
-print "\nRFC 5092 URL parsing:\n";
-foreach ($imap_urls as $val) {
-    print "URL: " . $val . "\n";
-    print "PARSED:\n";
-    print_r($imap_utils->parseImapUrl($val));
-    print "\n";
+foreach ($url_types as $type => $label) {
+    print "\n" . $label . ":\n";
+    foreach ($urls as $val) {
+        $val = $type . '://' . $val;
+        print "URL: " . $val . "\n";
+        print "PARSED:\n";
+        $parseurl = $imap_utils->parseUrl($val);
+        if ($parseurl === false) {
+            print "INVALID URL\n";
+        } else {
+            print_r($parseurl);
+        }
+        print "\n";
+    }
 }
 
 if (isset($fetch_res) &&