示例#1
0
 /**
  * Return complete file within an array, one item per line
  * @param string filename name of file
  * @return mixed contents array or false on failure
  */
 protected function readdata($filename)
 {
     if (is_readable($filename)) {
         $filearray = file($filename);
         // If the first line of the file starts with a UTF-8 BOM, remove it.
         $filearray[0] = core_text::trim_utf8_bom($filearray[0]);
         // Check for Macintosh OS line returns (ie file on one line), and fix.
         if (preg_match("~\r~", $filearray[0]) and !preg_match("~\n~", $filearray[0])) {
             return explode("\r", $filearray[0]);
         } else {
             return $filearray;
         }
     }
     return false;
 }
示例#2
0
/**
 * Check a CSV input line format for empty or commented lines
 * Ensures compatbility to UTF-8 BOM or unBOM formats
 */
function vmoodle_is_empty_line_or_format(&$text, $resetfirst = false)
{
    global $CFG;
    vmoodle_setup_local_config();
    static $first = true;
    // We may have a risk the BOM is present on first line.
    if ($resetfirst) {
        $first = true;
    }
    if ($first && $CFG->local_vmoodle_encoding == 'UTF-8') {
        $text = core_text::trim_utf8_bom($text);
        $first = false;
    }
    $text = preg_replace("/\n?\r?/", '', $text);
    if ($CFG->local_vmoodle_encoding != 'UTF-8') {
        $text = utf8_encode($text);
    }
    // last chance
    if ('ASCII' == mb_detect_encoding($text)) {
        $text = utf8_encode($text);
    }
    // Check the text is empty or comment line and answer true if it is
    return preg_match('/^$/', $text) || preg_match('/^(\\(|\\[|-|#|\\/| )/', $text);
}
示例#3
0
 /**
  * Tests the static trim_utf8_bom method.
  */
 public function test_trim_utf8_bom()
 {
     $bom = "";
     $str = "Žluťoučký koníček";
     $this->assertSame($str . $bom, core_text::trim_utf8_bom($bom . $str . $bom));
 }
示例#4
0
 /**
  * Parse this content
  *
  * @global object
  * @global object
  * @param string $content passed by ref for memory reasons, unset after return
  * @param string $encoding content encoding
  * @param string $delimiter_name separator (comma, semicolon, colon, cfg)
  * @param string $column_validation name of function for columns validation, must have one param $columns
  * @param string $enclosure field wrapper. One character only.
  * @return bool false if error, count of data lines if ok; use get_error() to get error string
  */
 function load_csv_content(&$content, $encoding, $delimiter_name, $column_validation = null, $enclosure = '"')
 {
     global $USER, $CFG;
     $this->close();
     $this->_error = null;
     $content = core_text::convert($content, $encoding, 'utf-8');
     // remove Unicode BOM from first line
     $content = core_text::trim_utf8_bom($content);
     // Fix mac/dos newlines
     $content = preg_replace('!\\r\\n?!', "\n", $content);
     // Remove any spaces or new lines at the end of the file.
     if ($delimiter_name == 'tab') {
         // trim() by default removes tabs from the end of content which is undesirable in a tab separated file.
         $content = trim($content, chr(0x20) . chr(0xa) . chr(0xd) . chr(0x0) . chr(0xb));
     } else {
         $content = trim($content);
     }
     $csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
     // $csv_encode    = csv_import_reader::get_encoded_delimiter($delimiter_name);
     // Create a temporary file and store the csv file there,
     // do not try using fgetcsv() because there is nothing
     // to split rows properly - fgetcsv() itself can not do it.
     $tempfile = tempnam(make_temp_directory('/csvimport'), 'tmp');
     if (!($fp = fopen($tempfile, 'w+b'))) {
         $this->_error = get_string('cannotsavedata', 'error');
         @unlink($tempfile);
         return false;
     }
     fwrite($fp, $content);
     fseek($fp, 0);
     // Create an array to store the imported data for error checking.
     $columns = array();
     // str_getcsv doesn't iterate through the csv data properly. It has
     // problems with line returns.
     while ($fgetdata = fgetcsv($fp, 0, $csv_delimiter, $enclosure)) {
         // Check to see if we have an empty line.
         if (count($fgetdata) == 1) {
             if ($fgetdata[0] !== null) {
                 // The element has data. Add it to the array.
                 $columns[] = $fgetdata;
             }
         } else {
             $columns[] = $fgetdata;
         }
     }
     $col_count = 0;
     // process header - list of columns
     if (!isset($columns[0])) {
         $this->_error = get_string('csvemptyfile', 'error');
         fclose($fp);
         unlink($tempfile);
         return false;
     } else {
         $col_count = count($columns[0]);
     }
     // Column validation.
     if ($column_validation) {
         $result = $column_validation($columns[0]);
         if ($result !== true) {
             $this->_error = $result;
             fclose($fp);
             unlink($tempfile);
             return false;
         }
     }
     $this->_columns = $columns[0];
     // cached columns
     // check to make sure that the data columns match up with the headers.
     foreach ($columns as $rowdata) {
         if (count($rowdata) !== $col_count) {
             $this->_error = get_string('csvweirdcolumns', 'error');
             fclose($fp);
             unlink($tempfile);
             $this->cleanup();
             return false;
         }
     }
     $filename = $CFG->tempdir . '/csvimport/' . $this->_type . '/' . $USER->id . '/' . $this->_iid;
     $filepointer = fopen($filename, "w");
     // The information has been stored in csv format, as serialized data has issues
     // with special characters and line returns.
     $storedata = csv_export_writer::print_array($columns, ',', '"', true);
     fwrite($filepointer, $storedata);
     fclose($fp);
     unlink($tempfile);
     fclose($filepointer);
     $datacount = count($columns);
     return $datacount;
 }