cvs: php4 /pear Mail.php /pear/Mail rfc822.php
| From: | Chuck Hagenbuch | Date: | Wed, 03 Jan 2001 18:24:20 +0000 |
| Subject: | cvs: php4 /pear Mail.php /pear/Mail rfc822.php | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-3352@lists.php.net to get a copy of this message | ||
chagenbu Wed Jan 3 10:24:20 2001 EDT
Modified files:
/php4/pear Mail.php
/php4/pear/Mail rfc822.php
Log:
Mail.php: use the Mail_rfc822:: class to parse addresses. There is a small
performance penalty, but it handles rfc822 groups, which
imap_rfc822_parse_adrlist doesn't.
rfc822.php: Style fixes, some small tweaks/optimizations, and work around a
bit of object weirdness that caused the test for $this (to see if we were
being called statically) to not be sufficient.
Index: php4/pear/Mail.php diff -u php4/pear/Mail.php:1.3 php4/pear/Mail.php:1.4 --- php4/pear/Mail.php:1.3 Thu Nov 2 13:40:38 2000 +++ php4/pear/Mail.php Wed Jan 3 10:24:20 2001 @@ -116,7 +116,9 @@ foreach ($headers as $key => $val) { if ($key == 'From') { - $from_arr = imap_rfc822_parse_adrlist($val, 'localhost'); + include_once 'Mail/rfc822.php'; + + $from_arr = Mail_rfc822::parseAddressList($val, 'localhost'); $from = $from_arr[0]->mailbox . '@' . $from_arr[0]->host; if (strstr($from, ' ')) { // Reject outright envelope From addresses with spaces. @@ -150,6 +152,8 @@ */ function parseRecipients($recipients) { + include_once 'Mail/rfc822.php'; + // if we're passed an array, assume addresses are valid and // implode them before parsing. if (is_array($recipients)) { @@ -159,11 +163,11 @@ // Parse recipients, leaving out all personal info. This is // for smtp recipients, etc. All relevant personal information // should already be in the headers. - $addresses = imap_rfc822_parse_adrlist($recipients, 'localhost'); + $addresses = Mail_rfc822::parseAddressList($recipients, 'localhost', false); $recipients = array(); if (is_array($addresses)) { foreach ($addresses as $ob) { - $recipients[] = $ob->mailbox . '@' . $ob->host . ' '; + $recipients[] = $ob->mailbox . '@' . $ob->host; } } Index: php4/pear/Mail/rfc822.php diff -u php4/pear/Mail/rfc822.php:1.1 php4/pear/Mail/rfc822.php:1.2 --- php4/pear/Mail/rfc822.php:1.1 Tue Jan 2 21:10:42 2001 +++ php4/pear/Mail/rfc822.php Wed Jan 3 10:24:20 2001 @@ -20,18 +20,19 @@ * RFC 822 Email address list validation Utility * * @author Richard Heyes <richard.heyes@heyes-computing.net> - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ class Mail_rfc822 { // Properties (All private) var $address = null; var $default_host = 'localhost'; - var $ext_format = true; + var $nestGroups = true; var $addresses = array(); var $structure = array(); var $error = null; var $index = null; + var $mailRFC822 = true; /** @@ -41,14 +42,13 @@ * @access public * @param $address The address(es) to validate. * @param $default_host (Optional) Default domain/host etc. If not supplied, will be set to localhost. - * @param $extended_format (Optional) Whether to return the structure in extended format or not. - * Nests groups for easier viewing. + * @param $nest_groups (Optional) Whether to return the structure with groups nested for easier viewing. */ - function Mail_rfc822($address = null, $default_host = null, $extended_format = null) + function Mail_rfc822($address = null, $default_host = null, $nest_groups = null) { - if (isset($address)) $this->address = $address; + if (isset($address)) $this->address = $address; if (isset($default_host)) $this->default_host = $default_host; - if (isset($extended_format)) $this->ext_format = $extended_format; + if (isset($nest_groups)) $this->nestGroups = $nest_groups; } @@ -59,45 +59,42 @@ * @access public * @param $address The address(es) to validate. * @param $default_host (Optional) Default domain/host etc. + * @param $nest_groups (Optional) Whether to return the structure with groups nested for easier viewing. */ - function parseAddressList($address = null, $default_host = null, $extended_format = null) + function parseAddressList($address = null, $default_host = null, $nest_groups = null) { - // if (function_exists('imap_rfc822_parse_adrlist') && !$extended_format) - // return imap_rfc822_parse_adrlist($address, $default_host); - - if (!isset($this)) { - $obj = new Mail_rfc822($address, $default_host, $extended_format); - $return = $obj->parseAddressList(); - unset($obj); - return $return; + if (!isset($this->mailRFC822)) { + $obj = new Mail_rfc822($address, $default_host, $nest_groups); + return $obj->parseAddressList(); } - if (isset($address)) $this->address = $address; - if (isset($default_host)) $this->default_host = $default_host; - if (isset($extended_format)) $this->ext_format = $extended_format; + if (isset($address)) $this->address = $address; + if (isset($default_host)) $this->default_host = $default_host; + if (isset($nest_groups)) $this->nestGroups = $nest_groups; + + $this->structure = array(); + $this->addresses = array(); + $this->error = null; + $this->index = null; - if (!$this->splitAddresses($this->address) || isset($this->error)) + if (!$this->splitAddresses($this->address) || isset($this->error)) { return (array)$this->error; + } for ($i = 0; $i < count($this->addresses); $i++){ - if (($return = $this->validateAddress($this->addresses[$i])) === FALSE || isset($this->error)) + if (($return = $this->validateAddress($this->addresses[$i])) === false + || isset($this->error)) { return (array)$this->error; + } - if (!$this->ext_format) + if (!$this->nestGroups) { $this->structure = array_merge($this->structure, $return); - else + } else { $this->structure[] = $return; + } } - $return = $this->structure; - - // Now clean up. - $this->structure = array(); - $this->addresses = array(); - $this->error = null; - $this->index = null; - - return $return; + return $this->structure; } /** @@ -115,7 +112,7 @@ $split_char = ','; $is_group = false; } elseif (isset($this->error)) { - return FALSE; + return false; } // Starts with a group, so split on semi-colon: @@ -128,14 +125,14 @@ // brackets/quotes etc then something's fubar. // First check there's a colon at all: - if (strpos($string, ':') === FALSE) { + if (strpos($string, ':') === false) { $this->error = 'Invalid address: ' . $string; - return FALSE; + return false; } // Now check it's outside of brackets/quotes: if (!$this->splitCheck(explode(':', $string), ':')) - return FALSE; + return false; } // $string now contains the first full address/group. @@ -152,20 +149,20 @@ // If the next char is a comma and this was a group, then // there are more addresses, otherwise, if there are any more // chars, then there is another address. - if ($is_group === TRUE && substr($address, 0, 1) == ','){ + if ($is_group && substr($address, 0, 1) == ','){ $address = trim(substr($address, 1)); $this->splitAddresses($address); - return TRUE; + return true; } elseif (strlen($address) > 0) { $this->splitAddresses($address); - return TRUE; + return true; } else { - return TRUE; + return true; } // If you got here then something's off - return FALSE; + return false; } /** @@ -189,7 +186,7 @@ $string2 = $this->splitCheck($parts, ':'); return ($string2 !== $string); } else { - return FALSE; + return false; } } @@ -213,7 +210,7 @@ $string = $string . $char . $parts[$i + 1]; } else { $this->error = 'Invalid address spec. Unclosed bracket or quotes'; - return FALSE; + return false; } } else { $this->index = $i; @@ -260,7 +257,7 @@ if ($num_angle_start < $num_angle_end) { $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')'; - return FALSE; + return false; } else { return ($num_angle_start > $num_angle_end); } @@ -295,10 +292,10 @@ */ function validateAddress($address) { - $is_group = FALSE; + $is_group = false; if ($address['group']) { - $is_group = TRUE; + $is_group = true; // Get the group part of the name $parts = explode(':', $address['address']); @@ -308,17 +305,12 @@ // And validate the group part of the name. if (!$this->validatePhrase($groupname)){ $this->error = 'Group name did not validate.'; - return FALSE; + return false; } else { - // Do differing things based upon format type. - if (!$this->ext_format) { - $temp_obj = new stdClass; - $temp_obj->mailbox = $groupname; - - $structure[] = $temp_obj; - unset($temp_obj); - } else { - $structure = new stdClass; + // Don't include groups if we are not nesting + // them. This avoids returning invalid addresses. + if ($this->nestGroups) { + $structure = new stdClass; $structure->groupname = $groupname; } } @@ -326,14 +318,13 @@ $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':'))); } - // If a group then split on comma and bung in an array. - // Otherwise, Just bung the whole address in an array. + // If a group then split on comma and put into an array. + // Otherwise, Just put the whole address in an array. if ($is_group) { - while (trim(strlen($address['address'])) > 0) { + while (strlen($address['address']) > 0) { $parts = explode(',', $address['address']); $addresses[] = $this->splitCheck($parts, ','); - - $address['address'] = substr($address['address'], strlen(end($addresses) . ',')); + $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ','))); } } else { $addresses[] = $address['address']; @@ -344,41 +335,45 @@ // Then errors were appearing. if (!isset($addresses)){ $this->error[] = 'Empty group.'; - return FALSE; + return false; } - // Trim those bad boys... - for ($i = 0; $i < count($addresses); $i++) + for ($i = 0; $i < count($addresses); $i++) { $addresses[$i] = trim($addresses[$i]); + } - // Validate the mailbox. - // This could be one of: name <geezer@domain.com> - // geezer@domain.com - // geezer - // - // Other combinations/variations are also possible - for ($i = 0; $i < count($addresses); $i++) - $addresses[$i] = $this->validateMailbox($addresses[$i]); - - // NORMAL format - if (!$this->ext_format) { - if ($is_group) - $structure = array_merge($structure, $addresses, array(new stdClass)); - else + // Validate each mailbox. + // Format could be one of: name <geezer@domain.com> + // geezer@domain.com + // geezer + // ... or any other format valid by RFC 822. + array_walk($addresses, array($this, 'validateMailbox')); + + // Nested format + if ($this->nestGroups) { + if ($is_group) { + $structure->addresses = $addresses; + } else { $structure = $addresses; - - // EXTENDED Format + } + // Flat format } else { - if ($is_group) - $structure->addresses = $addresses; - else + if ($is_group) { + // $structure = array_merge($structure, $addresses, array(new stdClass)); + $structure = array_merge($structure, $addresses); + } else { $structure = $addresses; + } } - // Finished. return $structure; } + function myTrim(&$string) + { + $string = trim($string); + } + /** * Function to validate a phrase. * @@ -390,6 +385,7 @@ // Splits on one or more Tab or space. $parts = preg_split('/[ \\x09]+/', $phrase, 0, PREG_SPLIT_NO_EMPTY); + $phrase_parts = array(); while (count($parts) > 0){ $phrase_parts[] = $this->splitCheck($parts, ' '); for ($i = 0; $i < $this->index + 1; $i++) @@ -400,15 +396,15 @@ // If quoted string: if (substr($phrase_parts[$i], 0, 1) == '"') { if (!$this->validateQuotedString($phrase_parts[$i])) - return FALSE; + return false; continue; } // Otherwise it's an atom: - if (!$this->validateAtom($phrase_parts[$i])) return FALSE; + if (!$this->validateAtom($phrase_parts[$i])) return false; } - return TRUE; + return true; } /** @@ -422,17 +418,17 @@ { // Check for any char from ASCII 0 - ASCII 127 if (!preg_match('/^[\\x00-\\x7E]+$/i', $atom, $matches)) - return FALSE; + return false; // Check for specials: if (preg_match('/[][()<>@,;\\:". ]/', $atom)) - return FALSE; + return false; // Check for control characters (ASCII 0-31): if (preg_match('/[\\x00-\\x1F]+/', $atom)) - return FALSE; + return false; - return TRUE; + return true; } /** @@ -459,7 +455,7 @@ * @access private * @param $mailbox The string to check. */ - function validateMailbox($mailbox) + function validateMailbox(&$mailbox) { // A couple of defaults. $phrase = ''; @@ -472,8 +468,8 @@ $phrase = trim($name); $route_addr = trim(substr($mailbox, strlen($name.'<'), -1)); - if ($this->validatePhrase($phrase) === FALSE || ($route_addr = $this->validateRouteAddr($route_addr)) === FALSE) - return FALSE; + if ($this->validatePhrase($phrase) === false || ($route_addr = $this->validateRouteAddr($route_addr)) === false) + return false; // Only got addr-spec } else { @@ -483,8 +479,8 @@ else $addr_spec = $mailbox; - if (($addr_spec = $this->validateAddrSpec($addr_spec)) === FALSE) - return FALSE; + if (($addr_spec = $this->validateAddrSpec($addr_spec)) === false) + return false; } // Construct the object that will be returned. @@ -500,7 +496,7 @@ $mbox->host = $addr_spec['domain']; } - return $mbox; + $mailbox = $mbox; } /** @@ -516,7 +512,7 @@ function validateRouteAddr($route_addr) { // Check for colon. - if (strpos($route_addr, ':') !== FALSE) { + if (strpos($route_addr, ':') !== false) { $parts = explode(':', $route_addr); $route = $this->splitCheck($parts, ':'); } else { @@ -528,18 +524,21 @@ if ($route === $route_addr){ unset($route); $addr_spec = $route_addr; - if (($addr_spec = $this->validateAddrSpec($addr_spec)) === FALSE) - return FALSE; - + if (($addr_spec = $this->validateAddrSpec($addr_spec)) === false) { + return false; + } } else { // Validate route part. - if (($route = $this->validateRoute($route)) === FALSE) - return FALSE; + if (($route = $this->validateRoute($route)) === false) { + return false; + } - $addr_spec = substr($route_addr, strlen($route.':')); + $addr_spec = substr($route_addr, strlen($route . ':')); // Validate addr-spec part. - if (($addr_spec = $this->validateAddrSpec($addr_spec)) === FALSE) return FALSE; + if (($addr_spec = $this->validateAddrSpec($addr_spec)) === false) { + return false; + } } if (isset($route)) { @@ -566,7 +565,7 @@ for ($i = 0; $i < count($domains); $i++) { $domains[$i] = str_replace('@', '', trim($domains[$i])); - if (!$this->validateDomain($domains[$i])) return FALSE; + if (!$this->validateDomain($domains[$i])) return false; } return $route; @@ -594,7 +593,7 @@ for ($i = 0; $i < count($sub_domains); $i++) { if (!$this->validateSubdomain(trim($sub_domains[$i]))) - return FALSE; + return false; } // Managed to get here, so return input. @@ -611,13 +610,13 @@ function validateSubdomain($subdomain) { if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){ - if (!$this->validateDliteral($arr[1])) return FALSE; + if (!$this->validateDliteral($arr[1])) return false; } else { - if (!$this->validateAtom($subdomain)) return FALSE; + if (!$this->validateAtom($subdomain)) return false; } // Got here, so return successful. - return TRUE; + return true; } /** @@ -645,7 +644,7 @@ $addr_spec = trim($addr_spec); // Split on @ sign if there is one. - if (strpos($addr_spec, '@') !== FALSE) { + if (strpos($addr_spec, '@') !== false) { $parts = explode('@', $addr_spec); $local_part = $this->splitCheck($parts, '@'); $domain = substr($addr_spec, strlen($local_part . '@')); @@ -656,8 +655,8 @@ $domain = $this->default_host; } - if (($local_part = $this->validateLocalPart($local_part)) === FALSE) return FALSE; - if (($domain = $this->validateDomain($domain)) === FALSE) return FALSE; + if (($local_part = $this->validateLocalPart($local_part)) === false) return false; + if (($domain = $this->validateDomain($domain)) === false) return false; // Got here so return successful. return array('local_part' => $local_part, 'domain' => $domain); @@ -684,7 +683,7 @@ // Validate each word. for ($i = 0; $i < count($words); $i++) { - if ($this->validatePhrase(trim($words[$i])) === FALSE) return FALSE; + if ($this->validatePhrase(trim($words[$i])) === false) return false; } // Managed to get here, so return the input.
Index: php4/pear/Mail.php diff -u php4/pear/Mail.php:1.3 php4/pear/Mail.php:1.4 --- php4/pear/Mail.php:1.3 Thu Nov 2 13:40:38 2000 +++ php4/pear/Mail.php Wed Jan 3 10:24:20 2001 @@ -116,7 +116,9 @@ foreach ($headers as $key => $val) { if ($key == 'From') { - $from_arr = imap_rfc822_parse_adrlist($val, 'localhost'); + include_once 'Mail/rfc822.php'; + + $from_arr = Mail_rfc822::parseAddressList($val, 'localhost'); $from = $from_arr[0]->mailbox . '@' . $from_arr[0]->host; if (strstr($from, ' ')) { // Reject outright envelope From addresses with spaces. @@ -150,6 +152,8 @@ */ function parseRecipients($recipients) { + include_once 'Mail/rfc822.php'; + // if we're passed an array, assume addresses are valid and // implode them before parsing. if (is_array($recipients)) { @@ -159,11 +163,11 @@ // Parse recipients, leaving out all personal info. This is // for smtp recipients, etc. All relevant personal information // should already be in the headers. - $addresses = imap_rfc822_parse_adrlist($recipients, 'localhost'); + $addresses = Mail_rfc822::parseAddressList($recipients, 'localhost', false); $recipients = array(); if (is_array($addresses)) { foreach ($addresses as $ob) { - $recipients[] = $ob->mailbox . '@' . $ob->host . ' '; + $recipients[] = $ob->mailbox . '@' . $ob->host; } } Index: php4/pear/Mail/rfc822.php diff -u php4/pear/Mail/rfc822.php:1.1 php4/pear/Mail/rfc822.php:1.2 --- php4/pear/Mail/rfc822.php:1.1 Tue Jan 2 21:10:42 2001 +++ php4/pear/Mail/rfc822.php Wed Jan 3 10:24:20 2001 @@ -20,18 +20,19 @@ * RFC 822 Email address list validation Utility * * @author Richard Heyes <richard.heyes@heyes-computing.net> - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ class Mail_rfc822 { // Properties (All private) var $address = null; var $default_host = 'localhost'; - var $ext_format = true; + var $nestGroups = true; var $addresses = array(); var $structure = array(); var $error = null; var $index = null; + var $mailRFC822 = true; /** @@ -41,14 +42,13 @@ * @access public * @param $address The address(es) to validate. * @param $default_host (Optional) Default domain/host etc. If not supplied, will be set to localhost. - * @param $extended_format (Optional) Whether to return the structure in extended format or not. - * Nests groups for easier viewing. + * @param $nest_groups (Optional) Whether to return the structure with groups nested for easier viewing. */ - function Mail_rfc822($address = null, $default_host = null, $extended_format = null) + function Mail_rfc822($address = null, $default_host = null, $nest_groups = null) { - if (isset($address)) $this->address = $address; + if (isset($address)) $this->address = $address; if (isset($default_host)) $this->default_host = $default_host; - if (isset($extended_format)) $this->ext_format = $extended_format; + if (isset($nest_groups)) $this->nestGroups = $nest_groups; } @@ -59,45 +59,42 @@ * @access public * @param $address The address(es) to validate. * @param $default_host (Optional) Default domain/host etc. + * @param $nest_groups (Optional) Whether to return the structure with groups nested for easier viewing. */ - function parseAddressList($address = null, $default_host = null, $extended_format = null) + function parseAddressList($address = null, $default_host = null, $nest_groups = null) { - // if (function_exists('imap_rfc822_parse_adrlist') && !$extended_format) - // return imap_rfc822_parse_adrlist($address, $default_host); - - if (!isset($this)) { - $obj = new Mail_rfc822($address, $default_host, $extended_format); - $return = $obj->parseAddressList(); - unset($obj); - return $return; + if (!isset($this->mailRFC822)) { + $obj = new Mail_rfc822($address, $default_host, $nest_groups); + return $obj->parseAddressList(); } - if (isset($address)) $this->address = $address; - if (isset($default_host)) $this->default_host = $default_host; - if (isset($extended_format)) $this->ext_format = $extended_format; + if (isset($address)) $this->address = $address; + if (isset($default_host)) $this->default_host = $default_host; + if (isset($nest_groups)) $this->nestGroups = $nest_groups; + + $this->structure = array(); + $this->addresses = array(); + $this->error = null; + $this->index = null; - if (!$this->splitAddresses($this->address) || isset($this->error)) + if (!$this->splitAddresses($this->address) || isset($this->error)) { return (array)$this->error; + } for ($i = 0; $i < count($this->addresses); $i++){ - if (($return = $this->validateAddress($this->addresses[$i])) === FALSE || isset($this->error)) + if (($return = $this->validateAddress($this->addresses[$i])) === false + || isset($this->error)) { return (array)$this->error; + } - if (!$this->ext_format) + if (!$this->nestGroups) { $this->structure = array_merge($this->structure, $return); - else + } else { $this->structure[] = $return; + } } - $return = $this->structure; - - // Now clean up. - $this->structure = array(); - $this->addresses = array(); - $this->error = null; - $this->index = null; - - return $return; + return $this->structure; } /** @@ -115,7 +112,7 @@ $split_char = ','; $is_group = false; } elseif (isset($this->error)) { - return FALSE; + return false; } // Starts with a group, so split on semi-colon: @@ -128,14 +125,14 @@ // brackets/quotes etc then something's fubar. // First check there's a colon at all: - if (strpos($string, ':') === FALSE) { + if (strpos($string, ':') === false) { $this->error = 'Invalid address: ' . $string; - return FALSE; + return false; } // Now check it's outside of brackets/quotes: if (!$this->splitCheck(explode(':', $string), ':')) - return FALSE; + return false; } // $string now contains the first full address/group. @@ -152,20 +149,20 @@ // If the next char is a comma and this was a group, then // there are more addresses, otherwise, if there are any more // chars, then there is another address. - if ($is_group === TRUE && substr($address, 0, 1) == ','){ + if ($is_group && substr($address, 0, 1) == ','){ $address = trim(substr($address, 1)); $this->splitAddresses($address); - return TRUE; + return true; } elseif (strlen($address) > 0) { $this->splitAddresses($address); - return TRUE; + return true; } else { - return TRUE; + return true; } // If you got here then something's off - return FALSE; + return false; } /** @@ -189,7 +186,7 @@ $string2 = $this->splitCheck($parts, ':'); return ($string2 !== $string); } else { - return FALSE; + return false; } } @@ -213,7 +210,7 @@ $string = $string . $char . $parts[$i + 1]; } else { $this->error = 'Invalid address spec. Unclosed bracket or quotes'; - return FALSE; + return false; } } else { $this->index = $i; @@ -260,7 +257,7 @@ if ($num_angle_start < $num_angle_end) { $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')'; - return FALSE; + return false; } else { return ($num_angle_start > $num_angle_end); } @@ -295,10 +292,10 @@ */ function validateAddress($address) { - $is_group = FALSE; + $is_group = false; if ($address['group']) { - $is_group = TRUE; + $is_group = true; // Get the group part of the name $parts = explode(':', $address['address']); @@ -308,17 +305,12 @@ // And validate the group part of the name. if (!$this->validatePhrase($groupname)){ $this->error = 'Group name did not validate.'; - return FALSE; + return false; } else { - // Do differing things based upon format type. - if (!$this->ext_format) { - $temp_obj = new stdClass; - $temp_obj->mailbox = $groupname; - - $structure[] = $temp_obj; - unset($temp_obj); - } else { - $structure = new stdClass; + // Don't include groups if we are not nesting + // them. This avoids returning invalid addresses. + if ($this->nestGroups) { + $structure = new stdClass; $structure->groupname = $groupname; } } @@ -326,14 +318,13 @@ $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':'))); } - // If a group then split on comma and bung in an array. - // Otherwise, Just bung the whole address in an array. + // If a group then split on comma and put into an array. + // Otherwise, Just put the whole address in an array. if ($is_group) { - while (trim(strlen($address['address'])) > 0) { + while (strlen($address['address']) > 0) { $parts = explode(',', $address['address']); $addresses[] = $this->splitCheck($parts, ','); - - $address['address'] = substr($address['address'], strlen(end($addresses) . ',')); + $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ','))); } } else { $addresses[] = $address['address']; @@ -344,41 +335,45 @@ // Then errors were appearing. if (!isset($addresses)){ $this->error[] = 'Empty group.'; - return FALSE; + return false; } - // Trim those bad boys... - for ($i = 0; $i < count($addresses); $i++) + for ($i = 0; $i < count($addresses); $i++) { $addresses[$i] = trim($addresses[$i]); + } - // Validate the mailbox. - // This could be one of: name <geezer@domain.com> - // geezer@domain.com - // geezer - // - // Other combinations/variations are also possible - for ($i = 0; $i < count($addresses); $i++) - $addresses[$i] = $this->validateMailbox($addresses[$i]); - - // NORMAL format - if (!$this->ext_format) { - if ($is_group) - $structure = array_merge($structure, $addresses, array(new stdClass)); - else + // Validate each mailbox. + // Format could be one of: name <geezer@domain.com> + // geezer@domain.com + // geezer + // ... or any other format valid by RFC 822. + array_walk($addresses, array($this, 'validateMailbox')); + + // Nested format + if ($this->nestGroups) { + if ($is_group) { + $structure->addresses = $addresses; + } else { $structure = $addresses; - - // EXTENDED Format + } + // Flat format } else { - if ($is_group) - $structure->addresses = $addresses; - else + if ($is_group) { + // $structure = array_merge($structure, $addresses, array(new stdClass)); + $structure = array_merge($structure, $addresses); + } else { $structure = $addresses; + } } - // Finished. return $structure; } + function myTrim(&$string) + { + $string = trim($string); + } + /** * Function to validate a phrase. * @@ -390,6 +385,7 @@ // Splits on one or more Tab or space. $parts = preg_split('/[ \\x09]+/', $phrase, 0, PREG_SPLIT_NO_EMPTY); + $phrase_parts = array(); while (count($parts) > 0){ $phrase_parts[] = $this->splitCheck($parts, ' '); for ($i = 0; $i < $this->index + 1; $i++) @@ -400,15 +396,15 @@ // If quoted string: if (substr($phrase_parts[$i], 0, 1) == '"') { if (!$this->validateQuotedString($phrase_parts[$i])) - return FALSE; + return false; continue; } // Otherwise it's an atom: - if (!$this->validateAtom($phrase_parts[$i])) return FALSE; + if (!$this->validateAtom($phrase_parts[$i])) return false; } - return TRUE; + return true; } /** @@ -422,17 +418,17 @@ { // Check for any char from ASCII 0 - ASCII 127 if (!preg_match('/^[\\x00-\\x7E]+$/i', $atom, $matches)) - return FALSE; + return false; // Check for specials: if (preg_match('/[][()<>@,;\\:". ]/', $atom)) - return FALSE; + return false; // Check for control characters (ASCII 0-31): if (preg_match('/[\\x00-\\x1F]+/', $atom)) - return FALSE; + return false; - return TRUE; + return true; } /** @@ -459,7 +455,7 @@ * @access private * @param $mailbox The string to check. */ - function validateMailbox($mailbox) + function validateMailbox(&$mailbox) { // A couple of defaults. $phrase = ''; @@ -472,8 +468,8 @@ $phrase = trim($name); $route_addr = trim(substr($mailbox, strlen($name.'<'), -1)); - if ($this->validatePhrase($phrase) === FALSE || ($route_addr = $this->validateRouteAddr($route_addr)) === FALSE) - return FALSE; + if ($this->validatePhrase($phrase) === false || ($route_addr = $this->validateRouteAddr($route_addr)) === false) + return false; // Only got addr-spec } else { @@ -483,8 +479,8 @@ else $addr_spec = $mailbox; - if (($addr_spec = $this->validateAddrSpec($addr_spec)) === FALSE) - return FALSE; + if (($addr_spec = $this->validateAddrSpec($addr_spec)) === false) + return false; } // Construct the object that will be returned. @@ -500,7 +496,7 @@ $mbox->host = $addr_spec['domain']; } - return $mbox; + $mailbox = $mbox; } /** @@ -516,7 +512,7 @@ function validateRouteAddr($route_addr) { // Check for colon. - if (strpos($route_addr, ':') !== FALSE) { + if (strpos($route_addr, ':') !== false) { $parts = explode(':', $route_addr); $route = $this->splitCheck($parts, ':'); } else { @@ -528,18 +524,21 @@ if ($route === $route_addr){ unset($route); $addr_spec = $route_addr; - if (($addr_spec = $this->validateAddrSpec($addr_spec)) === FALSE) - return FALSE; - + if (($addr_spec = $this->validateAddrSpec($addr_spec)) === false) { + return false; + } } else { // Validate route part. - if (($route = $this->validateRoute($route)) === FALSE) - return FALSE; + if (($route = $this->validateRoute($route)) === false) { + return false; + } - $addr_spec = substr($route_addr, strlen($route.':')); + $addr_spec = substr($route_addr, strlen($route . ':')); // Validate addr-spec part. - if (($addr_spec = $this->validateAddrSpec($addr_spec)) === FALSE) return FALSE; + if (($addr_spec = $this->validateAddrSpec($addr_spec)) === false) { + return false; + } } if (isset($route)) { @@ -566,7 +565,7 @@ for ($i = 0; $i < count($domains); $i++) { $domains[$i] = str_replace('@', '', trim($domains[$i])); - if (!$this->validateDomain($domains[$i])) return FALSE; + if (!$this->validateDomain($domains[$i])) return false; } return $route; @@ -594,7 +593,7 @@ for ($i = 0; $i < count($sub_domains); $i++) { if (!$this->validateSubdomain(trim($sub_domains[$i]))) - return FALSE; + return false; } // Managed to get here, so return input. @@ -611,13 +610,13 @@ function validateSubdomain($subdomain) { if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){ - if (!$this->validateDliteral($arr[1])) return FALSE; + if (!$this->validateDliteral($arr[1])) return false; } else { - if (!$this->validateAtom($subdomain)) return FALSE; + if (!$this->validateAtom($subdomain)) return false; } // Got here, so return successful. - return TRUE; + return true; } /** @@ -645,7 +644,7 @@ $addr_spec = trim($addr_spec); // Split on @ sign if there is one. - if (strpos($addr_spec, '@') !== FALSE) { + if (strpos($addr_spec, '@') !== false) { $parts = explode('@', $addr_spec); $local_part = $this->splitCheck($parts, '@'); $domain = substr($addr_spec, strlen($local_part . '@')); @@ -656,8 +655,8 @@ $domain = $this->default_host; } - if (($local_part = $this->validateLocalPart($local_part)) === FALSE) return FALSE; - if (($domain = $this->validateDomain($domain)) === FALSE) return FALSE; + if (($local_part = $this->validateLocalPart($local_part)) === false) return false; + if (($domain = $this->validateDomain($domain)) === false) return false; // Got here so return successful. return array('local_part' => $local_part, 'domain' => $domain); @@ -684,7 +683,7 @@ // Validate each word. for ($i = 0; $i < count($words); $i++) { - if ($this->validatePhrase(trim($words[$i])) === FALSE) return FALSE; + if ($this->validatePhrase(trim($words[$i])) === false) return false; } // Managed to get here, so return the input.