1<?php
2/**
3 * MyBB 1.8
4 * Copyright 2014 MyBB Group, All Rights Reserved
5 *
6 * Website: http://www.mybb.com
7 * License: http://www.mybb.com/about/license
8 *
9 */
10
11/**
12 * Upgrade Script: 1.4 or 1.4.1
13 */
14
15
16$upgrade_detail = array(
17	"revert_all_templates" => 0,
18	"revert_all_themes" => 0,
19	"revert_all_settings" => 0
20);
21
22@set_time_limit(0);
23
24function upgrade13_dbchanges()
25{
26	global $db, $output, $mybb;
27
28	$output->print_header("Performing Queries");
29
30	echo "<p>Performing necessary upgrade queries..</p>";
31	flush();
32
33	if($db->type == "mysql" || $db->type == "mysqli")
34	{
35		$db->write_query("ALTER TABLE ".TABLE_PREFIX."adminsessions ADD INDEX ( `uid` )");
36		$db->write_query("ALTER TABLE ".TABLE_PREFIX."adminsessions ADD INDEX ( `dateline` )");
37	}
38
39	if($db->type != "sqlite")
40	{
41		if($db->index_exists("users", "username"))
42		{
43			$db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP KEY username");
44		}
45
46		$query = $db->simple_select("users", "username, uid", "1=1 GROUP BY uid, username HAVING count(*) > 1");
47		while($user = $db->fetch_array($query))
48		{
49			$db->update_query("users", array('username' => $user['username']."_dup".$user['uid']), "uid='{$user['uid']}'", 1);
50		}
51
52		if($db->type == "pgsql")
53		{
54			$db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD UNIQUE(username)");
55		}
56		else
57		{
58			$db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD UNIQUE KEY username (username)");
59		}
60	}
61
62	if($db->type == "pgsql")
63	{
64		$db->write_query("ALTER TABLE ".TABLE_PREFIX."users CHANGE longregip longregip int NOT NULL default '0'");
65		$db->write_query("ALTER TABLE ".TABLE_PREFIX."users CHANGE longlastip longlastip int NOT NULL default '0'");
66
67		$db->write_query("ALTER TABLE ".TABLE_PREFIX."posts CHANGE longipaddress longipaddress int NOT NULL default '0'");
68	}
69	else
70	{
71		$db->write_query("ALTER TABLE ".TABLE_PREFIX."users CHANGE longregip longregip int(11) NOT NULL default '0'");
72		$db->write_query("ALTER TABLE ".TABLE_PREFIX."users CHANGE longlastip longlastip int(11) NOT NULL default '0'");
73
74		$db->write_query("ALTER TABLE ".TABLE_PREFIX."posts CHANGE longipaddress longipaddress int(11) NOT NULL default '0'");
75	}
76
77	$contents .= "Click next to continue with the upgrade process.</p>";
78	$output->print_contents($contents);
79	$output->print_footer("13_dbchanges1");
80}
81
82function upgrade13_dbchanges1()
83{
84	global $db, $output;
85
86	$output->print_header("Post IP Repair Conversion");
87
88	if(!$_POST['ipspage'])
89	{
90		$ipp = 5000;
91	}
92	else
93	{
94		$ipp = (int)$_POST['ipspage'];
95	}
96
97	if($_POST['ipstart'])
98	{
99		$startat = (int)$_POST['ipstart'];
100		$upper = $startat+$ipp;
101		$lower = $startat;
102	}
103	else
104	{
105		$startat = 0;
106		$upper = $ipp;
107		$lower = 1;
108	}
109
110	$query = $db->simple_select("posts", "COUNT(pid) AS ipcount");
111	$cnt = $db->fetch_array($query);
112
113	if($upper > $cnt['ipcount'])
114	{
115		$upper = $cnt['ipcount'];
116	}
117
118	echo "<p>Repairing ip {$lower} to {$upper} ({$cnt['ipcount']} Total)</p>";
119	flush();
120
121	$ipaddress = false;
122
123	$query = $db->simple_select("posts", "ipaddress, longipaddress, pid", "", array('limit_start' => $lower, 'limit' => $ipp));
124	while($post = $db->fetch_array($query))
125	{
126		// Have we already converted this ip?
127		if(my_ip2long($post['ipaddress']) < 0)
128		{
129			$db->update_query("posts", array('longipaddress' => my_ip2long($post['ipaddress'])), "pid = '{$post['pid']}'");
130		}
131		$ipaddress = true;
132	}
133
134	$remaining = $upper-$cnt['ipcount'];
135	if($remaining && $ipaddress)
136	{
137		$nextact = "13_dbchanges1";
138		$startat = $startat+$ipp;
139		$contents = "<p><input type=\"hidden\" name=\"ipspage\" value=\"$ipp\" /><input type=\"hidden\" name=\"ipstart\" value=\"$startat\" />Done. Click Next to move on to the next set of post ips.</p>";
140	}
141	else
142	{
143		$nextact = "13_dbchanges2";
144		$contents = "<p>Done</p><p>All post ips have been converted to the new ip format. Click next to continue.</p>";
145	}
146	$output->print_contents($contents);
147
148	global $footer_extra;
149	$footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
150
151	$output->print_footer($nextact);
152}
153
154function upgrade13_dbchanges2()
155{
156	global $db, $output;
157
158	$output->print_header("User IP Repair Conversion");
159
160	if(!$_POST['ipspage'])
161	{
162		$ipp = 5000;
163	}
164	else
165	{
166		$ipp = (int)$_POST['ipspage'];
167	}
168
169	if($_POST['ipstart'])
170	{
171		$startat = (int)$_POST['ipstart'];
172		$upper = $startat+$ipp;
173		$lower = $startat;
174	}
175	else
176	{
177		$startat = 0;
178		$upper = $ipp;
179		$lower = 1;
180	}
181
182	$query = $db->simple_select("users", "COUNT(uid) AS ipcount");
183	$cnt = $db->fetch_array($query);
184
185	if($upper > $cnt['ipcount'])
186	{
187		$upper = $cnt['ipcount'];
188	}
189
190	$contents .= "<p>Repairing ip {$lower} to {$upper} ({$cnt['ipcount']} Total)</p>";
191
192	$ipaddress = false;
193	$update_array = array();
194
195	$query = $db->simple_select("users", "regip, lastip, longlastip, longregip, uid", "", array('limit_start' => $lower, 'limit' => $ipp));
196	while($user = $db->fetch_array($query))
197	{
198		// Have we already converted this ip?
199		if(my_ip2long($user['regip']) < 0)
200		{
201			$update_array['longregip'] = (int)my_ip2long($user['regip']);
202		}
203
204		if(my_ip2long($user['lastip']) < 0)
205		{
206			$update_array['longlastip'] = (int)my_ip2long($user['lastip']);
207		}
208
209		if(!empty($update_array))
210		{
211			$db->update_query("users", $update_array, "uid = '{$user['uid']}'");
212		}
213
214		$update_array = array();
215		$ipaddress = true;
216	}
217
218	$remaining = $upper-$cnt['ipcount'];
219	if($remaining && $ipaddress)
220	{
221		$nextact = "13_dbchanges2";
222		$startat = $startat+$ipp;
223		$contents .= "<p><input type=\"hidden\" name=\"ipspage\" value=\"$ipp\" /><input type=\"hidden\" name=\"ipstart\" value=\"$startat\" />Done. Click Next to move on to the next set of user ips.</p>";
224	}
225	else
226	{
227		$nextact = "13_done";
228		$contents .= "<p>Done</p><p>All user ips have been converted to the new ip format. Click next to continue.</p>";
229	}
230	$output->print_contents($contents);
231
232	global $footer_extra;
233	$footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
234
235	$output->print_footer($nextact);
236}
237
238