history.php
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/**
* @package Joomla.Administrator
* @subpackage com_contenthistory
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Methods supporting a list of contenthistory records.
*
* @since 3.2
*/
class ContenthistoryModelHistory extends JModelList
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JControllerLegacy
* @since 3.2
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'version_id',
'h.version_id',
'version_note',
'h.version_note',
'save_date',
'h.save_date',
'editor_user_id',
'h.editor_user_id',
);
}
parent::__construct($config);
}
/**
* Method to test whether a record is editable
*
* @param JTableContenthistory $record A JTable object.
*
* @return boolean True if allowed to edit the record. Defaults to the permission set in the component.
*
* @since 3.2
*/
protected function canEdit($record)
{
$result = false;
if (!empty($record->ucm_type_id))
{
// Check that the type id matches the type alias
$typeAlias = JFactory::getApplication()->input->get('type_alias');
/** @var JTableContenttype $contentTypeTable */
$contentTypeTable = JTable::getInstance('Contenttype', 'JTable');
if ($contentTypeTable->getTypeId($typeAlias) == $record->ucm_type_id)
{
/**
* Make sure user has edit privileges for this content item. Note that we use edit permissions
* for the content item, not delete permissions for the content history row.
*/
$user = JFactory::getUser();
$result = $user->authorise('core.edit', $typeAlias . '.' . (int) $record->ucm_item_id);
}
// Finally try session (this catches edit.own case too)
if (!$result)
{
$contentTypeTable->load($record->ucm_type_id);
$typeEditables = (array) JFactory::getApplication()->getUserState(str_replace('.', '.edit.', $contentTypeTable->type_alias) . '.id');
$result = in_array((int) $record->ucm_item_id, $typeEditables);
}
}
return $result;
}
/**
* Method to test whether a history record can be deleted. Note that we check whether we have edit permissions
* for the content item row.
*
* @param JTableContenthistory $record A JTable object.
*
* @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
*
* @since 3.6
*/
protected function canDelete($record)
{
return $this->canEdit($record);
}
/**
* Method to delete one or more records from content history table.
*
* @param array $pks An array of record primary keys.
*
* @return boolean True if successful, false if an error occurs.
*
* @since 3.2
*/
public function delete(&$pks)
{
$pks = (array) $pks;
$table = $this->getTable();
// Iterate the items to delete each one.
foreach ($pks as $i => $pk)
{
if ($table->load($pk))
{
if ($table->keep_forever === "1")
{
unset($pks[$i]);
continue;
}
if ($this->canEdit($table))
{
if (!$table->delete($pk))
{
$this->setError($table->getError());
return false;
}
}
else
{
// Prune items that you can't change.
unset($pks[$i]);
$error = $this->getError();
if ($error)
{
try
{
JLog::add($error, JLog::WARNING, 'jerror');
}
catch (RuntimeException $exception)
{
JFactory::getApplication()->enqueueMessage($error, 'warning');
}
return false;
}
else
{
try
{
JLog::add(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), JLog::WARNING, 'jerror');
}
catch (RuntimeException $exception)
{
JFactory::getApplication()->enqueueMessage(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), 'warning');
}
return false;
}
}
}
else
{
$this->setError($table->getError());
return false;
}
}
// Clear the component's cache
$this->cleanCache();
return true;
}
/**
* Method to get an array of data items.
*
* @return mixed An array of data items on success, false on failure.
*
* @since 3.4.5
*/
public function getItems()
{
$items = parent::getItems();
$user = JFactory::getUser();
if ($items === false)
{
return false;
}
// This should be an array with at least one element
if (!is_array($items) || !isset($items[0]))
{
return $items;
}
// Get the content type's record so we can check ACL
/** @var JTableContenttype $contentTypeTable */
$contentTypeTable = JTable::getInstance('Contenttype');
$ucmTypeId = $items[0]->ucm_type_id;
if (!$contentTypeTable->load($ucmTypeId))
{
// Assume a failure to load the content type means broken data, abort mission
return false;
}
// Access check
if ($user->authorise('core.edit', $contentTypeTable->type_alias . '.' . (int) $items[0]->ucm_item_id) || $this->canEdit($items[0]))
{
return $items;
}
else
{
$this->setError(JText::_('JERROR_ALERTNOAUTHOR'));
return false;
}
}
/**
* Method to get a table object, load it if necessary.
*
* @param string $type The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A JTable object
*
* @since 3.2
*/
public function getTable($type = 'Contenthistory', $prefix = 'JTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Method to toggle on and off the keep forever value for one or more records from content history table.
*
* @param array $pks An array of record primary keys.
*
* @return boolean True if successful, false if an error occurs.
*
* @since 3.2
*/
public function keep(&$pks)
{
$pks = (array) $pks;
$table = $this->getTable();
// Iterate the items to delete each one.
foreach ($pks as $i => $pk)
{
if ($table->load($pk))
{
if ($this->canEdit($table))
{
$table->keep_forever = $table->keep_forever ? 0 : 1;
if (!$table->store())
{
$this->setError($table->getError());
return false;
}
}
else
{
// Prune items that you can't change.
unset($pks[$i]);
$error = $this->getError();
if ($error)
{
try
{
JLog::add($error, JLog::WARNING, 'jerror');
}
catch (RuntimeException $exception)
{
JFactory::getApplication()->enqueueMessage($error, 'warning');
}
return false;
}
else
{
try
{
JLog::add(JText::_('COM_CONTENTHISTORY_ERROR_KEEP_NOT_PERMITTED'), JLog::WARNING, 'jerror');
}
catch (RuntimeException $exception)
{
JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTHISTORY_ERROR_KEEP_NOT_PERMITTED'), 'warning');
}
return false;
}
}
}
else
{
$this->setError($table->getError());
return false;
}
}
// Clear the component's cache
$this->cleanCache();
return true;
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
*
* @since 3.2
*/
protected function populateState($ordering = 'h.save_date', $direction = 'DESC')
{
$input = JFactory::getApplication()->input;
$itemId = $input->get('item_id', 0, 'integer');
$typeId = $input->get('type_id', 0, 'integer');
$typeAlias = $input->get('type_alias', '', 'string');
$this->setState('item_id', $itemId);
$this->setState('type_id', $typeId);
$this->setState('type_alias', $typeAlias);
$this->setState('sha1_hash', $this->getSha1Hash());
// Load the parameters.
$params = JComponentHelper::getParams('com_contenthistory');
$this->setState('params', $params);
// List state information.
parent::populateState($ordering, $direction);
}
/**
* Build an SQL query to load the list data.
*
* @return JDatabaseQuery
*
* @since 3.2
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'h.version_id, h.ucm_item_id, h.ucm_type_id, h.version_note, h.save_date, h.editor_user_id,' .
'h.character_count, h.sha1_hash, h.version_data, h.keep_forever'
)
)
->from($db->quoteName('#__ucm_history') . ' AS h')
->where($db->quoteName('h.ucm_item_id') . ' = ' . (int) $this->getState('item_id'))
->where($db->quoteName('h.ucm_type_id') . ' = ' . (int) $this->getState('type_id'))
// Join over the users for the editor
->select('uc.name AS editor')
->join('LEFT', '#__users AS uc ON uc.id = h.editor_user_id');
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering');
$orderDirn = $this->state->get('list.direction');
$query->order($db->quoteName($orderCol) . $orderDirn);
return $query;
}
/**
* Get the sha1 hash value for the current item being edited.
*
* @return string sha1 hash of row data
*
* @since 3.2
*/
protected function getSha1Hash()
{
$result = false;
$typeTable = JTable::getInstance('Contenttype', 'JTable');
$typeId = JFactory::getApplication()->input->getInteger('type_id', 0);
$typeTable->load($typeId);
$typeAliasArray = explode('.', $typeTable->type_alias);
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/' . $typeAliasArray[0] . '/tables');
$contentTable = $typeTable->getContentTable();
$keyValue = JFactory::getApplication()->input->getInteger('item_id', 0);
if ($contentTable && $contentTable->load($keyValue))
{
$helper = new JHelper;
$dataObject = $helper->getDataObject($contentTable);
$result = $this->getTable('Contenthistory', 'JTable')->getSha1(json_encode($dataObject), $typeTable);
}
return $result;
}
}