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