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

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