PluginProbe
Redirection / 2.2.7
Redirection v2.2.7
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.php

redirection.php in Redirection 2.2.7, at redirection.php

347 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Redirection
4 Plugin URI: http://urbangiraffe.com/plugins/redirection/
5 Description: Manage all your 301 redirects and monitor 404 errors
6 Version: 2.2.7
7 Author: John Godley
8 Author URI: http://urbangiraffe.com
9 ============================================================================================================
10 This software is provided "as is" and any express or implied warranties, including, but not limited to, the
11 implied warranties of merchantibility and fitness for a particular purpose are disclaimed. In no event shall
12 the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or
13 consequential damages(including, but not limited to, procurement of substitute goods or services; loss of
14 use, data, or profits; or business interruption) however caused and on any theory of liability, whether in
15 contract, strict liability, or tort(including negligence or otherwise) arising in any way out of the use of
16 this software, even if advised of the possibility of such damage.
17
18 For full license details see license.txt
19 ============================================================================================================
20 */
21
22 include dirname( __FILE__ ).'/plugin.php';
23 include dirname( __FILE__ ).'/models/redirect.php';
24 include dirname( __FILE__ ).'/models/match.php';
25 include dirname( __FILE__ ).'/models/log.php';
26 include dirname( __FILE__ ).'/models/group.php';
27 include dirname( __FILE__ ).'/models/module.php';
28 include dirname( __FILE__ ).'/models/action.php';
29 include dirname( __FILE__ ).'/models/monitor.php';
30 include dirname( __FILE__ ).'/modules/wordpress.php';
31 include dirname( __FILE__ ).'/modules/404.php';
32
33 define( 'REDIRECTION_VERSION', '2.2' );
34
35 if ( class_exists( 'Redirection' ) )
36 return;
37
38 class Redirection extends Redirection_Plugin {
39 var $hasMatched = false;
40
41 function Redirection() {
42 $this->register_plugin( 'redirection', __FILE__ );
43
44 if ( is_admin() ) {
45 $this->add_action( 'admin_menu' );
46 $this->add_action( 'load-tools_page_redirection', 'redirection_head' );
47 $this->add_action( 'init', 'inject' );
48
49 $this->register_activation( __FILE__ );
50 $this->register_plugin_settings( __FILE__ );
51
52 // Ajax functions
53 if ( defined( 'DOING_AJAX' ) ) {
54 include_once dirname( __FILE__ ).'/ajax.php';
55 $this->ajax = new RedirectionAjax();
56 }
57 }
58 else {
59 $this->update();
60
61 // Create a WordPress exporter and let it handle the load
62 $this->wp = new WordPress_Module();
63 $this->wp->start();
64
65 $this->error = new Error404_Module();
66 $this->error->start();
67 }
68
69 $this->monitor = new Red_Monitor( $this->get_options() );
70 }
71
72 function update() {
73 $version = get_option( 'redirection_version' );
74
75 if ( $version != REDIRECTION_VERSION ) {
76 include_once dirname( __FILE__ ).'/models/database.php';
77
78 $db = new RE_Database();
79 return $db->upgrade( $version, REDIRECTION_VERSION );
80 }
81
82 return true;
83 }
84
85 function activate() {
86 if ( $this->update() === false ) {
87 $db = new RE_Database();
88 $db->remove( $version, REDIRECTION_VERSION );
89 exit();
90 }
91 }
92
93 function plugin_settings( $links ) {
94 $settings_link = '<a href="tools.php?page='.basename( __FILE__ ).'">'.__( 'Settings', 'redirection' ).'</a>';
95 array_unshift( $links, $settings_link );
96 return $links;
97 }
98
99 function version() {
100 $plugin_data = implode( '', file( __FILE__ ) );
101
102 if ( preg_match( '|Version:(.*)|i', $plugin_data, $version ) )
103 return trim( $version[1] );
104 return '';
105 }
106
107 function redirection_head() {
108 wp_enqueue_script( 'redirection', plugin_dir_url( __FILE__ ).'js/redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $this->version() );
109 wp_enqueue_style( 'redirection', plugin_dir_url( __FILE__ ).'admin.css', $this->version() );
110
111 wp_localize_script( 'redirection', 'Redirectioni10n', array(
112 'please_wait' => __( 'Please wait...', 'redirection' ),
113 'type' => 1,
114 'progress' => '<img src="'.plugin_dir_url( __FILE__ ).'/images/progress.gif" alt="loading" width="50" height="16"/>',
115 'are_you_sure' => __( 'Are you sure?', 'redirection' ),
116 'none_select' => __( 'No items have been selected', 'redirection' )
117 ) );
118 }
119
120 function admin_menu() {
121 add_management_page( __( "Redirection", 'redirection' ), __( "Redirection", 'redirection' ), "administrator", basename( __FILE__ ), array( &$this, "admin_screen" ) );
122 }
123
124 function admin_screen() {
125 $this->update();
126
127 // Decide what to do
128 $sub = isset( $_GET['sub'] ) ? $_GET['sub'] : '';
129
130 $options = $this->get_options();
131
132 if ( isset($_GET['sub']) ) {
133 if ( $_GET['sub'] == 'log' )
134 return $this->admin_screen_log();
135 elseif ( $_GET['sub'] == 'options' )
136 return $this->admin_screen_options();
137 elseif ( $_GET['sub'] == 'process' )
138 return $this->admin_screen_process();
139 elseif ( $_GET['sub'] == 'groups' )
140 return $this->admin_groups( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
141 elseif ( $_GET['sub'] == 'modules' )
142 return $this->admin_screen_modules();
143 elseif ( $_GET['sub'] == 'support' )
144 return $this->render_admin('support');
145 }
146
147 return $this->admin_redirects( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
148 }
149
150 function admin_screen_modules() {
151 if ( isset( $_POST['create'] ) && check_admin_referer( 'redirection-module_add' ) ) {
152 $data = stripslashes_deep( $_POST );
153
154 if ( ( $module = Red_Module::create( $data ) ) ) {
155 $moduleid = 0;
156 if ( isset( $_POST['module'] ) )
157 $moduleid = intval( $_POST['module'] );
158
159 $this->render_message( __( 'Your module was successfully created', 'redirection' ) );
160 Red_Module::flush( $moduleid );
161 }
162 else
163 $this->render_error( __( 'Your module was not created - did you provide a name?', 'redirection' ) );
164 }
165
166 $options = $this->get_options();
167 $this->render_admin( 'module_list', array( 'modules' => Red_Module::get_all(), 'module_types' => Red_Module::get_types(), 'token' => $options['token'] ) );
168 }
169
170 function get_options() {
171 $options = get_option( 'redirection_options' );
172 if ( $options === false )
173 $options = array();
174
175 $defaults = array (
176 'lookup' => 'http://urbangiraffe.com/map/?from=redirection&amp;ip=',
177 'support' => false,
178 'log_redirections' => true,
179 'log_404s' => true,
180 'expire' => 0,
181 'token' => '',
182 'monitor_new_posts' => false,
183 'monitor_post' => 0,
184 'auto_target' => '',
185 );
186
187 foreach ( $defaults AS $key => $value ) {
188 if ( !isset( $options[$key] ) )
189 $options[$key] = $value;
190 }
191
192 if ( $options['lookup'] == 'http://geomaplookup.cinnamonthoughts.org/?ip=' || $options['lookup'] == 'http://geomaplookup.net/?ip=' )
193 $options['lookup'] = 'http://urbangiraffe.com/map/?from=redirection&amp;ip=';
194
195 return $options;
196 }
197
198 function inject() {
199 $options = $this->get_options();
200
201 if ( isset($_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['token'] == $options['token'] && $_GET['page'] == 'redirection.php' && in_array( $_GET['sub'], array( 'rss', 'xml', 'csv', 'apache' ) ) ) {
202 include dirname( __FILE__ ).'/models/file_io.php';
203
204 $exporter = new Red_FileIO;
205 if ( $exporter->export( $_GET['sub'] ) )
206 die();
207 }
208 }
209
210 function admin_screen_options() {
211 if ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) {
212 $options['lookup'] = stripslashes( $_POST['lookup'] );
213 $options['monitor_post'] = stripslashes( $_POST['monitor_post'] );
214 // $options['monitor_category'] = stripslashes( $_POST['monitor_category'] );
215 $options['auto_target'] = stripslashes( $_POST['auto_target'] );
216 $options['support'] = isset( $_POST['support'] ) ? true : false;
217 $options['log_redirections'] = (bool) @ $_POST['log_redirections'];
218 $options['log_404s'] = (bool) @ $_POST['log_404s'];
219 $options['monitor_new_posts'] = isset( $_POST['monitor_new_posts'] ) ? true : false;
220 $options['expire'] = intval( $_POST['expire'] );
221 $options['token'] = stripslashes( $_POST['token'] );
222
223 if ( trim( $options['token'] ) == '' )
224 $options['token'] = md5( uniqid() );
225
226 update_option( 'redirection_options', $options );
227
228 $this->render_message( __( 'Your options were updated', 'redirection' ) );
229 }
230 elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
231 include dirname( __FILE__ ).'/models/database.php';
232
233 $db = new RE_Database;
234 $db->remove( __FILE__ );
235
236 $this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
237 return;
238 }
239 elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
240 include dirname( __FILE__ ).'/models/file_io.php';
241
242 $importer = new Red_FileIO;
243
244 $count = $importer->import( $_POST['group'], $_FILES['upload'] );
245 if ( $count > 0 )
246 $this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
247 else
248 $this->render_message( __( 'No items were imported', 'redirection' ) );
249 }
250
251 $groups = Red_Group::get_for_select();
252 $this->render_admin( 'options', array( 'options' => $this->get_options(), 'groups' => $groups ) );
253 }
254
255 function admin_screen_log() {
256 include dirname( __FILE__ ).'/models/pager.php';
257
258 if ( isset( $_POST['deleteall'] ) && check_admin_referer( 'redirection-process_logs' ) ) {
259 if ( isset( $_GET['module'] ) )
260 RE_Log::delete_all( array( 'module_id' => intval( $_GET['module'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
261 else if (isset($_GET['group']))
262 RE_Log::delete_all( array( 'group_id' => intval( $_GET['group'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
263 else
264 RE_Log::delete_all( array(), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
265
266 $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
267 }
268
269 $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' );
270
271 if ( isset( $_GET['module'] ) )
272 $logs = RE_Log::get_by_module( $pager, intval( $_GET['module'] ) );
273 else if (isset($_GET['group']))
274 $logs = RE_Log::get_by_group( $pager, intval( $_GET['group'] ) );
275 else if (isset($_GET['redirect']))
276 $logs = RE_Log::get_by_redirect( $pager, intval( $_GET['redirect'] ) );
277 else
278 $logs = RE_Log::get( $pager );
279
280 $options = $this->get_options();
281 $this->render_admin( 'log', array( 'logs' => $logs, 'pager' => $pager, 'lookup' => $options['lookup'] ) );
282 }
283
284 function admin_groups( $module ) {
285 include dirname( __FILE__ ).'/models/pager.php';
286
287 if ( isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
288 if ( Red_Group::create( stripslashes_deep( $_POST ) ) ) {
289 $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
290 Red_Module::flush( $module );
291 }
292 else
293 $this->render_error( __( 'Please specify a group name', 'redirection' ) );
294 }
295
296 if ( $module == 0 )
297 $module = Red_Module::get_first_id();
298
299 $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
300 $items = Red_Group::get_all( $module, $pager );
301
302 $this->render_admin( 'group_list', array( 'groups' => $items, 'pager' => $pager, 'modules' => Red_Module::get_for_select(), 'module' => Red_Module::get( $module ) ) );
303 }
304
305 function admin_redirects( $group ) {
306 include dirname( __FILE__ ).'/models/pager.php';
307
308 if ( $group == 0 )
309 $group = Red_Group::get_first_id();
310
311 $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
312 $items = Red_Item::get_by_group( $group, $pager );
313
314 $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' ) ) );
315 }
316
317 function setMatched( $match ) {
318 $this->hasMatched = $match;
319 }
320
321 function hasMatched() {
322 return $this->hasMatched;
323 }
324
325 function locales() {
326 $locales = array();
327 if ( file_exists( dirname( __FILE__ ).'/readme.txt' ) ) {
328 $readme = file_get_contents( dirname( __FILE__ ).'/readme.txt' );
329
330 $start = strpos( $readme, __( 'Redirection is available in' ) );
331 $end = strpos( $readme, '==', $start );
332 if ( $start !== false && $end !== false ) {
333 if ( preg_match_all( '/^\* (.*?) by (.*?)/m', substr( $readme, $start, $end ), $matches ) > 0 ) {
334 $locales = $matches[1];
335 }
336 }
337
338 sort( $locales );
339 }
340
341 return $locales;
342 }
343 }
344
345 // Instantiate the plugin
346 $redirection = new Redirection;
347