-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathAdminParams.php
More file actions
287 lines (257 loc) 路 8.99 KB
/
Copy pathAdminParams.php
File metadata and controls
287 lines (257 loc) 路 8.99 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
<?php
/**
* Parameters used to display links the admin view (eg admin/index.php)
*
* @since 1.8.2
*/
namespace YOURLS\Views;
/**
* Class AdminParams to get admin view parameters (number of links to display, search, ...)
*
* @since 1.8.2
* @package YOURLS\Views
*/
class AdminParams
{
/**
* All possible search parameters. Populated in the constructor.
*
* @var array
*/
private $possible_search_params;
/**
* All possible sort parameters. Populated in the constructor.
*
* @var array
*/
private $possible_sort_params;
/**
* All possible date sorting parameters. Populated in the constructor.
*
* @var array
*/
private $possible_date_sorting;
/**
* Parameter translations. Populated in the constructor.
*
* @var array
*/
private $params_translations;
/**
* Admin constructor : populate all default parameters
*
*/
public function __construct()
{
// Cast return values of yourls_apply_filter() to array in case a hook would incorrectly return something else
$this->possible_search_params = (array)yourls_apply_filter('admin_params_possible_search',
['all', 'keyword', 'url', 'title', 'ip']);
$this->possible_sort_params = (array)yourls_apply_filter('admin_params_possible_sort',
['keyword', 'url', 'title', 'ip', 'timestamp', 'clicks']);
$this->params_translations = (array)yourls_apply_filter('admin_params_possible_translations',[
'all' => yourls__('All fields'),
'keyword' => yourls__('Short URL'),
'url' => yourls__('URL'),
'title' => yourls__('Title'),
'ip' => yourls__('IP Address'),
'timestamp' => yourls__('Date'),
'clicks' => yourls__('Clicks'),
]);
$this->possible_date_sorting = (array)yourls_apply_filter('admin_params_possible_date_sort',
['before', 'after', 'between']);
}
/**
* Get the number of links to display per page
*
* @since 1.8.2
*
* @param int $default default number of links to display
* @return int
*/
public function get_per_page(int $default): int
{
// return if we have a value and it's not 0
if (isset($_GET['perpage']) && intval($_GET['perpage'])) {
$per_page = intval($_GET['perpage']);
// otherwise return filtered default value
} else {
// @hook Default number of links to display (value provided by caller eg /admin/index.php)
$per_page = yourls_apply_filter('admin_view_per_page', $default);
}
return $per_page;
}
/**
* Get the current page number to be displayed
*
* @since 1.8.2
*
* @return int
*/
public function get_page(): int
{
return isset($_GET['page']) ? intval($_GET['page']) : 1;
}
/**
* Get search text (the 'Search for') from query string variables search_protocol, search_slashes and search
*
* Some servers don't like query strings containing "(ht|f)tp(s)://". A javascript bit
* explodes the search text into protocol, slashes and the rest (see JS function
* split_search_text_before_search()) and this function glues pieces back together
* See issue https://github.com/YOURLS/YOURLS/issues/1576
*
* @since 1.8.2
*
* @return string
*/
public function get_search(): string
{
$search = '';
if (isset($_GET['search_protocol'])) {
$search .= $_GET['search_protocol'];
}
if (isset($_GET['search_slashes'])) {
$search .= $_GET['search_slashes'];
}
if (isset($_GET['search'])) {
$search .= $_GET['search'];
}
// @hook Default search text in links displayed
return yourls_apply_filter('admin_view_get_search_text', htmlspecialchars(trim($search)));
}
/**
* Get the 'Search In' parameter (one of 'all', 'keyword', 'url', 'title', 'ip')
*
* @since 1.8.2
*
* @return string
*/
public function get_search_in(): string
{
if (isset($_GET['search_in']) && in_array($_GET['search_in'], $this->possible_search_params)) {
$search_in = $_GET['search_in'];
} else {
// @hook Default searching in the admin view (in all fields)
$search_in = yourls_apply_filter('admin_view_search_in', 'all');
}
return $search_in;
}
/**
* Get the 'Sort by' parameter
*
* @since 1.8.2
*
* @return string
*/
public function get_sort_by(): string
{
if (isset($_GET['sort_by']) && in_array($_GET['sort_by'], $this->possible_sort_params)) {
$sort_by = $_GET['sort_by'];
} else {
// @hook Default sorting in the admin view (by Timestamp)
$sort_by = yourls_apply_filter('admin_view_sort_by', 'timestamp');
}
return $sort_by;
}
/**
* Get the correct phrasing associated to a search or sort parameter (ie 'all' -> 'All fields' for instance)
*
* No checks : you need to supply an existing parameter, see $params_translations
*
* @since 1.8.2
*
* @param string $param
* @return string
*/
public function get_param_long_name(string $param): string
{
return $this->params_translations[$param];
}
/**
* Get the sort order (asc or desc)
*
* @since 1.8.2
*
* @return mixed
*/
public function get_sort_order()
{
// @hook Default sorting order in the admin view (descending)
return isset($_GET['sort_order']) && $_GET['sort_order'] == 'asc' ? 'asc' : yourls_apply_filter('admin_view_sort_order', 'desc');
}
/**
* Get the click "more or less than"
*
* @since 1.8.2
*
* @return mixed
*/
public function get_click_filter()
{
// @hook Default 'Show links with more/less than' ('more')
return isset($_GET['click_filter']) && $_GET['click_filter'] == 'less' ? 'less' : yourls_apply_filter('admin_view_click_filter', 'more');
}
/**
* Get the click threshold
*
* @since 1.8.2
*
* @return int|string
*/
public function get_click_limit()
{
// @hook Default link click threshold (unset)
if (
isset($_GET['click_limit']) // Exists in the query string
&& ($_GET['click_limit'] !== '') // Not empty (&stuff=&click_limit=&otherstuff=)
&& intval($_GET['click_limit']) >= 0 // A number >= 0
) {
return intval($_GET['click_limit']);
} else {
return yourls_apply_filter('admin_view_click_limit', '');
}
}
/**
* Get the date parameters : the date "filter" and the two dates
*
* @since 1.8.2
*
* @return array
*/
public function get_date_params(): array
{
if (isset($_GET['date_filter']) && in_array($_GET['date_filter'], $this->possible_date_sorting)) {
$date_filter = $_GET['date_filter'];
} else {
// @hook Default date filtering (unset)
$date_filter = yourls_apply_filter('admin_view_date_filter', '');
}
switch ($date_filter) {
case 'after':
case 'before':
if (isset($_GET['date_first']) && yourls_sanitize_date($_GET['date_first'])) {
$date_first = yourls_sanitize_date($_GET['date_first']);
} else {
// @hook Default date when date filter is either 'after' or 'before' (unset)
// In such case, the filter is either 'admin_view_date_first_after' or 'admin_view_date_first_before'
$date_first = yourls_apply_filter('admin_view_date_first_' . $date_filter, '');
}
$date_second = '';
break;
case 'between':
if (isset($_GET['date_first']) && isset($_GET['date_second']) && yourls_sanitize_date($_GET['date_first']) && yourls_sanitize_date($_GET['date_second'])) {
$date_first = yourls_sanitize_date($_GET['date_first']);
$date_second = yourls_sanitize_date($_GET['date_second']);
} else {
// @hook Default dates when date filter is 'between' (unset)
$date_first = yourls_apply_filter('admin_view_date_first_between', '');
$date_second = yourls_apply_filter('admin_view_date_second_between', '');
}
break;
default:
// @hook Default date when date filter is unset (unset)
$date_first = yourls_apply_filter('admin_view_date_first_unset', '');
$date_second = yourls_apply_filter('admin_view_date_second_unset', '');
}
return ['date_filter' => $date_filter, 'date_first' => $date_first, 'date_second' => $date_second];
}
}