From 38d33e0698af55e3a52a032d3afa2e3c7279c5cf Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 3 Mar 2009 13:51:12 -0700 Subject: [PATCH] Add support for parsing RFC 2384 URLs. --- .../Imap_Client/lib/Horde/Imap/Client/Socket.php | 2 +- .../Imap_Client/lib/Horde/Imap/Client/Utils.php | 26 +++++++---- .../Imap_Client/test/Horde/Imap/test_client.php | 51 ++++++++++++++-------- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php b/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php index 88ab9de7d..c6c612c6d 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php @@ -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': diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Utils.php b/framework/Imap_Client/lib/Horde/Imap/Client/Utils.php index e010e5c85..91f073a3a 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Utils.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Utils.php @@ -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: *
      * '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.
      * 
*/ - 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://[/] style URLs. */ + /* At present, only support type://[/] 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; diff --git a/framework/Imap_Client/test/Horde/Imap/test_client.php b/framework/Imap_Client/test/Horde/Imap/test_client.php index 501488c33..070dfe6ce 100644 --- a/framework/Imap_Client/test/Horde/Imap/test_client.php +++ b/framework/Imap_Client/test/Horde/Imap/test_client.php @@ -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) && -- 2.11.0