private function add_referrer(Red_Item $item)
 {
     if ($item->match->url_from) {
         $lines[] = 'if ( $http_referer ~* ^' . $item->match->referrer . '$ ) {';
         $lines[] = '        ' . $this->add_redirect($item->get_url(), $item->match->url_from, $this->get_redirect_code($item));
         $lines[] = '    }';
     }
     if ($item->match->url_notfrom) {
         $lines[] = 'if ( $http_referer !~* ^' . $item->match->referrer . '$ ) {';
         $lines[] = '        ' . $this->add_redirect($item->get_url(), $item->match->url_notfrom, $this->get_redirect_code($item));
         $lines[] = '    }';
     }
     return implode("\n", $lines);
 }
Example #2
0
	function init ()
	{
		global $redirection;
		
		$url = $_SERVER['REQUEST_URI'];

		// Make sure we don't try and redirect something essential
		if (!$this->protected_url ($url) && !$redirection->hasMatched ()) {
			do_action ('redirection_first', $url, $this);

			$redirects = Red_Item::get_for_url( $url, 'wp' );

			if ( !empty( $redirects) ) {
				foreach ($redirects AS $key => $item) {
					if ( $item->matches( $url ) ) {
						global $redirection;
						
						$redirection->setMatched( true );
						$this->matched = $item;
						break;
					}
				}
			}

			do_action ('redirection_last', $url, $this);
		}
	}
Example #3
0
 function flush($id)
 {
     $module = Red_Module::get($id);
     if ($module && $module->is_valid()) {
         $module->module_flush(Red_Item::get_all_for_module($id));
     }
 }
 /**
  * Get redirect
  *
  * @since 0.1.0
  */
 public function get_redirect($id)
 {
     $item = Red_Item::get_by_id($id);
     $object = new StdClass();
     $object->id = $item->get_id();
     $object->from = $item->get_url();
     $object->to = $item->get_action_data();
     return $object;
 }
 public function post_updated($post_id, $post, $post_before)
 {
     if ($this->can_monitor_post($post, $post_before, $_POST)) {
         $after = parse_url(get_permalink($post_id));
         $after = $after['path'];
         $before = esc_url($_POST['redirection_slug']);
         $site = parse_url(get_site_url());
         if ($before !== $after && $before !== '/' && (!isset($site['path']) || isset($site['path']) && $before !== $site['path'] . '/')) {
             Red_Item::create(array('source' => $before, 'target' => $after, 'match' => 'url', 'red_action' => 'url', 'group_id' => $this->monitor_group_id));
         }
     }
 }
Example #6
0
 function post_updated($post_id, $post, $post_before)
 {
     if (isset($_POST['redirection_slug'])) {
         $after = parse_url(get_permalink($post_id));
         $after = $after['path'];
         $before = esc_url($_POST['redirection_slug']);
         $site = parse_url(get_site_url());
         if (in_array($post->post_status, array('publish', 'static')) && $before != $after && $before != '/' && $before != $site['path'] . '/') {
             Red_Item::create(array('source' => $before, 'target' => $after, 'match' => 'url', 'red_action' => 'url', 'group' => $this->monitor_post));
         }
     }
 }
 function post_changed($id)
 {
     if (isset($_POST['redirection_slug'])) {
         $post = get_post($id);
         $newslug = get_permalink($id);
         $oldslug = $_POST['redirection_slug'];
         $base = get_option('home');
         if ($newslug != $oldslug && strlen($oldslug) > 0 && ($post->post_status == 'publish' || $post->post_status == 'static') && $_POST['redirection_status'] != 'draft' && $_POST['redirection_status'] != 'pending' && $newslug != '/') {
             $old_url = parse_url($oldslug);
             $new_url = parse_url($newslug);
             Red_Item::create(array('source' => $old_url['path'], 'target' => $new_url['path'], 'match' => 'url', 'red_action' => 'url', 'group' => $this->monitor_post));
         }
     }
 }
Example #8
0
 function load($group, $data, $filename)
 {
     $count = 0;
     $file = fopen($filename, 'r');
     if ($file) {
         while ($csv = fgetcsv($file, 1000, ',')) {
             if ($csv[0] != 'source' && $csv[1] != 'target') {
                 Red_Item::create(array('source' => trim($csv[0]), 'target' => trim($csv[1]), 'regex' => $this->is_regex($csv[0]), 'group' => $group, 'match' => 'url', 'red_action' => 'url'));
                 $count++;
             }
         }
     }
     return $count;
 }
 public function init()
 {
     $url = $_SERVER['REQUEST_URI'];
     // Make sure we don't try and redirect something essential
     if (!$this->protected_url($url) && $this->matched === false) {
         do_action('redirection_first', $url, $this);
         $redirects = Red_Item::get_for_url($url, 'wp');
         foreach ((array) $redirects as $item) {
             if ($item->matches($url)) {
                 $this->matched = $item;
                 break;
             }
         }
         do_action('redirection_last', $url, $this);
     }
 }
Example #10
0
 function process_bulk_action()
 {
     if (!isset($_POST['item'])) {
         return;
     }
     if (in_array($this->current_action(), array('reset', 'enable', 'disable', 'delete'))) {
         $redirections = array();
         foreach ((array) $_POST['item'] as $id) {
             $redirect = Red_Item::get_by_id(intval($id));
             if ($redirect) {
                 $redirections[] = $redirect;
             }
         }
         array_map(array(&$this, 'process_action_items'), $redirections);
         Red_Module::flush($this->current_group->module_id);
     }
 }
Example #11
0
 function load($group, $data)
 {
     // Split it into lines
     $lines = array_filter(explode("\r", $data));
     if (count($lines) > 0) {
         $count = 0;
         foreach ($lines as $line) {
             $csv = $this->parse_csv($line);
             if ($csv[0] != 'source' && $csv[1] != 'target') {
                 Red_Item::create(array('source' => trim($csv[0]), 'target' => $csv[1], 'regex' => $this->is_regex($csv[0]), 'group' => $group, 'match' => 'url', 'red_action' => $csv[2] == 0 ? 'pass' : 'url'));
                 $count++;
             }
         }
         return $count;
     }
     return 0;
 }
Example #12
0
 protected function flush_module()
 {
     include_once dirname(dirname(__FILE__)) . '/models/htaccess.php';
     if (empty($this->location)) {
         return;
     }
     $items = Red_Item::get_by_module($this->get_id());
     // Produce the .htaccess file
     $htaccess = new Red_Htaccess();
     if (is_array($items) && count($items) > 0) {
         foreach ($items as $item) {
             if ($item->is_enabled()) {
                 $htaccess->add($item);
             }
         }
     }
     return $htaccess->save($this->location);
 }
Example #13
0
 function collect($module)
 {
     $this->name = $module->name;
     $this->id = $module->id;
     $this->type = $module->type;
     $this->options = unserialize($module->options);
     if (!is_array($this->options)) {
         $this->options = array();
     }
     $this->groups = Red_Group::get_for_module($module->id);
     if (is_array($this->groups) && count($this->groups) > 0) {
         $pager = new RE_Pager($_GET, admin_url('redirection.php'), 'position', 'ASC', 'log');
         $pager->per_page = 0;
         foreach ($this->groups as $pos => $group) {
             $this->groups[$pos]->items = Red_Item::get_by_group($group->id, $pager);
         }
     } else {
         $this->groups = array();
     }
     return true;
 }
Example #14
0
 function load($group, $data, $filename = '')
 {
     // Remove any comments
     $data = preg_replace('@#(.*)@', '', $data);
     $data = str_replace("\n", "\r", $data);
     $data = str_replace('\\ ', '%20', $data);
     // Split it into lines
     $lines = array_filter(explode("\r", $data));
     if (count($lines) > 0) {
         foreach ($lines as $line) {
             if (preg_match('@rewriterule\\s+(.*?)\\s+(.*?)\\s+(\\[.*\\])*@i', $line, $matches) > 0) {
                 $items[] = array('source' => $this->regex_url($matches[1]), 'target' => $this->decode_url($matches[2]), 'code' => $this->get_code($matches[3]), 'regex' => $this->is_regex($matches[1]));
             } elseif (preg_match('@Redirect\\s+(.*?)\\s+(.*?)\\s+(.*)@i', $line, $matches) > 0) {
                 $items[] = array('source' => $this->decode_url($matches[2]), 'target' => $this->decode_url($matches[3]), 'code' => $this->get_code($matches[1]));
             } elseif (preg_match('@Redirect\\s+(.*?)\\s+(.*?)@i', $line, $matches) > 0) {
                 $items[] = array('source' => $this->decode_url($matches[1]), 'target' => $this->decode_url($matches[2]), 'code' => 302);
             } elseif (preg_match('@Redirectmatch\\s+(.*?)\\s+(.*?)\\s+(.*)@i', $line, $matches) > 0) {
                 $items[] = array('source' => $this->decode_url($matches[2]), 'target' => $this->decode_url($matches[3]), 'code' => $this->get_code($matches[1]), 'regex' => true);
             } elseif (preg_match('@Redirectmatch\\s+(.*?)\\s+(.*?)@i', $line, $matches) > 0) {
                 $items[] = array('source' => $this->decode_url($matches[1]), 'target' => $this->decode_url($matches[2]), 'code' => 302, 'regex' => true);
             }
         }
         // Add items to group
         if (count($items) > 0) {
             foreach ($items as $item) {
                 $item['group'] = $group;
                 $item['red_action'] = 'url';
                 $item['match'] = 'url';
                 if ($item['code'] == 0) {
                     $item['red_action'] = 'pass';
                 }
                 Red_Item::create($item);
             }
             return count($items);
         }
     }
     return 0;
 }
Example #15
0
 function template_redirect()
 {
     global $redirection;
     if (is_404() && !$redirection->hasMatched()) {
         $url = $_SERVER['REQUEST_URI'];
         $redirects = Red_Item::get_for_url($url, '404');
         if (!empty($redirects)) {
             foreach ($redirects as $key => $item) {
                 if ($item->matches($url)) {
                     $redirection->setMatched(true);
                     $this->matched = $item;
                     break;
                 }
             }
         }
         if (empty($this->matched)) {
             $modules = Red_Module::get_by_type('404');
             if (count($modules) > 0) {
                 foreach ($modules as $module) {
                     // Log 404 errors
                     if ($module->log_404) {
                         if (isset($_SERVER['REMOTE_ADDR'])) {
                             $myip = $_SERVER['REMOTE_ADDR'];
                         } else {
                             if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                                 $myip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                             }
                         }
                         $options = $redirection->get_options();
                         if ($options['log_404s']) {
                             $log = RE_Log::create($_SERVER['REQUEST_URI'], '', $_SERVER['HTTP_USER_AGENT'], $myip, isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', 'NULL', $module->id);
                         }
                     }
                 }
             }
         }
     }
 }
 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());
         $json = array('html' => $pager->get_row($item));
     } else {
         $json['error'] = __('Sorry, but your redirection was not created', 'redirection');
     }
     $this->output_ajax_response($json);
 }
 function admin_redirects($group)
 {
     include dirname(__FILE__) . '/models/pager.php';
     if ($group == 0) {
         $group = Red_Group::get_first_id();
     }
     $pager = new RE_Pager($_GET, admin_url(add_query_arg(array(), 'tools.php?page=redirection.php')), 'position', 'ASC');
     $items = Red_Item::get_by_group($group, $pager);
     $this->render_admin('item_list', array('options' => $this->get_options(), 'items' => $items, 'pager' => $pager, 'group' => Red_Group::get($group), 'groups' => Red_Group::get_for_select(), 'date_format' => get_option('date_format')));
 }
 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&amp;token=' . $options['token'] . '&amp;sub=' . $export_type . '&amp;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);
 }
Example #19
0
 function collect($module)
 {
     $this->name = $module->name;
     $this->items = Red_Item::get_by_module($module->id);
 }
Example #20
0
 function process_bulk_action()
 {
     if (!isset($_POST['item'])) {
         return;
     }
     if (in_array($this->current_action(), array('reset', 'enable', 'disable', 'delete'))) {
         $redirections = array();
         $flush = array();
         foreach ((array) $_POST['item'] as $id) {
             $redirect = Red_Item::get_by_id(intval($id));
             if ($redirect) {
                 if ($this->current_action() === 'reset') {
                     $redirect->reset();
                 } elseif ($this->current_action() === 'enable') {
                     $redirect->enable();
                     $flush[] = $redirect->get_group_id();
                 } elseif ($this->current_action() === 'disable') {
                     $redirect->disable();
                     $flush[] = $redirect->get_group_id();
                 } elseif ($this->current_action() === 'delete') {
                     $redirect->delete();
                 }
             }
         }
         $flush = array_unique($flush);
         foreach ($flush as $group_id) {
             Red_Module::flush($group_id);
         }
     }
 }
 function upgrade_from_0()
 {
     // Convert to new format
     $items = get_option('redirection_list');
     if (is_array($items) && count($items) > 0) {
         foreach ($items as $item) {
             $data = array('old' => $item->url_old, 'type' => $item->type, 'red_action' => 'A_Redirector_URL', 'new' => $item->url_new);
             if ($item->regex) {
                 $data['regex'] = 'on';
             }
             Red_Item::create($data);
         }
         delete_option('redirection_list');
     }
     $this->upgrade_from_1();
     $this->upgrade_from_2();
     $this->upgrade_from_21();
     $this->upgrade_from_22();
 }
Example #22
0
?>
:</th>
				<td>
					<select name="match">
						<?php 
echo $this->select(Red_Match::all());
?>
					</select>

					<strong><?php 
_e('Action', 'redirection');
?>
:</strong>
					<select name="red_action">
						<?php 
echo $this->select(Red_Item::actions(), 'url');
?>
					</select>

					<label><?php 
_e('Regular expression', 'redirection');
?>
: <input id="regex" type="checkbox" name="regex"/></label>
				</td>
			</tr>
			<tr id="target">
				<th align="right"><?php 
_e('Target URL', 'redirection');
?>
:</th>
				<td><input type="text" name="target" style="width: 95%"/></td>
Example #23
0
 function red_redirect_add()
 {
     if (check_ajax_referer('redirection-redirect_add')) {
         $item = Red_Item::create($this->post);
         if (is_wp_error($item)) {
             $this->render_error($item->get_error_message());
         } elseif ($item !== false) {
             echo '<li class="type_' . $item->action_type . '" id="item_' . $item->id . '">';
             $this->render_admin('item', array('redirect' => $item, 'date_format' => get_option('date_format')));
             echo '</li>';
         } else {
             $this->render_error(__('Sorry, but your redirection was not created', 'redirection'));
         }
         die;
     }
 }
Example #24
0
 function admin_redirects($group)
 {
     include dirname(__FILE__) . '/models/pager.php';
     if ($group == 0) {
         $group = Red_Group::get_first_id();
     }
     $pager = new RE_Pager($_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC');
     $items = Red_Item::get_by_group($group, $pager);
     $this->render_admin('item_list', array('items' => $items, 'pager' => $pager, 'group' => Red_Group::get($group), 'groups' => Red_Group::get_for_select(), 'date_format' => get_option('date_format')));
 }
 function save_order($items, $start)
 {
     global $wpdb;
     foreach ($items as $pos => $id) {
         $wpdb->query("UPDATE {$wpdb->prefix}redirection_items SET position='" . ($pos + $start) . "' WHERE id='{$id}'");
     }
     $item = Red_Item::get_by_id($id);
     $group = Red_Group::get($item->group_id);
     Red_Module::flush($group->module_id);
 }
 function save_order($items, $start)
 {
     global $wpdb;
     foreach ($items as $pos => $id) {
         $wpdb->update($wpdb->prefix . 'redirection_items', array('position' => $pos + $start), array('id' => $id));
     }
     $item = Red_Item::get_by_id($id);
     $group = Red_Group::get($item->group_id);
     if ($group) {
         Red_Module::flush($group->module_id);
     }
 }