<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * create and send mails from form data
 *
 * @access      public
 * @package     yana
 * @subpackage  mail
 */
class FormMailer extends SerializeableObject
{

    /**#@+
     * @access public
     */

    /** @var string */  var $subject  = "";
    /** @var string */  var $headline = "";
    /** @var string */  var $footline = "";
    /** @var array  */  var $content  = array();

    /**#@-*/

    /**
     * constructor
     */
    function FormMailer() {
        /* intentionally left blank */
    }

    /**
     * send an e-mail
     *
     * @access public
     * @param string $recipient
     * @return boolean
     */ 
    function send($recipient)
    {
        assert('is_string($recipient) && !empty($recipient);');
        settype($recipient, "String");

        if (count($this->content) > 0 && !empty($recipient)) {

            /* untaint subject */
            assert('is_string($this->subject);');
            $this->subject = untaintInput($this->subject, "string", 128, YANA_ESCAPE_LINEBREAK);
            $this->subject = strip_tags($this->subject);
            $this->subject = preg_replace("/[^\w \(\)äÄüÜöÖß]/",    "", $this->subject);

            $test = Mailer::mail($recipient, "[MAILFORM] ".$this->subject, $this->makeMail());
            assert('is_bool($test);');
            return $test;

        } else {
            return false;
        }

    }

    /**
     * makeMail
     *
     * @access private
     * @return string
     * @ignore
     */
    function makeMail()
    {
        assert('is_string($this->headline);');
        $mail  = $this->headline;

        $mail .= "==========================================\n";
        assert('is_string($this->subject);');
        if (!empty($this->subject)) {
            $mail .= "  " . $this->subject . "\n";
            $mail .= "==========================================\n\n";
        }

        assert('is_array($this->content) || is_string($this->content);');
        settype($this->content, "array");
        foreach ($this->content as $a => $b)
        {
            if (is_scalar($b)) {
                $mail .= "    ".$a.":\t".$b."\n";
            } else if (is_array($b)) {
                $mail .= "    ".$a.":\t".print_r($b,true)."\n";
            } else {
                trigger_error("Invalid form data in key '".$a."'. Expected a value of a scalar type or an array, found '".gettype($b)."' instead.", E_USER_NOTICE);
            }
        }

        $mail .= "\n==========================================\n";
        $mail .= "Date:\t".date("d.m.Y H:i:s", time())."\n";
        $mail .= "IP:\t".$_SERVER['REMOTE_ADDR']."\n";

        assert('is_string($this->footline);');
        $mail .= $this->footline;

        return $mail;
    }

    /**
     * mail
     *
     * create an automatically formated e.mail from an array of form data
     *
     * @access public
     * @static
     * @param string $recipient
     * @param string $subject
     * @param array &$formdata
     * @return boolean
     * @since Version 2.8
     */
    function mail($recipient, $subject, &$formdata)
    {
        assert('is_string($recipient) && !empty($recipient);');
        assert('is_string($subject);');
        assert('is_array($formdata);');
        settype($recipient, "string");
        settype($subject, "string");
        global $YANA;
    
        unset($formdata['action']);
    
        $formMail = new formMailer();
        $formMail->subject = $subject;
        $formMail->content =& $formdata;
    
        $recipient = untaintInput($recipient, 'mail');
    
        if (!empty($recipient)) {
            $test = $formMail->send($recipient);
            assert('is_bool($test);');
        } else {
            trigger_error("Invalid recipient, the provided value is not a valid mail adress.", E_USER_NOTICE);
            return false;
        }
    }

}

?>