| 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_module', |
| 13 |
'set_module', |
| 14 |
'get_redirect', |
| 15 |
'set_redirect', |
| 16 |
'redirect_action', |
| 17 |
'get_group', |
| 18 |
'set_group', |
| 19 |
'group_action', |
| 20 |
); |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
foreach ( $this->endpoints as $point ) { |
| 24 |
add_action( 'wp_ajax_red_'.$point, array( $this, 'check_auth' ), 9 ); |
| 25 |
add_action( 'wp_ajax_red_'.$point, array( $this, 'ajax_'.$point ), 10 ); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function check_auth( $params ) { |
| 30 |
if ( check_ajax_referer( 'wp_rest', false, false ) === false ) { |
| 31 |
wp_die( $this->output_ajax_response( array( 'error' => __( 'Unable to perform action' ).' - bad nonce "wp_rest" ('.__LINE__.')' ) ) ); |
| 32 |
} |
| 33 |
|
| 34 |
if ( $this->user_has_access() === false ) { |
| 35 |
wp_die( $this->output_ajax_response( array( 'error' => __( 'No permissions to perform action ('.__LINE__.')' ) ) ) ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
private function get_params( $params ) { |
| 40 |
if ( empty( $params ) ) { |
| 41 |
$params = $_POST; |
| 42 |
} |
| 43 |
|
| 44 |
return $params; |
| 45 |
} |
| 46 |
|
| 47 |
public function ajax_group_action( $params ) { |
| 48 |
$params = $this->get_params( $params ); |
| 49 |
|
| 50 |
$action = false; |
| 51 |
$items = array(); |
| 52 |
if ( isset( $params['items'] ) ) { |
| 53 |
$items = array_map( 'intval', explode( ',', $params['items'] ) ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable' ) ) ) { |
| 57 |
$action = $params['bulk']; |
| 58 |
|
| 59 |
foreach ( $items as $item ) { |
| 60 |
$group = Red_Group::get( intval( $item, 10 ) ); |
| 61 |
|
| 62 |
if ( $group ) { |
| 63 |
if ( $action === 'delete' ) { |
| 64 |
$group->delete(); |
| 65 |
} else if ( $action === 'disable' ) { |
| 66 |
$group->disable(); |
| 67 |
} else if ( $action === 'enable' ) { |
| 68 |
$group->enable(); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $this->output_ajax_response( Red_Group::get_filtered( $params ) ); |
| 75 |
} |
| 76 |
|
| 77 |
public function ajax_get_group( $params ) { |
| 78 |
$params = $this->get_params( $params ); |
| 79 |
|
| 80 |
return $this->output_ajax_response( Red_Group::get_filtered( $params ) ); |
| 81 |
} |
| 82 |
|
| 83 |
public function ajax_set_group( $params ) { |
| 84 |
$params = $this->get_params( $params ); |
| 85 |
|
| 86 |
$groupId = 0; |
| 87 |
$name = ''; |
| 88 |
$moduleId = 0; |
| 89 |
if ( isset( $params['id'] ) ) { |
| 90 |
$groupId = intval( $params['id'], 10 ); |
| 91 |
} |
| 92 |
|
| 93 |
$result = array( 'error' => 'Invalid group or parameters ('.__LINE__.')' ); |
| 94 |
if ( $groupId > 0 ) { |
| 95 |
$group = Red_Group::get( $groupId ); |
| 96 |
|
| 97 |
if ( $group && isset( $params['name'] ) && isset( $params['moduleId'] ) ) { |
| 98 |
if ( $group->update( $params ) ) { |
| 99 |
$result = array( 'item' => $group->to_json() ); |
| 100 |
} |
| 101 |
} |
| 102 |
} else { |
| 103 |
$group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 ); |
| 104 |
|
| 105 |
if ( $group ) { |
| 106 |
$result = Red_Group::get_filtered( $params ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $this->output_ajax_response( $result ); |
| 111 |
} |
| 112 |
|
| 113 |
public function ajax_get_redirect( $params ) { |
| 114 |
$params = $this->get_params( $params ); |
| 115 |
|
| 116 |
return $this->output_ajax_response( Red_Item::get_filtered( $params ) ); |
| 117 |
} |
| 118 |
|
| 119 |
public function ajax_set_redirect( $params ) { |
| 120 |
$params = $this->get_params( $params ); |
| 121 |
|
| 122 |
$redirectId = 0; |
| 123 |
if ( isset( $params['id'] ) ) { |
| 124 |
$redirectId = intval( $params['id'], 10 ); |
| 125 |
} |
| 126 |
|
| 127 |
$result = array( 'error' => 'Invalid redirect details ('.__LINE__.')' ); |
| 128 |
if ( $redirectId === 0 ) { |
| 129 |
$redirect = Red_Item::create( $params ); |
| 130 |
|
| 131 |
if ( ! is_wp_error( $redirect ) ) { |
| 132 |
$result = Red_Item::get_filtered( $params ); |
| 133 |
} |
| 134 |
} else { |
| 135 |
$redirect = Red_Item::get_by_id( $redirectId ); |
| 136 |
|
| 137 |
if ( $redirect && ! is_wp_error( $redirect->update( $params ) ) ) { |
| 138 |
$result = array( 'item' => $redirect->to_json() ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $this->output_ajax_response( $result ); |
| 143 |
} |
| 144 |
|
| 145 |
public function ajax_redirect_action( $params ) { |
| 146 |
$params = $this->get_params( $params ); |
| 147 |
|
| 148 |
$action = false; |
| 149 |
$items = array(); |
| 150 |
if ( isset( $params['items'] ) ) { |
| 151 |
$items = array_map( 'intval', explode( ',', $params['items'] ) ); |
| 152 |
} |
| 153 |
if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable', 'reset' ) ) ) { |
| 154 |
$action = $params['bulk']; |
| 155 |
|
| 156 |
foreach ( $items as $item ) { |
| 157 |
$redirect = Red_Item::get_by_id( intval( $item, 10 ) ); |
| 158 |
|
| 159 |
if ( $redirect ) { |
| 160 |
if ( $action === 'delete' ) { |
| 161 |
$redirect->delete(); |
| 162 |
} else if ( $action === 'disable' ) { |
| 163 |
$redirect->disable(); |
| 164 |
} else if ( $action === 'enable' ) { |
| 165 |
$redirect->enable(); |
| 166 |
} else if ( $action === 'reset' ) { |
| 167 |
$redirect->reset(); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return $this->output_ajax_response( Red_Item::get_filtered( $params ) ); |
| 174 |
} |
| 175 |
|
| 176 |
public function ajax_get_module( $params ) { |
| 177 |
$params = $this->get_params( $params ); |
| 178 |
|
| 179 |
$moduleType = false; |
| 180 |
|
| 181 |
$modules = array( 1, 2, 3 ); |
| 182 |
if ( isset( $params['moduleId'] ) ) { |
| 183 |
$moduleId = intval( $params['moduleId'], 10 ); |
| 184 |
|
| 185 |
if ( $moduleId > 0 && $moduleId <= 3 ) { |
| 186 |
$modules = array( $moduleId ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if ( isset( $params['moduleType'] ) && in_array( $params['moduleType'], array( 'csv', 'apache', 'nginx' ) ) ) { |
| 191 |
$moduleType = $params['moduleType']; |
| 192 |
} |
| 193 |
|
| 194 |
foreach ( $modules as $moduleId ) { |
| 195 |
$module = Red_Module::get( $moduleId ); |
| 196 |
$moduleData = $this->get_module_data( $module, $moduleType ); |
| 197 |
|
| 198 |
if ( $module->get_id() === Apache_Module::MODULE_ID ) { |
| 199 |
$moduleData['data'] = array( |
| 200 |
'installed' => ABSPATH, |
| 201 |
'location' => $module->get_location(), |
| 202 |
'canonical' => $module->get_canonical(), |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
$result[] = $moduleData; |
| 207 |
} |
| 208 |
|
| 209 |
return $this->output_ajax_response( $result ); |
| 210 |
} |
| 211 |
|
| 212 |
public function ajax_set_module( $params ) { |
| 213 |
$params = $this->get_params( $params ); |
| 214 |
|
| 215 |
$result = array( 'error' => 'Failed to save module' ); |
| 216 |
if ( isset( $params['module'] ) ) { |
| 217 |
$module = Red_Module::get( $params[ 'module' ] ); |
| 218 |
|
| 219 |
if ( $module ) { |
| 220 |
$module->update( $params ); |
| 221 |
return $this->ajax_get_module( $params ); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return $this->output_ajax_response( $result ); |
| 226 |
} |
| 227 |
|
| 228 |
private function get_module( $module_name ) { |
| 229 |
$module_id_name = array( |
| 230 |
'apache' => Apache_Module::MODULE_ID, |
| 231 |
'wordpress' => WordPress_Module::MODULE_ID, |
| 232 |
'nginx' => Nginx_Module::MODULE_ID, |
| 233 |
); |
| 234 |
|
| 235 |
if ( isset( $module_id_name[ $module_name ] ) ) { |
| 236 |
return Red_Module::get( $module_id_name[ $module_name ] ); |
| 237 |
} |
| 238 |
|
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
private function get_module_data( $module, $type ) { |
| 243 |
$data = array( |
| 244 |
'redirects' => $module->get_total_redirects(), |
| 245 |
'module_id' => $module->get_id(), |
| 246 |
'displayName' => $module->get_name(), |
| 247 |
); |
| 248 |
|
| 249 |
if ( $type ) { |
| 250 |
$exporter = Red_FileIO::create( $type ); |
| 251 |
$data['data'] = $exporter->get( Red_Item::get_all_for_module( $module->get_id() ) ); |
| 252 |
} |
| 253 |
|
| 254 |
return $data; |
| 255 |
} |
| 256 |
|
| 257 |
public function ajax_delete_plugin() { |
| 258 |
$plugin = Redirection_Admin::init(); |
| 259 |
$plugin->plugin_uninstall(); |
| 260 |
|
| 261 |
$current = get_option( 'active_plugins' ); |
| 262 |
array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 ); |
| 263 |
update_option( 'active_plugins', $current ); |
| 264 |
|
| 265 |
return $this->output_ajax_response( array( 'location' => admin_url().'plugins.php' ) ); |
| 266 |
} |
| 267 |
|
| 268 |
public function ajax_load_settings() { |
| 269 |
return $this->output_ajax_response( array( 'settings' => red_get_options(), 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) ); |
| 270 |
} |
| 271 |
|
| 272 |
public function ajax_save_settings( $settings = array() ) { |
| 273 |
$settings = $this->get_params( $settings ); |
| 274 |
$options = red_get_options(); |
| 275 |
|
| 276 |
if ( isset( $settings['monitor_post'] ) ) { |
| 277 |
$options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) ); |
| 278 |
} |
| 279 |
|
| 280 |
if ( isset( $settings['auto_target'] ) ) { |
| 281 |
$options['auto_target'] = stripslashes( $settings['auto_target'] ); |
| 282 |
} |
| 283 |
|
| 284 |
if ( isset( $settings['support'] ) ) { |
| 285 |
$options['support'] = $settings['support'] === 'true' ? true : false; |
| 286 |
} |
| 287 |
|
| 288 |
if ( isset( $settings['token'] ) ) { |
| 289 |
$options['token'] = stripslashes( $settings['token'] ); |
| 290 |
} |
| 291 |
|
| 292 |
if ( !isset( $settings['token'] ) || trim( $options['token'] ) === '' ) { |
| 293 |
$options['token'] = md5( uniqid() ); |
| 294 |
} |
| 295 |
|
| 296 |
if ( isset( $settings['newsletter'] ) ) { |
| 297 |
$options['newsletter'] = $settings['newsletter'] === 'true' ? true : false; |
| 298 |
} |
| 299 |
|
| 300 |
if ( isset( $settings['expire_redirect'] ) ) { |
| 301 |
$options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) ); |
| 302 |
} |
| 303 |
|
| 304 |
if ( isset( $settings['expire_404'] ) ) { |
| 305 |
$options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) ); |
| 306 |
} |
| 307 |
|
| 308 |
update_option( 'redirection_options', $options ); |
| 309 |
do_action( 'redirection_save_options', $options ); |
| 310 |
|
| 311 |
return $this->output_ajax_response( array( 'settings' => $options, 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) ); |
| 312 |
} |
| 313 |
|
| 314 |
public function ajax_get_logs( $params ) { |
| 315 |
$params = $this->get_params( $params ); |
| 316 |
$result = $this->get_logs( $params ); |
| 317 |
|
| 318 |
return $this->output_ajax_response( $result ); |
| 319 |
} |
| 320 |
|
| 321 |
public function ajax_log_action( $params ) { |
| 322 |
$params = $this->get_params( $params ); |
| 323 |
|
| 324 |
// Do the action |
| 325 |
if ( isset( $params['bulk'] ) && isset( $params['items'] ) && $params['bulk'] === 'delete' ) { |
| 326 |
$items = explode( ',', $params['items'] ); |
| 327 |
|
| 328 |
if ( $this->get_log_type( $params ) === 'log' ) { |
| 329 |
array_map( array( 'RE_Log', 'delete' ), $items ); |
| 330 |
} else { |
| 331 |
array_map( array( 'RE_404', 'delete' ), $items ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
$result = $this->get_logs( $params ); |
| 336 |
|
| 337 |
return $this->output_ajax_response( $result ); |
| 338 |
} |
| 339 |
|
| 340 |
public function ajax_delete_all( $params ) { |
| 341 |
$params = $this->get_params( $params ); |
| 342 |
|
| 343 |
if ( isset( $params['logType'] ) ) { |
| 344 |
if ( $params['logType'] === 'log' ) { |
| 345 |
RE_Log::delete_all(); |
| 346 |
} else { |
| 347 |
RE_404::delete_all(); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
$result = $this->get_logs( $params ); |
| 352 |
|
| 353 |
return $this->output_ajax_response( $result ); |
| 354 |
} |
| 355 |
|
| 356 |
private function get_log_type( $params ) { |
| 357 |
$type = 'log'; |
| 358 |
|
| 359 |
if ( isset( $params['logType'] ) && in_array( $params['logType'], array( 'log', '404' ), true ) ) { |
| 360 |
$type = $params['logType']; |
| 361 |
} |
| 362 |
|
| 363 |
return $type; |
| 364 |
} |
| 365 |
|
| 366 |
private function get_logs( array $params ) { |
| 367 |
$type = $this->get_log_type( $params ); |
| 368 |
|
| 369 |
if ( $type === 'log' ) { |
| 370 |
return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params ); |
| 371 |
} else if ( $type === '404' ) { |
| 372 |
if ( isset( $params['filterBy'] ) && isset( $params['filter'] ) && $params['filterBy'] === 'ip' ) { |
| 373 |
$params['filter'] = ip2long( $params['filter'] ); |
| 374 |
} |
| 375 |
|
| 376 |
return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params ); |
| 377 |
} |
| 378 |
|
| 379 |
return array( 'items' => array(), 'total' => 0 ); |
| 380 |
} |
| 381 |
|
| 382 |
private function groups_to_json( $groups, $depth = 0 ) { |
| 383 |
$items = array(); |
| 384 |
|
| 385 |
foreach ( $groups as $text => $value ) { |
| 386 |
if ( is_array( $value ) && $depth === 0 ) { |
| 387 |
$items[] = (object)array( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) ); |
| 388 |
} else { |
| 389 |
$items[] = (object)array( 'text' => $value, 'value' => $text ); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return $items; |
| 394 |
} |
| 395 |
|
| 396 |
private function output_ajax_response( $data ) { |
| 397 |
$result = wp_json_encode( $data ); |
| 398 |
|
| 399 |
if ( defined( 'DOING_AJAX' ) ) { |
| 400 |
header( 'Content-Type: application/json' ); |
| 401 |
echo $result; |
| 402 |
wp_die(); |
| 403 |
} |
| 404 |
|
| 405 |
return $result; |
| 406 |
} |
| 407 |
|
| 408 |
private function user_has_access() { |
| 409 |
return current_user_can( apply_filters( 'redirection_role', 'administrator' ) ); |
| 410 |
} |
| 411 |
} |
| 412 |
|