}
/**
- * 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) {
$url = substr($url, 0, $pos);
}
+ if (strpos($url, ' ') !== false) {
+ return false;
+ }
+
$ret_array['hostspec'] = $url;
return $ret_array;
$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) {
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) &&