-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
185 lines (108 loc) · 4.96 KB
/
functions.php
File metadata and controls
185 lines (108 loc) · 4.96 KB
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
<?php
session_start();
$link = mysqli_connect("localhost","root","","twitter");
if (mysqli_connect_errno()) {
print_r(mysqli_connect_error());
exit();
}
if (isset($_GET['function']) == "logout") {
session_unset();
}
function time_since($since) {
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'min'),
array(1 , 'sec')
);
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
return $print;
}
function displayTweets($type) {
global $link;
if ($type == 'public') {
$whereClause = "";
} else if ($type == 'isFollowing') {
$query = "SELECT * FROM isFollowing WHERE follower = ".mysqli_real_escape_string($link, isset($_SESSION['id']));
$result = mysqli_query($link, $query);
$whereClause = "";
while ($row = mysqli_fetch_assoc($result)) {
if ($whereClause == "") $whereClause = "WHERE";
else $whereClause.= " OR";
$whereClause.= " userid = ".$row['isFollowing'];
}
} else if ($type == 'yourtweets') {
$whereClause = "WHERE userid = ". mysqli_real_escape_string($link, $_SESSION['id']);
} else if ($type == 'search') {
echo '<p>Showing search results for "'.mysqli_real_escape_string($link, $_GET['q']).'":</p>';
$whereClause = "WHERE tweet LIKE '%". mysqli_real_escape_string($link, $_GET['q'])."%'";
} else if (is_numeric($type)) {
$userQuery = "SELECT * FROM users WHERE id = ".mysqli_real_escape_string($link, $type)." LIMIT 1";
$userQueryResult = mysqli_query($link, $userQuery);
$user = mysqli_fetch_assoc($userQueryResult);
echo "<h2>".mysqli_real_escape_string($link, $user['email'])."'s Tweets</h2>";
$whereClause = "WHERE userid = ". mysqli_real_escape_string($link, $type);
}
$query = "SELECT * FROM tweets ".$whereClause." ORDER BY `date` DESC LIMIT 10";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) == 0) {
echo "There are no tweets to display.";
} else {
while ($row = mysqli_fetch_assoc($result)) {
$userQuery = "SELECT * FROM users WHERE id = ".mysqli_real_escape_string($link, $row['userid'])." LIMIT 1";
$userQueryResult = mysqli_query($link, $userQuery);
$user = mysqli_fetch_assoc($userQueryResult);
echo "<div class='tweet'><p><a href='?page=publicprofiles&userid=".$user['id']."'>".$user['email']."</a> <span class='time'>".time_since(time() - strtotime($row['date']))." ago</span>:</p>";
echo "<p>".$row['tweet']."</p>";
echo "<p><a class='toggleFollow' data-userId='".$row['userid']."'>";
$isFollowingQuery = "SELECT * FROM isFollowing WHERE follower = ".mysqli_real_escape_string($link, isset($_SESSION['id']))." AND isFollowing = ".mysqli_real_escape_string($link, $row['userid'])." LIMIT 1";
$isFollowingQueryResult = mysqli_query($link, $isFollowingQuery);
if (mysqli_num_rows($isFollowingQueryResult) > 0) {
echo "Unfollow";
} else {
echo "Follow";
}
echo "</a></p></div>";
}
}
}
function displaySearch() {
echo '<form class="form-inline">
<div class="form-group">
<input type="hidden" name="page" value="search">
<input type="text" name="q" class="form-control" id="search" placeholder="Search">
</div>
<button type="submit" class="btn btn-primary">Search Tweets</button>
</form>';
}
function displayTweetBox() {
if (isset($_SESSION['id']) > 0) {
echo '<div id="tweetSuccess" class="alert alert-success">Your tweet was posted successfully.</div>
<div id="tweetFail" class="alert alert-danger"></div>
<div class="form">
<div class="form-group">
<textarea class="form-control" id="tweetContent"></textarea>
</div>
<button id="postTweetButton" class="btn btn-primary">Post Tweet</button>
</div>';
}
}
function displayUsers() {
global $link;
$query = "SELECT * FROM users LIMIT 10";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo "<p><a href='?page=publicprofiles&userid=".$row['id']."'>".$row['email']."</a></p>";
}
}
?>