Mailinglist
 All Data Structures Files Functions Variables Pages
Public Member Functions | Data Fields
MailinglistMessagePart Class Reference

MailinglistMessagePart A class to represent a piece of a email message. More...

Inheritance diagram for MailinglistMessagePart:
Inheritance graph
[legend]

Public Member Functions

 headerRaw ($name)
 headerRaw(). More...
 
 header ($name)
 header() More...
 
 headerEmail ($name)
 Return header parsed as an email address into an array with indexes email and name. More...
 
 headerNoComments ($name)
 
 addHeader ($header_name, $header_value)
 Add a given header to the message, if header already exists, add as an array. More...
 
 setHeader ($header_name, $header_value)
 Set a header to a given value. More...
 
 body ()
 Return the body of the message. More...
 
 headers ()
 Return array of all headers. More...
 
 __construct ($msg=NULL)
 Build an message part. More...
 
 parseHeaders ()
 parseHeaders() Converts the headers member from a single text string with the contents of the part (or message) headers to an associtive array of the headers. More...
 
 parseBody ()
 parseBody()) converts text string in the body variable into a processed body For MIME type multipart, the body will become an array of the sections For MIME type message/rfc822, the body will become a MailingMessage object More...
 
 parseEmailAddr ($addr)
 Parse an Email address. More...
 
 stripComments ($header)
 
 logError ($str, $parm=array())
 logError() More...
 

Data Fields

 $headers
 An array of the headers of the message part Header name is the index to the array. More...
 
 $body
 The body of the message, if it is a multi-part then it is an array of the various multi-parts. More...
 

Detailed Description

MailinglistMessagePart A class to represent a piece of a email message.

Definition at line 268 of file MailinglistMessage.class.php.

Constructor & Destructor Documentation

__construct (   $msg = NULL)

Build an message part.

Parameters
$msgThe data to build the message from. Expecting a string with the data to build the part
Returns
Build the MailinglistMessagePart

Definition at line 402 of file MailinglistMessage.class.php.

References body(), headers(), parseBody(), and parseHeaders().

402  {
403  if (is_string($msg)) {
404  $break = strpos($msg, "\n\n");
405  $this->headers = substr($msg, 0, $break+1);
406  $this->body = substr($msg, $break+2);
407 
408  $this->parseHeaders();
409  $this->parseBody();
410  }
411  }
parseHeaders()
parseHeaders() Converts the headers member from a single text string with the contents of the part (o...
body()
Return the body of the message.
parseBody()
parseBody()) converts text string in the body variable into a processed body For MIME type multipart...
headers()
Return array of all headers.

Here is the call graph for this function:

Member Function Documentation

addHeader (   $header_name,
  $header_value 
)

Add a given header to the message, if header already exists, add as an array.

Definition at line 352 of file MailinglistMessage.class.php.

References headers().

Referenced by parseHeaders().

352  {
353  if (is_array($header_value)) {
354  // if given an array, add each element
355  foreach ($header_value as $value) {
356  $this->addHeader($header_name, $value);
357  }
358  }
359  else {
360  $header_value = trim($header_value);
361  // duplicate headers become arrays
362  if (isset($this->headers[$header_name])) {
363  if (!is_array($this->headers[$header_name])) {
364  $this->headers[$header_name] = array($this->headers[$header_name]);
365  }
366  $this->headers[$header_name][] = $header_value;
367  }
368  else {
369  $this->headers[$header_name] = $header_value;
370  }
371  }
372  }
addHeader($header_name, $header_value)
Add a given header to the message, if header already exists, add as an array.
headers()
Return array of all headers.

Here is the call graph for this function:

Here is the caller graph for this function:

body ( )

Return the body of the message.

Definition at line 384 of file MailinglistMessage.class.php.

References $body.

Referenced by __construct(), and parseBody().

384  {
385  return $this->body;
386  }
$body
The body of the message, if it is a multi-part then it is an array of the various multi-parts...

Here is the caller graph for this function:

header (   $name)

header()

Retrieves a specifed header from the message, performing general conversions on the data. This presumes that we will not be parsing the header latter for atoms.

Parameters
$nameString with the name of the header to get.
Returns
the value of the header (a string or an array if multiple headers of that type).

Definition at line 307 of file MailinglistMessage.class.php.

References headerRaw(), and mailinglist_message_rfc2047().

Referenced by headerNoComments().

307  {
308  $header = $this->headerRaw($name);
309  $header = mailinglist_message_rfc2047($header);
310 
311  return $header;
312  }
mailinglist_message_rfc2047($string)
mailinglist_message_rfc2047()

Here is the call graph for this function:

Here is the caller graph for this function:

headerEmail (   $name)

Return header parsed as an email address into an array with indexes email and name.

Definition at line 318 of file MailinglistMessage.class.php.

References headerRaw(), and parseEmailAddr().

318  {
319  $name = $this->headerRaw($name);
320  if (is_array($name)) {
321  $result = array();
322  foreach ($name as $key => $data) {
323  $result[$key] = parseEmailAddr($data);
324  }
325  }
326  else {
327  $result = $this->parseEmailAddr($name);
328  }
329 
330  return $result;
331  }
parseEmailAddr($addr)
Parse an Email address.

Here is the call graph for this function:

headerNoComments (   $name)

Definition at line 336 of file MailinglistMessage.class.php.

References header(), and stripComments().

336  {
337  $header = $this->header($name);
338  if (is_array($header)) {
339  foreach($header as $key => $data) {
340  $result[$key] = $this->stripComments($data);
341  }
342  }
343  else {
344  $result = $this->stripComments($header);
345  }
346  return $result;
347  }

Here is the call graph for this function:

headerRaw (   $name)

headerRaw().

Get raw header.

Parameters
$nameString with the name of the header to get.
Returns
the value of the header (a string or an array if multiple headers of that type).

Definition at line 290 of file MailinglistMessage.class.php.

References headers().

Referenced by header(), and headerEmail().

290  {
291  if (isset($this->headers[$name])) {
292  return $this->headers[$name];
293  }
294  else return FALSE;
295  }
headers()
Return array of all headers.

Here is the call graph for this function:

Here is the caller graph for this function:

headers ( )

Return array of all headers.

Definition at line 391 of file MailinglistMessage.class.php.

References $headers.

Referenced by __construct(), addHeader(), headerRaw(), parseBody(), parseHeaders(), and setHeader().

391  {
392  return $this->headers;
393  }
$headers
An array of the headers of the message part Header name is the index to the array.

Here is the caller graph for this function:

logError (   $str,
  $parm = array() 
)

logError()

Log a error

Definition at line 663 of file MailinglistMessage.class.php.

Referenced by parseBody(), and parseHeaders().

663  {
664  dpm($str, NULL, 'error');
665  watchdog('MailinglistMessage', $str, $parms, WATCHDOG_ERROR);
666  }

Here is the caller graph for this function:

parseBody ( )

parseBody()) converts text string in the body variable into a processed body For MIME type multipart, the body will become an array of the sections For MIME type message/rfc822, the body will become a MailingMessage object

Process encoding, character sets to make data stored as UTF-8 Thus the encoding/character set do NOT represent what is in the buffer, but how the data was sent.

Notes: Content-Type: Text/plain multipart/mixed multipart/alternate multipart/digest charset= US-ASCII UTF-8 ISO-8859-#

Content-Transfer-Encoding: BASE64, Quoted-printable, 7bit, 8bit

Todo:
we may want to somehow special mark text/plain sections?

Definition at line 525 of file MailinglistMessage.class.php.

References $body, body(), headers(), and logError().

Referenced by __construct().

525  {
526 
527  if (isset($this->headers['Content-Transfer-Encoding'])) {
528  $header = new MailinglistHeader($this->headers['Content-Transfer-Encoding']);
529  $cte = $header->get_atom();
530 
531  switch (strtolower($cte)) {
532  case 'quoted-printable':
533  $this->body = quoted_printable_decode($this->body);
534  break;
535  case 'base64':
536  $this->body = base64_decode($this->body);
537  break;
538  default:
539  $this->logError('Unknown CTE "@cte"', array('@cte' => $cte));
540  case 'binary':
541  case '8bit':
542  case '7bit':
543  // These encodings use a null transform, so we can ignore them
544  break;
545  }
546  }
547  else {
548  $cte = '7bit';
549  }
550 
551  if (isset($this->headers['Content-Type'])) {
552  $header = new MailinglistHeader($this->headers['Content-Type']);
553  $ct = $header->get_atom();
554  $pos = strpos($ct, '/');
555  if (!$pos) {
556  $this->logError('Bad Content-Type Format', $ct);
557  $pos = 0;
558  }
559  $type = substr($ct, 0, $pos);
560  $subtype = substr($ct, $pos+1);
561  $header->get_char(); // Remove the terminator (;) on the atom.
562  $parms = array();
563  // Get parameters to Content-Type
564  while ($parm = $header->get_token()) {
565  $char = $header->get_char(); // eat the =
566  $value = $header->get_value();
567  $parms[$parm] = $value;
568  }
569  switch (strtolower($type)) {
570  case 'multipart':
571  $bodies = explode("--" . $parms['boundary'], $this->body);
572  $this->body = array();
573  foreach ($bodies as $body) {
574  if (!empty($body) && $body[0] == "\n" && strlen($body) > 2) {
575  $this->body[] = new MailinglistMessagePart(substr($body, 1));
576  }
577  }
578  break;
579  case 'message':
580  if ($subtype == 'rfc822') {
581  $this->body = new MailinglistMessage($this->body);
582  }
583  break;
584 
585  case 'text':
587  if (isset($parms['charset'])) {
588  $charset = strtoupper($parms['charset']);
589  if ($charset == 'US-ASCII') $charset = 'ASCII';
590  $encodings = mb_list_encodings();
591  $this->body = mb_convert_encoding($this->body, 'UTF-8', $charset);
592  }
593  break;
594 
595  default:
596  $this->logError('Unkown Content Type ' . $type . ' / ' . $subtype);
597  dpm(array($type, $subtype));
598  }
599  }
600  }
Class defining the contents of an e-mail message.
$body
The body of the message, if it is a multi-part then it is an array of the various multi-parts...
body()
Return the body of the message.
class MailinglistHeader
headers()
Return array of all headers.
logError($str, $parm=array())
logError()
MailinglistMessagePart A class to represent a piece of a email message.

Here is the call graph for this function:

Here is the caller graph for this function:

parseEmailAddr (   $addr)

Parse an Email address.

Parameters
$addrThe email address to parse
Returns
array ['email'] = email address ['name'] = the name associated with it.
Todo:
this is a bit too simple in parsing.

Definition at line 609 of file MailinglistMessage.class.php.

Referenced by headerEmail().

609  {
611  $pos = strpos($addr, '<');
612  if ($pos !== FALSE) {
613  // Address uses name <email> format.
614  $pos1 = strpos($addr, '>', $pos);
615  $email = substr($addr, $pos+1, $pos1-$pos-2);
616  if ($pos > 0) {
617  $name = substr($addr, 0, $pos) . substr($addr, $pos1+1);
618  }
619  else {
620  $name = substr($addr, $pos1+1);
621  }
622  }
623  else {
624  $pos = strpos($addr, '(');
625  if ($pos !== FALSE) {
626  $pos1 = strpos($addr, ')');
627  $name = substr($addr, $pos+1, $pos1-$pos-1);
628  if ($pos > 0) {
629  $email = substr($addr, 0, $pos) . substr($addr, $pos1+1);
630  }
631  else {
632  $email = substr($addr, $pos1+1);
633  }
634  }
635  else {
636  $email = $addr;
637  $name = "";
638  }
639  }
640 
641  return array('email' => trim($email), 'name' => trim($name));
642  }

Here is the caller graph for this function:

parseHeaders ( )

parseHeaders() Converts the headers member from a single text string with the contents of the part (or message) headers to an associtive array of the headers.

The index of the array is the Header name, and the value is the header value. If header was repeated, the value will be an array with each header as an element.

Todo:
make header character fix better

Definition at line 420 of file MailinglistMessage.class.php.

References $headers, addHeader(), headers(), and logError().

Referenced by __construct().

420  {
421  // Fix any character problems. If real email message, should only be ascii,
422  // but some mbox files will have been converted, see if UTF or ISO-8859
424 
425  $charset = "";
426  @$headers = iconv('UTF-8//IGNORE', 'UTF-8', $this->headers);
427  if ($headers != $this->headers) {
428  $headers = iconv('ISO-8859-1//IGNORE', 'UTF-8', $this->headers);
429  $charset = 'ISO-8859-1';
430  }
431 
432  // Unfold headers newline-whitespace => space
433  $headers = preg_replace('/\n\s+/', ' ', $headers);
434  // Convert to an array of headers
435  $headers = explode("\n", $headers);
436  // Build up the associative array of the headers
437  $this->headers = array();
438  if(!empty($charset)){
439  $this->addHeader('XX-HeaderCharset', $charset);
440  }
441  foreach ($headers as $header) {
442  $colon = strpos($header, ': ');
443  if ($colon > 0) {
444  $header_name = substr($header, 0, $colon);
445  $header_value = substr($header, $colon+2);
446 
447  switch ($header_name) {
448  case 'References':
449  $header_value = str_replace("\t", " ", $header_value);
450  $header_value = str_replace('><', '> <', $header_value);
451  $header_value = explode(' ', $header_value);
452  break;
453  case 'To':
454  case 'Cc':
455  case 'From':
456  case 'Reply-To':
457  $pos = 0;
458  // See if there is a comma to process, start at beginning
459  while( FALSE !== ($posc = strpos($header_value, ',', $pos))){
460  $pos1 = strpos($header_value, '(', $pos);
461  $pos2 = strpos($header_value, '<', $pos);
462  $pos3 = strpos($header_value, '\\', $pos);
463  $pos4 = strpos($header_value, '"', $pos);
464  if($pos1 === FALSE) $pos1 = $posc;
465  if($pos2 === FALSE) $pos2 = $posc;
466  if($pos3 === FALSE) $pos3 = $posc;
467  if($pos4 === FALSE) $pos4 = $posc;
468  $pos1 = min($pos1, $pos2, $pos3, $pos4);
469  if($pos1 < $posc) {
470  switch($header_value[$pos1]) {
471  case '(':
472  $pos = strpos($header_value, ')', $pos1);
473  if(!$pos) $pos = $pos1+1;
474  break;
475  case '<':
476  $pos = strpos($header_value, '>', $pos1);
477  if(!$pos) $pos = $pos1+1;
478  break;
479  case '\\':
480  $pos = $pos1+2;
481  break;
482  case '"':
483  $pos = strpos($header_value, '"', $pos1+1);
484  if(!$pos) $pos = $pos1+1;
485  break;
486  default:
487  $this->logError('Error Processing Header: ' . $header_value);
488  break;
489  }
490  }
491  else {
492  // All quotes are after the comma, so process it.
493  $value = substr($header_value, 0, $posc);
494  $this->addHeader($header_name, $value);
495  // Strip out value, and process after the ,
496  $header_value = substr($header_value, $posc+1);
497  $pos = 0;
498  }
499  }
500  default:
501  break;
502  }
503  $this->addHeader($header_name, $header_value);
504  }
505  }
506  }
addHeader($header_name, $header_value)
Add a given header to the message, if header already exists, add as an array.
$headers
An array of the headers of the message part Header name is the index to the array.
headers()
Return array of all headers.
logError($str, $parm=array())
logError()

Here is the call graph for this function:

Here is the caller graph for this function:

setHeader (   $header_name,
  $header_value 
)

Set a header to a given value.

Definition at line 377 of file MailinglistMessage.class.php.

References headers().

Referenced by MailinglistMessage\__construct().

377  {
378  $this->headers[$header_name] = $header_value;
379  }
headers()
Return array of all headers.

Here is the call graph for this function:

Here is the caller graph for this function:

stripComments (   $header)

Definition at line 644 of file MailinglistMessage.class.php.

Referenced by headerNoComments().

644  {
645  while(FALSE !== ($pos = strpos($header, '('))) {
646  $pos1 = strpos($header, ')', $pos);
647  if($pos1 != FALSE) {
648  $header = substr($header, 0, $pos) . ' ' . substr($header, $pos1+1);
649  }
650  else {
651  // badly formated, strip rest of header
652  $header = substr($header, 0, $pos);
653  }
654  }
655  return $header;
656  }

Here is the caller graph for this function:

Field Documentation

$body

The body of the message, if it is a multi-part then it is an array of the various multi-parts.

if it is a message/rfc822 part, then body will be a Mailinglist Message

Definition at line 280 of file MailinglistMessage.class.php.

Referenced by body(), and parseBody().

An array of the headers of the message part Header name is the index to the array.

If header occures multiple times, then entry is an array of the occurances of it.

Definition at line 273 of file MailinglistMessage.class.php.

Referenced by headers(), and parseHeaders().


The documentation for this class was generated from the following file: