-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaveComment.php
132 lines (109 loc) · 4.21 KB
/
leaveComment.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
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "vendor/autoload.php";
include 'db_connection.php';
session_start();
if(isset($_GET['itemID']) && isset($_GET['CommenterID']) && isset($_GET['CommentedID']) && !empty($_POST['rate']) && $_GET['CommenterID'] == $_SESSION["id"] && !empty($_POST["textArea"]))
{
$itemID = test_input($_GET["itemID"]);
echo "Item ID: " . $itemID;
$commenterID = test_input($_GET['CommenterID']);
echo "CommenterID" . $commenterID;
$commentedID = test_input($_GET['CommentedID']);
echo "CommentedID" . $commentedID;
$rating = test_input($_POST['rate']);
$comment = test_input($_POST["textArea"]);
$matchedSQL = "SELECT * FROM Matched WHERE MislayerID='$commenterID' AND FinderID='$commentedID' AND ItemID='$itemID' AND Status='1'";
$matchedResult = $conn->query($matchedSQL);
if($matchedResult->num_rows > 0)
{
$commentInsertQuery = "INSERT INTO Ratings (CommenterID, CommentedID, Rating, Comment) VALUES ('{$commenterID}', '{$commentedID}', '$rating', '{$comment}')";
if($conn->query($commentInsertQuery) === TRUE)
{
$ratingSQL = "SELECT Rating FROM Ratings WHERE CommentedID='$commentedID'";
$ratingResult = $conn->query($ratingSQL);
$ratingSoFar = 0;
$count = 0;
while ($ratingRow = $ratingResult->fetch_assoc())
{
$ratingSoFar += (int)$ratingRow['Rating'];
$count++;
}
$userRating = (float)$ratingSoFar / $count;
$userRating = round($userRating, 1);
$insertRating = "UPDATE Users SET Rating='$userRating' WHERE ID='$commentedID'";
if($conn->query($insertRating) === TRUE)
{
$commentedSQL = "SELECT Name, Email FROM Users WHERE ID='$commentedID'";
$commentedResult = $conn->query($commentedSQL);
$commentedRow = $commentedResult->fetch_assoc();
$commenterSQL = "SELECT Name, Email FROM Users WHERE ID='$commenterID'";
$commenterResult = $conn->query($commenterSQL);
$commenterRow = $commenterResult->fetch_assoc();
$msg = "You received a new comment from " . $commenterRow["Name"] . " saying \'" . $comment . " \'.";
$mail = new PHPMailer(TRUE);
try {
$mail->setFrom('[email protected]', 'Lost & Found');
$mail->addAddress($commentedRow['Email'], $commentedRow['Name']);
$mail->Subject = 'New comment on your Lost & Found Account';
$mail->Body = $msg;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = '[email protected]';
$mail->Password = 'lost&found';
$mail->Port = 587;
/* Disable some SSL checks. */
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
/* Enable SMTP debug output. */
//$mail->SMTPDebug = 4;
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
header("Location: itemRecieved.php?itemID=" . $itemID);
}
else
{
echo "Insert rating error";
}
}
else
{
echo "Insert ERROR. You will be redirected to the account page in 5 seconds";
sleep(5);
//header("Location: account.php");
}
}
else
{
echo "No matching Result. You will be redirected to the account page in 5 seconds";
sleep(5);
//header("Location: account.php");
}
}
else
echo "Get variables Error. You will be redirected to the account page in 5 seconds";
sleep(5);
//header("Location: account.php");
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>