-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpost.php
144 lines (124 loc) · 4.77 KB
/
post.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
<?php
// TODO: Test that it handles errors.
//start session
session_start();
include_once("config.php");
include_once("include.php");
// if user not logged in, redirect to homepage
checkUserSession();
$uid = $_SESSION['login_user'];
if (isset($_POST["submit"])) {
// Open database connection
$conn = mysqli_connect(DB_HOST_NAME, DB_USER, DB_PASS, DB_NAME);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$uniqueId = uniqid();
$uploaded = false;
$tempFile = USER_IMAGE_UPLOAD_DIRECTORY . basename($_FILES["postPic"]["name"]);
$ext = strtolower(pathinfo($tempFile, PATHINFO_EXTENSION));
$target_file = USER_IMAGE_UPLOAD_DIRECTORY . $uniqueId . "." . $ext;
$filename = $uniqueId . "." . $ext;
//error check image upload
if ($tempFile == USER_IMAGE_UPLOAD_DIRECTORY)// if user did not upload file
{
// do nothing
} else if (!preg_match("/(\.jpg|gif|png|jpeg)/",$target_file)) { // if file extension is bad
$err = "Image must be png, gif, jpg or jpeg format."; // output error message
} else {
if (move_uploaded_file($_FILES["postPic"]["tmp_name"], $target_file)) {
//echo basename( $_FILES["postPic"]["name"]). " has been uploaded.";
$uploaded = true;
} else {
$err = "Error uploading image to database.";
}
}
$content = htmlspecialchars(addslashes(trim($_POST['post1'])));
$date = date('Y/m/d H:i:s', time());
// if user uploaded image and link
if ($uploaded) {
//user uploaded image
$sql = "INSERT INTO Posts (creatorId, text, uploadedFile, timestamp)
VALUES ('$uid','$content','$filename','$date');";
} else { // user has not uploaded image
$sql = "INSERT INTO Posts (creatorId, text, timestamp)
VALUES ('$uid','$content','$date');";
}
//upload post to database
if (!mysqli_query($conn, $sql)) {
$err = mysql_error($conn);
}
// close database connection
mysqli_close($conn);
header('Location: ' . SIDEBAR_VIEW_POSTS);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css"></link>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <!-- This is the link for bootstrap !-->
<script type = "text/javascript" src = "scripts/java1.js" ></script>
<title>Wall Post</title>
</head>
<body class="allPages">
<div class="container-fluid"> <!-- This is the container div for the page; it is flued so it spands the viewport !-->
<div class="row"> <!-- Header row !-->
<div class="col-xs-12">
<div class="header">
<h1>
<a href="<?=SIDEBAR_VIEW_POSTS?>" class="homeLink">
<img src="logo.png" class="placeHolder" alt="img"></img> <?php echo WEBSITE_NAME; ?>
</a>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="row row-eq-height contentRow"> <!-- Content row!-->
<div class="col-xs-2 sideBarCol"> <!--Sidebar column !-->
<div class="sideBar">
<br/>
<a class="buttons" href="<?php echo SIDEBAR_VIEW_POSTS; ?>">View Wall</a>
<p class="blankButton">Create Post</p>
<a class="buttons" href="<?php echo SIDEBAR_VIEW_NOTES; ?>">View Notes</a>
<a class="buttons" href="<?php echo SIDEBAR_CREATE_NOTES; ?>">Create Notes</a>
<?php if (isAdmin($uid)) { ?><a class="buttons" href="<?php echo SIDEBAR_ADMIN; ?>">Admin</a><?php } ?>
<a class="buttons" href="<?php echo SIDEBAR_LOGOUT; ?>">Logout</a>
</div>
</div>
<div class="col-xs-10 col-md-6 col-md-offset-2"> <!-- Content column !-->
<div class="signupSection">
<?php if (isset($err)) echo "<p>An error has occurred.<br/>'$err'</p>" ?>
<form action="post.php" method="POST" enctype="multipart/form-data" id="postForm">
<fieldset class="largeColorsec">
<legend>New Anonymous Post</legend>
Post: <br/>
<textarea class="textBox" name="post1" rows="7" cols="100"></textarea>
<br/>
<span class="errorMsg" id="errorComments"></span>
Picture (optional): <input type="file" name="postPic"></input>
<span class="errorMsg" id="errorUrl"></span>
<br/>
<p>
<input type="submit" class="contentButtons" name="submit" value="Submit"/>
<input type="reset" class="contentButtons" value="Reset"/>
</p>
</fieldset>
</form>
</div>
</div>
</div>
</div>
<div class="row"> <!-- footer row !-->
<div class="col-xs-12">
<div class="footer">
<p class="p2">UR Space Copyright © 2016 All Rights Reserved</p>
</div>
</div>
</div>
</div>
<script type = "text/javascript" src = "scripts/post1.js" ></script>
</body>
</html>