<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php
require_once 'base_object.php';

##
## Attachments are stored in PostgreSQL Large Objects, these are not compatible with other
## databases.  PHP pgsql functions are called directly in this object specifically for the
## pgsql LO managment.
##
## be sure to wrap any LOB calls with transaction begin/end
##

class Email extends Base_Object {

  function Valid ($email) {

    if (!preg_match ('/^(.*)@(.*)$/', $email, $all)) {
      return array();
    }

    $to = preg_split ('/\s+/', $all[1]);
    $user = array_pop($to);

    preg_match ('/^(\W*)((\w|-|\.|\+)+)$/', $user, $real_user);

    $user = $real_user[2];

    $name = join (' ', $to);

    $from = preg_split ('/\s+/', $all[2]);

    $domain_parts = preg_split ('/\./', $from[0]);
    $domain_count = count($domain_parts);

    # validate domain w/o a dns lookup
    for ($i=0; $i<($domain_count-1); $i++) {
      if (!preg_match ('/^((\w|-)+)$/', $domain_parts[$i], $piece)) {
        return array();
      }
    }

    preg_match ('/^(\w+).*$/', $domain_parts[$domain_count-1], $dotcom);

    $domain_parts[$domain_count-1] = $dotcom[1];

    if (count($domain_parts) < 2) {
      return array();
    }

    $domain = join ('.', $domain_parts);

    if ($user and $domain) {
      return (array($user.'@'.$domain, $name));

    } else {
      return array();
    }
  } 

  function Parts ($msg) {
    $lines = preg_split ('/\n/', $msg);

    $c = count($lines)-1;

    $headers = array ();

    for ($i=0; $i<$c; $i++) {
      $line = $lines[$i];

      if (!$line) $curr_header_type = 'rest_of_message';

      if (($curr_header_type != 'rest_of_message') and
           preg_match ('/^(\S+?):\s*(.*)$/', $line, $header_line)) {
        $curr_header_type = strtolower ($header_line[1]);
        $line = $header_line[2];
      }

      if ($curr_header_type == 'rest_of_message') $line .= "\n";
      $headers[$curr_header_type] .= $line;
    }

    $mailp = mailparse_msg_create ();

    mailparse_msg_parse ($mailp, $msg);

    $struct = mailparse_msg_get_structure($mailp);

    $c = count($struct);

    $headers[message_parts] = array ();

    for ($i=0; $i<$c; $i++) {
      $st = $struct[$i];
      $section = mailparse_msg_get_part($mailp, $st);
      $info = mailparse_msg_get_part_data($section);

      ob_start ();
      mailparse_msg_extract_part ($section, $msg);
      $data = ob_get_contents ();
      ob_end_clean ();

      $headers[message_parts][] = array (
        info => $info,
        data => $data,
      );
    }

    mailparse_msg_free ($mailp);

    return $headers;
  }

  function Msg_Bodies ($headers) {
    $c = count($headers[message_parts]);
    $attachments = array ();

    for ($i=0; $i<$c; $i++) {
      $mp = $headers[message_parts][$i];
  
      if ($mp[info]['content-disposition'] == 'attachment') {
        $attachments[] = array (
          'name' => $mp[info]['disposition-filename'],
          'type' => $mp[info]['content-type'],
          'data' => $mp[data],
        );
  
      } elseif (!(($i == 0) and ($mp[info]['content-type'] == 'multipart/mixed'))) {
        $msg .= $mp[data];
      }
    }

    return array ($msg, $attachments);
  }

  function Send_Msg ($to, $subject, $body, $from) {
    $headers = array ();

    if ($from) {
      $headers[] = "From: $from";
      $headers[] = "Reply-To: $from";
      $headers[] = "Return-Path: $from";
    }

    return mail ($to, $subject, $body, join("\n",$headers));
  }

  function Make_Eyecatch() {
    return "=========== previous message(s) ===========";
  }
}
?>
