Merge pull request #17707 from seamuslee001/ref_permission_clause_search
[civicrm-core.git] / CRM / Logging / Differ.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Logging_Differ {
18 private $db;
19 private $log_conn_id;
20 private $log_date;
21 private $interval;
22
23 /**
24 * Class constructor.
25 *
26 * @param string $log_conn_id
27 * @param string $log_date
28 * @param string $interval
29 */
30 public function __construct($log_conn_id, $log_date, $interval = '10 SECOND') {
31 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
32 $this->db = $dsn['database'];
33 $this->log_conn_id = $log_conn_id;
34 $this->log_date = $log_date;
35 $this->interval = self::filterInterval($interval);
36 }
37
38 /**
39 * @param $tables
40 *
41 * @return array
42 */
43 public function diffsInTables($tables) {
44 $diffs = [];
45 foreach ($tables as $table) {
46 $diff = $this->diffsInTable($table);
47 if (!empty($diff)) {
48 $diffs[$table] = $diff;
49 }
50 }
51 return $diffs;
52 }
53
54 /**
55 * @param $table
56 * @param int $contactID
57 *
58 * @return array
59 */
60 public function diffsInTable($table, $contactID = NULL) {
61 $diffs = [];
62
63 $params = [
64 1 => [$this->log_conn_id, 'String'],
65 ];
66
67 $logging = new CRM_Logging_Schema();
68 $addressCustomTables = $logging->entityCustomDataLogTables('Address');
69
70 $contactIdClause = $join = '';
71 if ($contactID) {
72 $params[3] = [$contactID, 'Integer'];
73 switch ($table) {
74 case 'civicrm_contact':
75 $contactIdClause = "AND id = %3";
76 break;
77
78 case 'civicrm_note':
79 $contactIdClause = "AND (( entity_id = %3 AND entity_table = 'civicrm_contact' ) OR (entity_id IN (SELECT note.id FROM `{$this->db}`.log_civicrm_note note WHERE note.entity_id = %3 AND note.entity_table = 'civicrm_contact') AND entity_table = 'civicrm_note'))";
80 break;
81
82 case 'civicrm_entity_tag':
83 $contactIdClause = "AND entity_id = %3 AND entity_table = 'civicrm_contact'";
84 break;
85
86 case 'civicrm_relationship':
87 $contactIdClause = "AND (contact_id_a = %3 OR contact_id_b = %3)";
88 break;
89
90 case 'civicrm_activity':
91 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
92 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
93 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
94 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
95
96 $join = "
97 LEFT JOIN civicrm_activity_contact at ON at.activity_id = lt.id AND at.contact_id = %3 AND at.record_type_id = {$targetID}
98 LEFT JOIN civicrm_activity_contact aa ON aa.activity_id = lt.id AND aa.contact_id = %3 AND aa.record_type_id = {$assigneeID}
99 LEFT JOIN civicrm_activity_contact source ON source.activity_id = lt.id AND source.contact_id = %3 AND source.record_type_id = {$sourceID} ";
100 $contactIdClause = "AND (at.id IS NOT NULL OR aa.id IS NOT NULL OR source.id IS NOT NULL)";
101 break;
102
103 case 'civicrm_case':
104 $contactIdClause = "AND id = (select case_id FROM civicrm_case_contact WHERE contact_id = %3 LIMIT 1)";
105 break;
106
107 default:
108 if (array_key_exists($table, $addressCustomTables)) {
109 $join = "INNER JOIN `{$this->db}`.`log_civicrm_address` et ON et.id = lt.entity_id";
110 $contactIdClause = "AND contact_id = %3";
111 break;
112 }
113
114 // allow tables to be extended by report hook query objects
115 list($contactIdClause, $join) = CRM_Report_BAO_Hook::singleton()->logDiffClause($this, $table);
116
117 if (empty($contactIdClause)) {
118 $contactIdClause = "AND contact_id = %3";
119 }
120 if (strpos($table, 'civicrm_value') !== FALSE) {
121 $contactIdClause = "AND entity_id = %3";
122 }
123 }
124 }
125
126 $logDateClause = '';
127 if ($this->log_date) {
128 $params[2] = [$this->log_date, 'String'];
129 $logDateClause = "
130 AND lt.log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND DATE_ADD(%2, INTERVAL {$this->interval})
131 ";
132 }
133
134 // find ids in this table that were affected in the given connection (based on connection id and a ±10 s time period around the date)
135 $sql = "
136 SELECT DISTINCT lt.id FROM `{$this->db}`.`log_$table` lt
137 {$join}
138 WHERE lt.log_conn_id = %1
139 $logDateClause
140 {$contactIdClause}";
141 $dao = CRM_Core_DAO::executeQuery($sql, $params);
142 while ($dao->fetch()) {
143 $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id));
144 }
145
146 return $diffs;
147 }
148
149 /**
150 * @param $table
151 * @param int $id
152 *
153 * @return array
154 * @throws \CRM_Core_Exception
155 */
156 private function diffsInTableForId($table, $id) {
157 $diffs = [];
158
159 $params = [
160 1 => [$this->log_conn_id, 'String'],
161 3 => [$id, 'Integer'],
162 ];
163
164 // look for all the changes in the given connection that happened less than {$this->interval} s later than log_date to the given id to catch multi-query changes
165 $logDateClause = "";
166 if ($this->log_date && $this->interval) {
167 $logDateClause = " AND log_date >= %2 AND log_date < DATE_ADD(%2, INTERVAL {$this->interval})";
168 $params[2] = [$this->log_date, 'String'];
169 }
170
171 $changedSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id = %1 $logDateClause AND id = %3 ORDER BY log_date DESC LIMIT 1";
172
173 $changedDAO = CRM_Core_DAO::executeQuery($changedSQL, $params);
174 while ($changedDAO->fetch()) {
175 if (empty($this->log_date) && !self::checkLogCanBeUsedWithNoLogDate($changedDAO->log_date)) {
176 throw new CRM_Core_Exception('The connection date must be passed in to disambiguate this logging entry per CRM-18193');
177 }
178 $changed = $changedDAO->toArray();
179
180 // return early if nothing found
181 if (empty($changed)) {
182 continue;
183 }
184
185 switch ($changed['log_action']) {
186 case 'Delete':
187 // the previous state is kept in the current state, current should keep the keys and clear the values
188 $original = $changed;
189 foreach ($changed as & $val) {
190 $val = NULL;
191 }
192 $changed['log_action'] = 'Delete';
193 break;
194
195 case 'Insert':
196 // the previous state does not exist
197 $original = [];
198 break;
199
200 case 'Update':
201 $params[2] = [$changedDAO->log_date, 'String'];
202 // look for the previous state (different log_conn_id) of the given id
203 $originalSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id != %1 AND log_date < %2 AND id = %3 ORDER BY log_date DESC LIMIT 1";
204 $original = $this->sqlToArray($originalSQL, $params);
205 if (empty($original)) {
206 // A blank original array is not possible for Update action, otherwise we 'll end up displaying all information
207 // in $changed variable as updated-info
208 $original = $changed;
209 }
210
211 break;
212 }
213
214 // populate $diffs with only the differences between $changed and $original
215 $skipped = ['log_action', 'log_conn_id', 'log_date', 'log_user_id'];
216 foreach (array_keys(array_diff_assoc($changed, $original)) as $diff) {
217 if (in_array($diff, $skipped)) {
218 continue;
219 }
220
221 if (CRM_Utils_Array::value($diff, $original) === CRM_Utils_Array::value($diff, $changed)) {
222 continue;
223 }
224
225 // hack: case_type_id column is a varchar with separator. For proper mapping to type labels,
226 // we need to make sure separators are trimmed
227 if ($diff == 'case_type_id') {
228 foreach (['original', 'changed'] as $var) {
229 if (!empty(${$var[$diff]})) {
230 $holder =& $$var;
231 $val = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, $holder[$diff]);
232 $holder[$diff] = $val[1] ?? NULL;
233 }
234 }
235 }
236
237 $diffs[] = [
238 'action' => $changed['log_action'],
239 'id' => $id,
240 'field' => $diff,
241 'from' => $original[$diff] ?? NULL,
242 'to' => $changed[$diff] ?? NULL,
243 'table' => $table,
244 'log_date' => $changed['log_date'],
245 'log_conn_id' => $changed['log_conn_id'],
246 ];
247 }
248 }
249
250 return $diffs;
251 }
252
253 /**
254 * Get the titles & metadata option values for the table.
255 *
256 * For custom fields the titles may change so we use the ones as at the reference date.
257 *
258 * @param string $table
259 * @param string $referenceDate
260 *
261 * @return array
262 */
263 public function titlesAndValuesForTable($table, $referenceDate) {
264 // static caches for subsequent calls with the same $table
265 static $titles = [];
266 static $values = [];
267
268 if (!isset($titles[$table]) or !isset($values[$table])) {
269 if (($tableDAO = CRM_Core_DAO_AllCoreTables::getClassForTable($table)) != FALSE) {
270 // FIXME: these should be populated with pseudo constants as they
271 // were at the time of logging rather than their current values
272 // FIXME: Use *_BAO:buildOptions() method rather than pseudoconstants & fetch programmatically
273 $values[$table] = [
274 'contribution_page_id' => CRM_Contribute_PseudoConstant::contributionPage(),
275 'contribution_status_id' => CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'),
276 'financial_type_id' => CRM_Contribute_PseudoConstant::financialType(),
277 'country_id' => CRM_Core_PseudoConstant::country(),
278 'gender_id' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
279 'location_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'),
280 'payment_instrument_id' => CRM_Contribute_PseudoConstant::paymentInstrument(),
281 'phone_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'),
282 'preferred_communication_method' => CRM_Contact_BAO_Contact::buildOptions('preferred_communication_method'),
283 'preferred_language' => CRM_Contact_BAO_Contact::buildOptions('preferred_language'),
284 'prefix_id' => CRM_Contact_BAO_Contact::buildOptions('prefix_id'),
285 'provider_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'),
286 'state_province_id' => CRM_Core_PseudoConstant::stateProvince(),
287 'suffix_id' => CRM_Contact_BAO_Contact::buildOptions('suffix_id'),
288 'website_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'),
289 'activity_type_id' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
290 'case_type_id' => CRM_Case_PseudoConstant::caseType('title', FALSE),
291 'priority_id' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'),
292 ];
293
294 // for columns that appear in more than 1 table
295 switch ($table) {
296 case 'civicrm_case':
297 $values[$table]['status_id'] = CRM_Case_PseudoConstant::caseStatus('label', FALSE);
298 break;
299
300 case 'civicrm_activity':
301 $values[$table]['status_id'] = CRM_Core_PseudoConstant::activityStatus();
302 break;
303 }
304
305 $dao = new $tableDAO();
306 foreach ($dao->fields() as $field) {
307 $titles[$table][$field['name']] = $field['title'] ?? NULL;
308
309 if ($field['type'] == CRM_Utils_Type::T_BOOLEAN) {
310 $values[$table][$field['name']] = ['0' => ts('false'), '1' => ts('true')];
311 }
312 }
313 }
314 elseif (substr($table, 0, 14) == 'civicrm_value_') {
315 list($titles[$table], $values[$table]) = $this->titlesAndValuesForCustomDataTable($table, $referenceDate);
316 }
317 else {
318 $titles[$table] = $values[$table] = [];
319 }
320 }
321
322 return [$titles[$table], $values[$table]];
323 }
324
325 /**
326 * @param $sql
327 * @param array $params
328 *
329 * @return mixed
330 */
331 private function sqlToArray($sql, $params) {
332 $dao = CRM_Core_DAO::executeQuery($sql, $params);
333 $dao->fetch();
334 return $dao->toArray();
335 }
336
337 /**
338 * Get the field titles & option group values for the custom table as at the reference date.
339 *
340 * @param string $table
341 * @param string $referenceDate
342 *
343 * @return array
344 */
345 private function titlesAndValuesForCustomDataTable($table, $referenceDate) {
346 $titles = [];
347 $values = [];
348
349 $params = [
350 1 => [$this->log_conn_id, 'String'],
351 2 => [$referenceDate, 'String'],
352 3 => [$table, 'String'],
353 ];
354
355 $sql = "SELECT id, title FROM `{$this->db}`.log_civicrm_custom_group WHERE log_date <= %2 AND table_name = %3 ORDER BY log_date DESC LIMIT 1";
356 $cgDao = CRM_Core_DAO::executeQuery($sql, $params);
357 $cgDao->fetch();
358
359 $params[3] = [$cgDao->id, 'Integer'];
360 $sql = "
361 SELECT column_name, data_type, label, name, option_group_id
362 FROM `{$this->db}`.log_civicrm_custom_field
363 WHERE log_date <= %2
364 AND custom_group_id = %3
365 ORDER BY log_date
366 ";
367 $cfDao = CRM_Core_DAO::executeQuery($sql, $params);
368
369 while ($cfDao->fetch()) {
370 $titles[$cfDao->column_name] = "{$cgDao->title}: {$cfDao->label}";
371
372 switch ($cfDao->data_type) {
373 case 'Boolean':
374 $values[$cfDao->column_name] = ['0' => ts('false'), '1' => ts('true')];
375 break;
376
377 case 'String':
378 $values[$cfDao->column_name] = [];
379 if (!empty($cfDao->option_group_id)) {
380 $params[3] = [$cfDao->option_group_id, 'Integer'];
381 $sql = "
382 SELECT label, value
383 FROM `{$this->db}`.log_civicrm_option_value
384 WHERE log_date <= %2
385 AND option_group_id = %3
386 ORDER BY log_date
387 ";
388 $ovDao = CRM_Core_DAO::executeQuery($sql, $params);
389 while ($ovDao->fetch()) {
390 $values[$cfDao->column_name][$ovDao->value] = $ovDao->label;
391 }
392 }
393 break;
394 }
395 }
396
397 return [$titles, $values];
398 }
399
400 /**
401 * Get all changes made in the connection.
402 *
403 * @param array $tables
404 * Array of tables to inspect.
405 *
406 * @return array
407 */
408 public function getAllChangesForConnection($tables) {
409 $params = [1 => [$this->log_conn_id, 'String']];
410 foreach ($tables as $table) {
411 if (empty($sql)) {
412 $sql = " SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1";
413 }
414 else {
415 $sql .= " UNION SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1";
416 }
417 }
418 $diffs = [];
419 $dao = CRM_Core_DAO::executeQuery($sql, $params);
420 while ($dao->fetch()) {
421 if (empty($this->log_date)) {
422 $this->log_date = CRM_Core_DAO::singleValueQuery("SELECT log_date FROM {$this->db}.log_{$table} WHERE log_conn_id = %1 LIMIT 1", $params);
423 }
424 $diffs = array_merge($diffs, $this->diffsInTableForId($dao->table_name, $dao->id));
425 }
426 return $diffs;
427 }
428
429 /**
430 * Check that the log record relates to a unique log id.
431 *
432 * If the record was recorded using the old non-unique style then the
433 * log_date
434 * MUST be set to get the (fairly accurate) list of changes. In this case the
435 * nasty 10 second interval rule is applied.
436 *
437 * See CRM-18193 for a discussion of unique log id.
438 *
439 * @param string $change_date
440 *
441 * @return bool
442 * @throws \CiviCRM_API3_Exception
443 */
444 public static function checkLogCanBeUsedWithNoLogDate($change_date) {
445
446 if (civicrm_api3('Setting', 'getvalue', ['name' => 'logging_all_tables_uniquid', 'group' => 'CiviCRM Preferences'])) {
447 return TRUE;
448 };
449 $uniqueDate = civicrm_api3('Setting', 'getvalue', [
450 'name' => 'logging_uniqueid_date',
451 'group' => 'CiviCRM Preferences',
452 ]);
453 if (strtotime($uniqueDate) <= strtotime($change_date)) {
454 return TRUE;
455 }
456 else {
457 return FALSE;
458 }
459
460 }
461
462 /**
463 * Filter a MySQL interval expression.
464 *
465 * @param string $interval
466 * @return string
467 * Normalized version of $interval
468 * @throws \CRM_Core_Exception
469 * If the expression is invalid.
470 * @see https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
471 */
472 private static function filterInterval($interval) {
473 if (empty($interval)) {
474 return $interval;
475 }
476
477 $units = ['MICROSECOND', 'SECOND', 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR'];
478 $interval = strtoupper($interval);
479 if (preg_match('/^([0-9]+) ([A-Z]+)$/', $interval, $matches)) {
480 if (in_array($matches[2], $units)) {
481 return $interval;
482 }
483 }
484 if (preg_match('/^\'([0-9: \.\-]+)\' ([A-Z]+)_([A-Z]+)$/', $interval, $matches)) {
485 if (in_array($matches[2], $units) && in_array($matches[3], $units)) {
486 return $interval;
487 }
488 }
489 throw new CRM_Core_Exception("Malformed interval");
490 }
491
492 }