/
class_lib.php
executable file
·219 lines (187 loc) · 7.23 KB
/
class_lib.php
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
<?php
// NONE OF THESE FUNCTIONS HAVE ERROR CHECKING (WHERE APPLICABLE)
$MAX_LINE_WIDTH = 80;
$MAX_MESSAGE_LENGTH = 400;
$MAX_FACT_LENGTH = 250;
$debug = FALSE;
if ($debug) { error_reporting(E_ALL); }
//$db_server = new mysqli($db_hostname, $db_username, $db_password, $db_database);
function connect() {
$db_server = new mysqli('z', 'bc23784', 'ygYXyRgsOB', 'cs105_bc23784');
if ($db_server->connect_errno) {
die ("<h1> There was an error:</h1> <p> " . $db_server->connect_error . "</p>");
//return false;
}
return $db_server;
}
function close($db_server) {
$db_server->close();
if ($db_server->connect_errno) {
dieErrMessage();
}
}
function checkQueryResults($result) {
if (!$result) {
die ("<h1> There was an error:</h1> <p> " . $db_server->connect_error . "</p>");
}
return true;
}
function userExists($username, $db_server) {
// search database to see if user already exists
$userExists = false;
$query = "SELECT * FROM users WHERE username=" . "'" . $username . "'";
$result = $db_server->query($query);
if (!$result) {
// error executing the query
printErrMessage();
}
else {
// check
if ($result->num_rows > 0) {
return true;
}
return false;
}
}
// requires an entry for the user to already be in the profiles table
// no precondition check!
function updateProfFacts($fact1, $fact2, $fact3, $username, $db_server) {
$query = "SELECT * FROM profiles WHERE username='$username'";
$result = $db_server->query($query);
checkQueryResults($result);
$row = $result->fetch_row();
if (strlen($fact1) > 0) {
$fact1 = $db_server->real_escape_string($fact1);
$query = "UPDATE profiles SET fact1='$fact1' WHERE username='$username';";
$result = $db_server->query($query);
checkQueryResults($result);
echo "<p>Fact 1 successfully updated.</p>";
}
if (strlen($fact2) > 0) {
$fact2 = $db_server->real_escape_string($fact2);
$query = "UPDATE profiles SET fact2='$fact2' WHERE username='$username';";
$result = $db_server->query($query);
checkQueryResults($result);
echo "<p>Fact 2 successfully updated.</p>";
}
if (strlen($fact1) > 0) {
$fact3 = $db_server->real_escape_string($fact3);
$query = "UPDATE profiles SET fact3='$fact3' WHERE username='$username';";
$result = $db_server->query($query);
checkQueryResults($result);
echo "<p>Fact 3 successfully updated.</p>";
}
}
function search() {
?>
<form id="Search" action="search.php" enctype="multipart/form-data" method="POST" style = "display: inline;">
<input type="submit" value="Search">
<input type="text" name="search" placeholder="USE ME (but be gentle)">
</form>
<?php
}
function printToolbar($username, $isProfPage) {
if (!isEmptyString($username) && !$isProfPage) {
//echo "<div style = \"text-align:left; float:left;\">
// <a href=profile.php?user=$username> Home</a> <a href=index.php> Index</a> <a href=newalbum1.php>
// Create Album</a> <a href=testupload1.php> Upload Image</a> <a href=logout.php>Sign Out ($username)</a>
//
// </br></div>";
echo " <a href=profile.php?user=$username>Home</a> <a href=index.php> Index</a> <a href=newalbum1.php>
Create Album</a> <a href=testupload1.php> Upload Image</a> <a href=logout.php>Sign Out ($username)</a>
" . search() . "
</br>";
return true;
}
else if (!isEmptyString($username) && $isProfPage) {
echo "
<a href=profile.php?user=$username>Home</a> <a href=index.php> Index</a> <a href=newalbum1.php>
Create Album</a> <a href=testupload1.php> Upload Image</a> <a href=update.php?user=$username>Update Profile</a>
<a href=logout.php>Sign Out ($username)</a>
" . search() . "
</br>";
return true;
}
else if (isEmptyString($username) && $isProfPage) {
echo "
<a href=index.php>Home</a> <a href=signup1.php> Register</a> <a href=newalbum1.php>
Create Album</a> <a href=testupload1.php> Upload Image</a> <a href=update.php?user=$username>Update Profile</a>
<a href=login.php>Sign In</a>
" . search() . "
</br>";
return false;
}
else {
echo "
<a href=index.php>Home</a> <a href=signup1.php> Register</a> <a href=newalbum1.php>
Create Album</a> <a href=testupload1.php> Upload Image</a> <a href=login.php>Sign In</a>
" . search() . "
</br>";
return false;
}
}
function isEmptyString($string) {
return (trim($string) === "" or $string === null);
}
/*
function updateProfPic($filename, $filetype, $filetmpname, $username, $db_server) {
//if ($_FILES) {
$name = $filename;
switch($filetype) {
case 'image/jpeg': $ext = 'jpg'; break;
case 'image/gif'; $ext = 'gif'; break;
case 'image/png'; $ext = 'png'; break;
case 'image/tiff'; $ext = 'tif'; break;
default: $ext = ''; break;
}
if ($ext) {
$picName = "profile" . "." . $ext;
$albumRoot = $username;
// check albums table to see if user has any albums (and thus a root directory)
$userExists = false;
//$query = "SELECT * FROM albums WHERE username=" . "'" . $username . "'";
$query = "SELECT * FROM albums WHERE username='$username'";
$result = $db_server->query($query);
checkQueryResults($result);
if ($result->num_rows > 0) {
$userExists = true;
}
if (!$userExists) {
mkdir($albumRoot, 0777, true);
}
$tempFile = $filetmpname;
$path = $albumRoot . "/" . $picName;
if (move_uploaded_file($tempFile, $path)){
//echo "<p>Image successfully uploaded.</p>";
chmod($path, 0777);
$updatedProfPicPath = $path;
// update profiles table
$query = "SELECT * FROM profiles WHERE username='$username'";
$result = $db_server->query($query);
checkQueryResults($result);
else {
// technically should check to see if user has an entry, but due to design logic this isn't necessary
// b/c user must sign up before being able to upload profile pic
$row = $result->fetch_row();
$profPicPath = $albumRoot . "/" . $picName;
$query = "UPDATE profiles SET profPicPath='$profPicPath' WHERE username='$username'";
$result = $db_server->query($query);
checkQueryResults($query);
echo "<p>Profile picture record successfully updated.</p>";
}
}
else {
echo "There was an error uploading the image.</br>";
}
}
else {
echo "<p>File extension not supported or no image was specified.</p>";
}
//}
//else {
// nothing uploaded
// echo "File extension not supported or no image was specified.</br>";
//}
}
*/
?>