| 1 | <?php |
| 2 | |
| 3 | namespace wcf\page; |
| 4 | |
| 5 | use wcf\data\edit\history\entry\EditHistoryEntry; |
| 6 | use wcf\data\edit\history\entry\EditHistoryEntryList; |
| 7 | use wcf\data\object\type\ObjectType; |
| 8 | use wcf\data\object\type\ObjectTypeCache; |
| 9 | use wcf\system\edit\IHistorySavingObject; |
| 10 | use wcf\system\edit\IHistorySavingObjectTypeProvider; |
| 11 | use wcf\system\exception\IllegalLinkException; |
| 12 | use wcf\system\request\LinkHandler; |
| 13 | use wcf\system\WCF; |
| 14 | use wcf\util\Diff; |
| 15 | use wcf\util\HeaderUtil; |
| 16 | use wcf\util\StringUtil; |
| 17 | |
| 18 | /** |
| 19 | * Compares two entries of the edit history. |
| 20 | * |
| 21 | * @author Tim Duesterhus, Marcel Werk |
| 22 | * @copyright 2001-2024 WoltLab GmbH |
| 23 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 24 | */ |
| 25 | class EditHistoryPage extends AbstractPage |
| 26 | { |
| 27 | /** |
| 28 | * @inheritDoc |
| 29 | */ |
| 30 | public $neededModules = ['MODULE_EDIT_HISTORY']; |
| 31 | |
| 32 | /** |
| 33 | * List of edit history entries. |
| 34 | */ |
| 35 | public ?EditHistoryEntryList $objectList = null; |
| 36 | |
| 37 | /** |
| 38 | * left / old version id |
| 39 | */ |
| 40 | public int $oldID = 0; |
| 41 | |
| 42 | /** |
| 43 | * left / old version |
| 44 | */ |
| 45 | public ?EditHistoryEntry $old = null; |
| 46 | |
| 47 | /** |
| 48 | * right / new version id |
| 49 | */ |
| 50 | public int|string $newID = 0; |
| 51 | |
| 52 | /** |
| 53 | * right / new version |
| 54 | */ |
| 55 | public EditHistoryEntry|IHistorySavingObject|null $new = null; |
| 56 | |
| 57 | /** |
| 58 | * differences between both versions |
| 59 | */ |
| 60 | public ?array $diff = null; |
| 61 | |
| 62 | /** |
| 63 | * object type of the requested object |
| 64 | */ |
| 65 | public ?ObjectType $objectType = null; |
| 66 | |
| 67 | /** |
| 68 | * id of the requested object |
| 69 | */ |
| 70 | public int $objectID = 0; |
| 71 | |
| 72 | /** |
| 73 | * requested object |
| 74 | */ |
| 75 | public ?IHistorySavingObject $object = null; |
| 76 | |
| 77 | /** |
| 78 | * Rendering mode (html or raw). |
| 79 | */ |
| 80 | public string $mode = 'html'; |
| 81 | |
| 82 | #[\Override] |
| 83 | public function readParameters(): void |
| 84 | { |
| 85 | parent::readParameters(); |
| 86 | |
| 87 | if (isset($_REQUEST['oldID'])) { |
| 88 | $this->oldID = \intval($_REQUEST['oldID']); |
| 89 | $this->old = new EditHistoryEntry($this->oldID); |
| 90 | if (!$this->old->entryID) { |
| 91 | throw new IllegalLinkException(); |
| 92 | } |
| 93 | |
| 94 | if (isset($_REQUEST['newID']) && $_REQUEST['newID'] !== 'current') { |
| 95 | $this->newID = \intval($_REQUEST['newID']); |
| 96 | $this->new = new EditHistoryEntry($this->newID); |
| 97 | if (!$this->new->entryID) { |
| 98 | throw new IllegalLinkException(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // if new version isn't 'current' check whether they are comparable |
| 103 | if ($this->new) { |
| 104 | // different objectTypes cannot be compared |
| 105 | if ($this->old->objectTypeID != $this->new->objectTypeID) { |
| 106 | throw new IllegalLinkException(); |
| 107 | } |
| 108 | // different items cannot be compared |
| 109 | if ($this->old->objectID != $this->new->objectID) { |
| 110 | throw new IllegalLinkException(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $this->objectID = $this->old->objectID; |
| 115 | $this->objectType = ObjectTypeCache::getInstance()->getObjectType($this->old->objectTypeID); |
| 116 | } elseif (isset($_REQUEST['objectID']) && isset($_REQUEST['objectType'])) { |
| 117 | $this->objectID = \intval($_REQUEST['objectID']); |
| 118 | $this->objectType = ObjectTypeCache::getInstance()->getObjectTypeByName( |
| 119 | 'com.woltlab.wcf.edit.historySavingObject', |
| 120 | $_REQUEST['objectType'] |
| 121 | ); |
| 122 | } else { |
| 123 | throw new IllegalLinkException(); |
| 124 | } |
| 125 | |
| 126 | if (!$this->objectType) { |
| 127 | throw new IllegalLinkException(); |
| 128 | } |
| 129 | |
| 130 | /** @var IHistorySavingObjectTypeProvider $processor */ |
| 131 | $processor = $this->objectType->getProcessor(); |
| 132 | |
| 133 | $this->object = $processor->getObjectByID($this->objectID); |
| 134 | if (!$this->object->getObjectID()) { |
| 135 | throw new IllegalLinkException(); |
| 136 | } |
| 137 | $processor->checkPermissions($this->object); |
| 138 | $this->object->setLocation(); |
| 139 | |
| 140 | if (isset($_REQUEST['newID']) && !$this->new) { |
| 141 | $this->new = $this->object; |
| 142 | $this->newID = 'current'; |
| 143 | } |
| 144 | |
| 145 | if (isset($_REQUEST['mode']) && $_REQUEST['mode'] == 'raw') { |
| 146 | $this->mode = 'raw'; |
| 147 | } |
| 148 | |
| 149 | if (!empty($_POST)) { |
| 150 | HeaderUtil::redirect(LinkHandler::getInstance()->getLink('EditHistory', [ |
| 151 | 'objectID' => $this->objectID, |
| 152 | 'objectType' => $this->objectType->objectType, |
| 153 | 'newID' => $this->newID, |
| 154 | 'oldID' => $this->oldID, |
| 155 | 'mode' => $this->mode, |
| 156 | ])); |
| 157 | |
| 158 | exit; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #[\Override] |
| 163 | public function readData(): void |
| 164 | { |
| 165 | parent::readData(); |
| 166 | |
| 167 | $this->objectList = new EditHistoryEntryList(); |
| 168 | $this->objectList->sqlOrderBy = "time DESC, entryID DESC"; |
| 169 | $this->objectList->getConditionBuilder()->add('objectTypeID = ?', [$this->objectType->objectTypeID]); |
| 170 | $this->objectList->getConditionBuilder()->add('objectID = ?', [$this->objectID]); |
| 171 | $this->objectList->readObjects(); |
| 172 | |
| 173 | // set default values |
| 174 | if (!isset($_REQUEST['oldID']) && !isset($_REQUEST['newID'])) { |
| 175 | foreach ($this->objectList as $object) { |
| 176 | $this->oldID = $object->entryID; |
| 177 | $this->old = $object; |
| 178 | break; |
| 179 | } |
| 180 | $this->newID = 'current'; |
| 181 | $this->new = $this->object; |
| 182 | } |
| 183 | |
| 184 | // valid IDs were given, calculate diff |
| 185 | if ($this->old && $this->new) { |
| 186 | $differ = Diff::getDefaultDiffer(); |
| 187 | |
| 188 | $a = \explode("\n", $this->prepareMessage($this->old->getMessage())); |
| 189 | $b = \explode("\n", $this->prepareMessage($this->new->getMessage())); |
| 190 | $this->diff = Diff::rawDiffFromSebastianDiff($differ->diffToArray($a, $b)); |
| 191 | |
| 192 | // create word diff for small changes (only one consecutive paragraph modified) |
| 193 | for ($i = 0, $max = \count($this->diff); $i < $max;) { |
| 194 | $previousIsNotRemoved = !isset($this->diff[$i - 1][0]) || $this->diff[$i - 1][0] !== Diff::REMOVED; |
| 195 | $currentIsRemoved = $this->diff[$i][0] === Diff::REMOVED; |
| 196 | $nextIsAdded = isset($this->diff[$i + 1][0]) && $this->diff[$i + 1][0] === Diff::ADDED; |
| 197 | $afterNextIsNotAdded = !isset($this->diff[$i + 2][0]) || $this->diff[$i + 2][0] !== Diff::ADDED; |
| 198 | |
| 199 | if ($previousIsNotRemoved && $currentIsRemoved && $nextIsAdded && $afterNextIsNotAdded) { |
| 200 | $a = \preg_split('/(\\W)/u', $this->diff[$i][1], -1, \PREG_SPLIT_DELIM_CAPTURE); |
| 201 | $b = \preg_split('/(\\W)/u', $this->diff[$i + 1][1], -1, \PREG_SPLIT_DELIM_CAPTURE); |
| 202 | |
| 203 | $diff = Diff::rawDiffFromSebastianDiff($differ->diffToArray($a, $b)); |
| 204 | $this->diff[$i][1] = ''; |
| 205 | $this->diff[$i + 1][1] = ''; |
| 206 | foreach ($diff as $entry) { |
| 207 | $entry[1] = StringUtil::encodeHTML($entry[1]); |
| 208 | |
| 209 | if ($entry[0] === Diff::SAME) { |
| 210 | $this->diff[$i][1] .= $entry[1]; |
| 211 | $this->diff[$i + 1][1] .= $entry[1]; |
| 212 | } elseif ($entry[0] === Diff::REMOVED) { |
| 213 | $this->diff[$i][1] .= '<del>' . $entry[1] . '</del>'; |
| 214 | } elseif ($entry[0] === Diff::ADDED) { |
| 215 | $this->diff[$i + 1][1] .= '<ins>' . $entry[1] . '</ins>'; |
| 216 | } |
| 217 | } |
| 218 | $i += 2; |
| 219 | } else { |
| 220 | $this->diff[$i][1] = StringUtil::encodeHTML($this->diff[$i][1]); |
| 221 | $i++; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private function prepareMessage(string $message): string |
| 228 | { |
| 229 | $message = $this->formatHtml($message); |
| 230 | $message = StringUtil::trim($message); |
| 231 | $message = StringUtil::unifyNewlines($message); |
| 232 | |
| 233 | return \preg_replace('/\n{2,}/', "\n", $message); |
| 234 | } |
| 235 | |
| 236 | private function formatHtml(string $html): string |
| 237 | { |
| 238 | $bothTags = ['ol', 'pre', 'table', 'tr', 'ul', 'woltlab-quote', 'woltlab-spoiler']; |
| 239 | $openingTag = \implode('|', ['br', ...$bothTags]); |
| 240 | $closingTag = \implode('|', ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'li', 'td', 'th', ...$bothTags]); |
| 241 | |
| 242 | return \preg_replace("/(<(?:{$openingTag})>|<\\/(?:{$closingTag})>)/", "\\0\n", $html); |
| 243 | } |
| 244 | |
| 245 | #[\Override] |
| 246 | public function assignVariables(): void |
| 247 | { |
| 248 | parent::assignVariables(); |
| 249 | |
| 250 | WCF::getTPL()->assign([ |
| 251 | 'oldID' => $this->oldID, |
| 252 | 'old' => $this->old, |
| 253 | 'newID' => $this->newID, |
| 254 | 'new' => $this->new, |
| 255 | 'object' => $this->object, |
| 256 | 'diff' => $this->diff, |
| 257 | 'objects' => $this->objectList, |
| 258 | 'objectID' => $this->objectID, |
| 259 | 'objectType' => $this->objectType, |
| 260 | 'mode' => $this->mode, |
| 261 | ]); |
| 262 | } |
| 263 | } |