diff --git a/modules/Vtiger/views/Import.php b/modules/Vtiger/views/Import.php index 901b723b746e5d18657183cd428d384b13a918c7..75ec101c4d300600dfdc7c2ff22fe98591b365c8 100644 --- a/modules/Vtiger/views/Import.php +++ b/modules/Vtiger/views/Import.php @@ -136,12 +136,6 @@ class Vtiger_Import_View extends Vtiger_Index_View { $fileFormat = strtolower($fileFormat); } - // Pre-conditional check for CSV import. - if ($fileFormat == 'csv') { - $isLocalInfileEnabled = Vtiger_Util_Helper::checkDbLocalInfileSupport(); - if (!$isLocalInfileEnabled) throw new Exception(vtranslate('ERR_LOCAL_INFILE_NOT_ON', 'Import')); - } - $viewer->assign('AVAILABLE_FIELDS', $moduleMeta->getMergableFields()); $viewer->assign('ENTITY_FIELDS', $moduleMeta->getEntityFields()); $viewer->assign('ERROR_MESSAGE', $request->get('error_message')); diff --git a/packages/vtiger/mandatory/Import.zip b/packages/vtiger/mandatory/Import.zip index 7f90f790a1b6763ba33c4ad47378754250c3bef2..718c58cb67f8e96504e17992d5a62eed0b39c3ed 100644 Binary files a/packages/vtiger/mandatory/Import.zip and b/packages/vtiger/mandatory/Import.zip differ diff --git a/pkg/vtiger/modules/Import/modules/Import/readers/CSVReader.php b/pkg/vtiger/modules/Import/modules/Import/readers/CSVReader.php index 938b04281ae49d67091f772737bf796e17abc937..c843965bc86ec7b9ad62b7504b278632d21617b1 100644 --- a/pkg/vtiger/modules/Import/modules/Import/readers/CSVReader.php +++ b/pkg/vtiger/modules/Import/modules/Import/readers/CSVReader.php @@ -68,56 +68,39 @@ class Import_CSVReader_Reader extends Import_FileReader_Reader { public function read() { global $default_charset; - $filePath = $this->getFilePath(); - // if file encoded type is other than over default database charset we need to convert - if($this->request->get('file_encoding') != $default_charset) { - $data = file_get_contents($filePath); - $result = mb_convert_encoding($data,$default_charset,$this->request->get('file_encoding')); - file_put_contents($filePath, $result); - } - // to add escape slashes - $filePath = addslashes($this->getFilePath()); + $fileHandler = $this->getFileHandler(); $status = $this->createTable(); if(!$status) { return false; } $fieldMapping = $this->request->get('field_mapping'); - $fieldNames = array(); - foreach($fieldMapping as $fieldName => $index) { - $fieldNames[$index] = $fieldName; - } - $this->addRecordsToDB($filePath,$fieldNames); - } - - public function addRecordsToDB($filePath,$columnNames) { - $db = PearDatabase::getInstance(); - $tableName = Import_Utils_Helper::getDbTableName($this->user); - $delimiter = $this->request->get('delimiter'); - $encoding = $this->request->get('file_encoding'); - $withEncoding = ' CHARACTER SET ' . ($encoding == 'UTF-8' ? 'UTF8' : 'latin1'); - $query = 'LOAD DATA LOCAL INFILE "'.$filePath.'" INTO TABLE '.$tableName. $withEncoding.' FIELDS TERMINATED BY "'.$delimiter.'" OPTIONALLY ENCLOSED BY "\"" LINES TERMINATED BY "\n"'; - if($this->hasHeader()){ - $query .= " IGNORE 1 LINES "; - } - - // to ignore values from file which are not mapped - $keys = array_keys($columnNames); - $maxValue = max($keys); - for($i=0;$i<$maxValue;$i++){ - if(!$columnNames[$i]){ - $columnNames[$i] = "@ignore"; + $delimiter = $this->request->get('delimiter'); + $hasHeader = $this->request->get('has_header'); + $fileEncoding = $this->request->get('file_encoding'); + + // NOTE: Retaining row-read and insert as LOAD DATA command is being disabled by default. + $i = -1; + while($data = fgetcsv($fileHandler, 0, $delimiter)) { + $i++; + if($hasHeader && $i == 0) continue; + $mappedData = array(); + $allValuesEmpty = true; + foreach($fieldMapping as $fieldName => $index) { + $fieldValue = $data[$index]; + $mappedData[$fieldName] = $fieldValue; + if($fileEncoding != $default_charset) { + $mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $fileEncoding, $default_charset); + } + if(!empty($fieldValue)) $allValuesEmpty = false; } + if($allValuesEmpty) continue; + $fieldNames = array_keys($mappedData); + $fieldValues = array_values($mappedData); + $this->addRecordToDB($fieldNames, $fieldValues); } - ksort($columnNames); - $query .= '('.implode(',',$columnNames).')'; - - global $dbconfigoption; - $db->database = null; // we shouldn't use existing connection with client flag = 0 - $dbconfigoption['clientFlags'] = 128; // To enable LOAD DATA INFILE... query for database - $db->pquery($query,array()); - $this->setNumberOfRecordsRead($tableName,$db); + unset($fileHandler); } } ?>