-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.php
183 lines (154 loc) · 5.73 KB
/
plugin.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
<?php
/*
Plugin Name: Mass Remove Links
Plugin URI: https://github.com/YOURLS/mass-remove-links/
Description: Remove several (or all) links.
Version: 1.2
Author: Ozh
Author URI: http://ozh.org/
*/
yourls_add_action( 'plugins_loaded', 'ozh_yourls_linkmr_add_page' );
function ozh_yourls_linkmr_add_page() {
yourls_register_plugin_page( 'ozh_lmr', 'Link Mass Remove', 'ozh_yourls_linkmr_do_page' );
}
// Display admin page
function ozh_yourls_linkmr_do_page() {
echo <<<HTML
<h2>Link Mass Remove</h2>
HTML;
if( isset( $_POST['action'] ) && $_POST['action'] == 'link_mass_remove' ) {
ozh_yourls_linkmr_process();
} else {
ozh_yourls_linkmr_form();
}
}
// Display form
function ozh_yourls_linkmr_form() {
$nonce = yourls_create_nonce('link_mass_remove');
echo <<<HTML
<p>Remove the following links:</p>
<form method="post">
<input type="hidden" name="action" value="link_mass_remove" />
<input type="hidden" name="nonce" value="$nonce" />
<p><label for="radio_date">
<input type="radio" name="what" id="radio_date" value="date"/>All links created on date
</label>
<input type="text" name="date" /> (YYYY/MM/DD)
</p>
<p><label for="radio_daterange">
<input type="radio" name="what" id="radio_daterange" value="daterange"/>All links created between
</label>
<input type="text" name="date1" /> and <input type="text" name="date2" /> (YYYY/MM/DD)
</p>
<p><label for="radio_ip">
<input type="radio" name="what" id="radio_ip" value="ip"/>All links created by IP
</label>
<input type="text" name="ip" />
</p>
<p><label for="radio_url">
<input type="radio" name="what" id="radio_url" value="url"/>All links pointing to a long URL containing
</label>
<input type="text" name="url" /> (case sensitive)
</p>
<p><label for="radio_clicks">
<input type="radio" name="what" id="radio_clicks" value="clicks"/>All links with the specified amount of clicks
</label>
<select name="clicks_filter">
<option value="equals">Equals (=)</option>
<option value="greaterThan">Greater than (>)</option>
<option value="lessThan">Less than (<)</option>
</select>
<input type="number" name="clicks" min="0" value="0" />
</p>
<p><label for="radio_all">
<input type="radio" name="what" id="radio_all" value="all"/>All links. All.
</label>
</p>
<p><label for="check_test"><input type="checkbox" id="check_test" checked="checked" name="test" value="test" /> <strong>Display results, do not delete. This is a test. </strong></label></p>
<p><input type="submit" value="Delete" /> (no undo!)</p>
</form>
<script>
function select_radio(el){
$(el).parent().find(':radio').click();
}
$('input:text, input[type="number"]')
.click(function(){select_radio($(this))})
.focus(function(){select_radio($(this))})
.change(function(){select_radio($(this))});
</script>
HTML;
}
function ozh_yourls_linkmr_process() {
// Check nonce
yourls_verify_nonce( 'link_mass_remove' );
$where = '';
$binds = array();
$validClicksFilters = array(
'equals' => '=',
'greaterThan' => '>',
'lessThan' => '<',
);
switch( $_POST['what'] ) {
case 'all':
$where = '1=1';
break;
case 'clicks':
$clicksFilter = $validClicksFilters[$_POST['clicks_filter']] ?? '=';
$binds['clicks'] = (int) $_POST['clicks'];
$where = "`clicks` $clicksFilter :clicks";
break;
case 'date':
$where = "`timestamp` BETWEEN :date1 and :date2";
$binds['date1'] = $_POST['date'] . ' 00:00:00';
$binds['date2'] = $_POST['date'] . ' 23:59:59';
break;
case 'daterange':
$where = "`timestamp` BETWEEN :date1 and :date2";
$binds['date1'] = $_POST['date1'] . ' 00:00:00';
$binds['date2'] = $_POST['date2'] . ' 00:00:00';
break;
case 'ip':
$where = "`ip` = :ip";
$binds['ip'] = $_POST['ip'];
break;
case 'url':
$where = "`url` LIKE (:url)";
$binds['url'] = str_replace( '*', '%', '*' . $_POST['url'] . '*' );
break;
default:
echo 'Not implemented';
return;
}
global $ydb;
$action = ( isset( $_POST['test'] ) && $_POST['test'] == 'test' ) ? 'SELECT' : 'DELETE' ;
$select = ( $action == 'SELECT' ) ? '`keyword`,`url`' : '';
$table = YOURLS_DB_TABLE_URL;
$sql = "$action $select FROM `$table` WHERE $where";
if( $action == 'SELECT' ) {
$query = $ydb->fetchObjects($sql, $binds);
if( !$query ) {
echo 'No link found.';
return;
} else {
echo '<p>'.count( $query ).' found:</p>';
echo '<ul>';
foreach( $query as $link ) {
$short = $link->keyword;
$url = $link->url;
echo "<li>$short: <a href='$url'>$url</a></li>\n";
}
echo '</ul>';
unset( $_POST['test'] );
echo '<form method="post">';
foreach( $_POST as $k=>$v ) {
if( $v ) {
echo "<input type='hidden' name='$k' value='$v' />";
}
}
echo '<input type="submit" value="OK. Delete" /></form>';
}
} else {
$query = $ydb->fetchAffected($sql, $binds);
echo $query . ' ' . yourls_n('link', 'links', $query) . ' deleted.';
}
}