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

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