-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,466 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 2.2 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2009 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2009. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License along with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC (c) 2004-2009 | ||
* $Id$ | ||
* | ||
*/ | ||
|
||
require_once 'CRM/Core/Form.php'; | ||
#require_once 'CRM/Import/Parser/Contact.php'; | ||
|
||
/** | ||
* This class defines the DataSource interface but must be subclassed to be | ||
* useful. | ||
*/ | ||
abstract class CRM_Import_DataSource { | ||
|
||
/** | ||
* Provides information about the data source | ||
* | ||
* @return array collection of info about this data source | ||
* | ||
* @access public | ||
* | ||
*/ | ||
abstract public function getInfo(); | ||
|
||
/** | ||
* Function to set variables up before form is built | ||
* | ||
* @access public | ||
*/ | ||
abstract public function preProcess( &$form ); | ||
|
||
/** | ||
* This is function is called by the form object to get the DataSource's | ||
* form snippet. It should add all fields necesarry to get the data | ||
* uploaded to the temporary table in the DB. | ||
* | ||
* @return None (operates directly on form argument) | ||
* @access public | ||
*/ | ||
abstract public function buildQuickForm( &$form ); | ||
|
||
/** | ||
* Function to process the form | ||
* | ||
* @access public | ||
*/ | ||
abstract public function postProcess( &$params, &$db ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 2.2 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2009 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2009. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License along with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC (c) 2004-2009 | ||
* $Id$ | ||
* | ||
*/ | ||
|
||
require_once 'CRM/Import/DataSource.php'; | ||
|
||
class CRM_Import_DataSource_CSV extends CRM_Import_DataSource | ||
{ | ||
function getInfo() | ||
{ | ||
return array('title' => 'CSV Import'); | ||
} | ||
|
||
function preProcess(&$form) | ||
{ | ||
} | ||
|
||
function buildQuickForm(&$form) | ||
{ | ||
$form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV'); | ||
|
||
$config =& CRM_Core_Config::singleton(); | ||
|
||
// FIXME: why do we limit the file size to 8 MiB if it's larger in config? | ||
$uploadFileSize = $config->maxImportFileSize >= 8388608 ? 8388608 : $config->maxImportFileSize; | ||
$uploadSize = round(($uploadFileSize / (1024*1024)), 2); | ||
$form->assign('uploadSize', $uploadSize); | ||
$form->add('file', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=60', true); | ||
|
||
$form->setMaxFileSize($uploadFileSize); | ||
$form->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize); | ||
$form->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File'); | ||
$form->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile'); | ||
|
||
$form->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers')); | ||
} | ||
|
||
function postProcess(&$params, &$db) | ||
{ | ||
$file = $params['uploadFile']['name']; | ||
|
||
$table = self::_CsvToTable($db, $file, $params['skipColumnHeader']); | ||
|
||
require_once 'CRM/Import/ImportJob.php'; | ||
$importJob = new CRM_Import_ImportJob($table); | ||
$this->set('importTableName', $importJob->getTableName()); | ||
} | ||
|
||
/** | ||
* Create a table that matches the CSV file and populate it with the file's contents | ||
* | ||
* @param object $db handle to the database connection | ||
* @param string $file file name to load | ||
* @param bool $headers whether the first row contains headers | ||
* | ||
* @return string name of the created table | ||
*/ | ||
private static function _CsvToTable(&$db, $file, $headers = false) | ||
{ | ||
$fd = fopen($file, 'r'); | ||
if (!$fd) CRM_Core_Error::fatal("Could not read $file"); | ||
|
||
$config =& CRM_Core_Config::singleton(); | ||
$firstrow = fgetcsv($fd, 0, $config->fieldSeparator); | ||
|
||
// create the column names from the CSV header or as col_0, col_1, etc. | ||
if ($headers) { | ||
$columns = array_map('strtolower', $firstrow); | ||
$columns = str_replace(' ', '_', $columns); | ||
$columns = preg_replace('/[^a-z_]/', '', $columns); | ||
} else { | ||
$columns = array(); | ||
foreach ($firstrow as $i => $_) $columns[] = "col_$i"; | ||
} | ||
|
||
// FIXME: we should regen this table's name if it exists rather than drop it | ||
$table = 'civicrm_import_job_' . md5(uniqid(rand(), true)); | ||
$db->query("DROP TABLE IF EXISTS $table"); | ||
|
||
$create = "CREATE TABLE $table (" . implode(' text, ', $columns) . " text)"; | ||
$db->query($create); | ||
|
||
// the proper approach, but some MySQL installs do not have this enabled | ||
// $load = "LOAD DATA LOCAL INFILE '$file' INTO TABLE $table FIELDS TERMINATED BY '$config->fieldSeparator' OPTIONALLY ENCLOSED BY '\"'"; | ||
// if ($headers) $load .= ' IGNORE 1 LINES'; | ||
// $db->query($load); | ||
|
||
// parse the CSV line by line and build one big INSERT (while MySQL-escaping the CSV contents) | ||
if (!$headers) rewind($fd); | ||
$sql = "INSERT IGNORE INTO $table VALUES "; | ||
$first = true; | ||
while ($row = fgetcsv($fd, 0, $config->fieldSeparator)) { | ||
if (!$first) $sql .= ', '; | ||
$first = false; | ||
$row = array_map('mysql_real_escape_string', $row); | ||
$sql .= "('" . implode("', '", $row) . "')"; | ||
} | ||
$db->query($sql); | ||
|
||
fclose($fd); | ||
|
||
return $table; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 2.2 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2009 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2007. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License along with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC (c) 2004-2009 | ||
* $Id$ | ||
* | ||
*/ | ||
|
||
require_once 'CRM/Import/DataSource.php'; | ||
|
||
class CRM_Import_DataSource_SQL extends CRM_Import_DataSource | ||
{ | ||
|
||
public function getInfo() | ||
{ | ||
return array('title' => 'SQL Import'); | ||
} | ||
|
||
public function preProcess(&$form) | ||
{ | ||
} | ||
|
||
public function buildQuickForm(&$form) | ||
{ | ||
$form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_SQL'); | ||
$form->add('textarea', 'sqlQuery', ts('Specify SQL Query'), 'rows=10 cols=45'); | ||
$form->addFormRule(array('CRM_Import_DataSource_SQL', 'formRule'), $form); | ||
} | ||
|
||
static function formRule(&$fields, &$files, &$form) | ||
{ | ||
$errors = array(); | ||
|
||
// poor man's query validation (case-insensitive regex matching on word boundaries) | ||
$forbidden = array('ALTER', 'CREATE', 'DELETE', 'DESCRIBE', 'DROP', 'SHOW', 'UPDATE', 'information_schema'); | ||
foreach ($forbidden as $pattern) { | ||
if (preg_match("/\\b$pattern\\b/i", $fields['sqlQuery'])) { | ||
$errors['sqlQuery'] = ts("The query contains the forbidden $pattern command."); | ||
} | ||
} | ||
|
||
return $errors ? $errors : true; | ||
} | ||
|
||
|
||
public function postProcess(&$params, &$db) | ||
{ | ||
require_once 'CRM/Import/ImportJob.php'; | ||
$importJob = new CRM_Import_ImportJob(null, $params['sqlQuery']); | ||
$this->set('importTableName', $importJob->getTableName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.