-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.php
236 lines (187 loc) · 8.15 KB
/
index.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
<?php
include("includes/header.php"); //Header file with the db connection etc, also includes Classes like User and Post
if (isset($_POST['post'])) {
$uploadOk = true;
$imageName = $_FILES['fileToUpload']['name']; //Get file NAME
$error_message = "";
if ($imageName != "") {
$targetDir = "assets/images/posts/";
$imageName = $targetDir . uniqid() . basename($imageName); //So two people adding file with the same name does not get overwritten
$imageFileType = pathinfo($imageName, PATHINFO_EXTENSION); //png, jpg etc...
if ($_FILES['fileToUpload']['size'] > 1000000) {
$errorMessage = "Sorry, your file is too large!";
$uploadOk = false;
}
if (strtolower($imageFileType) != "jpeg" && strtolower($imageFileType) != "png" && strtolower($imageFileType) != "jpg") {
$errorMessage = "Sorry, only jpeg, jpg and png files are allowed.";
$uploadOk = false;
}
if ($uploadOk) {
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $imageName)) {
//Image uploaded okay
}
else {
//Img did not upload
$uploadOk = false;
}
}
}
if ($uploadOk) {
$post = new Post($con, $userLoggedIn); //Create a new post instance of this class, pass the user who created it
$post->submitPost($_POST['post_text'], 'none', $imageName); //Submit the post via submit method in the Post.php class file, $user_to is none cause it is the index page
header("location: index.php"); //Removes the resubmission of form when refreshing the page!
}
else {
echo "<div style='text-align: center;' class='alert alert-danger'>
" . $errorMessage . "
</div>";
}
}
?>
<!-- USER DETAILS -->
<div class="user_details column">
<!-- comes from header page, rewrite in .htaccess -->
<a href="<?php echo $userLoggedIn; ?>">
<img src="<?php echo $user['profile_pic']; ?>" alt="Profile picture">
</a>
<div class="user_details_left_right">
<a href="<?php echo $userLoggedIn; ?>">
<?php
echo $user['first_name'] . " " . $user['last_name'];
?>
</a>
<br>
<?php
echo "Posts: " . $user['num_posts'] . "<br>";
echo "Likes: " . $user['num_likes'];
?>
</div>
</div>
<!-- MAIN COLUMN -->
<div class="main_column column">
<!-- enctype allows forms to process file data -->
<form class="post_form" action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload"><br><br>
<textarea name="post_text" id="post_text" placeholder="Got something to say?"></textarea>
<input type="submit" name="post" id="post_button" value="Post">
<hr>
</form>
<?php
/* $post = new Post($con, $userLoggedIn); //Create a new post instance of this class, pass the user who created it
$post->loadPostsFriends(); */
?>
<div class="posts_area">
<!-- Posts are going to be loaded via ajax, 10 at a time -->
</div>
<img id="loading" src="assets/images/icons/loading.gif" alt="Loading">
</div>
<div class="user_details column">
<h4>Trending</h4>
<div class="trends">
<?php
$query = mysqli_query($con, "SELECT * FROM trends ORDER BY hits DESC LIMIT 9");
foreach($query as $row) {
$word = $row['title'];
$word_dot = strlen($word) >= 14 ? "..." : ""; //Append ... if word is too long
$trimmed_word = str_split($word, 14);
$trimmed_word = $trimmed_word[0];
echo "<div style='padding: 1px;'>";
echo $trimmed_word . $word_dot;
echo "<br></div>";
}
?>
</div>
</div>
<!-- INFINITE SCROLLING -->
<script>
$(function() {
var userLoggedIn = '<?php echo $userLoggedIn; ?>'; //Save the session variable to js value, to be used with ajax $_REQUEST later
var inProgress = false;
loadPosts(); //Load first posts
$(window).scroll(function() {
var bottomElement = $(".status_post").last();
var noMorePosts = $('.posts_area').find('.noMorePosts').val();
// isElementInViewport uses getBoundingClientRect(), which requires the HTML DOM object, not the jQuery object. The jQuery equivalent is using [0] as shown below.
if (isElementInView(bottomElement[0]) && noMorePosts == 'false') {
loadPosts();
}
});
function loadPosts() {
if (inProgress) { //If it is already in the process of loading some posts, just return
return;
}
inProgress = true;
$('#loading').show();
var page = $('.posts_area').find('.nextPage').val() || 1; //If .nextPage couldn't be found, it must not be on the page yet (it must be the first time loading posts), so use the value '1'
$.ajax({
url: "includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=" + page + "&userLoggedIn=" + userLoggedIn,
cache: false,
success: function(response) {
$('.posts_area').find('.nextPage').remove(); //Removes current .nextpage
$('.posts_area').find('.noMorePosts').remove(); //Removes current .nextpage
$('.posts_area').find('.noMorePostsText').remove(); //Removes current .nextpage
$('#loading').hide();
$(".posts_area").append(response);
inProgress = false;
}
});
}
//Check if the element is in view
function isElementInView(el) {
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && //* or $(window).height()
rect.right <= (window.innerWidth || document.documentElement.clientWidth) //* or $(window).width()
);
}
});
</script>
<script>
/* //Username of the logged in user
let userLoggedIn = '<?php //echo $userLoggedIn;
?>';
$(document).ready(function() {
$('#loading').show();
//Original ajax request for loading first posts
$.ajax({
url: "includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=1&userLoggedIn=" + userLoggedIn,
cache: false,
success: function(data) {
$('#loading').hide();
$('.posts_area').html(data);
}
});
$(window).scroll(function() {
let height = $('.posts_area').height(); //div containing posts
let scroll_top = $(this).scrollTop();
let page = $('.posts_area').find('.nextPage').val();
let noMorePosts = $('.posts_area').find('.noMorePosts').val();
//When user scrolls and more posts have to be loaded
if ((document.body.scrollHeight == document.body.scrollTop + window.innerHeight) && noMorePosts == 'false') {
$('#loading').show();
let ajaxReq = $.ajax({
url: "includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=" + page + "&userLoggedIn=" + userLoggedIn,
cache: false,
success: function(response) {
$('.posts_area').find('.nextPage').remove(); //Removes current .nextPage
$('.posts_area').find('.noMorePosts').remove(); //Removes current .nextPage
$('#loading').hide();
$('.posts_area').append(response);
}
});
} //End if
return false; //If there are no more posts
}); //End $(window).scroll(function())
}); */
</script>
</div> <!-- End of wrapper div in header.php -->
</body>
</html>