PluginProbe
Redirection / 2.8.1
Redirection v2.8.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-api.php

redirection-api.php in Redirection 2.8.1, at redirection-api.php

482 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Todo: use the JSON API
4 class Redirection_Api {
5 public $endpoints = array(
6 'load_settings',
7 'save_settings',
8 'get_logs',
9 'log_action',
10 'delete_all',
11 'delete_plugin',
12 'get_redirect',
13 'set_redirect',
14 'redirect_action',
15 'get_group',
16 'set_group',
17 'group_action',
18 'import_data',
19 'export_data',
20 'ping',
21 'plugin_status',
22 );
23
24 public function __construct() {
25 global $wpdb;
26
27 $wpdb->hide_errors();
28
29 foreach ( $this->endpoints as $point ) {
30 add_action( 'wp_ajax_red_'.$point, array( $this, 'check_auth' ), 9 );
31 add_action( 'wp_ajax_red_'.$point, array( $this, 'ajax_'.$point ), 10 );
32 }
33 }
34
35 private function addDatabaseError( $response ) {
36 global $wpdb;
37
38 if ( isset( $response['error'] ) && isset( $wpdb->last_error ) && $wpdb->last_error ) {
39 $response['error']['wpdb'] = $wpdb->last_error;
40 }
41
42 return $response;
43 }
44
45 private function getError( $message, $line ) {
46 return array(
47 'error' => array(
48 'message' => $message,
49 'code' => $line,
50 )
51 );
52 }
53
54 public function check_auth( $params ) {
55 if ( check_ajax_referer( 'wp_rest', false, false ) === false ) {
56 $error = $this->getError( 'Unable to perform action - bad nonce "wp_rest"', __LINE__ );
57 $error['error']['action'] = 'reload';
58 wp_die( $this->output_ajax_response( $error ) );
59 }
60
61 if ( $this->user_has_access() === false ) {
62 wp_die( $this->output_ajax_response( $this->getError( 'No permissions to perform action', __LINE__ ) ) );
63 }
64 }
65
66 private function get_params( $params ) {
67 if ( empty( $params ) ) {
68 $params = $_POST;
69 }
70
71 return $params;
72 }
73
74 public function ajax_ping() {
75 return $this->output_ajax_response( array( 'nonce' => wp_create_nonce( 'wp_rest' ) ) );
76 }
77
78 public function ajax_import_data( $params ) {
79 $params = $this->get_params( $params );
80 $upload = isset( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : false;
81 $group_id = isset( $params['group'] ) ? intval( $params['group'], 10 ) : 0;
82
83 $result = $this->getError( 'Invalid file', __LINE__ );
84 if ( $upload && is_uploaded_file( $upload['tmp_name'] ) ) {
85 $result = $this->getError( 'Invalid group', __LINE__ );
86
87 $count = Red_FileIO::import( $group_id, $upload );
88 if ( $count !== false ) {
89 $result = array(
90 'imported' => $count,
91 );
92 }
93 }
94
95 return $this->output_ajax_response( $result );
96 }
97
98 public function ajax_export_data( $params ) {
99 $params = $this->get_params( $params );
100 $moduleId = isset( $params['module'] ) ? intval( $params['module'], 10 ) : false;
101 $format = 'json';
102
103 if ( isset( $params['format'] ) && in_array( $params['format'], array( 'csv', 'apache', 'nginx', 'json' ) ) ) {
104 $format = $params['format'];
105 }
106
107 $result = $this->getError( 'Invalid module', __LINE__ );
108
109 $export = Red_FileIO::export( $moduleId, $format );
110 if ( $export !== false ) {
111 $result = array(
112 'data' => $export['data'],
113 'total' => $export['total'],
114 );
115 }
116
117 return $this->output_ajax_response( $result );
118 }
119
120 public function ajax_group_action( $params ) {
121 $params = $this->get_params( $params );
122
123 $action = false;
124 $items = array();
125 if ( isset( $params['items'] ) ) {
126 $items = array_map( 'intval', explode( ',', $params['items'] ) );
127 }
128
129 if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable' ) ) ) {
130 $action = $params['bulk'];
131
132 foreach ( $items as $item ) {
133 $group = Red_Group::get( intval( $item, 10 ) );
134
135 if ( $group ) {
136 if ( $action === 'delete' ) {
137 $group->delete();
138 } else if ( $action === 'disable' ) {
139 $group->disable();
140 } else if ( $action === 'enable' ) {
141 $group->enable();
142 }
143 }
144 }
145 }
146
147 return $this->output_ajax_response( Red_Group::get_filtered( $params ) );
148 }
149
150 public function ajax_get_group( $params ) {
151 $params = $this->get_params( $params );
152
153 return $this->output_ajax_response( Red_Group::get_filtered( $params ) );
154 }
155
156 public function ajax_set_group( $params ) {
157 $params = $this->get_params( $params );
158
159 $groupId = 0;
160 $name = '';
161 $moduleId = 0;
162 if ( isset( $params['id'] ) ) {
163 $groupId = intval( $params['id'], 10 );
164 }
165
166 $result = $this->getError( 'Invalid group or parameters', __LINE__ );
167 if ( $groupId > 0 ) {
168 $group = Red_Group::get( $groupId );
169
170 if ( $group && isset( $params['name'] ) && isset( $params['moduleId'] ) ) {
171 if ( $group->update( $params ) ) {
172 $result = array( 'item' => $group->to_json() );
173 }
174 }
175 } else {
176 $group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 );
177
178 if ( $group ) {
179 $result = Red_Group::get_filtered( $params );
180 }
181 }
182
183 return $this->output_ajax_response( $result );
184 }
185
186 public function ajax_get_redirect( $params ) {
187 $params = $this->get_params( $params );
188
189 return $this->output_ajax_response( Red_Item::get_filtered( $params ) );
190 }
191
192 public function ajax_set_redirect( $params ) {
193 $params = $this->get_params( $params );
194
195 $redirectId = 0;
196 if ( isset( $params['id'] ) ) {
197 $redirectId = intval( $params['id'], 10 );
198 }
199
200 $result = $this->getError( 'Invalid redirect details', __LINE__ );
201 if ( $redirectId === 0 ) {
202 $redirect = Red_Item::create( $params );
203
204 if ( is_wp_error( $redirect ) ) {
205 $result = $this->getError( $redirect->get_error_message(), __LINE__ );
206 } else {
207 $result = Red_Item::get_filtered( $params );
208 }
209 } else {
210 $redirect = Red_Item::get_by_id( $redirectId );
211
212 if ( $redirect ) {
213 $result = $redirect->update( $params );
214
215 if ( is_wp_error( $result ) ) {
216 $result = $this->getError( $result->get_error_message(), __LINE__ );
217 } else {
218 $result = array( 'item' => $redirect->to_json() );
219 }
220 }
221 }
222
223 return $this->output_ajax_response( $result );
224 }
225
226 public function ajax_redirect_action( $params ) {
227 $params = $this->get_params( $params );
228
229 $action = false;
230 $items = array();
231
232 if ( isset( $params['items'] ) ) {
233 $items = array_map( 'intval', explode( ',', $params['items'] ) );
234 }
235
236 if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable', 'reset' ) ) ) {
237 $action = $params['bulk'];
238
239 foreach ( $items as $item ) {
240 $redirect = Red_Item::get_by_id( intval( $item, 10 ) );
241
242 if ( $redirect ) {
243 if ( $action === 'delete' ) {
244 $redirect->delete();
245 } else if ( $action === 'disable' ) {
246 $redirect->disable();
247 } else if ( $action === 'enable' ) {
248 $redirect->enable();
249 } else if ( $action === 'reset' ) {
250 $redirect->reset();
251 }
252 }
253 }
254 }
255
256 return $this->output_ajax_response( Red_Item::get_filtered( $params ) );
257 }
258
259 public function ajax_delete_plugin() {
260 $plugin = Redirection_Admin::init();
261 $plugin->plugin_uninstall();
262
263 $current = get_option( 'active_plugins' );
264 array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 );
265 update_option( 'active_plugins', $current );
266
267 return $this->output_ajax_response( array( 'location' => admin_url().'plugins.php' ) );
268 }
269
270 public function ajax_load_settings() {
271 return $this->output_ajax_response( array(
272 'settings' => red_get_options(),
273 'groups' => $this->groups_to_json( Red_Group::get_for_select() ),
274 'installed' => get_home_path(),
275 ) );
276 }
277
278 public function ajax_save_settings( $settings = array() ) {
279 $settings = $this->get_params( $settings );
280 $options = red_get_options();
281 $monitor_types = array();
282
283 if ( isset( $settings['monitor_type_post'] ) && $settings['monitor_type_post'] === 'true' ) {
284 $monitor_types[] = 'post';
285 }
286
287 if ( isset( $settings['monitor_type_page'] ) && $settings['monitor_type_page'] === 'true' ) {
288 $monitor_types[] = 'page';
289 }
290
291 if ( isset( $settings['monitor_type_trash'] ) && $settings['monitor_type_trash'] === 'true' ) {
292 $monitor_types[] = 'trash';
293 }
294
295 if ( isset( $settings['monitor_post'] ) ) {
296 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
297
298 if ( ! Red_Group::get( $options['monitor_post'] ) ) {
299 $groups = Red_Group::get_all();
300 $options['monitor_post'] = $groups[ 0 ]['id'];
301 }
302 }
303
304 if ( isset( $settings['associated_redirect'] ) ) {
305 $sanitizer = new Red_Item_Sanitize();
306 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
307 }
308
309 if ( count( $monitor_types ) === 0 ) {
310 $options['monitor_post'] = 0;
311 $options['associated_redirect'] = '';
312 }
313
314 if ( isset( $settings['auto_target'] ) ) {
315 $options['auto_target'] = stripslashes( $settings['auto_target'] );
316 }
317
318 if ( isset( $settings['support'] ) ) {
319 $options['support'] = $settings['support'] === 'true' ? true : false;
320 }
321
322 if ( isset( $settings['token'] ) ) {
323 $options['token'] = stripslashes( $settings['token'] );
324 }
325
326 if ( !isset( $settings['token'] ) || trim( $options['token'] ) === '' ) {
327 $options['token'] = md5( uniqid() );
328 }
329
330 if ( isset( $settings['newsletter'] ) ) {
331 $options['newsletter'] = $settings['newsletter'] === 'true' ? true : false;
332 }
333
334 if ( isset( $settings['expire_redirect'] ) ) {
335 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
336 }
337
338 if ( isset( $settings['expire_404'] ) ) {
339 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
340 }
341
342 $module = Red_Module::get( 2 );
343 $options['modules'][2] = $module->update( $settings );
344 $options['monitor_types'] = $monitor_types;
345
346 update_option( 'redirection_options', $options );
347 do_action( 'redirection_save_options', $options );
348
349 return $this->ajax_load_settings();
350 }
351
352 public function ajax_get_logs( $params ) {
353 $params = $this->get_params( $params );
354 $result = $this->get_logs( $params );
355
356 return $this->output_ajax_response( $result );
357 }
358
359 public function ajax_log_action( $params ) {
360 $params = $this->get_params( $params );
361
362 // Do the action
363 if ( isset( $params['bulk'] ) && isset( $params['items'] ) && $params['bulk'] === 'delete' ) {
364 $items = explode( ',', $params['items'] );
365
366 if ( $this->get_log_type( $params ) === 'log' ) {
367 array_map( array( 'RE_Log', 'delete' ), $items );
368 } else {
369 array_map( array( 'RE_404', 'delete' ), $items );
370 }
371 }
372
373 $result = $this->get_logs( $params );
374
375 return $this->output_ajax_response( $result );
376 }
377
378 public function ajax_delete_all( $params ) {
379 $params = $this->get_params( $params );
380 $filter = '';
381 $filterBy = '';
382 if ( isset( $params['filter'] ) ) {
383 $filter = $params['filter'];
384 }
385
386 if ( isset( $params['filterBy'] ) && in_array( $params['filterBy'], array( 'url', 'ip', 'url-exact' ), true ) ) {
387 $filterBy = $params['filterBy'];
388 unset( $params['filter'] );
389 unset( $params['filterBy'] );
390 }
391
392 if ( isset( $params['logType'] ) ) {
393 if ( $params['logType'] === 'log' ) {
394 RE_Log::delete_all( $filterBy, $filter );
395 } else {
396 RE_404::delete_all( $filterBy, $filter );
397 }
398 }
399
400 $result = $this->get_logs( $params );
401
402 return $this->output_ajax_response( $result );
403 }
404
405 public function ajax_plugin_status( $params ) {
406 $params = $this->get_params( $params );
407
408 $fixit = false;
409 if ( isset( $params['fixIt'] ) && $params['fixIt'] === 'true' ) {
410 $fixit = true;
411 }
412
413 include_once dirname( REDIRECTION_FILE ).'/models/fixer.php';
414
415 $fixer = new Red_Fixer();
416 $result = $fixer->get_status();
417
418 if ( $fixit ) {
419 $result = $fixer->fix( $result );
420 }
421
422 return $this->output_ajax_response( $result );
423 }
424
425 private function get_log_type( $params ) {
426 $type = 'log';
427
428 if ( isset( $params['logType'] ) && in_array( $params['logType'], array( 'log', '404' ), true ) ) {
429 $type = $params['logType'];
430 }
431
432 return $type;
433 }
434
435 private function get_logs( array $params ) {
436 $type = $this->get_log_type( $params );
437
438 if ( $type === 'log' ) {
439 return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params );
440 } else if ( $type === '404' ) {
441 if ( isset( $params['filterBy'] ) && isset( $params['filter'] ) && $params['filterBy'] === 'ip' ) {
442 $params['filter'] = ip2long( $params['filter'] );
443 }
444
445 return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
446 }
447
448 return array( 'items' => array(), 'total' => 0 );
449 }
450
451 private function groups_to_json( $groups, $depth = 0 ) {
452 $items = array();
453
454 foreach ( $groups as $text => $value ) {
455 if ( is_array( $value ) && $depth === 0 ) {
456 $items[] = (object)array( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) );
457 } else {
458 $items[] = (object)array( 'text' => $value, 'value' => $text );
459 }
460 }
461
462 return $items;
463 }
464
465 private function output_ajax_response( $data ) {
466 $data = $this->addDatabaseError( $data );
467 $result = wp_json_encode( $data );
468
469 if ( defined( 'DOING_AJAX' ) ) {
470 header( 'Content-Type: application/json' );
471 echo $result;
472 wp_die();
473 }
474
475 return $result;
476 }
477
478 private function user_has_access() {
479 return current_user_can( apply_filters( 'redirection_role', 'administrator' ) );
480 }
481 }
482