PluginProbe
Redirection / 2.4.5
Redirection v2.4.5
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-admin.php

redirection-admin.php in Redirection 2.4.5, at redirection-admin.php

531 lines 17.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 include dirname( __FILE__ ).'/models/group.php';
4 include dirname( __FILE__ ).'/models/monitor.php';
5 include dirname( __FILE__ ).'/models/pager.php';
6 include dirname( __FILE__ ).'/models/file-io.php';
7
8 class Redirection_Admin {
9 private static $instance = null;
10 private $monitor;
11
12 static function init() {
13 if ( is_null( self::$instance ) ) {
14 self::$instance = new Redirection_Admin();
15
16 load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ).'/locale/' );
17 }
18
19 return self::$instance;
20 }
21
22 function __construct() {
23 add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
24 add_action( 'load-tools_page_redirection', array( &$this, 'redirection_head' ) );
25 add_action( 'plugin_action_links_'.basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), array( &$this, 'plugin_settings' ), 10, 4 );
26
27 add_filter( 'set-screen-option', array( $this, 'set_per_page' ), 10, 3 );
28
29 register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) );
30 register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) );
31
32 add_action( 'wp_ajax_red_log_delete', array( &$this, 'ajax_log_delete' ) );
33 add_action( 'wp_ajax_red_module_edit', array( &$this, 'ajax_module_edit' ) );
34 add_action( 'wp_ajax_red_module_save', array( &$this, 'ajax_module_save' ) );
35 add_action( 'wp_ajax_red_group_edit', array( &$this, 'ajax_group_edit' ) );
36 add_action( 'wp_ajax_red_group_save', array( &$this, 'ajax_group_save' ) );
37 add_action( 'wp_ajax_red_redirect_add', array( &$this, 'ajax_redirect_add' ) );
38 add_action( 'wp_ajax_red_redirect_edit', array( &$this, 'ajax_redirect_edit' ) );
39 add_action( 'wp_ajax_red_redirect_save', array( &$this, 'ajax_redirect_save' ) );
40 add_action( 'wp_ajax_red_get_htaccess', array( &$this, 'ajax_get_htaccess' ) );
41 add_action( 'wp_ajax_red_get_nginx', array( &$this, 'ajax_get_nginx' ) );
42
43 $this->monitor = new Red_Monitor( red_get_options() );
44 }
45
46 public static function plugin_activated() {
47 Redirection_Admin::update();
48 Red_Flusher::schedule();
49 }
50
51 public static function plugin_deactivated() {
52 Red_Flusher::clear();
53 }
54
55 public static function plugin_uninstall() {
56 include dirname( REDIRECTION_FILE ).'/models/database.php';
57
58 $db = new RE_Database();
59 $db->remove( REDIRECTION_FILE );
60 }
61
62 private function render( $template, $template_vars = array() ) {
63 foreach ( $template_vars as $key => $val ) {
64 $$key = $val;
65 }
66
67 if ( file_exists( dirname( REDIRECTION_FILE )."/view/$template.php" ) )
68 include dirname( REDIRECTION_FILE )."/view/$template.php";
69 }
70
71 private function capture( $ug_name, $ug_vars = array() ) {
72 ob_start();
73
74 $this->render( $ug_name, $ug_vars );
75 $output = ob_get_contents();
76
77 ob_end_clean();
78 return $output;
79 }
80
81 private function render_error( $message ) {
82 ?>
83 <div class="fade error" id="message">
84 <p><?php echo $message ?></p>
85 </div>
86 <?php
87 }
88
89 private function render_message( $message, $timeout = 0 ) {
90 ?>
91 <div class="updated" id="message" onclick="this.parentNode.removeChild(this)">
92 <p><?php echo $message ?></p>
93 </div>
94 <?php
95 }
96
97 private static function update() {
98 $version = get_option( 'redirection_version' );
99
100 Red_Flusher::schedule();
101
102 if ( $version !== REDIRECTION_VERSION ) {
103 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
104
105 $database = new RE_Database();
106 return $database->upgrade( $version, REDIRECTION_VERSION );
107 }
108
109 return true;
110 }
111
112 private function select( $items, $default = '' ) {
113 foreach ( $items as $key => $value ) {
114 if ( is_array( $value ) ) {
115 echo '<optgroup label="'.esc_attr( $key ).'">';
116
117 foreach ( $value as $sub => $subvalue ) {
118 echo '<option value="'.esc_attr( $sub ).'"'.( $sub === $default ? ' selected="selected"' : '' ).'>'.esc_html( $subvalue ).'</option>';
119 }
120
121 echo '</optgroup>';
122 }
123 else
124 echo '<option value="'.esc_attr( $key ).'"'.( $key === $default ? ' selected="selected"' : '' ).'>'.esc_html( $value ).'</option>';
125 }
126 }
127
128 function set_per_page( $status, $option, $value ) {
129 if ( $option === 'redirection_log_per_page' )
130 return $value;
131 return $status;
132 }
133
134 function plugin_settings( $links ) {
135 $settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&amp;sub=options">'.__( 'Settings', 'redirection' ).'</a>';
136 array_unshift( $links, $settings_link );
137 return $links;
138 }
139
140 function redirection_head() {
141 $version = get_plugin_data( REDIRECTION_FILE );
142 $version = $version['Version'];
143
144 $this->inject();
145
146 if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) )
147 add_screen_option( 'per_page', array( 'label' => __( 'Log entries', 'redirection' ), 'default' => 25, 'option' => 'redirection_log_per_page' ) );
148
149 wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $version );
150 wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'admin.css', $version );
151
152 wp_localize_script( 'redirection', 'Redirectioni10n', array(
153 'error_msg' => __( 'Sorry, unable to do that. Please try refreshing the page.' ),
154 ) );
155 }
156
157 function admin_menu() {
158 add_management_page( __( 'Redirection', 'redirection' ), __( 'Redirection', 'redirection' ), apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) );
159 }
160
161 function admin_screen() {
162 Redirection_Admin::update();
163
164 if ( isset( $_GET['sub'] ) ) {
165 if ( $_GET['sub'] === 'log' )
166 return $this->admin_screen_log();
167 elseif ( $_GET['sub'] === '404s' )
168 return $this->admin_screen_404();
169 elseif ( $_GET['sub'] === 'options' )
170 return $this->admin_screen_options();
171 elseif ( $_GET['sub'] === 'process' )
172 return $this->admin_screen_process();
173 elseif ( $_GET['sub'] === 'groups' )
174 return $this->admin_groups( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
175 elseif ( $_GET['sub'] === 'modules' )
176 return $this->admin_screen_modules();
177 elseif ( $_GET['sub'] === 'support' )
178 return $this->render( 'support', array( 'options' => red_get_options() ) );
179 }
180
181 return $this->admin_redirects( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
182 }
183
184 function admin_screen_modules() {
185 $options = red_get_options();
186 $pager = new Redirection_Module_Table( $options['token'] );
187 $pager->prepare_items();
188
189 $this->render( 'module-list', array( 'options' => $options, 'table' => $pager ) );
190 }
191
192 function inject() {
193 $options = red_get_options();
194
195 if ( isset( $_POST['id'] ) && ! isset( $_POST['action'] ) ) {
196 wp_safe_redirect( add_query_arg( 'id', intval( $_POST['id'] ), $_SERVER['REQUEST_URI'] ) );
197 die();
198 }
199
200 if ( isset( $_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['token'] === $options['token'] && $_GET['page'] === 'redirection.php' ) {
201 $exporter = Red_FileIO::create( $_GET['sub'] );
202 if ( $exporter ) {
203 $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
204
205 $exporter->export( $items );
206 die();
207 }
208 }
209 elseif ( isset( $_POST['export-csv'] ) && check_admin_referer( 'redirection-log_management' ) ) {
210 if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' )
211 RE_Log::export_to_csv();
212 else
213 RE_404::export_to_csv();
214 die();
215 }
216 }
217
218 function admin_screen_options() {
219 if ( isset( $_POST['regenerate'] ) && check_admin_referer( 'redirection-update_options' ) ) {
220 $options = red_get_options();
221 $options['token'] = md5( uniqid() );
222
223 update_option( 'redirection_options', $options );
224
225 $this->render_message( __( 'Your options were updated', 'redirection' ) );
226 }
227 elseif ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) {
228 $options['monitor_post'] = stripslashes( $_POST['monitor_post'] );
229 $options['auto_target'] = stripslashes( $_POST['auto_target'] );
230 $options['support'] = isset( $_POST['support'] ) ? true : false;
231 $options['token'] = stripslashes( $_POST['token'] );
232 $options['expire_redirect'] = min( intval( $_POST['expire_redirect'] ), 60 );
233 $options['expire_404'] = min( intval( $_POST['expire_404'] ), 60 );
234
235 if ( trim( $options['token'] ) === '' )
236 $options['token'] = md5( uniqid() );
237
238 update_option( 'redirection_options', $options );
239
240 Red_Flusher::schedule();
241 $this->render_message( __( 'Your options were updated', 'redirection' ) );
242 }
243 elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
244 $this->plugin_uninstall();
245
246 $current = get_option( 'active_plugins' );
247 array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 );
248 update_option( 'active_plugins', $current );
249
250 $this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
251 return;
252 }
253 elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
254 $count = Red_FileIO::import( $_POST['group'], $_FILES['upload'] );
255
256 if ( $count > 0 )
257 $this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
258 else
259 $this->render_message( __( 'No items were imported', 'redirection' ) );
260 }
261
262 $groups = Red_Group::get_for_select();
263 $this->render( 'options', array( 'options' => red_get_options(), 'groups' => $groups ) );
264 }
265
266 function admin_screen_log() {
267 $options = red_get_options();
268
269 if ( isset( $_POST['delete-all'] ) && check_admin_referer( 'redirection-log_management' ) ) {
270 RE_Log::delete_all();
271 $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
272 }
273
274 $table = new Redirection_Log_Table( $options );
275
276 if ( isset( $_GET['module'] ) )
277 $table->prepare_items( 'module', intval( $_GET['module'] ) );
278 else if ( isset( $_GET['group'] ) )
279 $table->prepare_items( 'group', intval( $_GET['group'] ) );
280 else if ( isset( $_GET['redirect'] ) )
281 $table->prepare_items( 'redirect', intval( $_GET['redirect'] ) );
282 else
283 $table->prepare_items();
284
285 $this->render( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'], 'type' => 'log' ) );
286 }
287
288 function admin_screen_404() {
289 if ( isset( $_POST['delete-all'] ) && check_admin_referer( 'redirection-log_management' ) ) {
290 RE_404::delete_all();
291 $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
292 }
293
294 $options = red_get_options();
295
296 $table = new Redirection_404_Table( $options );
297 $table->prepare_items( isset( $_GET['ip'] ) ? $_GET['ip'] : false );
298
299 $this->render( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'], 'type' => '404s' ) );
300 }
301
302 function admin_groups( $module ) {
303 if ( isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
304 if ( Red_Group::create( stripslashes( $_POST['name'] ), intval( $_POST['module_id'] ) ) ) {
305 $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
306 }
307 else
308 $this->render_error( __( 'Please specify a group name', 'redirection' ) );
309 }
310
311 $table = new Redirection_Group_Table( Red_Module::get_for_select() );
312 $table->prepare_items();
313
314 $this->render( 'group-list', array( 'options' => red_get_options(), 'table' => $table, 'modules' => Red_Module::get_for_select(), 'module' => $module ) );
315 }
316
317 function admin_redirects( $group_id ) {
318 $table = new Redirection_Table( Red_Group::get_for_select(), $group_id );
319 $table->prepare_items();
320
321 $this->render( 'item-list', array( 'options' => red_get_options(), 'group' => $group_id, 'table' => $table, 'date_format' => get_option( 'date_format' ) ) );
322 }
323
324 function locales() {
325 $locales = array();
326 if ( file_exists( dirname( REDIRECTION_FILE ).'/readme.txt' ) ) {
327 $readme = file_get_contents( dirname( REDIRECTION_FILE ).'/readme.txt' );
328
329 $start = strpos( $readme, 'Redirection is available in' );
330 $end = strpos( $readme, '==', $start );
331 if ( $start !== false && $end !== false ) {
332 if ( preg_match_all( '/^\* (.*?) by (.*?)/m', substr( $readme, $start, $end ), $matches ) > 0 ) {
333 $locales = $matches[1];
334 }
335 }
336
337 sort( $locales );
338 }
339
340 return $locales;
341 }
342
343 public function ajax_log_delete() {
344 if ( check_ajax_referer( 'redirection-items' ) ) {
345 if ( preg_match_all( '/=(\d*)/', $_POST['checked'], $items ) > 0 ) {
346 foreach ( $items[1] as $item ) {
347 RE_Log::delete( intval( $item ) );
348 }
349 }
350 }
351 }
352
353 private function check_ajax_referer( $nonce ) {
354 if ( check_ajax_referer( $nonce, false, false ) === false )
355 $this->output_ajax_response( array( 'error' => __( 'Unable to perform action' ).' - bad nonce' ) );
356 }
357
358 public function ajax_module_edit() {
359 $module_id = intval( $_POST['id'] );
360
361 $this->check_ajax_referer( 'red_edit-'.$module_id );
362
363 $module = Red_Module::get( $module_id );
364 if ( $module )
365 $json['html'] = $this->capture( 'module-edit', array( 'module' => $module ) );
366 else
367 $json['error'] = __( 'Unable to perform action' ).' - could not find module';
368
369 $this->output_ajax_response( $json );
370 }
371
372 public function ajax_module_save() {
373 $module_id = intval( $_POST['id'] );
374
375 $this->check_ajax_referer( 'red_module_save_'.$module_id );
376
377 $module = Red_Module::get( $module_id );
378 if ( $module ) {
379 $result = $module->update( $_POST );
380
381 if ( $result === true ) {
382 global $hook_suffix;
383
384 $hook_suffix = '';
385 $options = red_get_options();
386 $pager = new Redirection_Module_Table( $options['token'] );
387
388 $json = array( 'html' => $pager->column_name( $module ) );
389 }
390 else
391 $json['error'] = $result;
392 }
393 else
394 $json['error'] = __( 'Unable to perform action' ).' - could not find module';
395
396 $this->output_ajax_response( $json );
397 }
398
399 public function ajax_group_edit() {
400 $group_id = intval( $_POST['id'] );
401
402 $this->check_ajax_referer( 'red-edit_'.$group_id );
403
404 $group = Red_Group::get( $group_id );
405 if ( $group )
406 $json['html'] = $this->capture( 'group-edit', array( 'group' => $group, 'modules' => Red_Module::get_for_select() ) );
407 else
408 $json['error'] = __( 'Unable to perform action' ).' - could not find group';
409
410 $this->output_ajax_response( $json );
411 }
412
413 public function ajax_group_save() {
414 global $hook_suffix;
415
416 $hook_suffix = '';
417 $group_id = intval( $_POST['id'] );
418
419 $this->check_ajax_referer( 'redirection-group_save_'.$group_id );
420
421 $group = Red_Group::get( $group_id );
422 if ( $group ) {
423 $group->update( $_POST );
424 $module = Red_Module::get( $group->get_module_id() );
425
426 $pager = new Redirection_Group_Table( array(), false );
427 $json = array( 'html' => $pager->column_name( $group ), 'module' => $module->get_name() );
428 }
429 else
430 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
431
432 $this->output_ajax_response( $json );
433 }
434
435 public function ajax_redirect_edit() {
436 $this->check_ajax_referer( 'red-edit_'.intval( $_POST['id'] ) );
437 $redirect = Red_Item::get_by_id( intval( $_POST['id'] ) );
438
439 if ( $redirect )
440 $json['html'] = $this->capture( 'item-edit', array( 'redirect' => $redirect, 'groups' => Red_Group::get_for_select() ) );
441 else
442 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
443
444 $this->output_ajax_response( $json );
445 }
446
447 public function ajax_redirect_save() {
448 global $hook_suffix;
449
450 $hook_suffix = '';
451
452 $red_id = intval( $_POST['id'] );
453
454 $this->check_ajax_referer( 'redirection-redirect_save_'.$red_id );
455
456 $redirect = Red_Item::get_by_id( $red_id );
457 if ( $redirect ) {
458 $redirect->update( $_POST );
459
460 $pager = new Redirection_Table( array(), 0 );
461 $json = array( 'html' => $pager->column_url( $redirect ), 'code' => $redirect->get_action_code() );
462 }
463 else
464 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
465
466 $this->output_ajax_response( $json );
467 }
468
469 public function ajax_redirect_add() {
470 global $hook_suffix;
471
472 $hook_suffix = '';
473
474 $this->check_ajax_referer( 'redirection-redirect_add' );
475
476 $item = Red_Item::create( $_POST );
477 if ( is_wp_error( $item ) )
478 $json['error'] = $item->get_error_message();
479 elseif ( $item !== false ) {
480 $pager = new Redirection_Table( array(), 0 );
481 $json = array( 'html' => $pager->get_row( $item ) );
482 }
483 else
484 $json['error'] = __( 'Sorry, but your redirection was not created', 'redirection' );
485
486 $this->output_ajax_response( $json );
487 }
488
489 private function get_module_column( $module_id, $export_type ) {
490 $json['error'] = __( 'Invalid module', 'redirection' );
491
492 $module = Red_Module::get( $module_id );
493 $exporter = Red_FileIO::create( $export_type );
494
495 if ( $module && $exporter ) {
496 global $hook_suffix;
497
498 $hook_suffix = '';
499 $options = red_get_options();
500 $pager = new Redirection_Module_Table( $options['token'] );
501 $items = Red_Item::get_all_for_module( $module_id );
502
503 $json = array( 'html' => $pager->column_name( $module ) );
504
505 $json['html'] .= '<textarea readonly="readonly" class="module-export" rows="10">'.esc_textarea( $exporter->get( $items ) ).'</textarea>';
506 $json['html'] .= '<div class="table-actions"><a href="?page=redirection.php&amp;token='.$options['token'].'&amp;sub='.$export_type.'&amp;module='.$module_id.'"><input class="button-primary" type="button" value="'.__( 'Download', 'redirection' ).'"/></a> ';
507 $json['html'] .= '<input class="button-secondary" type="submit" name="cancel" value="'.__( 'Cancel', 'redirection' ).'"/>';
508 }
509
510 $this->output_ajax_response( $json );
511 }
512
513 public function ajax_get_nginx() {
514 $this->get_module_column( intval( $_POST['id'] ), 'nginx' );
515 }
516
517 public function ajax_get_htaccess() {
518 $this->get_module_column( intval( $_POST['id'] ), 'apache' );
519 }
520
521 private function output_ajax_response( array $data ) {
522 header( 'Content-Type: application/json' );
523 echo wp_json_encode( $data );
524 die();
525 }
526 }
527
528 register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
529
530 add_action( 'init', array( 'Redirection_Admin', 'init' ) );
531