-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
link.php
263 lines (225 loc) · 6.53 KB
/
link.php
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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_redirect
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Redirect link model.
*
* @since 1.6
*/
class RedirectModelLink extends JModelAdmin
{
/**
* @var string The prefix to use with controller messages.
* @since 1.6
*/
protected $text_prefix = 'COM_REDIRECT';
/**
* Method to test whether a record can be deleted.
*
* @param object $record A record object.
*
* @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
*
* @since 1.6
*/
protected function canDelete($record)
{
if ($record->published != -2)
{
return false;
}
$user = JFactory::getUser();
return $user->authorise('core.delete', 'com_redirect');
}
/**
* Method to test whether a record can have its state edited.
*
* @param object $record A record object.
*
* @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
*
* @since 1.6
*/
protected function canEditState($record)
{
$user = JFactory::getUser();
// Check the component since there are no categories or other assets.
return $user->authorise('core.edit.state', 'com_redirect');
}
/**
* Returns a reference to the a Table object, always creating it.
*
* @param string $type The table type to instantiate
* @param string $prefix A prefix for the table class name. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A database object
*
* @since 1.6
*/
public function getTable($type = 'Link', $prefix = 'RedirectTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return JForm A JForm object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_redirect.link', 'link', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
// Modify the form based on access controls.
if ($this->canEditState((object) $data) != true)
{
// Disable fields for display.
$form->setFieldAttribute('published', 'disabled', 'true');
// Disable fields while saving.
// The controller has already verified this is a record you can edit.
$form->setFieldAttribute('published', 'filter', 'unset');
}
// If in advanced mode then we make sure the new url field is not compulsory and the header
// field compulsory in case people select non-3xx redirects
if (JComponentHelper::getParams('com_redirect')->get('mode', 0) == true)
{
$form->setFieldAttribute('new_url', 'required', 'false');
$form->setFieldAttribute('header', 'required', 'true');
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_redirect.edit.link.data', array());
if (empty($data))
{
$data = $this->getItem();
}
$this->preprocessData('com_redirect.link', $data);
return $data;
}
/**
* Method to activate links.
*
* @param array &$pks An array of link ids.
* @param string $url The new URL to set for the redirect.
* @param string $comment A comment for the redirect links.
*
* @return boolean Returns true on success, false on failure.
*
* @since 1.6
*/
public function activate(&$pks, $url, $comment = null)
{
$user = JFactory::getUser();
$db = $this->getDbo();
// Sanitize the ids.
$pks = (array) $pks;
JArrayHelper::toInteger($pks);
// Populate default comment if necessary.
$comment = (!empty($comment)) ? $comment : JText::sprintf('COM_REDIRECT_REDIRECTED_ON', JHtml::_('date', time()));
// Access checks.
if (!$user->authorise('core.edit', 'com_redirect'))
{
$pks = array();
$this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
return false;
}
if (!empty($pks))
{
// Update the link rows.
$query = $db->getQuery(true)
->update($db->quoteName('#__redirect_links'))
->set($db->quoteName('new_url') . ' = ' . $db->quote($url))
->set($db->quoteName('published') . ' = ' . (int) 1)
->set($db->quoteName('comment') . ' = ' . $db->quote($comment))
->where($db->quoteName('id') . ' IN (' . implode(',', $pks) . ')');
$db->setQuery($query);
try
{
$db->execute();
}
catch (RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
}
return true;
}
/**
* Method to batch update URLs to have new redirect urls and comments. Note will publish any unpublished URLs.
*
* @param array &$pks An array of link ids.
* @param string $url The new URL to set for the redirect.
* @param string $comment A comment for the redirect links.
*
* @return boolean Returns true on success, false on failure.
*
* @since 3.6.0
*/
public function duplicateUrls(&$pks, $url, $comment = null)
{
$user = JFactory::getUser();
$db = $this->getDbo();
// Sanitize the ids.
$pks = (array) $pks;
JArrayHelper::toInteger($pks);
// Access checks.
if (!$user->authorise('core.edit', 'com_redirect'))
{
$pks = array();
$this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
return false;
}
if (!empty($pks))
{
$date = JFactory::getDate()->toSql();
// Update the link rows.
$query = $db->getQuery(true)
->update($db->quoteName('#__redirect_links'))
->set($db->quoteName('new_url') . ' = ' . $db->quote($url))
->set($db->quoteName('modified_date') . ' = ' . $db->quote($date))
->set($db->quoteName('published') . ' = ' . 1)
->where($db->quoteName('id') . ' IN (' . implode(',', $pks) . ')');
if (!empty($comment))
{
$query->set($db->quoteName('comment') . ' = ' . $db->quote($comment));
}
$db->setQuery($query);
try
{
$db->execute();
}
catch (RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
}
return true;
}
}