function MakePlainEmail($a_fld_order, $a_clean_fields, $s_to, $s_cc, $s_bcc, $a_raw_fields, $s_filter, $a_filter_list)
{
    global $SPECIAL_VALUES, $aPHPVERSION;
    $s_unfiltered_results = $s_filtered_results = "";
    $b_got_filter = isset($s_filter) && !empty($s_filter);
    if ($b_got_filter) {
        if (isset($a_filter_list) && count($a_filter_list) > 0) {
            $b_limited_filter = true;
        } else {
            $b_limited_filter = false;
        }
    }
    $b_used_template = false;
    if (IsMailOptionSet("PlainTemplate")) {
        //
        // need PHP 4.0.5 for the preg_replace_callback function
        //
        if (!IsPHPAtLeast("4.0.5")) {
            SendAlert(GetMessage(MSG_PHP_PLAIN_TEMPLATES, array("PHPVERS" => implode(".", $aPHPVERSION))));
        } else {
            $s_template = GetMailOption("PlainTemplate");
            if (ProcessTemplate($s_template, $a_lines, $a_raw_fields, GetMailOption('TemplateMissing'), 'SubstituteValuePlain')) {
                $b_used_template = true;
                $s_unfiltered_results = implode(BODY_LF, $a_lines);
                if ($b_got_filter) {
                    //
                    // with a limited filter, the template goes unfiltered
                    // and the named fields get filtered
                    //
                    if ($b_limited_filter) {
                        list($s_discard, $s_filtered_results) = GetFilteredOutput($a_fld_order, $a_clean_fields, $s_filter, $a_filter_list);
                    } else {
                        $s_filtered_results = Filter($s_filter, $s_unfiltered_results);
                        $s_unfiltered_results = "";
                    }
                }
            }
        }
    }
    if (!$b_used_template) {
        $res_hdr = "";
        if (IsMailOptionSet("DupHeader")) {
            //
            // write some standard mail headers
            //
            $res_hdr = "To: {$s_to}" . BODY_LF;
            if (!empty($s_cc)) {
                $res_hdr .= "Cc: {$s_cc}" . BODY_LF;
            }
            if (!empty($SPECIAL_VALUES["email"])) {
                $res_hdr .= "From: " . MakeFromLine($SPECIAL_VALUES["email"], $SPECIAL_VALUES["realname"]) . BODY_LF;
            }
            $res_hdr .= BODY_LF;
            if (IsMailOptionSet("StartLine")) {
                $res_hdr .= "--START--" . BODY_LF;
            }
            // signals the beginning of the text to filter
        }
        //
        // put the realname and the email address at the top of the results
        // (if not excluded)
        //
        if (!IsMailExcluded("realname")) {
            array_unshift($a_fld_order, "realname");
            $a_clean_fields["realname"] = $SPECIAL_VALUES["realname"];
        }
        if (!IsMailExcluded("email")) {
            array_unshift($a_fld_order, "email");
            $a_clean_fields["email"] = $SPECIAL_VALUES["email"];
        }
        if ($b_got_filter) {
            if ($b_limited_filter) {
                list($s_unfiltered_results, $s_filtered_results) = GetFilteredOutput($a_fld_order, $a_clean_fields, $s_filter, $a_filter_list);
            } else {
                //
                // make text output and filter it (filter all fields)
                //
                $s_filtered_results = MakeFieldOutput($a_fld_order, $a_clean_fields);
                $s_filtered_results = Filter($s_filter, $s_filtered_results);
            }
        } else {
            //SendAlert("There are ".count($a_fld_order)." fields in the order array");
            //SendAlert("Here is the clean fields array:\r\n".var_export($a_clean_fields,true));
            $s_unfiltered_results = MakeFieldOutput($a_fld_order, $a_clean_fields);
        }
        $s_unfiltered_results = $res_hdr . $s_unfiltered_results;
    }
    $s_results = $s_unfiltered_results;
    if ($b_got_filter && !empty($s_filtered_results)) {
        if (!empty($s_results)) {
            $s_results .= BODY_LF;
        }
        $s_results .= $s_filtered_results;
    }
    //
    // append the environment variables report
    //
    if (isset($SPECIAL_VALUES["env_report"]) && !empty($SPECIAL_VALUES["env_report"])) {
        $s_results .= BODY_LF . "==================================" . BODY_LF;
        $s_results .= BODY_LF . GetEnvVars(TrimArray(explode(",", $SPECIAL_VALUES["env_report"])), BODY_LF);
    }
    return array($s_results, $s_unfiltered_results, $s_filtered_results);
}
Exemplo n.º 2
0
function SendResults($a_fld_order, $a_clean_fields, $s_to, $s_cc, $s_bcc, $a_raw_fields)
{
    global $SPECIAL_VALUES, $aFileVars;
    //
    // check for a filter and how to use it
    //
    $b_got_filter = isset($SPECIAL_VALUES["filter"]) && !empty($SPECIAL_VALUES["filter"]);
    $b_filter_attach = false;
    $a_attach_spec = array();
    $s_filter = "";
    $a_filter_list = array();
    if ($b_got_filter) {
        $s_filter = $SPECIAL_VALUES["filter"];
        if (isset($SPECIAL_VALUES["filter_fields"]) && !empty($SPECIAL_VALUES["filter_fields"])) {
            $b_limited_filter = true;
            $a_filter_list = TrimArray(explode(",", $SPECIAL_VALUES["filter_fields"]));
        } else {
            $b_limited_filter = false;
        }
        $s_filter_attach_name = GetFilterOption("Attach");
        if (isset($s_filter_attach_name)) {
            if (!is_string($s_filter_attach_name) || empty($s_filter_attach_name)) {
                SendAlert(GetMessage(MSG_ATTACH_NAME));
            } else {
                $b_filter_attach = true;
                $a_attach_spec = array("Name" => $s_filter_attach_name);
                if (($s_mime = GetFilterAttrib($s_filter, "MIME")) !== false) {
                    $a_attach_spec["MIME"] = $s_mime;
                }
                //
                // Regarding the character set...
                // A filter will not generally change the character set
                // of the message, however, if it does, then we
                // provide that information to the MIME encoder.
                // Remember: this character set specification refers
                // to the data *after* the effect of the filter
                // has been reversed (e.g. an encrypted message
                // in UTF-8 is in UTF-8 when it is decrypted).
                //
                if (($s_cset = GetFilterAttrib($s_filter, "CharSet")) !== false) {
                    $a_attach_spec["CharSet"] = $s_cset;
                }
            }
        }
    }
    //
    // check the need for MIME formatted mail
    //
    $b_mime_mail = IsMailOptionSet("HTMLTemplate") || count($aFileVars) > 0 || $b_filter_attach;
    //
    // create the email header lines - CC, BCC, and From
    //
    $a_headers = array();
    if (!empty($s_cc)) {
        $a_headers['Cc'] = $s_cc;
    }
    //
    // note that BCC is documented to not work prior to PHP 4.3
    //
    if (!empty($s_bcc)) {
        global $aPHPVERSION;
        if ($aPHPVERSION[0] < 4 || $aPHPVERSION[0] == 4 && $aPHPVERSION[1] < 3) {
            SendAlert(GetMessage(MSG_PHP_BCC, array("PHPVERS" => implode(".", $aPHPVERSION))));
        }
        $a_headers['Bcc'] = $s_bcc;
    }
    //
    // create the From address
    //
    // Some servers won't let you set the email address to the
    // submitter of the form.  Therefore, use FromAddr if it's been
    // specified.  If it's empty, don't specify a sender.
    //
    $s_sender = GetMailOption("FromAddr");
    if (!isset($s_sender)) {
        $s_sender = "";
        if (!empty($SPECIAL_VALUES["email"])) {
            $a_headers['From'] = MakeFromLine($SPECIAL_VALUES["email"], $SPECIAL_VALUES["realname"]);
        }
    } elseif ($s_sender !== "") {
        $a_headers['From'] = UnMangle($s_sender);
    }
    //
    // special case: if there is only one non-special string value, then
    // format it as an email (unless an option says not to)
    //
    $a_keys = array_keys($a_raw_fields);
    if (count($a_keys) == 1 && is_string($a_raw_fields[$a_keys[0]]) && !IsMailOptionSet("AlwaysList") && !IsMailOptionSet("DupHeader")) {
        if (IsMailExcluded($a_keys[0])) {
            SendAlert("Exclusion of single field '" . $a_keys[0] . "' ignored");
        }
        $s_value = $a_raw_fields[$a_keys[0]];
        //
        // replace carriage return/linefeeds with <br>
        //
        $s_value = str_replace("\r\n", '<br />', $s_value);
        //
        // replace lone linefeeds with <br>
        //
        $s_value = str_replace("\n", '<br />', $s_value);
        //
        // remove lone carriage returns
        //
        $s_value = str_replace("\r", "", $s_value);
        //
        // replace all control chars with <br>
        //
        $s_value = preg_replace('/[[:cntrl:]]+/', '<br />', $s_value);
        //
        // strip HTML (note that all the <br> above will now be
        // replaced with BODY_LF)
        //
        $s_value = StripHTML($s_value, BODY_LF);
        if ($b_mime_mail) {
            if ($b_got_filter) {
                //
                // filter the whole value (ignore filter_fields for this
                // special case) if a filter has been specified
                //
                $s_results = Filter($s_filter, $s_value);
                if ($b_filter_attach) {
                    $a_attach_spec["Data"] = $s_results;
                    $s_results = "";
                    $s_filter = "";
                    // no more filtering
                }
            } else {
                $s_results = $s_value;
            }
            //
            // send this single value off to get formatted in a MIME
            // email
            //
            if (!MakeMimeMail($s_results, $a_headers, $a_raw_fields, GetMailOption('HTMLTemplate'), GetMailOption('TemplateMissing'), IsMailOptionSet("NoPlain"), $s_filter, $aFileVars, $a_attach_spec)) {
                return false;
            }
        } elseif ($b_got_filter) {
            //
            // filter the whole value (ignore filter_fields for this special case)
            // if a filter has been specified
            //
            $s_results = Filter($s_filter, $s_value);
        } else {
            $s_results = $s_value;
        }
    } else {
        if ($b_mime_mail) {
            //
            // get the plain text version of the email then send it
            // to get MIME formatted
            //
            list($s_results, $s_unfiltered_results, $s_filtered_results) = MakePlainEmail($a_fld_order, $a_clean_fields, $s_to, $s_cc, $s_bcc, $a_raw_fields, $s_filter, $a_filter_list);
            if ($b_filter_attach) {
                //
                // attached the filtered results
                //
                $a_attach_spec["Data"] = $s_filtered_results;
                //
                // put the unfiltered results in the body of the message
                //
                $s_results = $s_unfiltered_results;
                $s_filter = "";
                // no more filtering
            }
            if (!MakeMimeMail($s_results, $a_headers, $a_raw_fields, GetMailOption('HTMLTemplate'), GetMailOption('TemplateMissing'), IsMailOptionSet("NoPlain"), $s_filter, $aFileVars, $a_attach_spec)) {
                return false;
            }
        } else {
            list($s_results, $s_unfiltered_results, $s_filtered_results) = MakePlainEmail($a_fld_order, $a_clean_fields, $s_to, $s_cc, $s_bcc, $a_raw_fields, $s_filter, $a_filter_list);
            if (!$b_got_filter && IsMailOptionSet("CharSet")) {
                //
                // sending plain text email, and the CharSet has been
                // specified; include a header
                //
                $a_headers['Content-Type'] = "text/plain; charset=" . GetMailOption("CharSet");
            }
        }
    }
    //
    // append the environment variables report
    //
    if (isset($SPECIAL_VALUES["env_report"])) {
        $s_results .= BODY_LF . "==================================" . BODY_LF;
        $s_results .= BODY_LF . GetEnvVars(TrimArray(explode(",", $SPECIAL_VALUES["env_report"])), BODY_LF);
    }
    //
    // send the mail - assumes the email addresses have already been checked
    //
    return SendCheckedMail($s_to, $SPECIAL_VALUES["subject"], $s_results, $s_sender, $a_headers);
}