cvs: php4 /pear/Mail RFC822.php
| From: | Richard Heyes | Date: | Mon, 01 Apr 2002 21:32:10 +0000 |
| Subject: | cvs: php4 /pear/Mail RFC822.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-2967@lists.php.net to get a copy of this message | ||
richard Mon Apr 1 16:32:10 2002 EDT
Modified files:
/php4/pear/Mail RFC822.php
Log:
* Added optional limit to prevent runaway parsing
* Changed recursive nature of _splitAddresses as it was barfing on
large amounts of addresses (1000+)
* Added approximateCount() function which will give an approximate count
of how many addresses are in a string
Index: php4/pear/Mail/RFC822.php
diff -u php4/pear/Mail/RFC822.php:1.18 php4/pear/Mail/RFC822.php:1.19
--- php4/pear/Mail/RFC822.php:1.18 Tue Dec 11 10:31:30 2001
+++ php4/pear/Mail/RFC822.php Mon Apr 1 16:32:10 2002
@@ -38,7 +38,7 @@
*
* @author Richard Heyes <richard@phpguru.org>
* @author Chuck Hagenbuch <chuck@horde.org>
-* @version $Revision: 1.18 $
+* @version $Revision: 1.19 $
* @package Mail
*/
@@ -105,6 +105,12 @@
* @var boolean $mailRFC822
*/
var $mailRFC822 = true;
+
+ /**
+ * A limit after which processing stops
+ * @var int $limit
+ */
+ var $limit = null;
/**
@@ -119,12 +125,13 @@
*
* @return object Mail_RFC822 A new Mail_RFC822 object.
*/
- function Mail_RFC822($address = null, $default_domain = null, $nest_groups = null, $validate =
null)
+ function Mail_RFC822($address = null, $default_domain = null, $nest_groups = null, $validate =
null, $limit = null)
{
if (isset($address)) $this->address = $address;
if (isset($default_domain)) $this->default_domain = $default_domain;
if (isset($nest_groups)) $this->nestGroups = $nest_groups;
if (isset($validate)) $this->validate = $validate;
+ if (isset($limit)) $this->limit = $limit;
}
@@ -140,11 +147,11 @@
*
* @return array A structured array of addresses.
*/
- function parseAddressList($address = null, $default_domain = null, $nest_groups = null,
$validate = null)
+ function parseAddressList($address = null, $default_domain = null, $nest_groups = null,
$validate = null, $limit = null)
{
if (!isset($this->mailRFC822)) {
- $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate);
+ $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
return $obj->parseAddressList();
}
@@ -152,17 +159,28 @@
if (isset($default_domain)) $this->default_domain = $default_domain;
if (isset($nest_groups)) $this->nestGroups = $nest_groups;
if (isset($validate)) $this->validate = $validate;
+ if (isset($limit)) $this->limit = $limit;
$this->structure = array();
$this->addresses = array();
$this->error = null;
$this->index = null;
- if (!$this->_splitAddresses($this->address) || isset($this->error)) {
+ while ($this->address = $this->_splitAddresses($this->address)) {
+ continue;
+ }
+
+ if ($this->address === false || isset($this->error)) {
return $this->raiseError($this->error);
}
+ // Reset timer since large amounts of addresses can take a long time to
+ // get here
+ set_time_limit(30);
+
+ // Loop through all the addresses
for ($i = 0; $i < count($this->addresses); $i++){
+
if (($return = $this->_validateAddress($this->addresses[$i])) === false
|| isset($this->error)) {
return $this->raiseError($this->error);
@@ -174,7 +192,7 @@
$this->structure[] = $return;
}
}
-
+
return $this->structure;
}
@@ -185,9 +203,13 @@
* @param string $address The addresses to split.
* @return boolean Success or failure.
*/
- function _splitAddresses($address = '')
+ function _splitAddresses($address)
{
+ if (!empty($this->limit) AND count($this->addresses) == $this->limit) {
+ return TRUE;
+ }
+
if ($this->_isGroup($address) && !isset($this->error)) {
$split_char = ';';
$is_group = true;
@@ -237,14 +259,13 @@
// chars, then there is another address.
if ($is_group && substr($address, 0, 1) == ','){
$address = trim(substr($address, 1));
- $this->_splitAddresses($address);
- return true;
+ return $address;
} elseif (strlen($address) > 0) {
- $this->_splitAddresses($address);
- return true;
+ return $address;
+
} else {
- return true;
+ return '';
}
// If you got here then something's off
@@ -829,5 +850,19 @@
return $local_part;
}
+ /**
+ * Returns an approximate count of how many addresses are
+ * in the given string. This is APPROXIMATE as it only splits
+ * based on a comma which has no preceding backslash. Could be
+ * useful as large amounts of addresses will end up producing
+ * *large* structures when used with parseAddressList().
+ *
+ * @param string $data Addresses to count
+ * @return int Approximate count
+ */
+ function approximateCount($data)
+ {
+ return count(preg_split('/(?<!\\\\),/', $data));
+ }
}
?>