-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.php
388 lines (336 loc) · 13.1 KB
/
home.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
// warning messages
error_reporting(E_ALL);
ini_set('display_errors', true);
// include library
include 'lib/utils.php';
// get logged in user, if any
if(isset($_COOKIE['author_id'])) $current_author_id = $_COOKIE['author_id'];
if(!isset($current_author_id)) $current_author_id = '';
// if no logon user, go to index.php
if($current_author_id == '') header('index.php');
$recentk = 5;
// get the recently added articles
function get_recentk_articles() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
global $recentk;
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
$article_order = array();
// get recentk articles
$sql = 'select spu_id, unix_timestamp(timestamp) as time from spu_versions where version = 1 order by time desc limit ' . $recentk;
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading versions table!'));}
$spus = array();
$latest = 1;
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$timestamp = $row['time'];
$spus[$spu_id] = array('id' => $spu_id, 'timestamp' => Timesince($timestamp), 'rank' => $latest);
$latest++;
array_push($article_order, $spu_id);
}
mysqli_free_result($result);
// get last updated timestamp
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from spu_versions where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\') group by spu_id';
$result = mysqli_query($cid, $sql);
if(!$result) {echo '<h3>Error retrieving version table!</h3>'; return;}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$time = $row['time'];
$spus[$spu_id]['last_updated'] = Timesince($time);
}
mysqli_free_result($result);
// get title of the articles
$sql = 'select spu_id, title from spu where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading article table!'));}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$title = $row['title'];
if(!isset($spus[$spu_id])) continue;
$spus[$spu_id]['title'] = $title;
}
mysqli_free_result($result);
// get authors
$sql = 'select aship.spu_id as spu_id, aship.author_id as author_id, concat(a.given_name, \' \', a.last_name) as name, aship.rank ' .
'from authorships aship, authors a where aship.author_id = a.author_id and aship.spu_id in (\'' .
implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading authors table!'));}
while($row = mysqli_fetch_assoc($result)) {
$author_id = $row['author_id'];
$author_name = $row['name'];
$spu_id = $row['spu_id'];
$rank = $row['rank'];
$authors = array();
if(isset($spus[$spu_id]['authors'])) $authors = $spus[$spu_id]['authors'];
$authors[$author_id] = array('id' => $author_id, 'name' => $author_name, 'rank' => $rank);
$spus[$spu_id]['authors'] = $authors;
}
mysqli_free_result($result);
// close connection
mysqli_close($cid);
// re sort articles
$sorted_spus = array();
foreach($article_order as $idx => $id) {
array_push($sorted_spus, $spus[$id]);
}
// return result
return json_encode($sorted_spus);
}
// get articles which has recent likes
function get_recentk_likes() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
global $recentk;
global $current_author_id;
if($current_author_id == '') return json_encode(array());
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
$article_order = array();
// get recent likes
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from likes where author_id = \'' . $current_author_id . '\' group by spu_id order by time desc limit ' . $recentk;
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading like table!'));}
$spus = array();
$latest = 1;
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$timestamp = $row['time'];
$spus[$spu_id] = array('id' => $spu_id, 'timestamp' => Timesince($timestamp), 'rank' => $latest);
$latest++;
array_push($article_order, $spu_id);
}
mysqli_free_result($result);
// get title of the articles
$sql = 'select spu_id, title from spu where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading article table!'));}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$title = $row['title'];
if(!isset($spus[$spu_id])) continue;
$spus[$spu_id]['title'] = $title;
}
mysqli_free_result($result);
// get authors
$sql = 'select aship.spu_id as spu_id, aship.author_id as author_id, concat(a.given_name, \' \', a.last_name) as name, aship.rank ' .
'from authorships aship, authors a where aship.author_id = a.author_id and aship.spu_id in (\'' .
implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading authors table!'));}
while($row = mysqli_fetch_assoc($result)) {
$author_id = $row['author_id'];
$author_name = $row['name'];
$rank = $row['rank'];
$spu_id = $row['spu_id'];
$authors = array();
if(isset($spus[$spu_id]['authors'])) $authors = $spus[$spu_id]['authors'];
$authors[$author_id] = array('id' => $author_id, 'name' => $author_name, 'rank' => $rank);
$spus[$spu_id]['authors'] = $authors;
}
mysqli_free_result($result);
// close connection
mysqli_close($cid);
// re sort articles
$sorted_spus = array();
foreach($article_order as $idx => $id) {
array_push($sorted_spus, $spus[$id]);
}
// return result
return json_encode($spus);
}
// get articles which has recent likes
function get_recentk_comments() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
global $recentk;
global $current_author_id;
if($current_author_id == '') return json_encode(array());
$article_order = array();
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
// get recent comments
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from comments where author_id = \'' . $current_author_id . '\' group by spu_id order by time desc limit ' . $recentk;
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading like table!'));}
$spus = array();
$latest = 1;
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$timestamp = $row['time'];
$spus[$spu_id] = array('id' => $spu_id, 'timestamp' => Timesince($timestamp), 'rank' => $latest);
$latest++;
array_push($article_order, $spu_id);
}
mysqli_free_result($result);
// get title of the articles
$sql = 'select spu_id, title from spu where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading article table!'));}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$title = $row['title'];
if(!isset($spus[$spu_id])) continue;
$spus[$spu_id]['title'] = $title;
}
mysqli_free_result($result);
// get authors
$sql = 'select aship.spu_id as spu_id, aship.author_id as author_id, concat(a.given_name, \' \', a.last_name) as name, aship.rank ' .
'from authorships aship, authors a where aship.author_id = a.author_id and aship.spu_id in (\'' .
implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading authors table!'));}
while($row = mysqli_fetch_assoc($result)) {
$author_id = $row['author_id'];
$author_name = $row['name'];
$rank = $row['rank'];
$spu_id = $row['spu_id'];
$authors = array();
if(isset($spus[$spu_id]['authors'])) $authors = $spus[$spu_id]['authors'];
$authors[$author_id] = array('id' => $author_id, 'name' => $author_name, 'rank' => $rank);
$spus[$spu_id]['authors'] = $authors;
}
mysqli_free_result($result);
// close database
mysqli_close($cid);
// re sort articles
$sorted_spus = array();
foreach($article_order as $idx => $id) {
array_push($sorted_spus, $spus[$id]);
}
// return result
return json_encode($spus);
}
?>
<html>
<head>
<title>openIvory - Home</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/index.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/bootstrap.min.css" />
<script language='javascript' type='text/javascript' charset='utf-8' src='js/jquery-1.8.3.min.js'></script>
<script language='javascript' type='text/javascript' charset='utf-8' src='js/jquery.cookie.js'></script>
<script language='javascript' type='text/javascript' charset='utf-8'>
$(function() {
// check if the user has already logged in.
// if not, go to login page
if(!$.cookie('author_id')) {document.location.href = 'login.php'; return;}
// add header
$('#header').load('header.php');
// add side bar
$('#sidebar').load('sidebar.php?' + (new Date()).getTime());
// add list of recently uploaded SPU
var recent_spus = <?php echo get_recentk_articles(); ?>;
var spus_div = '';
if(recent_spus) {
if(size(recent_spus) > 0) {
spus_div = format_articles('article', recent_spus);
}
}
// add list of recently commented article by current user
var recent_commented = <?php echo get_recentk_comments(); ?>;
var comments_div = '';
if(recent_commented) {
if(size(recent_commented) > 0) {
comments_div = format_articles('comment', recent_commented);
}
}
// add list of recently liked article by current user
var recent_likes = <?php echo get_recentk_likes(); ?>;
var likes_div = '';
if(recent_likes) {
if(size(recent_likes) > 0) {
likes_div = format_articles('like', recent_likes);
}
}
var content = '';
if(spus_div.length > 0) {
var location = document.location.href;
location = location.substring(0, location.lastIndexOf('\\') + 1);
content += '<h4><a href=\'' + location + 'recent_articles.php\' title=\'Click for more\' style=\'color:#000\'>Recently uploaded articles</a></h4>' + spus_div;
}
if(likes_div.length > 0) {
content += '<h4>Recently liked articles</h4>' + likes_div;
}
if(comments_div.length > 0) {
content += '<h4>Recently commented articles</h4>' + comments_div;
}
$('#main').html(content);
var sidebar_height = $('#sidebar').height();
var content_height = $('#main').height();
if(sidebar_height < content_height) $('#sidebar').css('height', content_height + 'px');
});
function format_articles(type, arr) {
var sortarr = new Array();
for(var id in arr) sortarr.push(arr[id]);
sortarr.sort(function(a, b) {
var rank_a = a['rank'];
var rank_b = b['rank'];
return rank_a - rank_b;
});
arr = sortarr;
var content = '';
for(var i in arr) {
var obj = arr[i];
var id = obj['id'];
var title = obj['title'];
var timestamp = obj['timestamp'];
var lastUpdate = obj['last_update'];
var author_map = obj['authors'];
var authors = new Array();
var author_arr = new Array();
for(var i in author_map) author_arr.push(author_map[i]);
author_arr.sort(function(a, b) {return a.rank - b.rank});
for(var i in author_arr) {
var author = author_arr[i];
var author_id = author['id'];
var author_name = author['name'];
authors.push('<a href=\'author_detail.php?id=' + author_id + '\'>' + author_name + '</a>');
}
authors = authors.join(', ');
var p = ''
if(type == 'article') {
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>First uploaded ' + timestamp + '</p></div>';
if(lastUpdate && (lastUpdate != '') && (lastUpdate != timestamp))
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>First uploaded ' + timestamp + '<br>Last updated on ' + lastUpdate + '</p></div>';
}
else if(type == 'like') {
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>Last liked ' + timestamp + '</p></div>';
}
else if(type == 'comment') {
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>Last commented ' + timestamp + '</p></div>';
}
content += p + '<br>';
}
return content;
}
function size(arr) {
var count = 0;
for(var i in arr) count++;
return count;
}
</script>
</head>
<body>
<div id="header"></div> <!-- header -->
<div id="sidebar"></div> <!-- sidebar -->
<div id="main"></div> <!-- main -->
</body>
</html>