-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.php
executable file
·264 lines (224 loc) · 8.43 KB
/
function.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
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
261
262
263
264
<?php
include "mysqlClass.inc.php";
//used by unblock.php
function unblock($accountid1, $accountid2) {
mysql_query("UPDATE contact SET block=0 WHERE accountid1='$accountid1' && accountid2='$accountid2'");
}
//used by add_block.php
function add_block($accountid1, $accountid2) {
$query = "select * from contact where accountid1='$accountid1' && accountid2='$accountid2'";
$result = mysql_query( $query )
or die ("add_block() failed. Could not query the database: <br />". mysql_error());
$count = mysql_num_rows($result);
if ($count == 0) { //add new item
mysql_query("INSERT INTO contact (contactid, accountid1, accountid2, block)
VALUES (NULL, '$accountid1','$accountid2', 1)");
}
else {
mysql_query("UPDATE contact SET block=1 WHERE accountid1='$accountid1' && accountid2='$accountid2'");
}
mysql_free_result($result);
}
// used by media/media_view.php: check if the user of current session is blocked to view media
function block_or_not($current_user, $uploaded_by)
{
$id1_result = mysql_query("select * from account where username='$current_user'");
$id1_row = mysql_fetch_row($id1_result);
$id1 = $id1_row[0];
$id2_result = mysql_query("select * from account where username='$uploaded_by'");
$id2_row = mysql_fetch_row($id2_result);
$id2 = $id2_row[0];
$query = "select * from contact where accountid1='$id2' and accountid2='$id1'";
$result = mysql_query($query)
or die ("block_or_not() failed. Could not query the database: <br />". mysql_error());
$count = mysql_num_rows($result);
if($count == 0) // the default is no block
{
return 0;
}
else
{
$row = mysql_fetch_row($result);
if ($row[4] == 0) return 0;
else if($row[4] == 1) return 1;
}
mysql_free_result($result);
}
//used by add_contact.php, unidirectional relationship: from accountid1 to accountid2
function add_contact($contact_type, $accountid1, $accountid2) {
$query = "select * from contact where accountid1='$accountid1' && accountid2='$accountid2'";
$result = mysql_query( $query )
or die ("add_contact() failed. Could not query the database: <br />". mysql_error());
$count = mysql_num_rows($result);
switch($contact_type) { //default is 0, which is no contact
case '': $type = 0; break;
case 'Family': $type = 1; break;
case 'Friend': $type = 2; break;
}
if ($count == 0 and $type != 0) { //add a new contact type
mysql_query("INSERT INTO contact (contactid, accountid1, accountid2, type)
VALUES (NULL, '$accountid1','$accountid2','$type')");
}
elseif ($count != 0) { //either already is a contact or blocked
mysql_query("UPDATE contact SET type=$type WHERE accountid1='$accountid1' && accountid2='$accountid2'");
}
mysql_free_result($result);
}
//used by remove_contact.php
function remove_contact($accountid1, $accountid2) {
$query = "select * from contact where accountid1='$accountid1' && accountid2='$accountid2'";
$result = mysql_query( $query )
or die ("remove_contact() failed. Could not query the database: <br />". mysql_error());
$count = mysql_num_rows($result);
mysql_query("UPDATE contact SET type=0 WHERE accountid1='$accountid1' && accountid2='$accountid2'");
mysql_free_result($result);
}
//used by profile.php
function get_user_profile($username) {
$query = "select * from account where username='$username'";
$result = mysql_query( $query )
or die ("get_user_profile() failed. Could not query the database: <br />". mysql_error());
$row = mysql_fetch_row($result);
return $row;
mysql_free_result($result);
}
function update_profile_check($username, $email) {
$query = "select * from account where email!='$email' && username='$username'";
$result = mysql_query( $query )
or die ("update_profile() failed. Could not query the database: <br />". mysql_error());
$count = mysql_num_rows($result);
if ($count != 0) {
echo '<script type="text/javascript">';
echo 'alert("'. $username.' already exists.Please create a new username.")';
echo '</script>';
return 1;
}
mysql_free_result($result);
}
function update_profile($username, $psw, $email, $sex) {
$query = "UPDATE account
SET username='$username',password='$psw',sex='$sex'
WHERE email='$email'";
mysql_query($query);
}
// used by register_check.php
function check_user_exist($username,$email){
$query1 = "select * from account where username='$username'";
$result1 = mysql_query($query1)
or die ("check_user_exist() failed. Could not query the database: <br />". mysql_error());
$count1 = mysql_num_rows($result1);
$query2 = "select * from account where email='$email'";
$result2 = mysql_query($query2)
or die ("check_user_exist() failed. Could not query the database: <br />". mysql_error());
$count2 = mysql_num_rows($result2);
if ($count1==0 and $count2!=0){
echo '<script type="text/javascript">';
echo 'alert("'. $email.' already exists.Please use a different email address.")';
echo '</script>';
return 1;
}
if ($count1!=0){
echo '<script type="text/javascript">';
echo 'alert("'. $username.' already exists.Please create a new username.")';
echo '</script>';
return 2;
}
if ($count1==0 and $count2==0){
return 0;
}
mysql_free_result($result1); // free memory and close connection
mysql_free_result($result2);
}
// used by register_check.php
function insert_usr($username, $psw, $email, $sex){
$query = "INSERT INTO `account` (`accountid`, `username`, `password`,`email`, `sex`)
VALUES (NULL, '$username','$psw','$email','$sex')";
$result = mysql_query($query);
if (!$result)
{
die ("insert_usr() failed. Could not query the database: <br />". mysql_error());
}
return 1;
mysql_free_result($result); // free memory and close connection
}
// used by login.php
function user_pass_check($username, $password)
{
$query = "select * from account where username='$username'";
$result = mysql_query($query);
if (!$result)
{
die ("user_pass_check() failed. Could not query the database: <br />". mysql_error());
}
else{
$row = mysql_fetch_row($result);
if ($row[1]==$username and $row[2]==$password) return 0;
elseif ($row[1]!=$username) return 1; // unmatched username;
elseif ($row[2]!=$password) return 2; // wrong password;
}
mysql_free_result($result); // free memory
}
// used by ../media/media_view.php
function updateMediaTime($mediaid)
{
$query = "update media set lastaccesstime=NOW() WHERE mediaid = '$mediaid'";
// Run the query created above on the database through the connection
$result = mysql_query($query);
if (!$result)
{
die ("updateMediaTime() failed. Could not query the database: <br />". mysql_error());
}
}
// used by ../media/media_view.php
function updateViewCount($mediaid, $viewcount)
{
$new_view_count = $viewcount + 1;
$query = "update media set viewcount='$new_view_count' where mediaid = '$mediaid'";
// run the query to increment the view count of a specific media by 1
$result = mysql_query($query) or die("Cannot query the database and update the view count" .mysql_error());
}
// check if two users are friends, used by ../media/media_view.php
function contact_or_not($current_user, $uploaded_by)
{
$id1_result = mysql_query("select * from account where username='$current_user'");
$id1_row = mysql_fetch_row($id1_result);
$id1 = $id1_row[0];
$id2_result = mysql_query("select * from account where username='$uploaded_by'");
$id2_row = mysql_fetch_row($id2_result);
$id2 = $id2_row[0];
$query = "select * from contact where accountid1='$id2' and accountid2='$id1'";
$result = mysql_query($query)
or die ("block_or_not() failed. Could not query the database: <br />". mysql_error());
$count = mysql_num_rows($result);
if($count == 0) // the default is not contact
{
return 0;
}
else
{
$row = mysql_fetch_row($result);
if ($row[3] == 1 or $row[3] ==2) return 1; // return 1 if two users are either "family" or "friend"
else return 0;
}
}
function upload_error($result)
{
//view erorr description in http://us2.php.net/manual/en/features.file-upload.errors.php
switch ($result){
case 1:
return "UPLOAD_ERR_INI_SIZE";
case 2:
return "UPLOAD_ERR_FORM_SIZE";
case 3:
return "UPLOAD_ERR_PARTIAL";
case 4:
return "UPLOAD_ERR_NO_FILE";
case 5:
return "File has already been uploaded";
case 6:
return "Failed to move file from temporary directory";
case 7:
return "Upload file failed";
}
}
?>