View difference between Paste ID: PzQcd8Yt and Lvi9D8cU
SHOW: | | - or go back to the newest paste.
1
<?php
2
//THIS CODE IS INTENDED FOR USE AS A VBULLETIN 4 CRON SCRIPT, IT REQUIRES THE VBULLETIN GLOBAL VAR PROVIDED BY CRONMANAGER.PHP - YOU MAY EMULATE THIS FUNCTIONALITY BY INCLUDING THE VBULLETIN GLOBAL.PHP
3
//RECCOMENDED SCHEDULE IS EVERY 30 MINUTES TO 1 HOUR
4
// ############################# VARIABLES ################################forgo
5-
mysql_pconnect("127.0.0.1", "nationg1_sb", "_e%nOpl04ZXz"); //MYSQL information for the SourceBans/Vbulletin Database
5+
mysql_pconnect("127.0.0.1", "lala", "lala"); //MYSQL information for the SourceBans/Vbulletin Database
6
$db_sourcebans = "nationg1_sourcebans"; //Database name for SourceBans
7
$db_vbulletin = "nationg1_vb4"; //Database name for vBulletin
8
define("DB_PREFIX", "sb"); //Table Prefix for SourceBans
9
$groups_with_admin= array(16,14,13,12,11,10,17); // Comma delimited list of groups with admin.
10
// ######################## SET PHP ENVIRONMENT ###########################
11
mysql_select_db($db_sourcebans);
12
mysql_set_charset('utf8');
13
require "CServerRcon.php"; //This file can be found in Sourcebans_web/includes/ and must be located in the same folder as this script.
14
error_reporting(E_ALL & ~E_NOTICE); //Prevents "Notices" From showing up
15
if (!is_object($vbulletin->db)) // Checks if the vbulletin database is accessiable, otherwise exit the script.
16
{
17
	exit;
18
}
19
$vbulletin->db->hide_errors(); // Hide Database errors from Vbulletin
20
21
// ############################ FUNCTIONS ################################
22
function encrypt_password($password, $salt='SourceBans') // If you use a different salt for your sourcebans installation, Change this.
23
{
24
	return sha1(sha1($salt . $password));
25
}	
26
function getRandomPassword($length = "") //Returns a random alphanumeric string of the specified length, 32 chars if unspecified
27
{	
28
	$code = md5(uniqid(rand(), true));
29
	if ($length != "") return substr($code, 0, $length);
30
	else return $code;
31
}
32
function AddAdmin($sourcebans_group_web, $sourcebans_group_server, $sourcebans_group_server_name, $sourcebans_group_server_access, $username, $email, $steamid, $userid)
33
{
34
	$password = getRandomPassword(8); // Generate a password for the user
35
	$res2 = mysql_query("SELECT `aid`, `group_id` FROM ".DB_PREFIX."_admins LEFT JOIN ".DB_PREFIX."_admins_servers_groups ON ".DB_PREFIX."_admins.aid=".DB_PREFIX."_admins_servers_groups.admin_id WHERE user='".$username."'") or die(mysql_error()); //A quick query so we can check to see if the admin is already an admin and whether they've been promoted or not
36
	$row2 = mysql_fetch_assoc($res2); // Fetch the result from the previous query
37
	mysql_query("INSERT INTO ".DB_PREFIX."_admins(user, authid, password, gid, email, srv_group)
38
				VALUES ('".$username."','".$steamid."','".encrypt_password($password)."',".$sourcebans_group_web.",'".$email."','".$sourcebans_group_server_name."') ON DUPLICATE KEY UPDATE srv_group='".$sourcebans_group_server_name."', gid='".$sourcebans_group_web."'") or die(mysql_error()); // Add the admin to the SourceBans database or, if they already exist, update their group data.
39
	if(mysql_affected_rows() > 0) //Check if the last query affected any rows, did it change anything or add a new admin? If no, skip.
40
	{
41
		$res3 = mysql_query("SELECT `aid` FROM ".DB_PREFIX."_admins WHERE user='".$username."'") or die(mysql_error()); // Select the users AdminID
42
		$row3 = mysql_fetch_assoc($res3);// Fetch the result from the previous query
43
		mysql_query("DELETE FROM ".DB_PREFIX."_admins_servers_groups WHERE admin_id='".$row3['aid']."'") or die(mysql_error());//Delete the users admin group data from the table
44
		mysql_query("INSERT IGNORE INTO ".DB_PREFIX."_admins_servers_groups(admin_id, group_id, srv_group_id, server_id) VALUES (".$row3['aid'].",".$sourcebans_group_server.",".$sourcebans_group_server_access.", -1)") or die(mysql_error()); //Add back their new data.
45
		
46
		//This next bit handles the PMs, the promotion/demotion will only work if your "highest power" admin group is the first id and the "lowest power" is the last id. i.e - Managers = GID1, Council = GID2, Admins = GID3, Members = GID4, Recruits = GID5
47
		if(mysql_num_rows($res2) == 0)
48
		{
49
			SendPM($userid, $username, $sourcebans_group_server_name, $password, 0); //Send the user a message with their password and username telling them that they've been added to the system.
50
		}
51
		elseif ($sourcebans_group_server > $row2['group_id'])
52
		{
53
			SendPM($userid, $username, $sourcebans_group_server_name, $password, 1); //Tell the user that they have been promoted in sourcebans.
54
		}
55
		elseif ($sourcebans_group_server < $row2['group_id'])
56
		{
57
			SendPM($userid, $username, $sourcebans_group_server_name, $password, 2); //Tell the user that they have been demoted in sourcebans.
58
		}
59
	}
60
}
61
function SendPM($pm_userid, $pm_username, $pm_group_name, $pm_password, $text) // Send a PM to the user, there is an actual datamanager API in vB for this, but it wasn't working reliable so I fell back on this method
62
{
63
	$pm_from_userid=1; //The Vbulletin UserID to send the PM from
64
	$pm_from_user_text="Bouncer";	//The Vbulletin UserName to send the PM from
65
	switch($text) //The messages, pretty self explanatory which is which.
66
	{
67
		case 0:
68
			$pm_text_title="Added to Sourcebans";
69
			$pm_text="[CENTER][IMG]http://nationgaming.org/bans/images/logos/sb-large.png[/IMG]
70
						[/CENTER]
71
						[LEFT][FONT=Garamond][SIZE=4]Hello ".$pm_username.",
72
73
						You have been added as a ".$pm_group_name." to the Nation Gaming admin system. Please find below the details required to login to the admin web panel, you will be able to access in-game admin automatically.
74
75
						Username: [B]".$pm_username."[/B]
76
						Password: [B]".$pm_password."[/B]
77
78
						You can use this information to login into your new account at: http://nationgaming.org/bans/index.php?p=login
79
80
						It is HIGHLY recommended for security reasons that you change your password to something memorable once you login. If you have any questions please contact me or any other Manager with your query.
81
82
						Many thanks,
83
						".$pm_from_user_text."
84
						[/SIZE][/FONT][/LEFT]";
85
			break;
86
		case 1:
87
			$pm_text_title="Promoted in Sourcebans";
88
			$pm_text="[CENTER][IMG]http://Nationgaming.org/bans/images/logos/sb-large.png[/IMG]
89
						[/CENTER]
90
						[LEFT][FONT=Garamond][SIZE=4]Hello ".$pm_username.",
91
92
						Congratulations! Your to a ".$pm_group_name." on the Nation Gaming admin system. Your login details are the same as before.
93
94
						You can use this information to login into your account at: http://nationgaming.org/bans/index.php?p=login
95
96
						If you have any questions please contact me or any other Manager with your query.
97
98
						Many thanks,
99
						".$pm_from_user_text."
100
						[/SIZE][/FONT][/LEFT]";
101
			break;
102
		case 2:
103
			$pm_text_title="Demoted in Sourcebans";
104
			$pm_text="[CENTER][IMG]http://nationgaming.org/bans/images/logos/sb-large.png[/IMG]
105
						[/CENTER]
106
						[LEFT][FONT=Garamond][SIZE=4]Hello ".$pm_username.",
107
108
						We are afraid that you have been demoted to a ".$pm_group_name." on the Nation Gaming's admin system. Your login details are the same as before.
109
110
						You can use this information to login into your account at: http://nationgaming.org/bans/index.php?p=login
111
112
						If you have any questions please contact me or any other Manager with your query.
113
114
						Many thanks,
115
						".$pm_from_user_text."
116
						[/SIZE][/FONT][/LEFT]";
117
			break;
118
		case 3:
119
			$pm_text_title="Removed from Sourcebans";
120
			$pm_text="[CENTER][IMG]http://nationgaming.org/bans/images/logos/sb-large.png[/IMG]
121
						[/CENTER]
122
						[LEFT][FONT=Garamond][SIZE=4]Hello ".$pm_username.",
123
124
						We are afraid that you have been removed from the Brotherhood of Gamers admin system. 
125
126
						If you have any questions please contact me or any other Manager with your query.
127
128
						Many thanks,
129
						".$pm_from_user_text."
130
						[/SIZE][/FONT][/LEFT]";
131
			break;
132
	}
133
	mysql_select_db($db_vbulletin); //Switch to vB Database
134
	mysql_query("INSERT INTO ". TABLE_PREFIX ."pmtext (`fromuserid`, `fromusername`, `title`, `message`, `touserarray`, `dateline`, `showsignature`) VALUES (".$pm_from_userid.", '".$pm_from_user_text."', '".$pm_text_title."', '".$pm_text."', '".serialize(array('cc' => array($pm_userid => $pm_username)))."', ".time().",1)") or die(mysql_error()); //Add the PM details to the pmtext table
135
	mysql_query("INSERT INTO ". TABLE_PREFIX ."pm (`pmtextid`, `userid`) VALUES (".mysql_insert_id().", ".$pm_userid.")") or die(mysql_error()); //Add the PM to the PM relational table
136
	mysql_query("UPDATE ". TABLE_PREFIX ."user SET pmunread=pmunread+1, pmtotal=pmtotal+1 WHERE `userid`=".$pm_userid) or die(mysql_error()); //Add to the user's unread and total PM count
137
	mysql_select_db($db_sourcebans); //Switch Back to SourceBans database
138
}
139
// ########################################################################
140
// ######################### START MAIN SCRIPT ############################
141
// ########################################################################
142
$res = $vbulletin->db->query_read("SELECT `username`, `user`.`userid` as userid, `usergroupid`, `membergroupids`, `email`, `field5` FROM ". TABLE_PREFIX ."user LEFT JOIN `". TABLE_PREFIX ."userfield` ON `user`.`userid`=`userfield`.`userid`"); //Select all users from the Vbulletin Database
143
while($row = mysql_fetch_assoc($res))
144
{
145
	// CLEANUP OLD/INVALID ADMINS
146
	$res2 = mysql_query("SELECT `aid` FROM ".DB_PREFIX."_admins WHERE user='".$row['username']."'") or die(mysql_error()); //Check if the user has a sourcebans account
147
	$row2 = mysql_fetch_assoc($res2);
148
	if (mysql_num_rows($res2)>0 && !in_array($row['usergroupid'], $groups_with_admin)) //If yes, and they are not in one of the admin groups defined above then...
149
	{
150
		$additionaluser=false;
151
		for($i = 0; $i < count($user_additional_groups); $i ++) //Check if they are marked as an additional user to any of the admin groups defined above
152
		{
153
			if (in_array($user_additional_groups[$i], $groups_with_admin))
154
			{
155
				$additionaluser=true;
156
				break;
157
			}
158
		}
159
		if($additionaluser == false) // If they aren't remove them from SourceBans.
160
		{
161
			mysql_query("DELETE FROM ".DB_PREFIX."_admins_servers_group WHERE admin_id='".$row2['aid']."'");
162
			mysql_query("DELETE FROM ".DB_PREFIX."_admins WHERE aid='".$row2['aid']."'");
163
			SendPM($row['userid'], $row['username'], "None", "None", 3); //Send a PM telling them that they've been removed from sourcebans
164
		}
165
	}
166
	
167
	// PRIMARY USERGROUP ADMIN CONTROL
168
	
169
	if (in_array($row['usergroupid'], $groups_with_admin)) //If the user is in a primary group defined as an admin, then add/edit them as such.
170
	{
171
		switch($row['usergroupid']) 
172
		{	
173
			case 16: //Group ID from vBulletin
174
				//AddAdmin(Sourcebans Web GroupID, Sourcebans Admin GroupID, Sourcebans Admin Group Name (Must be EXACT), Sourcebans Server GroupID, $row['username'], $row['email'], $row['field5'], $row['userid'])
175
				AddAdmin(3, 9, "Managers", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
176
				break;
177
			case 14:
178
				AddAdmin(9, 8, "Council", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
179
				break;
180
			case 13:
181
				AddAdmin(4, 7, "Division Leaders", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
182
				break;
183
			case 12:
184
				AddAdmin(5, 6, "Advisors", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
185
				break;
186
			case 11:
187
				AddAdmin(6, 4, "Honor Members", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
188
				break;
189
			case 10:
190
				AddAdmin(7, 3, "Members", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
191
				break;
192
			case 17:
193
				AddAdmin(8, 2, "Recruits", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
194
				break;
195
		}
196
		continue;
197
	}
198
	
199
	// ADDITIONAL USERGROUP ADMIN CONTROL
200
	if (strlen ($row['membergroupids']) > 0){
201
	$user_additional_groups=explode(",", $row['membergroupids']);
202
	}
203
	
204
	for($i = 0; $i < count($user_additional_groups); $i ++) //If the user is in a secondary group defined as an admin, then add/edit them as such, same format as above.
205
	{
206
		if (in_array($user_additional_groups[$i], $groups_with_admin))
207
		{
208
			switch($user_additional_groups[$i]) 
209
			{	
210
				case 16:
211
					AddAdmin(3, 9, "Managers", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
212
					break;
213
				case 14:
214
					AddAdmin(9, 8, "Council Members", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
215
					break;
216
				case 13:
217
					AddAdmin(4, 7, "Division Leaders", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
218
					break;
219
				case 12:
220
					AddAdmin(5, 6, "Advisors", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
221
					break;
222
				case 11:
223
					AddAdmin(6, 4, "Honor Members", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
224
					break;
225
				case 10:
226
					AddAdmin(7, 3, "Members", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
227
					break;
228
				case 17:
229
					AddAdmin(8, 2, "Recruits", 1, $row['username'], $row['email'], $row['field5'], $row['userid']);
230
					break;
231
			}
232
			continue;
233
		}
234
	}
235
}
236
//REHASH SERVERS, DOES NOT REQUIRE EDITING
237
$res = mysql_query("SELECT ip, port, rcon FROM ".DB_PREFIX."_servers WHERE enabled=1") or die(mysql_error());
238
while($row = mysql_fetch_assoc($res))
239
{
240
	$rcon = new CServerRcon($row['ip'], $row['port'], $row['rcon']);
241
	$rcon->rconCommand("sm_rehash");
242
	unset($rcon);
243
}
244
?>