contrib modules security updates
This commit is contained in:
@@ -66,6 +66,8 @@ class ParserCSVIterator implements Iterator {
|
||||
*/
|
||||
class ParserCSV {
|
||||
private $delimiter;
|
||||
private $fromEncoding;
|
||||
private $toEncoding;
|
||||
private $skipFirstLine;
|
||||
private $columnNames;
|
||||
private $timeout;
|
||||
@@ -73,9 +75,12 @@ class ParserCSV {
|
||||
private $startByte;
|
||||
private $lineLimit;
|
||||
private $lastLinePos;
|
||||
private $useMbString;
|
||||
|
||||
public function __construct() {
|
||||
$this->delimiter = ',';
|
||||
$this->fromEncoding = 'UTF-8';
|
||||
$this->toEncoding = 'UTF-8';
|
||||
$this->skipFirstLine = FALSE;
|
||||
$this->columnNames = FALSE;
|
||||
$this->timeout = FALSE;
|
||||
@@ -83,6 +88,10 @@ class ParserCSV {
|
||||
$this->startByte = 0;
|
||||
$this->lineLimit = 0;
|
||||
$this->lastLinePos = 0;
|
||||
ini_set('auto_detect_line_endings', TRUE);
|
||||
if (extension_loaded('mbstring') && variable_get('feeds_use_mbstring', TRUE)) {
|
||||
$this->useMbString = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,6 +102,18 @@ class ParserCSV {
|
||||
$this->delimiter = $delimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the source file encoding.
|
||||
*
|
||||
* By default, the encoding is UTF-8.
|
||||
*
|
||||
* @param string $encoding
|
||||
* The encoding to set.
|
||||
*/
|
||||
public function setEncoding($encoding) {
|
||||
$this->fromEncoding = $encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set this to TRUE if the parser should skip the first line of the CSV text,
|
||||
* which might be desired if the first line contains the column names.
|
||||
@@ -196,7 +217,7 @@ class ParserCSV {
|
||||
for ($lineIterator->rewind($this->startByte); $lineIterator->valid(); $lineIterator->next()) {
|
||||
|
||||
// Make really sure we've got lines without trailing newlines.
|
||||
$line = trim($lineIterator->current(), "\r\n");
|
||||
$line = trim($this->fixEncoding($lineIterator->current()), "\r\n");
|
||||
|
||||
// Skip empty lines.
|
||||
if (empty($line)) {
|
||||
@@ -236,7 +257,7 @@ class ParserCSV {
|
||||
}
|
||||
// Ok, so, on with fetching the next line, as mentioned above.
|
||||
$currentField .= "\n";
|
||||
$line = trim($lineIterator->current(), "\r\n");
|
||||
$line = trim($this->fixEncoding($lineIterator->current()), "\r\n");
|
||||
$currentIndex = 0;
|
||||
continue;
|
||||
}
|
||||
@@ -324,4 +345,38 @@ class ParserCSV {
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts encoding of input data.
|
||||
*
|
||||
* @param string $data
|
||||
* A chunk of data.
|
||||
*
|
||||
* @return string
|
||||
* The encoded data.
|
||||
*
|
||||
* @throws ParserCSVEncodingException
|
||||
* Thrown when a given encoding does not match.
|
||||
*/
|
||||
public function fixEncoding($data) {
|
||||
if ($this->useMbString) {
|
||||
if (mb_check_encoding($data, $this->fromEncoding)) {
|
||||
if ($this->toEncoding != $this->fromEncoding) {
|
||||
// Convert encoding. The conversion is to UTF-8 by default to prevent
|
||||
// SQL errors.
|
||||
$data = mb_convert_encoding($data, $this->toEncoding, $this->fromEncoding);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new ParserCSVEncodingException(t('Source file is not in %encoding encoding.', array('%encoding' => $this->fromEncoding)));
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception thrown when an encoding error occurs during parsing.
|
||||
*/
|
||||
class ParserCSVEncodingException extends Exception {}
|
||||
|
||||
Reference in New Issue
Block a user