PluginProbe
Redirection / 2.10
Redirection v2.10
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.10, at redirection-api.php

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