This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplugin.php
233 lines (208 loc) · 8.16 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
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
<?php
/*
Plugin Name: Update Shortened URL
Plugin URI: https://github.com/timcrockford/yourls-api-edit-url
Description: Define a custom API action 'update' and 'geturl'
Version: 0.2.3
Author: Tim Crockford
Author URI: http://codearoundcorners.com/
*/
yourls_add_filter( 'api_action_update', 'api_edit_url_update' );
yourls_add_filter( 'api_action_geturl', 'api_edit_url_get' );
yourls_add_filter( 'api_action_change_keyword', 'api_edit_url_change_keyword' );
function process_title( $title, $url, $keyword ) {
if ( strcasecmp($title, 'keep') == 0 ) {
return yourls_get_keyword_title( $keyword, '' );
} elseif ( strcasecmp($title, 'auto') == 0 ){
return yourls_get_remote_title( $url );
} else {
return $title;
}
}
function api_edit_url_update() {
if ( ! isset( $_REQUEST['shorturl'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'shorturl' parameter",
'message' => 'error: missing param',
);
}
if ( ! isset( $_REQUEST['url'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'url' parameter",
'message' => 'error: missing param',
);
}
$shorturl = $_REQUEST['shorturl'];
$url = $_REQUEST['url'];
if ( yourls_get_protocol( $shorturl ) ) {
$keyword = yourls_get_relative_url( $shorturl );
} else {
$keyword = $shorturl;
}
if ( ! yourls_is_shorturl( $keyword ) ) {
return array(
'statusCode' => 404,
'status' => 'fail',
'simple ' => "Error: keyword $keyword not found",
'message' => 'error: not found',
);
}
$title = '';
if ( isset($_REQUEST['title']) ) $title = $_REQUEST['title'];
$title = process_title( $title, $url, $keyword );
if( yourls_edit_link( $url, $keyword, $keyword, $title ) ) {
return array(
'statusCode' => 200,
'simple' => "Keyword $keyword updated to $url",
'message' => 'success: updated',
);
} else {
return array(
'statusCode' => 500,
'status' => 'fail',
'simple' => 'Error: could not edit keyword, not sure why :-/',
'message' => 'error: unknown error',
);
}
}
function api_edit_url_change_keyword() {
if ( ! isset( $_REQUEST['newshorturl'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'newshorturl' parameter",
'message' => 'error: missing param',
);
}
if ( ! isset( $_REQUEST['url'] ) && ! isset( $_REQUEST['oldshorturl'] )) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'url' or 'oldshorturl' parameter",
'message' => 'error: missing param',
);
}
if ( isset( $_REQUEST['url'] ) && ! isset( $_REQUEST['oldshorturl'] )) {
$url = urldecode($_REQUEST['url']);
if ( ! yourls_get_longurl_keywords( $url )) {
return array(
'statusCode' => 500,
'status' => 'fail',
'simple' => "Error: could not find keyword for url $url",
'message' => 'error: not found $url',
);
}
$keywords = yourls_get_longurl_keywords( $url );
if ( sizeof($keywords) > 1) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Given URL has multiple shortcodes",
'message' => 'error: ambiguous url',
);
} else {
$oldshorturl = array_values( $keywords )[0];
}
} elseif ( ! isset( $_REQUEST['url'] )) {
$oldshorturl = $_REQUEST['oldshorturl'];
if ( ! yourls_is_shorturl( $oldshorturl ) ) {
return array(
'statusCode' => 404,
'status' => 'fail',
'simple ' => "Error: keyword $keyword not found",
'message' => 'error: not found',
);
}
$url = yourls_get_keyword_longurl( $oldshorturl );
} else {
$oldshorturl = $_REQUEST['oldshorturl'];
$url = urldecode($_REQUEST['url']);
}
$newshorturl = $_REQUEST['newshorturl'];
if ( yourls_get_protocol( $oldshorturl ) ) {
$oldkeyword = yourls_get_relative_url( $oldshorturl );
} else {
$oldkeyword = $oldshorturl;
}
if ( yourls_get_protocol( $newshorturl ) ) {
$newkeyword = yourls_get_relative_url( $newshorturl );
} else {
$newkeyword = $newshorturl;
}
if ( yourls_keyword_is_taken( $newkeyword ) ) {
return array(
'statusCode' => 409,
'status' => 'fail',
'simple ' => "Error: keyword $newkeyword already exists",
'message' => 'error: already exists',
);
}
if ( ! yourls_keyword_is_taken( $oldkeyword ) ) {
return array(
'statusCode' => 404,
'status' => 'fail',
'simple ' => "Error: keyword $oldkeyword not found",
'message' => 'error: not found',
);
}
$title = '';
if ( isset($_REQUEST['title']) ) $title = $_REQUEST['title'];
$title = process_title( $title, $url, $oldshorturl );
if( yourls_edit_link( $url, $oldkeyword, $newkeyword, $title ) ) {
return array(
'statusCode' => 200,
'simple' => "Keyword $oldkeyword updated to $newkeyword for $url",
'message' => 'success: updated',
);
} else {
return array(
'statusCode' => 500,
'status' => 'fail',
'simple' => 'Error: could not edit keyword, not sure why :-/',
'message' => 'error: unknown error',
);
}
}
function api_edit_url_get() {
if ( ! isset( $_REQUEST['url'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'url' parameter",
'message' => 'error: missing param',
);
}
$url = $_REQUEST['url'];
$url_exists = yourls_url_exists($url);
if ( $url_exists ) {
if ( isset( $_REQUEST['exactly_one'] ) && ! filter_var( $_REQUEST['exactly_one'], FILTER_VALIDATE_BOOLEAN )) {
$keywords = yourls_get_longurl_keywords( $url );
return array(
'statusCode' => 200,
'simple' => "Keywords for $url are " . json_encode ( $keywords ),
'message' => 'success: found',
'keyword' => json_encode( $keywords ),
);
} else {
return array(
'statusCode' => 200,
'simple' => "Keyword for $url is " . $url_exists->keyword,
'message' => 'success: found',
'keyword' => $url_exists->keyword,
);
}
} else {
return array(
'statusCode' => 500,
'status' => 'fail',
'simple' => "Error: could not find keyword for url $url",
'message' => 'error: not found',
'keyword' => '',
);
}
}
?>