Passed
Push — master ( 69d941...0f6eba )
by Maxwell
02:19
created

getGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<script type='text/javascript' src='//www.midijs.net/lib/midi.js'></script>
2
<?php
3
require(__DIR__ . "/../vendor/autoload.php");
4
5
define("DEBUG_MODE", true);
6
session_start();
7
if(defined("DEBUG_MODE") && DEBUG_MODE) {
8
    ini_set('display_errors', 1);
9
    ini_set('display_startup_errors', 1);
10
    error_reporting(E_ALL);
11
}
12
13
function validateCSS($validate) {
14
	$DISALLOWED = array("<?php", "?>", "behavior: url", ".php", "@import", "@\import", "@/import"); 
15
16
	$validated = str_replace($DISALLOWED, "", $validate);
17
    return $validated;
18
}
19
function validateMarkdown($comment) {
20
	$markdown = new Michelf\Markdown;
21
	$markdown->no_markup = true;
22
	$transformed = $markdown->transform($comment);
23
	return preg_replace(
24
		"/<a href=(?:'|\")javascript:(.*?)(?:'|\")>(.*?)<\/a>/i",
25
		"Attempted XSS: $2 ($1)",
26
		$transformed
27
	);
28
}
29
30
function validateCaptcha($privatekey, $response) {
31
	$responseData = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$privatekey.'&response='.$response));
32
	return $responseData->success;
33
}
34
35
function requireLogin() {
36
	if (!isset($_SESSION['user'])) {
37
		header("Location: /login.php?r_login"); die();
38
	}
39
}
40
41
function getGroup($id, $conn) {
42
	$stmt = $conn->prepare("SELECT * FROM `groups` WHERE `id` = ?");
43
	$stmt->bind_param("i", $id);
44
	$stmt->execute();
45
	$result = $stmt->get_result();
46
	if ($result->num_rows === 0) return 'error';
47
	return $result->fetch_assoc();
48
}
49
50
function getID($user, $connection) {
51
	$stmt = $connection->prepare("SELECT * FROM users WHERE username = ?");
52
	$stmt->bind_param("s", $user);
53
	$stmt->execute();
54
	$result = $stmt->get_result();
55
	if($result->num_rows === 0) return 'error';
56
	while($row = $result->fetch_assoc()) {
57
		$id = $row['id'];
58
	} 
59
	$stmt->close();
60
	return $id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.
Loading history...
61
}
62
63
function getName($id, $connection) {
64
	$stmt = $connection->prepare("SELECT * FROM users WHERE id = ?");
65
	$stmt->bind_param("s", $id);
66
	$stmt->execute();
67
	$result = $stmt->get_result();
68
	if($result->num_rows === 0) return('error');
69
	while($row = $result->fetch_assoc()) {
70
		$name = htmlspecialchars($row['username']);
71
	} 
72
	$stmt->close();
73
	return $name;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.
Loading history...
74
}
75
76
function getPFP($user, $connection) {
77
	$stmt = $connection->prepare("SELECT * FROM users WHERE username = ?");
78
	$stmt->bind_param("s", $user);
79
	$stmt->execute();
80
	$result = $stmt->get_result();
81
	if($result->num_rows === 0) return('error');
82
	while($row = $result->fetch_assoc()) {
83
		$pfp = htmlspecialchars($row['pfp']);
84
	} 
85
	$stmt->close();
86
	return $pfp;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pfp does not seem to be defined for all execution paths leading up to this point.
Loading history...
87
}
88
89
function checkIfFriended($friend1, $friend2, $connection)
90
{
91
	$stmt = $connection->prepare("SELECT * FROM `friends` WHERE reciever = ? AND sender = ? OR reciever = ? AND sender = ?");
92
	$stmt->bind_param("ssss", $friend1, $friend2, $friend2, $friend1);
93
	$stmt->execute();
94
	$result = $stmt->get_result();
95
	if($result->num_rows === 1){ return true; }
96
	return false;
97
}
98
99
function getUser($id, $connection) {
100
	$userResult = array();
101
	$stmt = $connection->prepare("SELECT * FROM users WHERE id = ?");
102
	$stmt->bind_param("i", $id);
103
	$stmt->execute();
104
	$result = $stmt->get_result();
105
	if($result->num_rows === 0) echo('That user does not exist.');
106
	while($row = $result->fetch_assoc()) {
107
		$userResult['username'] = $row['username'];
108
		$userResult['id'] = $row['id'];
109
		$userResult['date'] = $row['date'];
110
		$userResult['bio'] = $row['bio'];
111
		$userResult['css'] = $row['css'];
112
		$userResult['pfp'] = $row['pfp'];
113
		$userResult['badges'] = explode(';', $row['badges']);
114
		$userResult['music'] = $row['music'];
115
		$userResult['rank'] = $row['rank'];
116
		$userResult['currentgroup'] = $row['currentgroup'];
117
	}
118
	$stmt->close();
119
120
	$stmt = $connection->prepare("SELECT * FROM gamecomments WHERE author = ?");
121
	$stmt->bind_param("s", $userResult['username']);
122
	$stmt->execute();
123
	$result = $stmt->get_result();
124
125
	$userResult['comments'] = 0;
126
	while($row = $result->fetch_assoc()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $row is dead and can be removed.
Loading history...
127
		$userResult['comments']++;
128
	}
129
	$stmt->close();
130
131
	$stmt = $connection->prepare("SELECT * FROM comments WHERE author = ?");
132
	$stmt->bind_param("s", $userResult['username']);
133
	$stmt->execute();
134
	$result = $stmt->get_result();
135
136
	$userResult['profilecomments'] = 0;
137
	while($row = $result->fetch_assoc()) {
138
		$userResult['profilecomments']++;
139
	}
140
	$stmt->close();
141
142
	$stmt = $connection->prepare("SELECT * FROM files WHERE author = ? AND status='y'");
143
	$stmt->bind_param("s", $userResult['username']);
144
	$stmt->execute();
145
	$result = $stmt->get_result();
146
147
	$userResult['filesuploaded'] = 0;
148
	while($row = $result->fetch_assoc()) {
149
		$userResult['filesuploaded']++;
150
	}
151
	$stmt->close();
152
	return $userResult;
153
}
154
?>