-
Notifications
You must be signed in to change notification settings - Fork 30
/
feed.php
executable file
·523 lines (458 loc) · 18.9 KB
/
feed.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
# *** LICENSE ***
# This file is part of BlogoText.
# https://github.com/BlogoText/blogotext/
#
# 2006 Frederic Nassar.
# 2010-2016 Timo Van Neerden.
# 2016-.... Mickaël Schoentgen and the community.
#
# BlogoText is free software.
# You can redistribute it under the terms of the MIT / X11 Licence.
# *** LICENSE ***
require_once 'inc/boot.php';
/**
*
*/
function display_form_feed_conf($errors = '')
{
if ($errors) {
echo erreurs($errors);
}
$out = '<form id="form-rss-config" method="post" action="feed.php?config">';
$out .= '<ul>';
foreach ($GLOBALS['liste_flux'] as $flux) {
$out .= '<li>';
$out .= '<span'.( ($flux['iserror'] > 2) ? ' class="feed-error" title="('.$flux['iserror'].' last requests were errors.)" ' : '' ).'>';
$out .= '<label for="i_'.$flux['checksum'].'">'.$GLOBALS['lang']['rss_label_title_flux'].'</label>';
$out .= '<input id="i_'.$flux['checksum'].'" name="i_'.$flux['checksum'].'" type="text" class="text" value="'.htmlspecialchars($flux['title']).'">';
$out .= '</span>';
$out .= '<span>';
$out .= '<label for="j_'.$flux['checksum'].'">'.$GLOBALS['lang']['rss_label_url_flux'].'</label>';
$out .= '<input id="j_'.$flux['checksum'].'" name="j_'.$flux['checksum'].'" type="text" class="text" value="'.htmlspecialchars($flux['link']).'">';
$out .= '</span>';
$out .= '<span>';
$out .= '<label for="l_'.$flux['checksum'].'">'.$GLOBALS['lang']['rss_label_folder'].'</label>';
$out .= '<input id="l_'.$flux['checksum'].'" name="l_'.$flux['checksum'].'" type="text" class="text" value="'.htmlspecialchars($flux['folder']).'">';
$out .= '<input class="remove-feed" name="k_'.$flux['checksum'].'" type="hidden" value="1">';
$out .= '</span>';
$out .= '<span>';
$out .= '<button type="button" class="submit button-cancel" onclick="unMarkAsRemove(this)">'.$GLOBALS['lang']['cancel'].'</button>';
$out .= '<button type="button" class="submit button-delete" onclick="markAsRemove(this)">'.$GLOBALS['lang']['delete'].'</button>';
$out .= '</span>';
$out .= '</li>';
}
$out .= '</ul>';
$out .= '<p class="submit-bttns">';
$out .= '<button class="submit button-submit" type="submit" name="send">'.$GLOBALS['lang']['send'].'</button>';
$out .= '</p>';
$out .= hidden_input('token', new_token());
$out .= hidden_input('verif_envoi', 1);
$out .= '</form>';
return $out;
}
/**
*
*/
function traitment_form_feed_conf()
{
$msg = (string)filter_input(INPUT_GET, 'msg');
$queryString = str_replace(($msg) ? '&msg='.$msg : '', '', $_SERVER['QUERY_STRING']);
$GLOBALS['db_handle']->beginTransaction();
foreach ($GLOBALS['liste_flux'] as $idx => $feed) {
$title = (string)filter_input(INPUT_POST, 'i_'.$feed['checksum']);
if ($title) {
$link = (string)filter_input(INPUT_POST, 'j_'.$feed['checksum']);
$folder = (string)filter_input(INPUT_POST, 'l_'.$feed['checksum']);
$status = (string)filter_input(INPUT_POST, 'k_'.$feed['checksum']);
if ($status == 0) {
// Feed marked to be removed
unset($GLOBALS['liste_flux'][$idx]);
$req = $GLOBALS['db_handle']->prepare('DELETE FROM rss WHERE bt_feed = ?');
$req->execute(array($feed['link']));
continue;
}
// Title has change
$GLOBALS['liste_flux'][$idx]['title'] = $title;
// Folder has changed: update & change folder where it must be changed
if ($folder != $GLOBALS['liste_flux'][$idx]['folder']) {
$GLOBALS['liste_flux'][$idx]['folder'] = $folder;
$req = $GLOBALS['db_handle']->prepare('UPDATE rss SET bt_folder = ? WHERE bt_feed = ?');
$req->execute(array($folder, $feed['link']));
}
// URL has change
if ($link != $GLOBALS['liste_flux'][$idx]['link']) {
$newUrl = $GLOBALS['liste_flux'][$idx];
$newUrl['link'] = $link;
unset($GLOBALS['liste_flux'][$idx]);
$GLOBALS['liste_flux'][$newUrl['link']] = $newUrl;
$req = $GLOBALS['db_handle']->prepare('UPDATE rss SET bt_feed = ? WHERE bt_feed = ?');
$req->execute(array($link, $feed['link']));
}
}
}
$GLOBALS['db_handle']->commit();
// Sort list with title
$GLOBALS['liste_flux'] = array_reverse(tri_selon_sous_cle($GLOBALS['liste_flux'], 'title'));
create_file_dtb(FEEDS_DB, $GLOBALS['liste_flux']);
$redir = basename($_SERVER['SCRIPT_NAME']).'?'.$queryString.'&msg=confirm_feeds_edit';
redirection($redir);
}
/**
*
*/
function rss_count_feed()
{
$sql = '
SELECT bt_feed, SUM(bt_statut) AS nbrun, SUM(bt_bookmarked) AS nbfav
FROM rss
GROUP BY bt_feed';
return $GLOBALS['db_handle']->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
/**
*
*/
function feed_list_html($selected = '')
{
// Counts unread feeds in DB
$numberOfFeeds = rss_count_feed();
$totalUnread = $totalFav = 0;
foreach ($numberOfFeeds as $feed) {
$totalUnread += $feed['nbrun'];
$totalFav += $feed['nbfav'];
}
// First item: link all feeds
$html = '<li class="all-feeds"><a href="feed.php">'.$GLOBALS['lang']['rss_label_all_feeds'].' <span id="global-post-counter" data-nbrun="'.$totalUnread.'">('.$totalUnread.')</span></a></li>';
// Next item: favorites items
$html .= '<li class="fav-feeds"><a href="feed.php?bookmarked">'.$GLOBALS['lang']['rss_label_favs_feeds'].' <span id="favs-post-counter" data-nbrun="'.$totalFav.'">('.$totalFav.')</span></a></li>';
$feedUrls = array();
foreach ($numberOfFeeds as $feed) {
$feedUrls[$feed['bt_feed']] = $feed;
}
// Sort feeds by folder
$folders = array();
foreach ($GLOBALS['liste_flux'] as $feed) {
$feed['nbrun'] = ((isset($feedUrls[$feed['link']]['nbrun'])) ? $feedUrls[$feed['link']]['nbrun'] : 0);
$folders[$feed['folder']][] = $feed;
}
krsort($folders);
// Creates HTML: lists RSS feeds without folder separately from feeds with a folder
foreach ($folders as $idx => $folder) {
$liHtml = '';
$folderCount = 0;
foreach ($folder as $feed) {
if ($feed['nbrun'] > 0) {
if ($feed['link'] == $selected) {
$liHtml .= '<li class="active-site" data-nbrun="'.$feed['nbrun'].'" data-feedurl="'.$feed['link'].'" title="'.$feed['link'].'">';
} else {
$liHtml .= '<li class="" data-nbrun="'.$feed['nbrun'].'" data-feedurl="'.$feed['link'].'" title="'.$feed['link'].'">';
}
$liHtml .= '<a href="?site='.urlencode($feed['link']).'" '.(($feed['iserror'] > 2) ? 'class="feed-error" ': ' ' ).' data-feed-domain="'.parse_url($feed['link'], PHP_URL_HOST).'">'.$feed['title'].'</a>';
$liHtml .= '<span>('.$feed['nbrun'].')</span>';
$liHtml .= '</li>';
$folderCount += $feed['nbrun'];
}
}
if ($idx != '') {
if ($selected == $idx) {
$html .= '<li class="feed-folder open" data-nbrun="'.$folderCount.'" data-folder="'.$idx.'">';
} else {
$html .= '<li class="feed-folder" data-nbrun="'.$folderCount.'" data-folder="'.$idx.'">';
}
$html .= '<span class="feed-folder-title">';
$html .= '<a href="?fold='.$idx.'">'.$idx.'<span>('.$folderCount.')</span></a>';
$html .= '<a href="#" onclick="return hideFolder(this)" class="unfold">unfold</a>';
$html .= '</span>';
$html .= '<ul>'."\n\t\t";
}
$html .= $liHtml;
if ($idx != '') {
$html .= '</ul>';
$html .= '</li>';
}
}
return $html;
}
/**
* Send all RSS entries data in a JSON format.
*/
function send_rss_json($feeds)
{
$to_json = array(
'list' => array(),
'count' => (count($feeds) - 1)
);
$clean = array();
foreach ($feeds as $feed) {
$to_json['list'][] = array(
'id' => $feed['bt_id'],
'date' => date_formate(date('YmdHis', $feed['bt_date'])),
'time' => heure_formate(date('YmdHis', $feed['bt_date'])),
'title' => $feed['bt_title'],
'link' => $feed['bt_link'],
'feed' => $feed['bt_feed'],
'sitename' => $GLOBALS['liste_flux'][$feed['bt_feed']]['title'],
'folder' => $GLOBALS['liste_flux'][$feed['bt_feed']]['folder'],
'content' => $feed['bt_content'],
'statut' => $feed['bt_statut'],
'fav' => $feed['bt_bookmarked']
);
}
return '<script>var rss_entries = '. json_encode($to_json) .';</script>';
}
/**
* Process
*/
$GLOBALS['liste_flux'] = open_serialzd_file(FEEDS_DB);
$errors = array();
if (filter_input(INPUT_POST, 'verif_envoi') !== null) {
$errors = valider_form_rss();
if (!$errors) {
traitment_form_feed_conf();
}
}
$tableau = array();
// Show N items per page
$page = filter_input(INPUT_GET, 'p');
$arr = array();
// For a site?
$site = (string)filter_input(INPUT_GET, 'site');
$fold = (string)filter_input(INPUT_GET, 'fold');
$bookmarked = (filter_input(INPUT_GET, 'bookmarked') !== null);
$query = (string)filter_input(INPUT_GET, 'q');
$page_date = filter_input(INPUT_GET, 'date');
$item_id = filter_input(INPUT_GET, 'id');
$sqlWhere = '';
$sqlWhereDate = '';
$sqlWhereStatus = '';
$sqlOrder = 'DESC';
$paramUrl = '';
$btn_previous_page = '';
$btn_next_page = '';
if (!empty($page_date)) {
if ($page == 'previous') {
$search_sign = '<';
} else if ($page == 'next') {
$search_sign = '>=';
$sqlOrder = 'ASC';
}
if (!empty($item_id)) {
$sqlWhereDate = ' AND ((bt_date = '.$page_date.' AND ID '.$search_sign.' '.$item_id.') OR bt_date '.$search_sign.' '.$page_date.')';
} else {
$sqlWhereDate = ' AND bt_date '.$search_sign.' '.$page_date;
}
}
if ($site) {
$sqlWhere .= 'bt_feed LIKE ?';
$arr[] = '%'.$site.'%';
$paramUrl = 'site='.$site.'&';
} elseif ($fold) {
$sqlWhere .= 'bt_folder LIKE ?';
$arr[] = '%'.$fold.'%';
$paramUrl = 'fold='.$fold.'&';
} elseif ($bookmarked) {
$sqlWhere .= 'bt_bookmarked = 1';
$paramUrl = 'bookmarked&';
}
if ($query) {
// Search "in:read"
if (substr($query, -8) == ' in:read') {
if ($sqlWhere) {
$sqlWhere .= ' AND ';
}
$sqlWhereStatus = 'bt_statut = 0';
$query = substr($query, 0, strlen($query) - 8);
}
// Search "in:unread"
if (substr($query, -10) == ' in:unread') {
if ($sqlWhere) {
$sqlWhere .= ' AND ';
}
$sqlWhereStatus = 'bt_statut = 1';
$query = substr($query, 0, strlen($query) - 10);
}
$criterias = parse_search($query);
if ($sqlWhere && $criterias) {
$sqlWhere .= ' AND ';
}
// AND operator between words
foreach ($criterias as $where) {
$arr[] = $where;
$sqlWhere .= '(bt_content || bt_title) LIKE ? AND ';
}
$sqlWhere = trim($sqlWhere, ' AND ');
} else {
$sqlWhereStatus = ' AND (bt_statut = 1 OR bt_bookmarked = 1)';
}
// add 1 more than max_rss_admin, for detecting if there is a previous or next page
$sql = '
SELECT * FROM rss
WHERE '. trim(trim($sqlWhere.$sqlWhereStatus.$sqlWhereDate, ' '), 'AND') .'
ORDER BY bt_date '.$sqlOrder.', ID '.$sqlOrder.'
LIMIT '.($GLOBALS['max_rss_admin'] + 1);
$tableau = liste_elements($sql, $arr, 'rss');
// using main SQL request, try to find previous/next page
$have_more = (count($tableau) === ($GLOBALS['max_rss_admin']+1));
if (isset($have_more)) {
if ($sqlOrder == 'ASC') {
unset($tableau['0']);
} else {
unset($tableau[$GLOBALS['max_rss_admin']]);
}
}
// reverse order to respect time
if ($sqlOrder == 'ASC') {
$tableau = array_reverse($tableau);
}
// get pagination
$btn_previous_page = '';
$btn_next_page = '';
if (is_array($tableau) && isset($tableau['0'])) {
// get pagination
$first_item = array_values($tableau)[0];
$last_item = end($tableau);
// detect previous / next page
if ($sqlOrder == 'ASC') {
if ($have_more) {
$btn_next_page =
'<li><button type="button" id="next_feeds" '
.'onclick="location.href=\'feed.php?'.$paramUrl.'p=next&date='.$first_item['bt_date'].'&id='.$first_item['ID'].'\'"></button></li>';
}
$sql = '
SELECT * FROM rss
WHERE '. trim(trim($sqlWhere.$sqlWhereStatus, ' '), 'AND') .'
AND ((bt_date = '.$last_item['bt_date'].' AND ID < '.$last_item['ID'].') OR bt_date > '.$last_item['bt_date'].')
ORDER BY bt_date DESC, ID DESC
LIMIT 1';
$t_sql = liste_elements($sql, $arr, 'rss');
if (isset($t_sql['0'])) {
$btn_previous_page = '<li><button type="button" id="prev_feeds" onclick="location.href=\'feed.php?'.$paramUrl.'p=previous&date='.$last_item['bt_date'].'&id='.$last_item['ID'].'\'"></button></li>';
}
} else {
if ($have_more) {
$btn_previous_page =
'<li><button type="button" id="prev_feeds" '
.'onclick="location.href=\'feed.php?'.$paramUrl.'p=previous&date='.$last_item['bt_date'].'&id='.$last_item['ID'].'\'"></button></li>';
}
$sql = '
SELECT * FROM rss
WHERE '. trim(trim($sqlWhere.$sqlWhereStatus, ' '), 'AND') .'
AND ((bt_date = '.$first_item['bt_date'].' AND ID > '.$first_item['ID'].') OR bt_date > '.$first_item['bt_date'].')
ORDER BY bt_date DESC, ID DESC
LIMIT 1';
$t_sql = liste_elements($sql, $arr, 'rss');
if (isset($t_sql['0'])) {
$btn_next_page = '<li><button type="button" id="next_feeds" onclick="location.href=\'feed.php?'.$paramUrl.'p=next&date='.$first_item['bt_date'].'&id='.$first_item['ID'].'\'"></button></li>';
}
}
} else {
// no datas ...
}
/**
* echo
*/
echo tpl_get_html_head($GLOBALS['lang']['mysubs']);
echo '<div id="header">';
echo '<div id="top">';
tpl_show_msg();
echo moteur_recherche();
echo tpl_show_topnav($GLOBALS['lang']['mysubs']);
echo '</div>';
$config = filter_input(INPUT_GET, 'config');
if ($config === null) {
echo '<div id="rss-menu">';
echo '<span id="count-posts"><span id="counter"></span></span>';
echo '<span id="message-return"></span>';
echo '<ul class="rss-menu-buttons">';
echo '<li><button type="button" onclick="refresh_all_feeds(this);" title="'.$GLOBALS['lang']['rss_label_refresh'].'"></button></li>';
echo '<li><button type="button" onclick="window.location= \'?config\';" title="'.$GLOBALS['lang']['rss_label_config'].'"></button></li>';
echo '<li><button type="button" onclick="window.location.href=\'maintenance.php#form_import\'" title="Import/export"></button></li>';
echo '<li><button type="button" onclick="return cleanList();" title="'.$GLOBALS['lang']['rss_label_clean'].'"></button></li>';
echo '</ul>';
echo '</div>';
echo '<button type="button" id="fab" class="add-feed" onclick="addNewFeed();" title="'.$GLOBALS['lang']['rss_label_config'].'">'.$GLOBALS['lang']['label_link_add'].'</button>';
}
echo '</div>';
echo '<div id="axe">';
echo '<div id="page">';
if ($config !== null) {
echo display_form_feed_conf($errors);
echo '<script src="style/javascript.js"></script>';
} else {
// Get list of posts from DB
$out = send_rss_json($tableau);
$out .= '<div id="rss-list">';
$out .= '<div id="posts-wrapper">';
$out .= '<ul id="feed-list">';
if ($site) {
$out .= feed_list_html($site);
} elseif ($fold) {
$out .= feed_list_html($fold);
} else {
$out .= feed_list_html();
}
$out .= '</ul>';
$out .= '<div id="post-list-wrapper">';
$out .= '<div id="post-list-title">';
$out .= '<ul class="rss-menu-buttons">';
if ($site) {
$out .= "\r".'<li><button type="button" onclick="sendMarkReadRequest(\'site\', \''.$site.'\', false);" id="markasread" title="'.$GLOBALS['lang']['rss_label_markasread'].'"></button></li>';
} elseif ($fold) {
$out .= "\r".'<li><button type="button" onclick="sendMarkReadRequest(\'folder\', \''.$fold.'\', true);" id="markasread" title="'.$GLOBALS['lang']['rss_label_markasread'].'"></button></li>';
} else {
$out .= "\r".'<li><button type="button" onclick="sendMarkReadRequest(\'all\');" id="markasread" title="'.$GLOBALS['lang']['rss_label_markasread'].'"></button></li>';
}
$out .= '<li><button type="button" onclick="openAllItems(this);" id="openallitemsbutton" title="'.$GLOBALS['lang']['rss_label_unfoldall'].'"></button></li>';
// Navigation: previous/next pages
// Navigation: previous/next pages
$out .= $btn_previous_page;
$out .= $btn_next_page;
$out .= '</ul>';
$out .= '<p><span id="post-counter"></span> '.$GLOBALS['lang']['label_elements'].'</p>';
$out .= '</div>';
// Here comes (in JS) the <ul id="post-list"></ul>
if (empty($GLOBALS['liste_flux'])) {
$out .= $GLOBALS['lang']['rss_nothing_here_note'].'<a href="maintenance.php#form_import">import OPML</a>.';
}
$out .= '</div>';
$out .= '</div>';
$out .= '<div class="keyshortcut">'.$GLOBALS['lang']['rss_key_shortcuts'].'</div>';
$out .= '</div>';
echo $out;
echo '<script src="style/javascript.js"></script>';
echo '
<script>
var active_site = document.querySelectorAll(".active-site");
if (active_site.length == 1){
var s = active_site[0],
p = s.parentNode.parentNode;
p.classList.add("open");
}
var token = "'.new_token().'",
openAllSwich = "open",
readQueue = { "count": 0, "urlList": [] },
Rss = rss_entries.list;
window.addEventListener("load", function() {
rss_feedlist(Rss);
window.addEventListener("keydown", keyboardNextPrevious);
});
window.addEventListener("beforeunload", function (e) {
if (readQueue.count == 0) {
return true;
}
sendMarkReadRequest("postlist", JSON.stringify(readQueue.urlList), false);
readQueue.urlList = [];
readQueue.count = 0;
});
var scrollPos = 0;
window.addEventListener("scroll", function() { scrollingFabHideShow(); });
window.addEventListener("load", function() {
var list = document.querySelectorAll("a[data-feed-domain]");
for (var i = 0, len = list.length; i < len; i++) {
list[i].style.backgroundImage = "url(\'" + "'.URL_ROOT.'favatar.php?w=favicon&q="+ list[i].getAttribute("data-feed-domain") + "\')";
}
});
'.php_lang_to_js(0).'
</script>';
}
echo tpl_get_footer($begin);