| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
| 32 | */ |
| 33 | class CRM_Logging_Reverter { |
| 34 | private $db; |
| 35 | private $log_conn_id; |
| 36 | private $log_date; |
| 37 | |
| 38 | /** |
| 39 | * The diffs to be reverted. |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | private $diffs = array(); |
| 44 | |
| 45 | /** |
| 46 | * Class constructor. |
| 47 | * |
| 48 | * @param string $log_conn_id |
| 49 | * @param $log_date |
| 50 | */ |
| 51 | public function __construct($log_conn_id, $log_date) { |
| 52 | $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN); |
| 53 | $this->db = $dsn['database']; |
| 54 | $this->log_conn_id = $log_conn_id; |
| 55 | $this->log_date = $log_date; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * |
| 60 | * Calculate a set of diffs based on the connection_id and changes at a close time. |
| 61 | * |
| 62 | * @param array $tables |
| 63 | */ |
| 64 | public function calculateDiffsFromLogConnAndDate($tables) { |
| 65 | $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date); |
| 66 | $this->diffs = $differ->diffsInTables($tables); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Setter for diffs. |
| 71 | * |
| 72 | * @param array $diffs |
| 73 | */ |
| 74 | public function setDiffs($diffs) { |
| 75 | $this->diffs = $diffs; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Revert changes in the array of diffs in $this->diffs. |
| 80 | */ |
| 81 | public function revert() { |
| 82 | |
| 83 | // get custom data tables, columns and types |
| 84 | $ctypes = array(); |
| 85 | $dao = CRM_Core_DAO::executeQuery('SELECT table_name, column_name, data_type FROM civicrm_custom_group cg JOIN civicrm_custom_field cf ON (cf.custom_group_id = cg.id)'); |
| 86 | while ($dao->fetch()) { |
| 87 | if (!isset($ctypes[$dao->table_name])) { |
| 88 | $ctypes[$dao->table_name] = array('entity_id' => 'Integer'); |
| 89 | } |
| 90 | $ctypes[$dao->table_name][$dao->column_name] = $dao->data_type; |
| 91 | } |
| 92 | |
| 93 | $diffs = $this->diffs; |
| 94 | $deletes = array(); |
| 95 | $reverts = array(); |
| 96 | foreach ($diffs as $table => $changes) { |
| 97 | foreach ($changes as $change) { |
| 98 | switch ($change['action']) { |
| 99 | case 'Insert': |
| 100 | if (!isset($deletes[$table])) { |
| 101 | $deletes[$table] = array(); |
| 102 | } |
| 103 | $deletes[$table][] = $change['id']; |
| 104 | break; |
| 105 | |
| 106 | case 'Delete': |
| 107 | case 'Update': |
| 108 | if (!isset($reverts[$table])) { |
| 109 | $reverts[$table] = array(); |
| 110 | } |
| 111 | if (!isset($reverts[$table][$change['id']])) { |
| 112 | $reverts[$table][$change['id']] = array('log_action' => $change['action']); |
| 113 | } |
| 114 | $reverts[$table][$change['id']][$change['field']] = $change['from']; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // revert inserts by deleting |
| 121 | foreach ($deletes as $table => $ids) { |
| 122 | CRM_Core_DAO::executeQuery("DELETE FROM `$table` WHERE id IN (" . implode(', ', array_unique($ids)) . ')'); |
| 123 | } |
| 124 | |
| 125 | // revert updates by updating to previous values |
| 126 | foreach ($reverts as $table => $row) { |
| 127 | switch (TRUE) { |
| 128 | // DAO-based tables |
| 129 | |
| 130 | case (($tableDAO = CRM_Core_DAO_AllCoreTables::getClassForTable($table)) != FALSE): |
| 131 | $dao = new $tableDAO (); |
| 132 | foreach ($row as $id => $changes) { |
| 133 | $dao->id = $id; |
| 134 | foreach ($changes as $field => $value) { |
| 135 | if ($field == 'log_action') { |
| 136 | continue; |
| 137 | } |
| 138 | if (empty($value) and $value !== 0 and $value !== '0') { |
| 139 | $value = 'null'; |
| 140 | } |
| 141 | // Date reaches this point in ISO format (possibly) so strip out stuff |
| 142 | // if it does have hyphens of colons demarking the date & it regexes as being a date |
| 143 | // or datetime format. |
| 144 | if (preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/', $value)) { |
| 145 | $value = str_replace('-', '', $value); |
| 146 | $value = str_replace(':', '', $value); |
| 147 | } |
| 148 | $dao->$field = $value; |
| 149 | } |
| 150 | $changes['log_action'] == 'Delete' ? $dao->insert() : $dao->update(); |
| 151 | |
| 152 | $dao->reset(); |
| 153 | } |
| 154 | break; |
| 155 | |
| 156 | // custom data tables |
| 157 | |
| 158 | case in_array($table, array_keys($ctypes)): |
| 159 | foreach ($row as $id => $changes) { |
| 160 | $inserts = array('id' => '%1'); |
| 161 | $updates = array(); |
| 162 | $params = array(1 => array($id, 'Integer')); |
| 163 | $counter = 2; |
| 164 | foreach ($changes as $field => $value) { |
| 165 | // don’t try reverting a field that’s no longer there |
| 166 | if (!isset($ctypes[$table][$field])) { |
| 167 | continue; |
| 168 | } |
| 169 | $fldVal = "%{$counter}"; |
| 170 | switch ($ctypes[$table][$field]) { |
| 171 | case 'Date': |
| 172 | $value = substr(CRM_Utils_Date::isoToMysql($value), 0, 8); |
| 173 | break; |
| 174 | |
| 175 | case 'Timestamp': |
| 176 | $value = CRM_Utils_Date::isoToMysql($value); |
| 177 | break; |
| 178 | |
| 179 | case 'Boolean': |
| 180 | if ($value === '') { |
| 181 | $fldVal = 'DEFAULT'; |
| 182 | } |
| 183 | } |
| 184 | $inserts[$field] = "%$counter"; |
| 185 | $updates[] = "{$field} = {$fldVal}"; |
| 186 | if ($fldVal != 'DEFAULT') { |
| 187 | $params[$counter] = array($value, $ctypes[$table][$field]); |
| 188 | } |
| 189 | $counter++; |
| 190 | } |
| 191 | if ($changes['log_action'] == 'Delete') { |
| 192 | $sql = "INSERT INTO `$table` (" . implode(', ', array_keys($inserts)) . ') VALUES (' . implode(', ', $inserts) . ')'; |
| 193 | } |
| 194 | else { |
| 195 | $sql = "UPDATE `$table` SET " . implode(', ', $updates) . ' WHERE id = %1'; |
| 196 | } |
| 197 | CRM_Core_DAO::executeQuery($sql, $params); |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | } |