summaryrefslogtreecommitdiffstats
path: root/sections/user/linkedfunctions.php
blob: 22fa9cc22010fc6b7d477cdd0cfccca9e09cedcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?
function link_users($UserID, $TargetID, $IgnoreComments) {

	global $DB, $LoggedUser;

	authorize();
	if (!check_perms('users_mod')) {
		error(403);
	}

	if (!is_number($UserID) || !is_number($TargetID)) {
		error(403);
	}
	if ($UserID == $TargetID) {
		return;
	}

	$DB->query("
		SELECT 1
		FROM users_main
		WHERE ID IN ($UserID, $TargetID)");
	if ($DB->record_count() !== 2) {
		error(403);
	}

	$DB->query("
		SELECT GroupID
		FROM users_dupes
		WHERE UserID = $TargetID");
	list($TargetGroupID) = $DB->next_record();
	$DB->query("
		SELECT u.GroupID, d.Comments
		FROM users_dupes AS u
			JOIN dupe_groups AS d ON d.ID = u.GroupID
		WHERE UserID = $UserID");
	list($UserGroupID, $Comments) = $DB->next_record();

	$UserInfo = Users::user_info($UserID);
	$TargetInfo = Users::user_info($TargetID);
	if (!$UserInfo || !$TargetInfo) {
		return;
	}

	if ($TargetGroupID) {
		if ($TargetGroupID == $UserGroupID) {
			return;
		}
		if ($UserGroupID) {
			$DB->query("
				UPDATE users_dupes
				SET GroupID = $TargetGroupID
				WHERE GroupID = $UserGroupID");
			$DB->query("
				UPDATE dupe_groups
				SET Comments = CONCAT('".db_string($Comments)."\n\n',Comments)
				WHERE ID = $TargetGroupID");
			$DB->query("DELETE FROM dupe_groups WHERE ID = $UserGroupID");
			$GroupID = $UserGroupID;
		} else {
			$DB->query("INSERT INTO users_dupes (UserID, GroupID) VALUES ($UserID, $TargetGroupID)");
			$GroupID = $TargetGroupID;
		}
	} elseif ($UserGroupID) {
		$DB->query("INSERT INTO users_dupes (UserID, GroupID) VALUES ($TargetID, $UserGroupID)");
		$GroupID = $UserGroupID;
	} else {
		$DB->query("INSERT INTO dupe_groups () VALUES ()");
		$GroupID = $DB->inserted_id();
		$DB->query("INSERT INTO users_dupes (UserID, GroupID) VALUES ($TargetID, $GroupID)");
		$DB->query("INSERT INTO users_dupes (UserID, GroupID) VALUES ($UserID, $GroupID)");
	}

	if (!$IgnoreComments) {
		$AdminComment = sqltime()." - Linked accounts updated: [user]".$UserInfo['Username']."[/user] and [user]".$TargetInfo['Username']."[/user] linked by ".$LoggedUser['Username'];
		$DB->query("
			UPDATE users_info AS i
				JOIN users_dupes AS d ON d.UserID = i.UserID
			SET i.AdminComment = CONCAT('".db_string($AdminComment)."\n\n', i.AdminComment)
			WHERE d.GroupID = $GroupID");
	}
}

function unlink_user($UserID) {
	global $DB, $LoggedUser;

	authorize();
	if (!check_perms('users_mod')) {
		error(403);
	}

	if (!is_number($UserID)) {
		error(403);
	}
	$UserInfo = Users::user_info($UserID);
	if ($UserInfo === false) {
		return;
	}
	$AdminComment = sqltime()." - Linked accounts updated: [user]".$UserInfo['Username']."[/user] unlinked by ".$LoggedUser['Username'];
	$DB->query("
		UPDATE users_info AS i
			JOIN users_dupes AS d1 ON d1.UserID = i.UserID
			JOIN users_dupes AS d2 ON d2.GroupID = d1.GroupID
		SET i.AdminComment = CONCAT('".db_string($AdminComment)."\n\n', i.AdminComment)
		WHERE d2.UserID = $UserID");
	$DB->query("DELETE FROM users_dupes WHERE UserID = '$UserID'");
	$DB->query("
		DELETE g.*
		FROM dupe_groups AS g
			LEFT JOIN users_dupes AS u ON u.GroupID = g.ID
		WHERE u.GroupID IS NULL");
}

function delete_dupegroup($GroupID) {
	global $DB;

	authorize();
	if (!check_perms('users_mod')) {
		error(403);
	}

	if (!is_number($GroupID)) {
		error(403);
	}

	$DB->query("DELETE FROM dupe_groups WHERE ID = '$GroupID'");
}

function dupe_comments($GroupID, $Comments, $IgnoreComments) {
	global $DB, $LoggedUser;

	authorize();
	if (!check_perms('users_mod')) {
		error(403);
	}

	if (!is_number($GroupID)) {
		error(403);
	}

	$DB->query("
		SELECT SHA1(Comments) AS CommentHash
		FROM dupe_groups
		WHERE ID = $GroupID");
	list($OldCommentHash) = $DB->next_record();
	if ($OldCommentHash != sha1($Comments)) {
		if (!$IgnoreComments) {
			$AdminComment = sqltime()." - Linked accounts updated: Comments updated by ".$LoggedUser['Username'];
		}
		if ($_POST['form_comment_hash'] == $OldCommentHash) {
			$DB->query("
				UPDATE dupe_groups
				SET Comments = '".db_string($Comments)."'
				WHERE ID = '$GroupID'");
		} else {
			$DB->query("
				UPDATE dupe_groups
				SET Comments = CONCAT('".db_string($Comments)."\n\n',Comments)
				WHERE ID = '$GroupID'");
		}

		if (!$IgnoreComments) {
			$DB->query("
				UPDATE users_info AS i
					JOIN users_dupes AS d ON d.UserID = i.UserID
				SET i.AdminComment = CONCAT('".db_string($AdminComment)."\n\n', i.AdminComment)
				WHERE d.GroupID = $GroupID");
		}
	}
}

function user_dupes_table($UserID) {
	global $DB, $LoggedUser;

	if (!check_perms('users_mod')) {
		error(403);
	}
	if (!is_number($UserID)) {
		error(403);
	}
	$DB->query("
		SELECT d.ID, d.Comments, SHA1(d.Comments) AS CommentHash
		FROM dupe_groups AS d
			JOIN users_dupes AS u ON u.GroupID = d.ID
		WHERE u.UserID = $UserID");
	if (list($GroupID, $Comments, $CommentHash) = $DB->next_record()) {
		$DB->query("
			SELECT m.ID
			FROM users_main AS m
				JOIN users_dupes AS d ON m.ID = d.UserID
			WHERE d.GroupID = $GroupID
			ORDER BY m.ID ASC");
		$DupeCount = $DB->record_count();
		$Dupes = $DB->to_array();
	} else {
		$DupeCount = 0;
		$Dupes = array();
	}
?>
		<form class="manage_form" name="user" method="post" id="linkedform" action="">
			<input type="hidden" name="action" value="dupes" />
			<input type="hidden" name="dupeaction" value="update" />
			<input type="hidden" name="userid" value="<?=$UserID?>" />
			<input type="hidden" id="auth" name="auth" value="<?=$LoggedUser['AuthKey']?>" />
			<input type="hidden" id="form_comment_hash" name="form_comment_hash" value="<?=$CommentHash?>" />
			<div class="box box2" id="l_a_box">
				<div class="head">
					Linked Accounts (<?=max($DupeCount - 1, 0)?>) <a href="#" onclick="$('.linkedaccounts').gtoggle(); return false;" class="brackets">View</a>
				</div>
				<table width="100%" class="layout hidden linkedaccounts">
					<?=($DupeCount ? "<tr>\n" : '')?>
<?
	$i = 0;
	foreach ($Dupes as $Dupe) {
		$i++;
		list($DupeID) = $Dupe;
		$DupeInfo = Users::user_info($DupeID);
?>
						<td align="left"><?=Users::format_username($DupeID, true, true, true, true)?>
							<a href="user.php?action=dupes&amp;dupeaction=remove&amp;auth=<?=$LoggedUser['AuthKey']?>&amp;userid=<?=$UserID?>&amp;removeid=<?=$DupeID?>" onclick="return confirm('Are you sure you wish to remove <?=$DupeInfo['Username']?> from this group?');" class="brackets tooltip" title="Remove linked account">X</a>
						</td>
<?
		if ($i == 4) {
			$i = 0;
			echo "\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n";
		}
	}
	if ($DupeCount) {
		if ($i !== 0) {
			for ($j = $i; $j < 4; $j++) {
				echo "\t\t\t\t\t\t<td>&nbsp;</td>\n";
			}
		}
?>
					</tr>
<?	}	?>
					<tr>
						<td colspan="5" align="left" style="border-top: thin solid;"><strong>Comments:</strong></td>
					</tr>
					<tr>
						<td colspan="5" align="left">
							<div id="dupecomments" class="<?=($DupeCount ? '' : 'hidden')?>"><?=Text::full_format($Comments);?></div>
							<div id="editdupecomments" class="<?=($DupeCount ? 'hidden' : '')?>">
								<textarea name="dupecomments" onkeyup="resize('dupecommentsbox');" id="dupecommentsbox" cols="65" rows="5" style="width: 98%;"><?=display_str($Comments)?></textarea>
							</div>
							<span style="float: right;"><a href="#" onclick="$('#dupecomments').gtoggle(); $('#editdupecomments').gtoggle(); resize('dupecommentsbox'); return false;" class="brackets">Edit linked account comments</a></span>
						</td>
					</tr>
				</table>
				<div class="pad hidden linkedaccounts">
					<label for="target">Link this user with: </label>
					<input type="text" name="target" id="target" /><br />
					<label for="ignore_comments">Do not update staff notes</label>
					<input type="checkbox" name="ignore_comments" id="ignore_comments" /><br />
					<input type="submit" value="Update" id="submitlink" />
				</div>
			</div>
		</form>
<?
}
?>