1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Retour allows you to intelligently redirect legacy URLs, so that you don't |
6
|
|
|
* lose SEO value when rebuilding & restructuring a website |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\controllers; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\db\Query; |
16
|
|
|
use craft\errors\SiteNotFoundException; |
17
|
|
|
use craft\helpers\UrlHelper; |
18
|
|
|
use craft\web\Controller; |
19
|
|
|
use nystudio107\retour\helpers\Permission as PermissionHelper; |
20
|
|
|
use yii\web\BadRequestHttpException; |
21
|
|
|
use yii\web\ForbiddenHttpException; |
22
|
|
|
use yii\web\Response; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package Retour |
|
|
|
|
27
|
|
|
* @since 3.0.0 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class TablesController extends Controller |
30
|
|
|
{ |
31
|
|
|
// Constants |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
const HANDLED_MAP = [ |
35
|
|
|
'handled' => 1, |
36
|
|
|
'nothandled' => 0, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
const SORT_MAP = [ |
40
|
|
|
'DESC' => SORT_DESC, |
41
|
|
|
'ASC' => SORT_ASC, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
const ALLOWED_STATS_SORT_FIELDS = [ |
45
|
|
|
'redirectSrcUrl', |
46
|
|
|
'referrerUrl', |
47
|
|
|
'remoteIp', |
48
|
|
|
'hitCount', |
49
|
|
|
'hitLastTime', |
50
|
|
|
'handledByRetour', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
const ALLOWED_REDIRECTS_SORT_FIELDS = [ |
54
|
|
|
'redirectSrcUrl', |
55
|
|
|
'redirectDestUrl', |
56
|
|
|
'redirectMatchType', |
57
|
|
|
'siteId', |
58
|
|
|
'redirectHttpCode', |
59
|
|
|
'hitCount', |
60
|
|
|
'hitLastTime', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
// Protected Properties |
64
|
|
|
// ========================================================================= |
65
|
|
|
|
66
|
|
|
/** |
|
|
|
|
67
|
|
|
* @var bool|array |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
protected $allowAnonymous = [ |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
// Public Methods |
73
|
|
|
// ========================================================================= |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Handle requests for the dashboard statistics table |
77
|
|
|
* |
78
|
|
|
* @param string $sort |
|
|
|
|
79
|
|
|
* @param int $page |
|
|
|
|
80
|
|
|
* @param int $per_page |
|
|
|
|
81
|
|
|
* @param string $filter |
|
|
|
|
82
|
|
|
* @param int $siteId |
|
|
|
|
83
|
|
|
* @param string|null $handled |
|
|
|
|
84
|
|
|
* |
85
|
|
|
* @return Response |
86
|
|
|
* @throws ForbiddenHttpException |
87
|
|
|
* @throws BadRequestHttpException |
88
|
|
|
*/ |
89
|
|
|
public function actionDashboard( |
90
|
|
|
string $sort = 'hitCount|desc', |
91
|
|
|
int $page = 1, |
92
|
|
|
int $per_page = 20, |
93
|
|
|
$filter = '', |
|
|
|
|
94
|
|
|
$siteId = 0, |
|
|
|
|
95
|
|
|
$handled = 'all' |
|
|
|
|
96
|
|
|
): Response |
97
|
|
|
{ |
|
|
|
|
98
|
|
|
PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
99
|
|
|
$data = []; |
100
|
|
|
$sortField = 'hitCount'; |
101
|
|
|
$sortType = 'DESC'; |
102
|
|
|
// Figure out the sorting type |
103
|
|
|
if ($sort !== '') { |
104
|
|
|
if (strpos($sort, '|') === false) { |
105
|
|
|
$sortField = $sort; |
106
|
|
|
} else { |
107
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
$sortType = strtoupper($sortType); |
111
|
|
|
$sortType = self::SORT_MAP[$sortType] ?? self::SORT_MAP['DESC']; |
112
|
|
|
// Validate untrusted data |
113
|
|
|
if (!in_array($sortField, self::ALLOWED_STATS_SORT_FIELDS, true)) { |
114
|
|
|
throw new BadRequestHttpException(Craft::t('retour', 'Invalid sort field specified.')); |
115
|
|
|
} |
116
|
|
|
// Query the db table |
117
|
|
|
$offset = ($page - 1) * $per_page; |
118
|
|
|
$query = (new Query()) |
119
|
|
|
->from(['{{%retour_stats}}']) |
120
|
|
|
->offset($offset) |
121
|
|
|
->limit($per_page) |
122
|
|
|
->orderBy([$sortField => $sortType]) |
123
|
|
|
->filterWhere(['like', 'redirectSrcUrl', $filter]) |
124
|
|
|
->orFilterWhere(['like', 'referrerUrl', $filter]); |
125
|
|
|
if ((int)$siteId !== 0) { |
126
|
|
|
$query->andWhere(['siteId' => $siteId]); |
127
|
|
|
} |
128
|
|
|
if ($handled !== 'all') { |
129
|
|
|
$query->andWhere(['handledByRetour' => self::HANDLED_MAP[$handled]]); |
130
|
|
|
} |
131
|
|
|
$stats = $query->all(); |
132
|
|
|
if ($stats) { |
|
|
|
|
133
|
|
|
// Add in the `addLink` field |
134
|
|
|
foreach ($stats as &$stat) { |
135
|
|
|
$stat['addLink'] = ''; |
136
|
|
|
if (!$stat['handledByRetour']) { |
137
|
|
|
$encodedUrl = urlencode('/' . ltrim($stat['redirectSrcUrl'], '/')); |
138
|
|
|
// Add the siteId to the URL, but keep the current behavior of passing in siteId=0 for "all" |
139
|
|
|
$statSiteId = $stat['siteId'] ?? 0; |
140
|
|
|
try { |
141
|
|
|
$primarySite = Craft::$app->getSites()->getPrimarySite(); |
142
|
|
|
} catch (SiteNotFoundException $e) { |
143
|
|
|
$primarySite = null; |
144
|
|
|
} |
145
|
|
|
if ($primarySite !== null && $statSiteId == (int)$primarySite->id) { |
146
|
|
|
$statSiteId = 0; |
147
|
|
|
} |
148
|
|
|
$stat['addLink'] = UrlHelper::cpUrl('retour/add-redirect', [ |
|
|
|
|
149
|
|
|
'defaultUrl' => $encodedUrl, |
150
|
|
|
'siteId' => $statSiteId |
151
|
|
|
]); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
// Format the data for the API |
155
|
|
|
$data['data'] = $stats; |
156
|
|
|
$count = $query->count(); |
157
|
|
|
$data['links']['pagination'] = [ |
158
|
|
|
'total' => $count, |
159
|
|
|
'per_page' => $per_page, |
160
|
|
|
'current_page' => $page, |
161
|
|
|
'last_page' => ceil($count / $per_page), |
162
|
|
|
'next_page_url' => null, |
163
|
|
|
'prev_page_url' => null, |
164
|
|
|
'from' => $offset + 1, |
165
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->asJson($data); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
|
|
|
|
173
|
|
|
* Handle requests for the dashboard redirects table |
174
|
|
|
* |
175
|
|
|
* @param string $sort |
|
|
|
|
176
|
|
|
* @param int $page |
|
|
|
|
177
|
|
|
* @param int $per_page |
|
|
|
|
178
|
|
|
* @param string $filter |
|
|
|
|
179
|
|
|
* @param null $siteId |
|
|
|
|
180
|
|
|
* |
181
|
|
|
* @return Response |
182
|
|
|
* @throws ForbiddenHttpException |
183
|
|
|
* @throws BadRequestHttpException |
184
|
|
|
*/ |
185
|
|
|
public function actionRedirects( |
186
|
|
|
string $sort = 'hitCount|desc', |
187
|
|
|
int $page = 1, |
188
|
|
|
int $per_page = 20, |
189
|
|
|
$filter = '', |
|
|
|
|
190
|
|
|
$siteId = 0, |
|
|
|
|
191
|
|
|
$shortLinks = false |
|
|
|
|
192
|
|
|
): Response |
193
|
|
|
{ |
|
|
|
|
194
|
|
|
PermissionHelper::controllerPermissionCheck('retour:redirects'); |
195
|
|
|
$data = []; |
196
|
|
|
$sortField = 'hitCount'; |
197
|
|
|
$sortType = 'DESC'; |
198
|
|
|
// Figure out the sorting type |
199
|
|
|
if ($sort !== '') { |
200
|
|
|
if (strpos($sort, '|') === false) { |
201
|
|
|
$sortField = $sort; |
202
|
|
|
} else { |
203
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
$sortType = strtoupper($sortType); |
207
|
|
|
$sortType = self::SORT_MAP[$sortType] ?? self::SORT_MAP['DESC']; |
208
|
|
|
// Validate untrusted data |
209
|
|
|
if (!in_array($sortField, self::ALLOWED_REDIRECTS_SORT_FIELDS, true)) { |
210
|
|
|
throw new BadRequestHttpException(Craft::t('retour', 'Invalid sort field specified.')); |
211
|
|
|
} |
212
|
|
|
// Query the db table |
213
|
|
|
$offset = ($page - 1) * $per_page; |
214
|
|
|
$query = (new Query()) |
215
|
|
|
->from(['{{%retour_static_redirects}}']) |
216
|
|
|
->offset($offset) |
217
|
|
|
->limit($per_page) |
218
|
|
|
->orderBy([$sortField => $sortType]) |
219
|
|
|
->filterWhere(['like', 'redirectSrcUrl', $filter]) |
220
|
|
|
->orFilterWhere(['like', 'redirectDestUrl', $filter]); |
221
|
|
|
if ((int)$siteId !== 0) { |
222
|
|
|
$query->andWhere(['siteId' => $siteId]); |
223
|
|
|
} |
224
|
|
|
if ($shortLinks) { |
225
|
|
|
$query->andWhere(['not', ['associatedElementId' => null]]); |
226
|
|
|
} else { |
227
|
|
|
$query->andWhere(['associatedElementId' => 0]); |
228
|
|
|
} |
229
|
|
|
$redirects = $query->all(); |
230
|
|
|
// Add in the `deleteLink` field and clean up the redirects |
231
|
|
|
foreach ($redirects as &$redirect) { |
232
|
|
|
// Make sure the destination URL is not a regex |
233
|
|
|
if ($redirect['redirectMatchType'] !== 'exactmatch') { |
234
|
|
|
if (preg_match("/\$\d+/", $redirect['redirectDestUrl'])) { |
235
|
|
|
$redirect['redirectDestUrl'] = ''; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
// Handle extracting the site name |
239
|
|
|
$redirect['siteName'] = Craft::t('retour', 'All Sites'); |
240
|
|
|
if ($redirect['siteId']) { |
241
|
|
|
$sites = Craft::$app->getSites(); |
242
|
|
|
$site = $sites->getSiteById($redirect['siteId']); |
243
|
|
|
if ($site) { |
244
|
|
|
$redirect['siteName'] = $site->name; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/' . $redirect['id']); |
249
|
|
|
} |
250
|
|
|
// Format the data for the API |
251
|
|
|
if ($redirects) { |
|
|
|
|
252
|
|
|
$data['data'] = $redirects; |
253
|
|
|
$count = $query->count(); |
254
|
|
|
$data['links']['pagination'] = [ |
255
|
|
|
'total' => $count, |
256
|
|
|
'per_page' => $per_page, |
257
|
|
|
'current_page' => $page, |
258
|
|
|
'last_page' => ceil($count / $per_page), |
259
|
|
|
'next_page_url' => null, |
260
|
|
|
'prev_page_url' => null, |
261
|
|
|
'from' => $offset + 1, |
262
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
263
|
|
|
]; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this->asJson($data); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
// Protected Methods |
270
|
|
|
// ========================================================================= |
271
|
|
|
} |
272
|
|
|
|