mybookbag /mybookbag/functions.php

Language PHP Lines 336
MD5 Hash 4a01ed45074e796c8c2fbe74901bb482 Estimated Cost $7,338 (why?)
Repository https://bitbucket.org/s2223902/mybookbag.git View Raw File View Project SPDX
  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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
include('db_connection.php');
function checkMessages(){ //Checks if there are any new messages or not, and returns the amount.

$checkmsg = mysql_query('select m1.id, m1.title, m1.timestamp, count(m2.id) as reps, users.id as userid, users.username from pm as m1, pm as m2,users where ((m1.user1="'.$_SESSION['id'].'" and m1.user1read="no" and users.id=m1.user2) or (m1.user2="'.$_SESSION['id'].'" and m1.user2read="no" and users.id=m1.user1)) and m1.id2="1" and m2.id=m1.id group by m1.id order by m1.id desc') or die ("fetch1:".mysql_error());
$amountmsg = mysql_num_rows($checkmsg);
return $amountmsg;
}

function books_Set($type){ //Checks how many books have been set with the given type for the user
$query = "select `user_book`.`entryid` from `book` natural join `user_book` where id={$_SESSION["id"]} and `book`.`type`='{$type}'";
$handle = mysql_query($query) or die(mysql_error());
$amount = intval(mysql_num_rows($handle));
return $amount;
}

function loadBook($type){ //Loads the books of the given type for the user.
$id=$_SESSION["id"];
if(isset($_SESSION["remoteid"])){ //If it tries to load books from a different user.
$addpermission = "and permission=1";
$id = $_SESSION["remoteid"];
}
else{
$addpermission = "";
}

$orderBy = array('title','author','isbn');
if (isset($_GET['order']) && in_array($_GET['order'], $orderBy)) {
    $order = $_GET['order'];
}
else{
	$order = 'title asc';
}
if($order==$_SESSION["Order"] && $_GET["order"]){
	$order = "$order desc";
	}
$_SESSION["Order"] = $order;

		$query = "select `title`,`author`,`isbn` from `book` natural join `author_book`,`user_book` where `author_book`.`entryid`=`user_book`.`entryid` and `user_book`.`entryid`=`book`.`entryid` and `user_book`.`id`=$id and type='{$type}' $addpermission order by $order";
		//echo $query;

	$handle = mysql_query($query) or die(mysql_error());
	$nr = 0;
	while($row = mysql_fetch_array($handle)){
		$userbooks[$nr]["title"] = $row["title"];
		$userbooks[$nr]["author"] = $row["author"];
		$userbooks[$nr]["isbn"] = $row["isbn"];
		$nr++;
		}
		$_SESSION["userbooks"] = $userbooks;
		return $userbooks;
}

function amount_Type(){	//Checks if there are any books at all with the given type, and returns False, or the corresponding number.
$type = $_SESSION["currtype"];
$query = "select `user_book`.`entryid` from `book` natural join `user_book` where id={$_SESSION["id"]} and `book`.`type`='{$type}'";
$handle = mysql_query($query) or die(mysql_error());
$amount = intval(mysql_num_rows($handle));
if($amount==0){$amount=False;}
return $amount;
}


function showBooks($userbooks){ //Prints all the books for the user page
if(amount_Type()){
	$Table = "<table><tr><td><b><a href=\"?order=title\">Title</a></b></td><td><b><a href=\"?order=author\">Author</a></b></td><td><b><a href=\"?order=isbn\">ISBn</a></b></td></tr>";
	for($i=1;$i<count($userbooks);$i++){
	$title = $userbooks[$i]["title"];
	$author = $userbooks[$i]["author"];
	$isbn = $userbooks[$i]["isbn"];
	$Table = "$Table<tr><td>$title</td><td>$author</td><td>$isbn</td></tr>";
	}
	$Table ="$Table</table>";
	return $Table;
	}
	else{
	return "You have not added any books yet. Please view the MyBookBag tab and add some books so we can display them for you here.";
	}
	}
/////
function stripArray($arr){ //Strips the array and creates session variables of it
	$Keys = array_keys($arr);
	foreach($Keys as $Key){
		$_SESSION["Create"][$Key] = $arr[$Key];
		}
	if(!isset($arr["author"])){
		$_SESSION["Create"]["author"] = False;
		}
	if(!isset($arr["title"])){
		$_SESSION["Create"]["title"] = False;
		}
	//else{
		//$_SESSION["Create"]["title"] = $arr["title"];
		//}
	return;
	}

	
function isAccepted($form){ //Checks if the inserted form will be accepted. A title is the minimum.
if (isset($_GET['order']) && isset($_SESSION["results"])){
return True;
}
 $accepted = True;
 if(isset($_GET["sort"])){
 return $accepted;
 }
	
	if(empty($form["title"]) && empty($form["author"])){
		$accepted = False;
		
	}
	else if ($form["title"]){
	$list = array("of","and","in","in the","against","against the","for","to","of the","to the","and the","with","with the","by","by the","because of","vs.","for the");
	foreach($list as $word){
	if($form["title"]==$word){
	$accepted = False;
	}
	}	
		return $accepted;
}
}

function loadFriends(){ //Loads all user's friends
$query = "select `friend_id`,`p_id`,`username`,`email` from `users` natural join `friend_of` where `friend_of`.`friend_id`=`users`.`id` and `p_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2";
$handle = mysql_query($query);
$query2 = "select `p_id`,`friend_id`,`username`,`email` from `users` natural join `friend_of` where `friend_of`.`p_id`=`users`.`id` and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2"; //For reverse cases! difference between the 'adder and the added' 
$handle2 = mysql_query($query2) or die(mysql_error());
if(mysql_num_rows($handle)==0 && mysql_num_rows($handle2)==0 ){
return False;
}
$friendTable = "<table><form action=\"remoteuser.php\" method=\"post\" name=\"form1\"><tr><td>Username</td><td>E-Mail</td></tr>";
while($row=mysql_fetch_assoc($handle)){ //Collects all the results it can find.
$username = $row["username"];
$email = $row["email"];
$id = $row["friend_id"];
$friendTable = "$friendTable<tr><td><a href=\"remoteuser.php?id=$id\">$username</a></td><td>$email</td></tr>";
}

while($row=mysql_fetch_assoc($handle2)){
$username = $row["username"];
$email = $row["email"];
$id = $row["p_id"];
$friendTable = "$friendTable<tr><td><a href=\"remoteuser.php?id=$id\">$username</a></td></td><td>$email</td></tr>";
}
$friendTable = "$friendTable</table>";
return $friendTable;
}

function getOutPending(){ //Retrieves outgoing friendrequests.
$query = "SELECT  `friend_id` 
FROM  `friend_of` 
WHERE  `p_id` ={$_SESSION["id"]}
AND  `friend_of`.`accepted` =1
LIMIT 0 , 30";
$handle = mysql_query($query);
$pending = mysql_num_rows($handle);
if($pending>0){
echo $pending;
}
else{
echo 0;
}
}

function getInPending(){ //Retrieves incoming friendrequests.
$query = "SELECT  `p_id` 
FROM  `friend_of` 
WHERE  `friend_id` ={$_SESSION["id"]}
AND  `friend_of`.`accepted` =1
LIMIT 0 , 30";
$handle = mysql_query($query);
$pending = mysql_num_rows($handle);
if($pending>0){
echo $pending;
}
else{
echo 0;
}
}

function showFriends(){ //Prints all user's friends
if(!$_SESSION["friends"]){
echo "<p>You currently have no friends! View the MyFriends tab to add some people you know!</p>";
}
else{ 
echo "<p>These are your friends!</p>";
echo $_SESSION["friends"];
}
}

function requestForm(){ //Retrieves if any friendship requests need answering.
$query = "
select `username`,`users`.`id`
from `users` 
natural join 
`friend_of` 
where `friend_of`.`friend_id`={$_SESSION["id"]} 
and `p_id`=`users`.`id` 
and `friend_of`.`accepted`=1";
$handle = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($handle)==0){
return "You currently have no requests that require answering.";
}

$requestForm = "<table><tr><td>Username</td></tr>";
while($row = mysql_fetch_array($handle)){
$request = $row["username"];
$id = $row["id"];
$requestForm = "<form action=\"confirmrequest.php\" method=\"post\">$requestForm<tr><td>$request</td><td><input type=\"submit\" name=\"Answer\" value=\"Accept\" ></input></td><td><input type=\"submit\" name=\"Answer\" value=\"Reject\" ></input></td><input type=\"hidden\" name=\"targetid\" value=\"{$id}\"></input></tr></form>";
}
return $requestForm;
}

function showRequests(){ //Prints all requests
return $_SESSION["requests"];
}

function sendAnswer(){ //Sends the answer (Accept/Reject) and creates additional rows corresponding to that.
if($_POST["Answer"]=="Accept"){
$query = "update `friend_of` set `accepted`=2 where `p_id`={$_POST["targetid"]} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=1";
$query2 = "delete from `friend_of` where `p_id`={$_POST["targetid"]} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=0";
mysql_query($query) or die(mysql_error());
mysql_query($query2) or die(mysql_error());
$_SESSION["notify"] = "You have accepted the friendrequest! Your friend has now newly appeared in your friendslist! Check it out!";
}
else{
$query = "update `friend_of` set `accepted`=0 where `p_id`={$_POST["targetid"]} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=1";
mysql_query($query) or die(mysql_error());
$_SESSION["notify"] = "You rejected the friendrequest! Boo-hoo you complex life-form with emotional issues!";
}
}

function addFriendForm(){ //Requests friendship form
$form = "<form action=\"processrequest.php\" method=\"post\">
<table><tr><td><input type=\"text\" name=\"username\" value=\"username\"></input></td><td><input type=\"text\" name=\"email\" value=\"E-mail\"></input></td><td><input type=\"submit\" name=\"submit\" value=\"Send Request!\"></input></td></tr></table>";
return $form;
}

function determineTarget($username,$email){ //Gets the userid from inserted username and/or e-mail
if(empty($username)){
$query = "select `id` from `users` where `email`='{$email}'";
}
if(empty($email)){
$query = "select `id` from `users` where `username`='{$username}'";
}
if($username&&$email){
$query = "select `id` from `users` where `email`='{$email}' or `username`='{$username}'";
}

$handle = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($handle)){
$id = $row["id"];
}
return $id;
}

function loadRemoteLibrary($called_id){ // Loads the books that can be seen by the current user FROM another user... So : Public books and friend books (if they are friends)
$totalRemoteLib = array();

if(friends($called_id)){
$totalRemoteLib[]= loadRemoteBook("b",1,$called_id);
$totalRemoteLib[]= loadRemoteBook("e",1,$called_id);
$totalRemoteLib[]= loadRemoteBook("j",1,$called_id);

}
$totalRemoteLib[]= loadRemoteBook("b",2,$called_id);
$totalRemoteLib[]= loadRemoteBook("e",2,$called_id);
$totalRemoteLib[]= loadRemoteBook("j",2,$called_id);
$RemoteTable = array();
foreach($totalRemoteLib as $piece){
$Table = "<table><tr><td><b>Title</b></td><td><b>Author</b></td><td><b>ISBn</b></td><td>Type</td></tr>";
for($i=1;$i<count($piece);$i++){
$title = $piece[$i]["title"];
$author = $piece[$i]["author"];
$isbn = $piece[$i]["isbn"];

$Table = "$Table<tr><td>$title</td><td>$author</td><td>$isbn</td></tr>";
}
$Table ="$Table</table>";
$RemoteTable[$i] = $Table;
}
return $RemoteTable;
}

function loadRemoteBook($type,$permission,$id){
		$query = "select `title`,`author`,`isbn` from `book` natural join `author_book`,`user_book` where `author_book`.`entryid`=`user_book`.`entryid` and `user_book`.`entryid`=`book`.`entryid` and `user_book`.`id`=$id and type='{$type}' and permission=$permission order by title" or die("?");

	$handle = mysql_query($query) or die(mysql_error());
	$nr = 0;
	while($row = mysql_fetch_array($handle)){
		$userbooks[$nr]["title"] = $row["title"];
		$userbooks[$nr]["author"] = $row["author"];
		$userbooks[$nr]["isbn"] = $row["isbn"];
		$nr++;
		}
		$_SESSION["userbooks"] = $userbooks;
		return $userbooks;
}

function friends($called_id){
$query = "select `p_id` from `users` natural join `friend_of` where `friend_of`.`friend_id`={$called_id} and `p_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2";
$handle = mysql_query($query);
$query2 = "select `friend_id` from `users` natural join `friend_of` where `friend_of`.`p_id`={$called_id} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2"; //For reverse cases! difference between the 'adder and the added' 
$handle2 = mysql_query($query2) or die(mysql_error());
if(mysql_num_rows($handle)==0 && mysql_num_rows($handle2)==0 ){
return False;
}
else{
return True;
}
}



function getRemoteName($id){
$query = "select username from `users` where `id`=$id";
$handle = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($handle)){
$name = $row["username"];
}
return $name;
}

function getid(){
	//Selects the highest ID in the database so new entries can be submitted accordingly
	$IDQ = "select max(entryid) from entry";
	$handle = mysql_query($IDQ) or die(mysql_error());
	while($row = mysql_fetch_array($handle)){
		$id = $row["max(entryid)"];
		}
		return $id;
	}



?>
Back to Top