function SendResults($a_fld_order, $a_clean_fields, $s_to, $s_cc, $s_bcc, $a_raw_fields)
{
    global $SPECIAL_VALUES, $aFileVars, $FILE_REPOSITORY, $FIXED_SENDER;
    //
    // check for a filter and how to use it
    //
    $b_filter_attach = false;
    $a_attach_spec = array();
    $s_filter = "";
    $a_filter_list = array();
    if ($b_got_filter = GetFilterSpec($s_filter, $a_filter_list)) {
        if ($a_filter_list === false) {
            //
            // not a limited filter, so filter all fields
            //
            $b_limited_filter = false;
            $a_filter_list = array();
        } else {
            $b_limited_filter = true;
        }
        FMDebug("SendResults: got filter '{$s_filter}', limited={$b_limited_filter}");
        $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, From, and Reply-To
    //
    $a_headers = array();
    if (!empty($s_cc)) {
        $a_headers['Cc'] = SafeHeader($s_cc);
    }
    if (!empty($SPECIAL_VALUES["replyto"])) {
        //
        // expand replyto list
        //
        CheckEmailAddress($SPECIAL_VALUES["replyto"], $s_list, $s_invalid, false);
        if (!empty($s_list)) {
            $a_headers['Reply-To'] = SafeHeader($s_list);
        }
    }
    //
    // 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'] = SafeHeader($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 to set the sender and the "From" address.
    //
    $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 !== "") {
        $s_sender = $a_headers['From'] = SafeHeader(UnMangle($s_sender));
    }
    /* 
     * Override sender if $FIXED_SENDER is set.
     */
    if ($FIXED_SENDER !== "") {
        $s_sender = $FIXED_SENDER;
    }
    if ($s_sender === "") {
        if (SET_SENDER_FROM_EMAIL) {
            $s_sender = $SPECIAL_VALUES["email"];
        }
    }
    //
    // 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;
                    //
                    // KeepInLine keeps the filtered version inline as well
                    // as an attachment
                    //
                    if (!IsFilterOptionSet("KeepInLine")) {
                        $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;
            if (IsMailOptionSet("CharSet")) {
                //
                // sending plain text email, and the CharSet has been
                // specified; include a header
                //
                $a_headers['Content-Type'] = "text/plain; charset=" . SafeHeader(GetMailOption("CharSet"));
            }
        }
    } 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;
                //
                // KeepInLine keeps the filtered version inline as well
                // as an attachment
                //
                if (!IsFilterOptionSet("KeepInLine")) {
                    //
                    // 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=" . SafeHeader(GetMailOption("CharSet"));
            }
        }
    }
    //
    // now save uploaded files to the repository
    //
    if (FILEUPLOADS && $FILE_REPOSITORY !== "") {
        if (!SaveAllFilesToRepository()) {
            return false;
        }
    }
    //
    // send the mail - assumes the email addresses have already been checked
    //
    return SendCheckedMail($s_to, $SPECIAL_VALUES["subject"], $s_results, $s_sender, $a_headers);
}
Пример #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("filter_options: Attach must contain a name (e.g. Attach=data.txt)");
            } 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("Warning: BCC is probably not supported on your PHP version (" . implode(".", $aPHPVERSION) . ")");
        }
        $a_headers['Bcc'] = $s_bcc;
    }
    //
    // create the From address
    //
    if (!empty($SPECIAL_VALUES["email"])) {
        $a_headers['From'] = MakeFromLine($SPECIAL_VALUES["email"], $SPECIAL_VALUES["realname"]);
    }
    //
    // 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, $SPECIAL_VALUES["email"], $a_headers);
}