-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpgd.xmlrpc.php
333 lines (267 loc) · 9.11 KB
/
wpgd.xmlrpc.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php /* -*- Mode: php; c-basic-offset:4; -*- */
/* Copyright (C) 2011 Governo do Estado do Rio Grande do Sul
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A shortcut to log the user in before making any `wp_' call and escape
* all the received arguments of an exposed method.
*
* Borrowed from `exapi'
*
* @params array $args Arguments that will be escaped with a wordpress
* xmlrpc utility
*/
function _wpgd_method_header(&$args) {
// We don't like smart-ass people
global $wp_xmlrpc_server;
$wp_xmlrpc_server->escape( $args );
// getting rid of blog_id
array_shift($args);
// Reading the attribute list
$username = array_shift($args);
$password = array_shift($args);
// All methods in this API are being protected
if (!$user = $wp_xmlrpc_server->login($username, $password))
return $wp_xmlrpc_server->error;
return $args;
}
/**
* A wrapper for the function that lists all highlighted videos
*
* @param array $args Holds parameters to be passed to the actuall
* function being wrapped and, in this case, you can pass the number of
* highlights you want.
*/
function wpgd_getHighlightedVideos($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
$limit = null;
if (isset($args[1]))
$limit = $args[1];
return wpgd_videos_get_highlighted_videos($limit);
}
function wpgd_setVideoViews($args){
global $wpdb;
$videos_table = $wpdb->prefix . "wpgd_admin_videos";
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
/* Just making sure that we can keep rocking */
if (!isset($args[1]))
return null;
if (!isset($args[2]))
return null;
$wpdb->update(
$videos_table,
array(
'views' => $args[1]
),
array('id' => $args[2])
);
}
function wpgd_getVideosCategories($args){
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
/* Just making sure that we can keep rocking */
if (!isset($args[0]))
$args[0] = array();
/* These are the parameters that we're waiting for */
$names = array('where', 'orderby', 'limit', 'offset');
$params = array();
/* Getting params from the $args array */
for ($i = 0; $i < count($names); $i++) {
if (array_key_exists($names[$i], $args[0])) {
$params[$i] = $args[0][$names[$i]];
} else {
$params[$i] = null;
}
}
return call_user_func_array('wpgd_videos_get_videos_categories', $params);
}
function wpgd_getVideosByCategory($args){
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
/* Just making sure that we can keep rocking */
if (!isset($args[0]))
$args[0] = array();
/* These are the parameters that we're waiting for */
$names = array('category', 'orderby', 'limit', 'offset');
$params = array();
/* Getting params from the $args array */
for ($i = 0; $i < count($names); $i++) {
if (array_key_exists($names[$i], $args[0])) {
$params[$i] = $args[0][$names[$i]];
} else {
$params[$i] = null;
}
}
return call_user_func_array('wpgd_videos_get_bycategory', $params);
}
/**
* A wrapper for the function that lists videos
*
* @param array $args Holds parameters to be passed to the actuall
* function being wrapped. Please see the wpgd_videos_get_videos()
* function for a deeper description.
*/
function wpgd_getVideos($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
/* Just making sure that we can keep rocking */
if (!isset($args[0]))
$args[0] = array();
/* These are the parameters that we're waiting for */
$names = array('where', 'orderby', 'limit', 'offset');
$params = array();
/* Getting params from the $args array */
for ($i = 0; $i < count($names); $i++) {
if (array_key_exists($names[$i], $args[0])) {
$params[$i] = $args[0][$names[$i]];
} else {
$params[$i] = null;
}
}
return call_user_func_array('wpgd_videos_get_videos', $params);
}
/**
* A wrapper for the function that gets a video by its id
*
* @param array $args Holds parameters to be passed to the actuall
* function being wrapped and, in this case, you can pass the video id.
*/
function wpgd_getVideo($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
return wpgd_videos_get_video($args[1]);
}
/**
* A wrapper for the function that lists the sources of a video
*
* @param array $args Holds parameters to be passed to the actuall
* function being wrapped and, in this case, you can pass the video id.
*/
function wpgd_getVideoSources($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
return wpgd_videos_get_sources($args[1]);
}
/* ---- Pairwise API ---- */
/**
* A wrapper for the function that gets pairwise choices sorted by its
* scores.
*
* @param array $args Holds parameters to be passed to the actuall
* function being wrapped and, in this case, you need to pass the
* `page', the `perpage' and `theme' params.
*/
function pairwise_getSortedByScore($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
return wpgd_pairwise_get_sorted_by_score($args[1], $args[2], $args[3]);
}
/* ---- Gallery related API ---- */
function wpgd_getGalleries($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
global $nggdb;
/* Actually getting the gallery list */
$galleries = $nggdb->find_all_galleries('gid', 'DESC', TRUE);
$result = array();
/* Getting the preview pic in the same request of the listing */
foreach ($galleries as $g) {
$g->front = $nggdb->find_image($g->previewpic);
$result[] = $g;
}
return $result;
}
function wpgd_getGallery($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
global $nggdb;
global $nggv;
$result = $nggdb->find_gallery($args[1]);
$result->images = array();
foreach ($nggdb->get_gallery($args[1]) as $image) {
$result->images[] = $image;
}
// $result->avg = nggv_getVotingResults($result->gid, array("avg"=>true, "list"=>false, "number"=>false, "likes"=>false, "dislikes"=>false))['avg'] ;
// $result->usercanvote = nggv_canVote($result->gid); #Verifica se a galeria pode ser votada
$result->avg = 0;
$result->usercanvote = true;
return $result;
}
function wpgd_getImage($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
global $nggdb;
return $nggdb->find_image($args[1]);
}
function wpgd_getLastFromGallery($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
global $nggdb;
$galery = $nggdb->get_gallery($args[1], 'pid', 'DESC');
if( $galery && is_array($galery) ){
foreach ($galery as $key => $value) {
return $galery[$key] ;
break;
}
}else{
return $galery;
}
}
function wpgd_searchGalleries($args) {
if (!is_array($args = _wpgd_method_header($args))) {
return $args;
}
global $nggdb;
$galleries = $nggdb->search_for_galleries($args[1]);
$result = array();
/* Getting the preview pic in the same request of the listing */
foreach ($galleries as $g) {
$g->front = $nggdb->find_image($g->previewpic);
$result[] = $g;
}
return $result;
}
/* Filter that registers our methods in the wordpress xmlrpc provider */
add_filter('xmlrpc_methods', function ($methods) {
$methods['wpgd.getVideo'] = 'wpgd_getVideo';
$methods['wpgd.getVideos'] = 'wpgd_getVideos';
$methods['wpgd.getHighlightedVideos'] = 'wpgd_getHighlightedVideos';
$methods['wpgd.getVideosCategories'] = 'wpgd_getVideosCategories';
$methods['wpgd.getVideosByCategory'] = 'wpgd_getVideosByCategory';
$methods['wpgd.setVideoViews'] = 'wpgd_setVideoViews';
$methods['wpgd.getVideoSources'] = 'wpgd_getVideoSources';
$methods['wpgd.getGalleries'] = 'wpgd_getGalleries';
$methods['wpgd.getGallery'] = 'wpgd_getGallery';
$methods['wpgd.getImage'] = 'wpgd_getImage';
$methods['wpgd.getLastFromGallery'] = 'wpgd_getLastFromGallery';
$methods['wpgd.searchGalleries'] = 'wpgd_searchGalleries';
$methods['pairwise.getSortedByScore'] = 'pairwise_getSortedByScore';
return $methods;
});
?>