PluginProbe
Redirection / 2.1.29
Redirection v2.1.29
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 / models / database.php

database.php in Redirection 2.1.29, at models/database.php

385 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!function_exists ('get_home_path'))
4 {
5 function get_home_path() {
6 $home = get_option( 'home' );
7 if ( $home != '' && $home != get_option( 'siteurl' ) ) {
8 $home_path = parse_url( $home );
9 $home_path = $home_path['path'];
10 $root = str_replace( $_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"] );
11 $home_path = trailingslashit( $root.$home_path );
12 } else {
13 $home_path = ABSPATH;
14 }
15
16 return $home_path;
17 }
18 }
19
20 class A_Redirector_URL
21 {
22 }
23
24 class Redirector_Login
25 {
26 }
27
28 class Redirector_LuckyDip
29 {
30 }
31
32 class Redirector_Random
33 {
34 }
35
36 class Redirector_Referrer
37 {
38 }
39
40 class RE_Database
41 {
42 function install ()
43 {
44 global $wpdb;
45
46 // Create database
47 $wpdb->query ("CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_items` (
48 `id` int(11) unsigned NOT NULL auto_increment,
49 `url` mediumtext NOT NULL,
50 `regex` int(11) unsigned NOT NULL default '0',
51 `position` int(11) unsigned NOT NULL default '0',
52 `last_count` int(10) unsigned NOT NULL default '0',
53 `last_access` datetime NOT NULL,
54 `group_id` int(11) NOT NULL default '0',
55 `status` enum('enabled','disabled') NOT NULL default 'enabled',
56 `action_type` varchar(20) NOT NULL,
57 `action_code` int(11) unsigned NOT NULL,
58 `action_data` mediumtext,
59 `match_type` varchar(20) NOT NULL,
60 `title` varchar(50) NULL,
61 PRIMARY KEY (`id`)
62 )");
63
64 $wpdb->query ("CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_groups` (
65 `id` int(11) NOT NULL auto_increment,
66 `name` varchar(50) NOT NULL,
67 `tracking` int(11) NOT NULL default '1',
68 `module_id` int(11) unsigned NOT NULL default '0',
69 `status` enum('enabled','disabled') NOT NULL default 'enabled',
70 `position` int(11) unsigned NOT NULL default '0',
71 PRIMARY KEY (`id`)
72 )");
73
74 $wpdb->query ("CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_logs` (
75 `id` int(11) unsigned NOT NULL auto_increment,
76 `created` datetime NOT NULL,
77 `url` mediumtext NOT NULL,
78 `sent_to` mediumtext,
79 `agent` mediumtext NOT NULL,
80 `referrer` mediumtext,
81 `redirection_id` int(11) unsigned default NULL,
82 `ip` varchar(17) NOT NULL default '',
83 `module_id` int(11) unsigned NOT NULL,
84 `group_id` int(11) unsigned default NULL,
85 PRIMARY KEY (`id`)
86 )");
87
88 $wpdb->query ("CREATE TABLE `{$wpdb->prefix}redirection_modules` (
89 `id` int(11) unsigned NOT NULL auto_increment,
90 `type` varchar(20) NOT NULL default '',
91 `name` varchar(50) NOT NULL default '',
92 `options` mediumtext,
93 PRIMARY KEY (`id`)
94 )");
95
96 $this->defaults ();
97 }
98
99 function defaults ()
100 {
101 global $wpdb;
102
103 $optionswp = '';
104 $options404 = '';
105 $optionsaoache = '';
106 // $optionsapache = $wpdb->escape (serialize (array ('location' => get_home_path ().'.htaccess')));
107
108 // Modules
109 if ($wpdb->get_var ("SELECT COUNT(*) FROM {$wpdb->prefix}redirection_modules") == 0)
110 {
111 $wpdb->query ("INSERT INTO {$wpdb->prefix}redirection_modules (id,type,name,options) VALUES (1,'wp','".__ ('WordPress', 'redirection')."','$optionswp')");
112 $wpdb->query ("INSERT INTO {$wpdb->prefix}redirection_modules (id,type,name,options) VALUES (2,'apache','".__ ('Apache', 'redirection')."','$optionsapache')");
113 $wpdb->query ("INSERT INTO {$wpdb->prefix}redirection_modules (id,type,name,options) VALUES (3,'404','".__ ('404 Errors', 'redirection')."','$options404')");
114 }
115
116 // Groups
117 if ($wpdb->get_var ("SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups") == 0)
118 {
119 $wpdb->query ("INSERT INTO {$wpdb->prefix}redirection_groups (id,name,module_id,position) VALUES (1,'".__ ('Redirections', 'redirection')."',1,0)");
120 $wpdb->query ("INSERT INTO {$wpdb->prefix}redirection_groups (id,name,module_id,position) VALUES (2,'".__ ('Modified posts', 'redirection')."',1,1)");
121 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET group_id='1', status='enabled'");
122
123 $options = get_option ('redirection_options');
124 $options['monitor_post'] = 2;
125 $options['monitor_category'] = 2;
126
127 update_option ('redirection_options', $options);
128 }
129 }
130
131 function upgrade ($current, $target)
132 {
133 global $wpdb;
134
135 if ($current === false)
136 $this->install ();
137 else if ($current == '1.2')
138 $this->upgrade_from_0 ();
139 else if ($current == '1.7')
140 $this->upgrade_from_1 ();
141 else if ($current == '1.9' || $current == 'DRAINHOLE_VERSION')
142 $this->upgrade_from_2 ();
143 else if ($current == '2.0')
144 $this->upgrade_from_20 ();
145 else if ($current == '2.0.1')
146 $this->upgrade_from_21 ();
147 else if ($current == '2.0.2')
148 $this->upgrade_from_22 ();
149
150 if ( version_compare( $current, '2.1.16' ) == -1 )
151 $this->upgrade_to_216();
152
153 // Check that the IP field exists - some users don't have this
154 $test = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT 0,1");
155 if ( !empty( $test ) && !isset( $test[0]->ip ) ) {
156 @$wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `ip` varchar(17) NOT NULL");
157 }
158
159 update_option ('redirection_version', $target);
160 }
161
162 function upgrade_from_0 ()
163 {
164 // Convert to new format
165 $items = get_option ('redirection_list');
166 if (is_array ($items) && count ($items) > 0)
167 {
168 foreach ($items AS $item)
169 {
170 $data = array ('old' => $item->url_old, 'type' => $item->type, 'red_action' => 'A_Redirector_URL', 'new' => $item->url_new);
171 if ($item->regex)
172 $data['regex'] = 'on';
173
174 Red_Item::create ($data);
175 }
176
177 delete_option ('redirection_list');
178 }
179
180 $this->upgrade_from_1 ();
181 $this->upgrade_from_2 ();
182 $this->upgrade_from_21 ();
183 $this->upgrade_from_22 ();
184 }
185
186 function upgrade_from_1 ()
187 {
188 global $wpdb;
189
190 $wpdb->query ("ALTER TABLE {$wpdb->prefix}redirection CHANGE `type` `type` enum('301','302','307','404','410','pass') NOT NULL DEFAULT '301' ;");
191
192 $this->upgrade_from_2 ();
193 $this->upgrade_from_21 ();
194 $this->upgrade_from_22 ();
195 }
196
197 function upgrade_from_2 ()
198 {
199 global $wpdb;
200
201 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection` ADD `group_id` int NOT NULL DEFAULT 0;");
202 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection` ADD `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled'");
203 $wpdb->query ("RENAME TABLE `{$wpdb->prefix}redirection` TO `{$wpdb->prefix}redirection_items`;");
204 $wpdb->query ("RENAME TABLE `{$wpdb->prefix}redirection_log` TO `{$wpdb->prefix}redirection_logs`;");
205 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE `redirection_id` `redirection_id` int(11) UNSIGNED DEFAULT NULL ;");
206 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `module_id` int(11) UNSIGNED DEFAULT NULL ;");
207 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `group_id` int(11) UNSIGNED DEFAULT NULL ;");
208
209 $this->install (); // Ensure new tables are created
210
211 // Merge options together
212 $options = get_option ('redirection_options');
213 if ($options === false)
214 $options = array ();
215
216 $options['auto_target'] = get_option ('redirection_auto_target');
217 $options['lookup'] = get_option ('redirection_lookup');
218
219 delete_option ('redirection_root');
220 delete_option ('redirection_index');
221 delete_option ('redirection_post');
222 delete_option ('redirection_404_log');
223 delete_option ('redirection_global_404');
224 delete_option ('redirection_auto_target');
225 delete_option ('redirection_lookup');
226 delete_option ('redirection_updates');
227
228 // Update all the redirectors
229 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `action_type` varchar(20) NOT NULL");
230 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `action_data` mediumtext");
231 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `match_type` varchar(20) NOT NULL");
232 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `action_code` int(11) UNSIGNED DEFAULT NULL");
233
234 $rows = $wpdb->get_results ("SELECT id,redirector,type FROM {$wpdb->prefix}redirection_items");
235 if ($rows)
236 {
237 foreach ($rows AS $row)
238 {
239 $obj = unserialize ($row->redirector);
240 $data = array ();
241
242 foreach ($obj AS $key => $value)
243 $data[$key] = $value;
244
245 if (count ($data) == 1 && !is_array (current ($data)))
246 $data = current ($data);
247
248 $type = strtolower (get_class ($obj));
249 if ($type == 'a_redirector_url')
250 {
251 $match = 'url';
252
253 if ($row->type == 'pass')
254 $action = 'pass';
255 else if ($row->type == '404' || $row->type == '410')
256 $action = 'error';
257 else
258 $action = 'url';
259 }
260 else if ($type == 'redirector_login')
261 {
262 $match = 'login';
263 $data = array ('url_loggedin' => $data['url'][0], 'url_loggedout' => $data['url'][1]);
264 $data = $wpdb->escape (serialize ($data));
265
266 if ($row->type == 'pass')
267 $action = 'pass';
268 else if ($row->type == '404' || $row->type == '410')
269 $action = 'error';
270 else
271 $action = 'url';
272 }
273 else if ($type == 'redirector_luckydip')
274 {
275 $match = 'url';
276 $action = 'random';
277 $data = '';
278 }
279 else if ($type == 'redirector_random')
280 {
281 $match = 'url';
282 $action = 'random';
283 $data = '';
284 }
285 else if ($type == 'redirector_referrer')
286 {
287 $match = 'referrer';
288 $action = 'url';
289 $data = array ('referrer' => $data['referrer'], 'regex' => $data['regex'], 'url_from' => $data['url'][0], 'url_notfrom' => $data['url'][1]);
290 $data = $wpdb->escape (serialize ($data));
291
292 if ($row->type == 'pass')
293 $action = 'pass';
294 else if ($row->type == '404' || $row->type == '410')
295 $action = 'error';
296 else
297 $action = 'url';
298 }
299
300 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET match_type='$match', action_type='$action', action_data='$data' WHERE id='{$row->id}'");
301 }
302 }
303
304 // Action code
305 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET action_code=type");
306 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET action_code=301 WHERE type = '301'");
307 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET action_code=302 WHERE type = '302'");
308 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET action_code=307 WHERE type = '307'");
309 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET action_code=404 WHERE type = '404'");
310 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET action_code=410 WHERE type = '410'");
311 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_items SET action_code=301 WHERE type = 'pass'");
312 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` DROP `type`;");
313
314 // Drop the old column
315 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` DROP `redirector`");
316
317 // Convert log format
318 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `ip2` varchar(17) NOT NULL");
319 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_logs SET ip2=INET_NTOA(ip)");
320 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_logs` DROP `ip`");
321 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE `ip2` `ip` varchar(17) NOT NULL DEFAULT '' ");
322 $wpdb->query ("UPDATE {$wpdb->prefix}redirection_logs SET group_id='1', module_id='1'");
323
324 update_option ('redirection_options', $options);
325
326 $this->upgrade_from_21 ();
327 $this->upgrade_from_22 ();
328 }
329
330 function upgrade_from_20 ()
331 {
332 global $wpdb;
333
334 $this->defaults ();
335 $this->upgrade_from_21 ();
336 $this->upgrade_from_22 ();
337 }
338
339 function upgrade_from_21 ()
340 {
341 global $wpdb;
342
343 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `title` varchar(50) NULL");
344
345 $this->upgrade_from_22 ();
346 }
347
348 function upgrade_from_22 () {
349 global $wpdb;
350
351 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` varchar(50) NULL");
352 }
353
354 function upgrade_to_216() {
355 global $wpdb;
356
357 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX (module_id)");
358 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX (status)");
359 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX (url(200))");
360 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX (status)");
361 $wpdb->query ("ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX (regex)");
362 }
363
364 function remove ($plugin)
365 {
366 global $wpdb;
367
368 $wpdb->query ("DROP TABLE IF EXISTS {$wpdb->prefix}redirection;");
369 $wpdb->query ("DROP TABLE IF EXISTS {$wpdb->prefix}redirection_items;");
370 $wpdb->query ("DROP TABLE IF EXISTS {$wpdb->prefix}redirection_logs;");
371 $wpdb->query ("DROP TABLE IF EXISTS {$wpdb->prefix}redirection_groups;");
372 $wpdb->query ("DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules;");
373
374 delete_option ('redirection_lookup');
375 delete_option ('redirection_post');
376 delete_option ('redirection_root');
377 delete_option ('redirection_index');
378 delete_option ('redirection_version');
379
380 $current = get_option('active_plugins');
381 array_splice ($current, array_search (basename (dirname ($plugin)).'/'.basename ($plugin), $current), 1 );
382 update_option('active_plugins', $current);
383 }
384 }
385 ?>