/
permission.php
278 lines (270 loc) · 11.3 KB
/
permission.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
require 'includes/connection.php';
//session continue
session_start();
//session check
if(!isset($_SESSION["user_email"])){
echo "you're not logged in<br>Click <a href=\"index.php\">here</a> to log in.";
exit();
}
//checking the url for proper format
if(!empty($_GET)){
if(isset($_GET['id'],$_GET['bookTitle'],$_GET['askId'])){
//some variables
$askerId = $_SESSION['user_id'];
$toWhomId = urldecode(trim($_GET['id']));
$bookTitle = urldecode(trim($_GET['bookTitle']));
$askId = urldecode(trim($_GET['askId']));
//checking for spaces ie. valid urls
if(!empty($toWhomId) && !empty($bookTitle) && !empty($askId)){
//connection - query
$query = "SELECT bookTitle,nStatus FROM bookShelf WHERE bookTitle='$bookTitle' AND ownerId='$toWhomId'";
//checking if found or not; ie query successful or not
if($query_run = mysqli_query($con, $query)){
//fetching the data
$query_row = mysqli_fetch_assoc($query_run);
//checking for availibility of books
if($query_row['nStatus'][0]==1){
//checking the status matching for the book
if($askId=='100'){
//verifying the permission with the owners status of the book
//askforborrow
if($query_row['nStatus'][1]==$askId[0]){
?>
<html>
<head>
<title>Permissions</title>
</head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/>
<title>View Profile</title>
<!-- CSS -->
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
</head>
<body>
<h3>Ask <?php echo searchOwner($toWhomId)?> for borrowing</h3>
<form method="post" action="">
<button class="waves-effect waves-light btn-large" name="confirm">Yes</button>
<button class="waves-effect waves-light btn-large" name="cancel">No</button>
<h5>Leave a message</h5>
<strong>Subject : </strong>
<input type="text" name="message_subject"><br>
<strong>Message : </strong>
<input type="text" name="message_content"><br>
</form>
</body>
</html>
<?php
if(isset($_POST['confirm'])){
$query = "SELECT * FROM messages WHERE (m_tag='01' OR m_tag='11') AND message_sender_id='$askerId' AND message_subject='__ASKFORBORROWING__' AND message_recipient_id='$toWhomId' AND message_content='$bookTitle'";
$query_run = mysqli_query($con,$query);
$check_query = mysqli_num_rows($query_run);
if($check_query!=0){
echo 'You already have asked him/her';
echo '<br><a href="bookdetails.php?bookTitle='.urlencode($bookTitle).'">Go Back.</a>';
}else{
$query = "INSERT INTO messages
(m_tag,message_time,message_sender_id,message_recipient_id,message_subject,message_content)
VALUES ('01',NOW(),'$askerId','$toWhomId','__ASKFORBORROWING__','$bookTitle')";
$user_email=$_SESSION['user_email'];
$user_id=searchOwnerId($user_email);
$recipient_id = $_GET['id'];
$user_name = searchOwner($user_id);
$message_content = trim($_POST['message_content']);
$message_subject = trim($_POST['message_subject']);
//$message_query = "INSERT INTO messages
// (m_tag,message_time,message_sender_id,message_recipient_id,message_subject,message_content)
// VALUES ('00',NOW(),'$user_id','$recipient_id','$message_subject','$message_content')";
if(mysqli_query($con,$query)){// && mysqli_query($con, $message_query)){
echo "We will notify him/her shortly.<br>Please Wait while we redirect.";
echo '<script>
setTimeout(function(){
window.location = "bookdetails.php?bookTitle='.urlencode($bookTitle).'";
}, 2000);
</script>';
}else{
echo "Something went wrong";
}
if(!empty($message_content)){
if(empty($message_subject))echo 'Sending without subject<br>';
$query = "INSERT INTO messages
(m_tag,message_time,message_sender_id,message_recipient_id,message_subject,message_content)
VALUES ('00',NOW(),'$user_id','$recipient_id','$message_subject','$message_content')";
if($query_run = mysqli_query($con, $query)){
echo 'Message sent successfully.....';
$_POST['message_content']='';
$_POST['message_subject']='';
}else{
echo "Something went wrong!!";
}
}
}
}
if(isset($_POST['cancel'])){
echo '<script>
setTimeout(function(){
window.location = "bookdetails.php?bookTitle='.urlencode($bookTitle).'";
}, 2000);
</script>';
}
}
}else{
if($askId=='010'){
//askforrent
//verifying the permission with the owners status of the book
if($query_row['nStatus'][2]==$askId[1]){
?>
<html>
<head>
<title>Permissions</title>
</head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/>
<title>View Profile</title>
<!-- CSS -->
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
</head>
<body>
<h3>Request <?php echo searchOwner($toWhomId)?> for renting</h3>
<form method="post" action="">
<button class="waves-effect waves-light btn-large" name="confirm">Yes</button>
<button class="waves-effect waves-light btn-large" name="cancel">No</button>
</form>
</body>
</html>
<?php
if(isset($_POST['confirm'])){
$query = "SELECT * FROM messages WHERE (m_tag='01' OR m_tag='11') AND message_sender_id='$askerId' AND message_subject='__GETONRENT__' AND message_recipient_id='$toWhomId' AND message_content='$bookTitle'";
$query_run = mysqli_query($con,$query);
$check_query = mysqli_num_rows($query_run);
if($check_query!=0){
echo 'You already have requested him/her';
echo '<br><a href="bookdetails.php?bookTitle='.urlencode($bookTitle).'">Go Back.</a>';
}else{
$query = "INSERT INTO messages
(m_tag,message_time,message_sender_id,message_recipient_id,message_subject,message_content)
VALUES ('01',NOW(),'$askerId','$toWhomId','__GETONRENT__','$bookTitle')";
if(mysqli_query($con,$query)){
echo "We will notify him/her shortly.<br>Please Wait while we redirect.";
echo '<script>
setTimeout(function(){
window.location = "bookdetails.php?bookTitle='.urlencode($bookTitle).'";
}, 0000);
</script>';
}else{
echo "Something went wrong";
//window.open(\'bookdetails.php?bookTitle='.urlencode($bookTitle).'\',\'_self\')
}
}
}
if(isset($_POST['cancel'])){
echo '<script>
setTimeout(function(){
window.location = "bookdetails.php?bookTitle='.urlencode($bookTitle).'";
}, 0000);
</script>';
}
}
}else{
if($askId=='001'){
//askforbuying
//echo "WorksTillHere";
//verifying the permission with the owners status of the book
if($query_row['nStatus'][2]==$askId[2]){
?>
<html>
<head>
<title>Permissions</title>
</head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/>
<title>View Profile</title>
<!-- CSS -->
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
</head>
<body>
<h3>Request <?php echo searchOwner($toWhomId)?> to buy</h3>
<form method="post" action="">
<button class="waves-effect waves-light btn-large" name="confirm">Yes</button>
<button class="waves-effect waves-light btn-large" name="cancel">No</button>
</form>
</body>
</html>
<?php
if(isset($_POST['confirm'])){
$query = "SELECT * FROM messages WHERE (m_tag='01' OR m_tag='11') AND message_sender_id='$askerId' AND message_subject='__WANTTOBUY__' AND message_recipient_id='$toWhomId' AND message_content='$bookTitle'";
$query_run = mysqli_query($con,$query);
$check_query = mysqli_num_rows($query_run);
if($check_query!=0){
echo 'You already have requested him/her';
echo '<br><a href="bookdetails.php?bookTitle='.urlencode($bookTitle).'">Go Back.</a>';
}else{
$query = "INSERT INTO messages
(m_tag,message_time,message_sender_id,message_recipient_id,message_subject,message_content)
VALUES ('01',NOW(),'$askerId','$toWhomId','__WANTTOBUY__','$bookTitle')";
if(mysqli_query($con,$query)){
echo "We will notify him/her shortly.<br>Please Wait while we redirect.";
echo '<script>
setTimeout(function(){
window.location = "bookdetails.php?bookTitle='.urlencode($bookTitle).'";
}, 0000);
</script>';
}else{
echo "Something went wrong";
//window.open(\'bookdetails.php?bookTitle='.urlencode($bookTitle).'\',\'_self\')
}
}
}
if(isset($_POST['cancel'])){
echo '<script>
setTimeout(function(){
window.location = "bookdetails.php?bookTitle='.urlencode($bookTitle).'";
}, 0000);
</script>';
}
}
}else{
echo "Invalid URL";
}
}
}
}else{
echo "Invalid URL";
}
}else{
echo "Invalid URL";
}
}else{
echo "Invalid URL";
}
}else{
echo "Invalid URL";
}
}else{
echo "Invalid URL";
}
//to search the owner of if owner id is mentioned
function searchOwner($ownerId){
global $con;
$userQuery = "SELECT user_id,user_name FROM user_login_details";
if(!($userQuery_run = mysqli_query($con,$userQuery))) die("Could load book owners data");
while ($userQuery_row = mysqli_fetch_assoc($userQuery_run)){
$id = $userQuery_row['user_id'];
$username = $userQuery_row['user_name'];
if($id==$ownerId) return $username;
}
}
//function to return $ownerId
function searchOwnerId($user_email){
global $con;
$userQuery = "SELECT user_id FROM user_login_details WHERE user_email='$user_email'";
if(!($userQuery_run = mysqli_query($con,$userQuery))) die("Could load book owners data");
return mysqli_fetch_assoc($userQuery_run)['user_id'];
}
?>