forked from johngodley/redirection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirection-admin.php
527 lines (404 loc) · 17.7 KB
/
redirection-admin.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
include dirname( __FILE__ ).'/models/group.php';
include dirname( __FILE__ ).'/models/monitor.php';
include dirname( __FILE__ ).'/models/pager.php';
include dirname( __FILE__ ).'/models/file-io.php';
class Redirection_Admin {
private static $instance = null;
private $monitor;
static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Redirection_Admin();
load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ).'/locale/' );
}
return self::$instance;
}
function __construct() {
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
add_action( 'load-tools_page_redirection', array( &$this, 'redirection_head' ) );
add_action( 'plugin_action_links_'.basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), array( &$this, 'plugin_settings' ), 10, 4 );
add_filter( 'set-screen-option', array( $this, 'set_per_page' ), 10, 3 );
register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) );
register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) );
add_action( 'wp_ajax_red_log_delete', array( &$this, 'ajax_log_delete' ) );
add_action( 'wp_ajax_red_module_edit', array( &$this, 'ajax_module_edit' ) );
add_action( 'wp_ajax_red_module_save', array( &$this, 'ajax_module_save' ) );
add_action( 'wp_ajax_red_group_edit', array( &$this, 'ajax_group_edit' ) );
add_action( 'wp_ajax_red_group_save', array( &$this, 'ajax_group_save' ) );
add_action( 'wp_ajax_red_redirect_add', array( &$this, 'ajax_redirect_add' ) );
add_action( 'wp_ajax_red_redirect_edit', array( &$this, 'ajax_redirect_edit' ) );
add_action( 'wp_ajax_red_redirect_save', array( &$this, 'ajax_redirect_save' ) );
add_action( 'wp_ajax_red_get_htaccess', array( &$this, 'ajax_get_htaccess' ) );
add_action( 'wp_ajax_red_get_nginx', array( &$this, 'ajax_get_nginx' ) );
$this->monitor = new Red_Monitor( red_get_options() );
}
public static function plugin_activated() {
Red_Flusher::schedule();
}
public static function plugin_deactivated() {
Red_Flusher::clear();
}
public static function plugin_uninstall() {
include dirname( REDIRECTION_FILE ).'/models/database.php';
$db = new RE_Database();
$db->remove( REDIRECTION_FILE );
}
private function render( $template, $template_vars = array() ) {
foreach ( $template_vars AS $key => $val ) {
$$key = $val;
}
if ( file_exists( dirname( REDIRECTION_FILE )."/view/$template.php" ) )
include dirname( REDIRECTION_FILE )."/view/$template.php";
}
private function capture( $ug_name, $ug_vars = array() ) {
ob_start();
$this->render( $ug_name, $ug_vars );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
private function render_error( $message ) {
?>
<div class="fade error" id="message">
<p><?php echo $message ?></p>
</div>
<?php
}
private function render_message( $message, $timeout = 0 ) {
?>
<div class="updated" id="message" onclick="this.parentNode.removeChild(this)">
<p><?php echo $message ?></p>
</div>
<?php
}
private function update() {
$version = get_option( 'redirection_version' );
if ( $version != REDIRECTION_VERSION ) {
include_once dirname( REDIRECTION_FILE ).'/models/database.php';
$database = new RE_Database();
return $database->upgrade( $version, REDIRECTION_VERSION );
}
return true;
}
private function select( $items, $default = '' ) {
foreach ( $items AS $key => $value ) {
if ( is_array( $value ) ) {
echo '<optgroup label="'.esc_attr( $key ).'">';
foreach ( $value AS $sub => $subvalue ) {
echo '<option value="'.esc_attr( $sub ).'"'.( $sub == $default ? ' selected="selected"' : '' ).'>'.esc_html( $subvalue ).'</option>';
}
echo '</optgroup>';
}
else
echo '<option value="'.esc_attr( $key ).'"'.( $key == $default ? ' selected="selected"' : '' ).'>'.esc_html( $value ).'</option>';
}
}
function set_per_page( $status, $option, $value ) {
if ( $option == 'redirection_log_per_page' )
return $value;
return $status;
}
function plugin_settings( $links ) {
$settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&sub=options">'.__( 'Settings', 'redirection' ).'</a>';
array_unshift( $links, $settings_link );
return $links;
}
function redirection_head() {
$version = get_plugin_data( REDIRECTION_FILE );
$version = $version['Version'];
$this->inject();
if ( !isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) )
add_screen_option( 'per_page', array( 'label' => __( 'Log entries', 'redirection' ), 'default' => 25, 'option' => 'redirection_log_per_page' ) );
wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $version );
wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'admin.css', $version );
wp_localize_script( 'redirection', 'Redirectioni10n', array(
'error_msg' => __( 'Sorry, unable to do that. Please try refreshing the page.' ),
) );
}
function admin_menu() {
add_management_page( __( "Redirection", 'redirection' ), __( "Redirection", 'redirection' ), apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, "admin_screen" ) );
}
function admin_screen() {
$this->update();
if ( isset( $_GET['sub'] ) ) {
if ( $_GET['sub'] == 'log' )
return $this->admin_screen_log();
elseif ( $_GET['sub'] == '404s' )
return $this->admin_screen_404();
elseif ( $_GET['sub'] == 'options' )
return $this->admin_screen_options();
elseif ( $_GET['sub'] == 'process' )
return $this->admin_screen_process();
elseif ( $_GET['sub'] == 'groups' )
return $this->admin_groups( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
elseif ( $_GET['sub'] == 'modules' )
return $this->admin_screen_modules();
elseif ( $_GET['sub'] == 'support' )
return $this->render('support', array( 'options' => red_get_options() ) );
}
return $this->admin_redirects( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
}
function admin_screen_modules() {
$options = red_get_options();
$pager = new Redirection_Module_Table( $options['token'] );
$pager->prepare_items();
$this->render( 'module-list', array( 'options' => $options, 'table' => $pager ) );
}
function inject() {
$options = red_get_options();
if ( isset( $_POST['id'] ) && !isset( $_POST['action'] ) ) {
wp_safe_redirect( add_query_arg( 'id', intval( $_POST['id'] ), $_SERVER['REQUEST_URI'] ) );
die();
}
if ( isset( $_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['token'] == $options['token'] && $_GET['page'] == 'redirection.php' ) {
$exporter = Red_FileIO::create( $_GET['sub'] );
if ( $exporter ) {
$items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
$exporter->export( $items );
die();
}
}
elseif ( isset( $_POST['export-csv'] ) && check_admin_referer( 'redirection-log_management' ) ) {
if ( isset( $_GET['sub'] ) && $_GET['sub'] == 'log' )
RE_Log::export_to_csv();
else
RE_404::export_to_csv();
die();
}
}
function admin_screen_options() {
if ( isset( $_POST['regenerate'] ) && check_admin_referer( 'redirection-update_options' ) ) {
$options = red_get_options();
$options['token'] = md5( uniqid() );
update_option( 'redirection_options', $options );
$this->render_message( __( 'Your options were updated', 'redirection' ) );
}
elseif ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) {
$options['monitor_post'] = stripslashes( $_POST['monitor_post'] );
$options['auto_target'] = stripslashes( $_POST['auto_target'] );
$options['support'] = isset( $_POST['support'] ) ? true : false;
$options['token'] = stripslashes( $_POST['token'] );
$options['expire_redirect'] = min( intval( $_POST['expire_redirect'] ), 60 );
$options['expire_404'] = min( intval( $_POST['expire_404'] ), 60 );
if ( trim( $options['token'] ) == '' )
$options['token'] = md5( uniqid() );
update_option( 'redirection_options', $options );
Red_Flusher::schedule();
$this->render_message( __( 'Your options were updated', 'redirection' ) );
}
elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
$this->plugin_uninstall();
$current = get_option( 'active_plugins' );
array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 );
update_option( 'active_plugins', $current );
$this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
return;
}
elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
$count = Red_FileIO::import( $_POST['group'], $_FILES['upload'] );
if ( $count > 0 )
$this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
else
$this->render_message( __( 'No items were imported', 'redirection' ) );
}
$groups = Red_Group::get_for_select();
$this->render( 'options', array( 'options' => red_get_options(), 'groups' => $groups ) );
}
function admin_screen_log() {
$options = red_get_options();
if ( isset( $_POST['delete-all'] ) && check_admin_referer( 'redirection-log_management' ) ) {
RE_Log::delete_all();
$this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
}
$table = new Redirection_Log_Table( $options );
if ( isset( $_GET['module'] ) )
$table->prepare_items( 'module', intval( $_GET['module'] ) );
else if (isset($_GET['group']))
$table->prepare_items( 'group', intval( $_GET['group'] ) );
else if (isset($_GET['redirect']))
$table->prepare_items( 'redirect', intval( $_GET['redirect'] ) );
else
$table->prepare_items();
$this->render( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'], 'type' => 'log' ) );
}
function admin_screen_404() {
if ( isset( $_POST['delete-all'] ) && check_admin_referer( 'redirection-log_management' ) ) {
RE_404::delete_all();
$this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
}
$options = red_get_options();
$table = new Redirection_404_Table( $options );
$table->prepare_items( isset( $_GET['ip'] ) ? $_GET['ip'] : false );
$this->render( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'], 'type' => '404s' ) );
}
function admin_groups( $module ) {
if ( isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
if ( Red_Group::create( stripslashes( $_POST['name'] ), intval( $_POST['module_id'] ) ) ) {
$this->render_message( __( 'Your group was added successfully', 'redirection' ) );
}
else
$this->render_error( __( 'Please specify a group name', 'redirection' ) );
}
$table = new Redirection_Group_Table( Red_Module::get_for_select() );
$table->prepare_items();
$this->render( 'group-list', array( 'options' => red_get_options(), 'table' => $table, 'modules' => Red_Module::get_for_select(), 'module' => $module ) );
}
function admin_redirects( $group_id ) {
$table = new Redirection_Table( Red_Group::get_for_select(), $group_id );
$table->prepare_items();
$this->render( 'item-list', array( 'options' => red_get_options(), 'group' => $group_id, 'table' => $table, 'date_format' => get_option( 'date_format' ) ) );
}
function locales() {
$locales = array();
if ( file_exists( dirname( REDIRECTION_FILE ).'/readme.txt' ) ) {
$readme = file_get_contents( dirname( REDIRECTION_FILE ).'/readme.txt' );
$start = strpos( $readme, 'Redirection is available in' );
$end = strpos( $readme, '==', $start );
if ( $start !== false && $end !== false ) {
if ( preg_match_all( '/^\* (.*?) by (.*?)/m', substr( $readme, $start, $end ), $matches ) > 0 ) {
$locales = $matches[1];
}
}
sort( $locales );
}
return $locales;
}
public function ajax_log_delete() {
if ( check_ajax_referer( 'redirection-items' ) ) {
if ( preg_match_all( '/=(\d*)/', $_POST['checked'], $items ) > 0) {
foreach ( $items[1] AS $item ) {
RE_Log::delete( intval( $item ) );
}
}
}
}
private function check_ajax_referer( $nonce ) {
if ( check_ajax_referer( $nonce, false, false ) === false )
$this->output_ajax_response( array( 'error' => __( 'Unable to perform action' ).' - bad nonce' ) );
}
public function ajax_module_edit() {
$module_id = intval( $_POST['id'] );
$this->check_ajax_referer( 'red_edit-'.$module_id );
$module = Red_Module::get( $module_id );
if ( $module )
$json['html'] = $this->capture( 'module-edit', array( 'module' => $module ) );
else
$json['error'] = __( 'Unable to perform action' ).' - could not find module';
$this->output_ajax_response( $json );
}
public function ajax_module_save() {
$module_id = intval( $_POST['id'] );
$this->check_ajax_referer( 'red_module_save_'.$module_id );
$module = Red_Module::get( $module_id );
if ( $module ) {
$result = $module->update( $_POST );
if ( $result === true ) {
global $hook_suffix;
$hook_suffix = '';
$options = red_get_options();
$pager = new Redirection_Module_Table( $options['token'] );
$json = array( 'html' => $pager->column_name( $module ) );
}
else
$json['error'] = $result;
}
else
$json['error'] = __( 'Unable to perform action' ).' - could not find module';
$this->output_ajax_response( $json );
}
public function ajax_group_edit() {
$group_id = intval( $_POST['id'] );
$this->check_ajax_referer( 'red-edit_'.$group_id );
$group = Red_Group::get( $group_id );
if ( $group )
$json['html'] = $this->capture( 'group-edit', array( 'group' => $group, 'modules' => Red_Module::get_for_select() ) );
else
$json['error'] = __( 'Unable to perform action' ).' - could not find group';
$this->output_ajax_response( $json );
}
public function ajax_group_save() {
global $hook_suffix;
$hook_suffix = '';
$group_id = intval( $_POST['id'] );
$this->check_ajax_referer( 'redirection-group_save_'.$group_id );
$group = Red_Group::get( $group_id );
if ( $group ) {
$group->update( $_POST );
$module = Red_Module::get( $group->get_module_id() );
$pager = new Redirection_Group_Table( array(), false );
$json = array( 'html' => $pager->column_name( $group ), 'module' => $module->get_name() );
}
else
$json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
$this->output_ajax_response( $json );
}
public function ajax_redirect_edit() {
$this->check_ajax_referer( 'red-edit_'.intval( $_POST['id'] ) );
$redirect = Red_Item::get_by_id( intval( $_POST['id'] ) );
if ( $redirect )
$json['html'] = $this->capture( 'item-edit', array( 'redirect' => $redirect, 'groups' => Red_Group::get_for_select() ) );
else
$json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
$this->output_ajax_response( $json );
}
public function ajax_redirect_save() {
global $hook_suffix;
$hook_suffix = '';
$red_id = intval( $_POST['id'] );
$this->check_ajax_referer( 'redirection-redirect_save_'.$red_id );
$redirect = Red_Item::get_by_id( $red_id );
if ( $redirect ) {
$redirect->update( $_POST );
$pager = new Redirection_Table( array(), 0 );
$json = array( 'html' => $pager->column_url( $redirect ), 'code' => $redirect->get_action_code() );
}
else
$json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
$this->output_ajax_response( $json );
}
public function ajax_redirect_add() {
global $hook_suffix;
$hook_suffix = '';
$this->check_ajax_referer( 'redirection-redirect_add' );
$item = Red_Item::create( $_POST );
if ( is_wp_error( $item ) )
$json['error'] = $item->get_error_message();
elseif ( $item !== false ) {
$pager = new Redirection_Table( array(), 0 );
$json = array( 'html' => $pager->get_row( $item ) );
}
else
$json['error'] = __( 'Sorry, but your redirection was not created', 'redirection' );
$this->output_ajax_response( $json );
}
private function get_module_column( $module_id, $export_type ) {
$json['error'] = __( 'Invalid module', 'redirection' );
$module = Red_Module::get( $module_id );
$exporter = Red_FileIO::create( $export_type );
if ( $module && $exporter ) {
global $hook_suffix;
$hook_suffix = '';
$options = red_get_options();
$pager = new Redirection_Module_Table( $options['token'] );
$items = Red_Item::get_all_for_module( $module_id );
$json = array( 'html' => $pager->column_name( $module ) );
$json['html'] .= '<textarea readonly="readonly" class="module-export" rows="10">'.esc_textarea( $exporter->get( $items ) ).'</textarea>';
$json['html'] .= '<div class="table-actions"><a href="?page=redirection.php&token='.$options['token'].'&sub='.$export_type.'&module='.$module_id.'"><input class="button-primary" type="button" value="'.__( 'Download', 'redirection' ).'"/></a> ';
$json['html'] .= '<input class="button-secondary" type="submit" name="cancel" value="'.__( 'Cancel', 'redirection' ).'"/>';
}
$this->output_ajax_response( $json );
}
public function ajax_get_nginx() {
$this->get_module_column( intval( $_POST['id'] ), 'nginx' );
}
public function ajax_get_htaccess() {
$this->get_module_column( intval( $_POST['id'] ), 'apache' );
}
private function output_ajax_response( array $data ) {
header( 'Content-Type: application/json' );
echo json_encode( $data );
die();
}
}
register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
add_action( 'init', array( 'Redirection_Admin', 'init' ) );