function WriteCSVFile($s_csv_file, $a_vars)
{
    global $SPECIAL_VALUES, $CSVSEP, $CSVINTSEP, $CSVQUOTE, $CSVOPEN, $CSVLINE;
    //
    // create an array of column values in the order specified
    // in $SPECIAL_VALUES["csvcolumns"]
    //
    $a_column_list = $SPECIAL_VALUES["csvcolumns"];
    if (!isset($a_column_list) || empty($a_column_list) || !is_string($a_column_list)) {
        SendAlert(GetMessage(MSG_CSVCOLUMNS, array("VALUE" => $a_column_list)));
        return;
    }
    if (!isset($s_csv_file) || empty($s_csv_file) || !is_string($s_csv_file)) {
        SendAlert(GetMessage(MSG_CSVFILE, array("VALUE" => $s_csv_file)));
        return;
    }
    @($fp = fopen($s_csv_file, "a" . $CSVOPEN));
    if ($fp === false) {
        SendAlert(GetMessage(MSG_FILE_OPEN_ERROR, array("NAME" => $s_csv_file, "TYPE" => "CSV", "ERROR" => CheckString($php_errormsg))));
        return;
    }
    //
    // convert the column list to an array, trim the names too
    //
    $a_column_list = TrimArray(explode(",", $a_column_list));
    $n_columns = count($a_column_list);
    //
    // if the file is currently empty, put the column names in the first line
    //
    $b_heading = false;
    if (filesize($s_csv_file) == 0) {
        $b_heading = true;
    }
    $csv_format = new CSVFormat();
    //
    // now configure the CSVFormat object
    // according to FormMail's configuration settings
    //
    $csv_format->SetQuote($CSVQUOTE);
    $csv_format->SetEscPolicy("conv");
    $csv_format->SetSep($CSVSEP);
    $csv_format->SetIntSep($CSVINTSEP);
    if (LIMITED_IMPORT) {
        $csv_format->SetCleanFunc(create_function('$m_value', 'return CleanValue($m_value);'));
    }
    $s_csv = $csv_format->MakeCSVRecord($a_column_list, $a_vars);
    if ($b_heading) {
        fwrite($fp, $csv_format->MakeHeading($a_column_list) . $CSVLINE);
    }
    fwrite($fp, $s_csv . $CSVLINE);
    fclose($fp);
    //  CreatePage($debug);
    //  exit;
}
示例#2
0
function BuiltinFilterCSV()
{
    global $aAllRawValues, $aRawDataValues, $SPECIAL_VALUES, $CSVLINE;
    $b_heading = false;
    $a_column_list = array();
    $s_cols = $SPECIAL_VALUES["filter_fields"];
    if (!isset($s_cols) || empty($s_cols) || !is_string($s_cols)) {
        $s_cols = $SPECIAL_VALUES["csvcolumns"];
        if (!isset($s_cols) || empty($s_cols) || !is_string($s_cols)) {
            /*
             * neither filter_fields nor csvcolumns defined - get all
             * columns
             */
            $s_cols = "";
            /*
             * special case - include these two special fields
             */
            $a_column_list = array("email", "name");
            /*
             * now include all the data fields
             */
            $a_column_list = array_merge($a_column_list, array_keys($aRawDataValues));
            $b_heading = true;
        }
    }
    if (empty($a_column_list)) {
        $a_column_list = TrimArray(explode(",", $s_cols));
    }
    $csv_format = new CSVFormat();
    /*
     * get the various options and set them
     */
    $m_temp = GetFilterOption("CSVQuote");
    if (isset($m_temp)) {
        $csv_format->SetQuote($m_temp);
    }
    $m_temp = GetFilterOption("CSVSep");
    if (isset($m_temp)) {
        $csv_format->SetSep($m_temp);
    }
    $m_temp = GetFilterOption("CSVIntSep");
    if (isset($m_temp)) {
        $csv_format->SetIntSep($m_temp);
    }
    $m_temp = GetFilterOption("CSVEscPolicy");
    if (isset($m_temp)) {
        $csv_format->SetEscPolicy($m_temp);
    }
    $m_temp = GetFilterOption("CSVHeading");
    if (isset($m_temp)) {
        $b_heading = true;
    }
    /*
     * clean fields unless CSVRaw is specified
     */
    $m_temp = GetFilterOption("CSVRaw");
    if (!isset($m_temp)) {
        $csv_format->SetCleanFunc(create_function('$m_value', 'return CleanValue($m_value,false);'));
    }
    $s_csv = $csv_format->MakeCSVRecord($a_column_list, $aAllRawValues);
    if ($b_heading) {
        $s_head = $csv_format->MakeHeading($a_column_list);
        /*
         * return the heading and the record with $CSVLINE as record separator
         */
        return $s_head . $CSVLINE . $s_csv . $CSVLINE;
    } else {
        /*
         * return this record with $CSVLINE appended
         */
        return $s_csv . $CSVLINE;
    }
}