Merge pull request #23275 from eileenmcnaughton/error
[civicrm-core.git] / CRM / Import / DataSource / SQL.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Import_DataSource_SQL extends CRM_Import_DataSource {
18
7b057b66
EM
19 /**
20 * Form fields declared for this datasource.
21 *
22 * @var string[]
23 */
24 protected $submittableFields = ['sqlQuery'];
25
e0ef6999 26 /**
fe482240 27 * Provides information about the data source.
e0ef6999 28 *
a6c01b45
CW
29 * @return array
30 * collection of info about this data source
e0ef6999 31 */
39dc35d4 32 public function getInfo(): array {
be2fb01f 33 return [
11749569 34 'title' => ts('SQL Query'),
be2fb01f
CW
35 'permissions' => ['import SQL datasource'],
36 ];
6a488035
TO
37 }
38
e0ef6999 39 /**
fe482240 40 * Set variables up before form is built.
67f947ac
EM
41 *
42 * @param CRM_Core_Form $form
e0ef6999 43 */
3a05d67e
TO
44 public function preProcess(&$form) {
45 }
6a488035 46
e0ef6999
EM
47 /**
48 * This is function is called by the form object to get the DataSource's
49 * form snippet. It should add all fields necesarry to get the data
50 * uploaded to the temporary table in the DB.
51 *
c490a46a 52 * @param CRM_Core_Form $form
e0ef6999 53 *
a6c01b45
CW
54 * @return void
55 * (operates directly on form argument)
e0ef6999 56 */
6a488035
TO
57 public function buildQuickForm(&$form) {
58 $form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_SQL');
4367e964 59 $form->add('textarea', 'sqlQuery', ts('Specify SQL Query'), ['rows' => 10, 'cols' => 45], TRUE);
be2fb01f 60 $form->addFormRule(['CRM_Import_DataSource_SQL', 'formRule'], $form);
6a488035
TO
61 }
62
e0ef6999
EM
63 /**
64 * @param $fields
65 * @param $files
c490a46a 66 * @param CRM_Core_Form $form
e0ef6999
EM
67 *
68 * @return array|bool
69 */
00be9182 70 public static function formRule($fields, $files, $form) {
be2fb01f 71 $errors = [];
6a488035 72
e047612e 73 // Makeshift query validation (case-insensitive regex matching on word boundaries)
4a01628c 74 $forbidden = ['ALTER', 'CREATE', 'DELETE', 'DESCRIBE', 'DROP', 'SHOW', 'UPDATE', 'REPLACE', 'information_schema'];
6a488035
TO
75 foreach ($forbidden as $pattern) {
76 if (preg_match("/\\b$pattern\\b/i", $fields['sqlQuery'])) {
be2fb01f 77 $errors['sqlQuery'] = ts('The query contains the forbidden %1 command.', [1 => $pattern]);
6a488035
TO
78 }
79 }
80
4a01628c 81 return $errors ?: TRUE;
6a488035
TO
82 }
83
e0ef6999 84 /**
fe482240 85 * Process the form submission.
54957108 86 *
87 * @param array $params
88 * @param string $db
89 * @param \CRM_Core_Form $form
0a66a182 90 *
4a01628c 91 * @throws \API_Exception
0a66a182 92 * @throws \CRM_Core_Exception
4a01628c 93 * @throws \Civi\API\Exception\UnauthorizedException
e0ef6999 94 */
6a488035 95 public function postProcess(&$params, &$db, &$form) {
719a6fec 96 $importJob = new CRM_Contact_Import_ImportJob(
481a74f4 97 CRM_Utils_Array::value('import_table_name', $params),
3a05d67e 98 $params['sqlQuery'], TRUE
6a488035
TO
99 );
100
101 $form->set('importTableName', $importJob->getTableName());
4a01628c
EM
102 // Get the names of the fields to be imported. Any fields starting with an
103 // underscore are considered to be internal to the import process)
104 $columnsResult = CRM_Core_DAO::executeQuery(
105 'SHOW FIELDS FROM ' . $importJob->getTableName() . "
106 WHERE Field NOT LIKE '\_%'");
107
108 $columnNames = [];
109 while ($columnsResult->fetch()) {
110 $columnNames[] = $columnsResult->Field;
111 }
112 $this->updateUserJobMetadata('DataSource', [
7b057b66 113 'table_name' => $importJob->getTableName(),
4a01628c
EM
114 'column_headers' => $columnNames,
115 'number_of_columns' => count($columnNames),
116 ]);
6a488035 117 }
96025800 118
6a488035 119}