<?

/**
 * mailattach.mod.php
 * @package   Modules
 */

/**
 * Module name
 */
define("MOD_MAILATTACH",$name);

/**
 * Attachment option
 *
 * This should start with a higher number because of the values defined in
 * mail.mod.php
 */
define("ATTACHMENT",16);

/**
 * Mimetype option
 *
 * This should start with a higher number because of the values defined in
 * mail.mod.php
 */
define("MIMETYPE",17);

/**
 * Name option
 */
define("ATTACHNAME",18);

/**
 * Attachment name
 */
define("ATTACHMENT_NAME","attachment");

include_once(
MODULES_DIR."/mail.mod.php");
if (isset(
$modules[$name])) {
  unset(
$modules[$name]); // This is created by mail.mod.php
  //echo("Module $name unset in MailAttach<br>");
/* if (isset($modules[$name])) */
// Turn off MailModule:
unset($modules[MOD_MAIL]);
//echo("Module ". MOD_MAIL ." unset in MailAttach");
define("MOD_MAIL",MOD_MAILATTACH);

$modules[$name]= new MailAttachModule();
// } //end if $name!=""

/**
 * MailAttachModule
 *
 * This is a module to send email with an attachment
 * @package   Modules
 * @author    Herman Suijs
 * @see          CMailfile
 */
class MailAttachModule extends MailModule {
  var 
$name "MailAttachModule";
  var 
$id MOD_MAILATTACH;
  
//var $execlevel = 81;
  
var $attachname "";
  var 
$attachment "";
  var 
$filename "";
  var 
$mimetype "";

  
/**
   * Constructor
   *
   * Set options and call the superConstructor
   */
  
function MailAttachModule() {
    
// Multiple optionnames are difficult to implement with PHP3, because
    // it uses different names. Should probably use an array.
    // The cmailfile.inc.php needs to be changed to add more than one
    // attachment
    
$this->phpversion substr(phpversion(),0,1);
    
// Only for version >4
    
$this->options[ATTACHNAME] = new Option("attachsystemname","^[a-zA-Z]+$",false,false,ATTACHMENT_NAME);
    if (
$this->phpversion<4) {
      
$this->options[ATTACHMENT] = new Option(ATTACHMENT_NAME,"");
    } 
/* if ($this->phpversion<4) */
    
$this->options[MIMETYPE] = new Option("attachment_type:mimetype:mime_type");
    
$this->MailModule();
  } 
/* function MailAttachModule() */
  
  /**
   * Before action
   *
   * Check if an attachment is added
   */
  
function before_action() {
    global 
$debug;
    
/*
    if ($version<4) {
      return false;
    }
    */

    
$this->check_options();

    if (
$this->phpversion<4) {
      
$debug->add(DB_ERR,$this->name,"PHP 3 and lower versions not supported by formHandler yet");
      return 
false;
      if (isset(
$this->options[ATTACHMENT])) {
      } else 
$debug-> /* if
      (isset($this->options[ATTAC... */
add(DB_ERR,$this->name,"Option %s not set",ATTACHMENT_NAME);
      if (isset(
$this->options[MIMETYPE]))
    
$this->mimetype $this->options[MIMETYPE]->value;
      else 
$this->mimetype = ${ATTACHMENT_NAME_type};

    } else { 
/* if ($this->phpversion<4) */
      
global $HTTP_POST_FILES;
      
$this->attachname $this->options[ATTACHNAME]->value// only for version >4
      
if (!isset($HTTP_POST_FILES)) {
    
$debug->add(DB_ERR,$this->name,"HTTP_POST_FILES not defined");
    return 
false;
      } 
/* if (!isset($HTTP_POST_FILES)) */
      //echo "HTTP_POST_FILES: ".fh_serialize($HTTP_POST_FILES);
      //$this->options[ATTACHMENT]->set_value($HTTP_POST_FILES[ATTACHMENT_NAME]['tmp_name']);
      //$this->options[MIMETYPE]->set_value($HTTP_POST_FILES[ATTACHMENT_NAME]['type']);
      
$this->filename $HTTP_POST_FILES[$this->attachname]['name'];
    
//$this->attachment = $this->options[ATTACHMENT]->value;
      
$this->attachment $HTTP_POST_FILES[$this->attachname]['tmp_name'];
      if (isset(
$this->options[MIMETYPE]))
    
$this->mimetype $this->options[MIMETYPE]->value;
      else 
$this->mimetype $HTTP_POST_FILES[$this->attachname]['type'];
   
/* if ($this->phpversion<4) { ...... */  }
   
/* if ($this->phpversion<4) { ...... */}

  
/**
   * Send mail routine
   */
  
function send_mail($recipient,$from,$subject,$content,$headers) {
    global 
$debug;
    if (
$this->attachment=="") {
      if (
$from$headers .= "From: $from\n";
      
mail($recipient,$subject,$content,$headers);
    } else { 
/* if ($this->attachment=="") */
      
include_once(INCLUDE_DIR."/cmailfile.inc.php");
      
$mailfile = new CMailFile($subject,$recipient,$from,$content,$this->attachment,$this->mimetype,$this->filename,$this->filename,$headers);
      
$debug->add(1,$this->name,"mail(%s,%s,%s,%s) send",$recipient,$subject,$content,$headers);
      
$debug->add_message("CMailFile($this->attachment,$this->mimetype,$this->filename,$headers);",$this->name);
      
$mailfile->sendfile();
      unset(
$mailfile);
   
/* if ($this->attachment=="") { .... */  }
   
/* if ($this->attachment=="") { .... */}

// end /* if ($this->attachment=="") { .... */ class MailAtach