function _Format($s_value, $s_format = "")
 {
     $s_value = $this->_Escape($s_value);
     $s_prefix = "";
     /*
      * now implement any special formatting to overcome
      * problems with importing
      */
     $i_len = strlen($s_format);
     for ($ii = 0; $ii < $i_len; $ii++) {
         switch ($s_format[$ii]) {
             case "c":
                 /*
                  * implement "c" formatting - CleanValue
                  */
                 $s_value = CleanValue($s_value);
                 break;
             case "r":
                 /*
                  * implement "r" formatting - remove
                  * carriage returns.  Useful for Microsoft Excel
                  */
                 $s_value = str_replace("\r", "", $s_value);
                 break;
             case "s":
                 /*
                  * implement "s" formatting - force
                  * a value to be a string (by making it a string
                  * formula).  Useful for Microsoft Excel and OpenOffice
                  * spreadsheet, which don't understand numeric phone
                  * numbers, for example.
                  */
                 if (strlen($s_value) > 0) {
                     $s_prefix = "=";
                 }
                 break;
         }
     }
     return $s_prefix . $this->_cQuote . $s_value . $this->_cQuote;
 }
Exemple #2
0
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
    //
    if (filesize($s_csv_file) == 0) {
        for ($ii = 0; $ii < $n_columns; $ii++) {
            fwrite($fp, $CSVQUOTE . $a_column_list[$ii] . $CSVQUOTE);
            if ($ii < $n_columns - 1) {
                fwrite($fp, "{$CSVSEP}");
            }
        }
        fwrite($fp, $CSVLINE);
    }
    //	$debug = "";
    //	$debug .= "gpc -> ".get_magic_quotes_gpc()."\n";
    //	$debug .= "runtime -> ".get_magic_quotes_runtime()."\n";
    for ($ii = 0; $ii < $n_columns; $ii++) {
        $s_col_name = $a_column_list[$ii];
        //
        // columns can be missing from some form submission and present
        // from others
        //
        if (isset($a_vars[$s_col_name])) {
            $m_value = $a_vars[$s_col_name];
        } else {
            $m_value = "";
        }
        if (LIMITED_IMPORT) {
            //
            // the target database doesn't understand escapes, so
            // remove various things, including newlines and truncate
            //
            $m_value = CleanValue($m_value, false);
        } else {
            //
            // the target database does understand escapes, so
            // we have to slash any slashes
            //
            $m_value = str_replace("\\", "\\\\", $m_value);
        }
        //
        // convert quotes, depending on the setting of $CSVQUOTE
        //
        switch ($CSVQUOTE) {
            case '"':
                //
                // convert double quotes in the data to single quotes
                //
                $m_value = str_replace("\"", "'", $m_value);
                break;
            case '\'':
                //
                // convert single quotes in the data to double quotes
                //
                $m_value = str_replace("'", "\"", $m_value);
                break;
            default:
                //
                // otherwise, leave the data unchanged
                //
                break;
        }
        //
        // we handle arrays and strings
        //
        if (is_array($m_value)) {
            //
            // separate the values with the internal field separator
            //
            $m_value = implode("{$CSVINTSEP}", $m_value);
        }
        //		$debug .= $a_column_list[$ii]." => ".$m_value."\n";
        fwrite($fp, $CSVQUOTE . $m_value . $CSVQUOTE);
        if ($ii < $n_columns - 1) {
            fwrite($fp, "{$CSVSEP}");
        }
    }
    fwrite($fp, $CSVLINE);
    fclose($fp);
    //	CreatePage($debug);
    //	exit;
}