| 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 |
); |
| 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_import_data( $params ) { |
| 48 |
$params = $this->get_params( $params ); |
| 49 |
$upload = isset( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : false; |
| 50 |
$group_id = isset( $params['group'] ) ? intval( $params['group'], 10 ) : 0; |
| 51 |
|
| 52 |
$result = array( 'error' => 'Invalid file ('.__LINE__.')' ); |
| 53 |
if ( $upload && is_uploaded_file( $upload['tmp_name'] ) ) { |
| 54 |
$result = array( 'error' => 'Invalid group ('.__LINE__.')' ); |
| 55 |
|
| 56 |
$count = Red_FileIO::import( $group_id, $upload ); |
| 57 |
if ( $count !== false ) { |
| 58 |
$result = array( |
| 59 |
'imported' => $count, |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $this->output_ajax_response( $result ); |
| 65 |
} |
| 66 |
|
| 67 |
public function ajax_export_data( $params ) { |
| 68 |
$params = $this->get_params( $params ); |
| 69 |
$moduleId = isset( $params['module'] ) ? intval( $params['module'], 10 ) : false; |
| 70 |
$format = 'json'; |
| 71 |
|
| 72 |
if ( isset( $params['format'] ) && in_array( $params['format'], array( 'csv', 'apache', 'nginx', 'json' ) ) ) { |
| 73 |
$format = $params['format']; |
| 74 |
} |
| 75 |
|
| 76 |
$result = array( 'error' => 'Invalid module ('.__LINE__.')' ); |
| 77 |
|
| 78 |
$export = Red_FileIO::export( $moduleId, $format ); |
| 79 |
if ( $export !== false ) { |
| 80 |
$result = array( |
| 81 |
'data' => $export['data'], |
| 82 |
'total' => $export['total'], |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
return $this->output_ajax_response( $result ); |
| 87 |
} |
| 88 |
|
| 89 |
public function ajax_group_action( $params ) { |
| 90 |
$params = $this->get_params( $params ); |
| 91 |
|
| 92 |
$action = false; |
| 93 |
$items = array(); |
| 94 |
if ( isset( $params['items'] ) ) { |
| 95 |
$items = array_map( 'intval', explode( ',', $params['items'] ) ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable' ) ) ) { |
| 99 |
$action = $params['bulk']; |
| 100 |
|
| 101 |
foreach ( $items as $item ) { |
| 102 |
$group = Red_Group::get( intval( $item, 10 ) ); |
| 103 |
|
| 104 |
if ( $group ) { |
| 105 |
if ( $action === 'delete' ) { |
| 106 |
$group->delete(); |
| 107 |
} else if ( $action === 'disable' ) { |
| 108 |
$group->disable(); |
| 109 |
} else if ( $action === 'enable' ) { |
| 110 |
$group->enable(); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return $this->output_ajax_response( Red_Group::get_filtered( $params ) ); |
| 117 |
} |
| 118 |
|
| 119 |
public function ajax_get_group( $params ) { |
| 120 |
$params = $this->get_params( $params ); |
| 121 |
|
| 122 |
return $this->output_ajax_response( Red_Group::get_filtered( $params ) ); |
| 123 |
} |
| 124 |
|
| 125 |
public function ajax_set_group( $params ) { |
| 126 |
$params = $this->get_params( $params ); |
| 127 |
|
| 128 |
$groupId = 0; |
| 129 |
$name = ''; |
| 130 |
$moduleId = 0; |
| 131 |
if ( isset( $params['id'] ) ) { |
| 132 |
$groupId = intval( $params['id'], 10 ); |
| 133 |
} |
| 134 |
|
| 135 |
$result = array( 'error' => 'Invalid group or parameters ('.__LINE__.')' ); |
| 136 |
if ( $groupId > 0 ) { |
| 137 |
$group = Red_Group::get( $groupId ); |
| 138 |
|
| 139 |
if ( $group && isset( $params['name'] ) && isset( $params['moduleId'] ) ) { |
| 140 |
if ( $group->update( $params ) ) { |
| 141 |
$result = array( 'item' => $group->to_json() ); |
| 142 |
} |
| 143 |
} |
| 144 |
} else { |
| 145 |
$group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 ); |
| 146 |
|
| 147 |
if ( $group ) { |
| 148 |
$result = Red_Group::get_filtered( $params ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $this->output_ajax_response( $result ); |
| 153 |
} |
| 154 |
|
| 155 |
public function ajax_get_redirect( $params ) { |
| 156 |
$params = $this->get_params( $params ); |
| 157 |
|
| 158 |
return $this->output_ajax_response( Red_Item::get_filtered( $params ) ); |
| 159 |
} |
| 160 |
|
| 161 |
public function ajax_set_redirect( $params ) { |
| 162 |
$params = $this->get_params( $params ); |
| 163 |
|
| 164 |
$redirectId = 0; |
| 165 |
if ( isset( $params['id'] ) ) { |
| 166 |
$redirectId = intval( $params['id'], 10 ); |
| 167 |
} |
| 168 |
|
| 169 |
$result = array( 'error' => 'Invalid redirect details ('.__LINE__.')' ); |
| 170 |
if ( $redirectId === 0 ) { |
| 171 |
$redirect = Red_Item::create( $params ); |
| 172 |
|
| 173 |
if ( is_wp_error( $redirect ) ) { |
| 174 |
$result = array( 'error' => $redirect->get_error_message() ); |
| 175 |
} else { |
| 176 |
$result = Red_Item::get_filtered( $params ); |
| 177 |
} |
| 178 |
} else { |
| 179 |
$redirect = Red_Item::get_by_id( $redirectId ); |
| 180 |
|
| 181 |
if ( $redirect ) { |
| 182 |
$result = $redirect->update( $params ); |
| 183 |
|
| 184 |
if ( is_wp_error( $result ) ) { |
| 185 |
$result = array( 'error' => $redirect->get_error_message() ); |
| 186 |
} else { |
| 187 |
$result = array( 'item' => $redirect->to_json() ); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return $this->output_ajax_response( $result ); |
| 193 |
} |
| 194 |
|
| 195 |
public function ajax_redirect_action( $params ) { |
| 196 |
$params = $this->get_params( $params ); |
| 197 |
|
| 198 |
$action = false; |
| 199 |
$items = array(); |
| 200 |
if ( isset( $params['items'] ) ) { |
| 201 |
$items = array_map( 'intval', explode( ',', $params['items'] ) ); |
| 202 |
} |
| 203 |
if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable', 'reset' ) ) ) { |
| 204 |
$action = $params['bulk']; |
| 205 |
|
| 206 |
foreach ( $items as $item ) { |
| 207 |
$redirect = Red_Item::get_by_id( intval( $item, 10 ) ); |
| 208 |
|
| 209 |
if ( $redirect ) { |
| 210 |
if ( $action === 'delete' ) { |
| 211 |
$redirect->delete(); |
| 212 |
} else if ( $action === 'disable' ) { |
| 213 |
$redirect->disable(); |
| 214 |
} else if ( $action === 'enable' ) { |
| 215 |
$redirect->enable(); |
| 216 |
} else if ( $action === 'reset' ) { |
| 217 |
$redirect->reset(); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return $this->output_ajax_response( Red_Item::get_filtered( $params ) ); |
| 224 |
} |
| 225 |
|
| 226 |
public function ajax_delete_plugin() { |
| 227 |
$plugin = Redirection_Admin::init(); |
| 228 |
$plugin->plugin_uninstall(); |
| 229 |
|
| 230 |
$current = get_option( 'active_plugins' ); |
| 231 |
array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 ); |
| 232 |
update_option( 'active_plugins', $current ); |
| 233 |
|
| 234 |
return $this->output_ajax_response( array( 'location' => admin_url().'plugins.php' ) ); |
| 235 |
} |
| 236 |
|
| 237 |
public function ajax_load_settings() { |
| 238 |
return $this->output_ajax_response( array( |
| 239 |
'settings' => red_get_options(), |
| 240 |
'groups' => $this->groups_to_json( Red_Group::get_for_select() ), |
| 241 |
'installed' => get_home_path(), |
| 242 |
) ); |
| 243 |
} |
| 244 |
|
| 245 |
public function ajax_save_settings( $settings = array() ) { |
| 246 |
$settings = $this->get_params( $settings ); |
| 247 |
$options = red_get_options(); |
| 248 |
|
| 249 |
if ( isset( $settings['monitor_post'] ) ) { |
| 250 |
$options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) ); |
| 251 |
} |
| 252 |
|
| 253 |
if ( isset( $settings['auto_target'] ) ) { |
| 254 |
$options['auto_target'] = stripslashes( $settings['auto_target'] ); |
| 255 |
} |
| 256 |
|
| 257 |
if ( isset( $settings['support'] ) ) { |
| 258 |
$options['support'] = $settings['support'] === 'true' ? true : false; |
| 259 |
} |
| 260 |
|
| 261 |
if ( isset( $settings['token'] ) ) { |
| 262 |
$options['token'] = stripslashes( $settings['token'] ); |
| 263 |
} |
| 264 |
|
| 265 |
if ( !isset( $settings['token'] ) || trim( $options['token'] ) === '' ) { |
| 266 |
$options['token'] = md5( uniqid() ); |
| 267 |
} |
| 268 |
|
| 269 |
if ( isset( $settings['newsletter'] ) ) { |
| 270 |
$options['newsletter'] = $settings['newsletter'] === 'true' ? true : false; |
| 271 |
} |
| 272 |
|
| 273 |
if ( isset( $settings['expire_redirect'] ) ) { |
| 274 |
$options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) ); |
| 275 |
} |
| 276 |
|
| 277 |
if ( isset( $settings['expire_404'] ) ) { |
| 278 |
$options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) ); |
| 279 |
} |
| 280 |
|
| 281 |
$module = Red_Module::get( 2 ); |
| 282 |
$options['modules'][2] = $module->update( $settings ); |
| 283 |
|
| 284 |
update_option( 'redirection_options', $options ); |
| 285 |
do_action( 'redirection_save_options', $options ); |
| 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 |
|
| 319 |
if ( isset( $params['logType'] ) ) { |
| 320 |
if ( $params['logType'] === 'log' ) { |
| 321 |
RE_Log::delete_all(); |
| 322 |
} else { |
| 323 |
RE_404::delete_all(); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
$result = $this->get_logs( $params ); |
| 328 |
|
| 329 |
return $this->output_ajax_response( $result ); |
| 330 |
} |
| 331 |
|
| 332 |
private function get_log_type( $params ) { |
| 333 |
$type = 'log'; |
| 334 |
|
| 335 |
if ( isset( $params['logType'] ) && in_array( $params['logType'], array( 'log', '404' ), true ) ) { |
| 336 |
$type = $params['logType']; |
| 337 |
} |
| 338 |
|
| 339 |
return $type; |
| 340 |
} |
| 341 |
|
| 342 |
private function get_logs( array $params ) { |
| 343 |
$type = $this->get_log_type( $params ); |
| 344 |
|
| 345 |
if ( $type === 'log' ) { |
| 346 |
return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params ); |
| 347 |
} else if ( $type === '404' ) { |
| 348 |
if ( isset( $params['filterBy'] ) && isset( $params['filter'] ) && $params['filterBy'] === 'ip' ) { |
| 349 |
$params['filter'] = ip2long( $params['filter'] ); |
| 350 |
} |
| 351 |
|
| 352 |
return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params ); |
| 353 |
} |
| 354 |
|
| 355 |
return array( 'items' => array(), 'total' => 0 ); |
| 356 |
} |
| 357 |
|
| 358 |
private function groups_to_json( $groups, $depth = 0 ) { |
| 359 |
$items = array(); |
| 360 |
|
| 361 |
foreach ( $groups as $text => $value ) { |
| 362 |
if ( is_array( $value ) && $depth === 0 ) { |
| 363 |
$items[] = (object)array( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) ); |
| 364 |
} else { |
| 365 |
$items[] = (object)array( 'text' => $value, 'value' => $text ); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return $items; |
| 370 |
} |
| 371 |
|
| 372 |
private function output_ajax_response( $data ) { |
| 373 |
$result = wp_json_encode( $data ); |
| 374 |
|
| 375 |
if ( defined( 'DOING_AJAX' ) ) { |
| 376 |
header( 'Content-Type: application/json' ); |
| 377 |
echo $result; |
| 378 |
wp_die(); |
| 379 |
} |
| 380 |
|
| 381 |
return $result; |
| 382 |
} |
| 383 |
|
| 384 |
private function user_has_access() { |
| 385 |
return current_user_can( apply_filters( 'redirection_role', 'administrator' ) ); |
| 386 |
} |
| 387 |
} |
| 388 |
|