PluginProbe
Redirection / 2.4
Redirection v2.4
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, at redirection-admin.php

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