]> git.wh0rd.org - tt-rss.git/blame - backend.php
improved article look
[tt-rss.git] / backend.php
CommitLineData
1cd17194 1<?
a2015351 2// header("Content-Type: application/xml");
1cd17194 3
82baad4a
AD
4 require_once "config.php";
5 require_once "functions.php";
6 require_once "magpierss/rss_fetch.inc";
1cd17194 7
f07c0eb4
AD
8 error_reporting(0);
9
d76a3b03
AD
10 $link = pg_connect(DB_CONN);
11
f07c0eb4
AD
12 error_reporting (E_ERROR | E_WARNING | E_PARSE);
13
14 if (!$link) {
15 print "Could not connect to database. Please check local configuration.";
16 return;
17 }
18
d76a3b03
AD
19 pg_query("set client_encoding = 'utf-8'");
20
6f428bc5 21 $op = $_GET["op"];
331900c6 22 $fetch = $_GET["fetch"];
175847de 23
c3b81db0 24 function outputFeedList($link) {
175847de 25
c3b81db0 26 $result = pg_query($link, "SELECT *,
d76a3b03
AD
27 (SELECT count(id) FROM ttrss_entries
28 WHERE feed_id = ttrss_feeds.id) AS total,
29 (SELECT count(id) FROM ttrss_entries
30 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
31 FROM ttrss_feeds ORDER BY title");
1cd17194 32
82baad4a 33 print "<table width=\"100%\" class=\"feeds\">";
1cd17194
AD
34
35 $lnum = 0;
36
76798ff3
AD
37 $total_unread = 0;
38
6f428bc5 39 while ($line = pg_fetch_assoc($result)) {
a1a8a2be 40
6f428bc5 41 $feed = $line["title"];
2c1dd701
AD
42 $feed_id = $line["id"];
43
d76a3b03
AD
44 $total = $line["total"];
45 $unread = $line["unread"];
46
1cd17194 47 $class = ($lnum % 2) ? "even" : "odd";
a1a8a2be
AD
48
49 if ($unread > 0) $class .= "Unread";
50
76798ff3
AD
51 $total_unread += $unread;
52
a1a8a2be
AD
53 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
54
55 $feed = "<a href=\"javascript:viewfeed($feed_id, 0);\">$feed</a>";
56 print "<td id=\"FEEDN-$feed_id\">$feed</td>";
c442fb6e
AD
57 print "<td>";
58 print "<span id=\"FEEDU-$feed_id\">$unread</span>&nbsp;/&nbsp;";
59 print "<span id=\"FEEDT-$feed_id\">$total</span>";
60 print "</td>";
a1a8a2be
AD
61
62 print "</tr>";
1cd17194
AD
63 ++$lnum;
64 }
65
476cac42
AD
66// print "<tr><td class=\"footer\" colspan=\"3\">
67// <a href=\"javascript:update_feed_list(false,true)\">Update all feeds</a></td></tr>";
68
69// print "<tr><td class=\"footer\" colspan=\"2\">&nbsp;";
70// print "</td></tr>";
82baad4a 71
a1a8a2be 72 print "</table>";
1cd17194 73
476cac42
AD
74 print "<p align=\"center\">All feeds:
75 <a class=\"button\"
cb246176 76 href=\"javascript:scheduleFeedUpdate(true)\">Update</a>";
476cac42
AD
77
78 print "&nbsp;<a class=\"button\"
79 href=\"javascript:catchupAllFeeds()\">Mark as read</a></p>";
80
76798ff3
AD
81 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
82
c3b81db0
AD
83
84
85 }
86
87
88 if ($op == "rpc") {
89
90 $subop = $_GET["subop"];
91
92 if ($subop == "forceUpdateAllFeeds") {
93 update_all_feeds($link, true);
94 outputFeedList($link);
95 }
96
97 if ($subop == "updateAllFeeds") {
98 update_all_feeds($link, false);
99 outputFeedList($link);
100 }
101
102 if ($subop == "catchupPage") {
103
104 $ids = split(",", $_GET["ids"]);
105
106 foreach ($ids as $id) {
107
108 pg_query("UPDATE ttrss_entries SET unread=false,last_read = NOW()
109 WHERE id = '$id'");
110
111 }
112
113 print "Marked active page as read.";
114 }
115
116 }
117
118 if ($op == "feeds") {
119
120 $subop = $_GET["subop"];
121
122 if ($subop == "catchupAll") {
123 pg_query("UPDATE ttrss_entries SET last_read = NOW(),unread = false");
124 }
125
126 outputFeedList($link);
127
1cd17194
AD
128 }
129
130 if ($op == "view") {
131
d76a3b03
AD
132 $id = $_GET["id"];
133
b197f117 134 $result = pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
a1a8a2be 135
d76a3b03
AD
136 $result = pg_query("SELECT title,link,content FROM ttrss_entries
137 WHERE id = '$id'");
1cd17194 138
d76a3b03 139 if ($result) {
1cd17194 140
d76a3b03 141 $line = pg_fetch_assoc($result);
1cd17194 142
f07c0eb4 143/* print "<table class=\"feedOverview\">";
d76a3b03
AD
144 print "<tr><td><b>Title:</b></td><td>".$line["title"]."</td></tr>";
145 print "<tr><td><b>Link:</b></td><td><a href=\"".$line["link"]."\">".$line["link"]."</a></td></tr>";
d76a3b03
AD
146 print "</table>";
147
f07c0eb4 148 print $line["content"]; */
d76a3b03
AD
149
150
f07c0eb4
AD
151 print "<table class=\"postTable\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">";
152 print "<tr class=\"titleTop\"><td align=\"right\"><b>Title:</b></td>
153 <td width=\"100%\">".$line["title"]."</td></tr>";
154
155 print "<tr class=\"titleBottom\"><td align=\"right\"><b>Link:</b></td>
156 <td><a href=\"".$line["link"]."\">".$line["link"]."</a></td></tr>";
157
158 print "<tr><td class=\"post\" colspan=\"2\">" . $line["content"] . "</td></tr>";
159 print "</table>";
d76a3b03 160 }
1cd17194
AD
161 }
162
163 if ($op == "viewfeed") {
164
165 $feed = $_GET["feed"];
d76a3b03 166 $skip = $_GET["skip"];
476cac42 167 $subop = $_GET["subop"];
a1a8a2be 168
ac53063a
AD
169 if (!$skip) $skip = 0;
170
476cac42 171 if ($subop == "undefined") $subop = "";
1cd17194 172
ac53063a
AD
173 // FIXME: check for null value here
174
a2015351 175 $result = pg_query("SELECT *,SUBSTRING(last_updated,1,16) as last_updated,
82baad4a
AD
176 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) as update_timeout
177 FROM ttrss_feeds WHERE id = '$feed'");
1cd17194 178
d76a3b03 179 if ($result) {
1cd17194 180
d76a3b03 181 $line = pg_fetch_assoc($result);
1cd17194 182
476cac42
AD
183 if ($subop == "ForceUpdate" ||
184 (!$subop && $line["update_timeout"] > MIN_UPDATE_TIME)) {
82baad4a
AD
185
186 update_rss_feed($link, $line["feed_url"], $feed);
d76a3b03 187
a1a8a2be 188 } else {
d76a3b03 189
476cac42 190 if ($subop == "MarkAllRead") {
d76a3b03 191
b197f117 192 pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW()
a1a8a2be
AD
193 WHERE feed_id = '$feed'");
194 }
a1a8a2be
AD
195 }
196 }
d76a3b03 197
175847de 198 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
ac53063a 199
a2015351
AD
200 $feed_last_updated = "Updated: " . $line["last_updated"];
201
1c37c607 202 print "<tr><td class=\"search\" colspan=\"3\">
83fe4d6d 203 Search: <input onchange=\"javascript:search($feed,this);\"></td></tr>";
a2015351
AD
204 print "<tr>
205 <td colspan=\"3\" class=\"title\">" . $line["title"] . "</td></tr>";
d76a3b03 206
ac53063a
AD
207 if ($ext == "SEARCH") {
208 $search = $_GET["search"];
209 $search_query_part = "(upper(title) LIKE upper('%$search%')
210 OR content LIKE '%$search%') AND";
211 }
212
e6d1c0a0
AD
213 $result = pg_query("SELECT count(id) AS total_entries
214 FROM ttrss_entries WHERE feed_id = '$feed'");
215
216 $total_entries = pg_fetch_result($result, 0, "total_entries");
217
b197f117
AD
218 $result = pg_query("SELECT
219 id,title,updated,unread,feed_id,
220 EXTRACT(EPOCH FROM last_read) AS last_read_ts,
221 EXTRACT(EPOCH FROM updated) AS updated_ts
222 FROM
223 ttrss_entries
224 WHERE
ac53063a
AD
225 $search_query_part
226 feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
d76a3b03 227
a1a8a2be 228 $lnum = 0;
d76a3b03 229
a1a8a2be 230 while ($line = pg_fetch_assoc($result)) {
d76a3b03 231
a1a8a2be 232 $class = ($lnum % 2) ? "even" : "odd";
d76a3b03 233
b197f117
AD
234 if ($line["last_read_ts"] < $line["updated_ts"] && $line["unread"] == "f") {
235 $update_pic = "<img src=\"updated.png\" alt=\"Updated\">";
236 } else {
237 $update_pic = "&nbsp;";
238 }
239
a1a8a2be
AD
240 if ($line["unread"] == "t")
241 $class .= "Unread";
d76a3b03 242
b197f117
AD
243 $id = $line["id"];
244 $feed_id = $line["feed_id"];
245
246 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
247 $line["title"] . "</a>";
248
249 print "<tr class='$class' id='RROW-$id'>";
250
251 print "<td id='FUPDPIC-$id' valign='center' class='headlineUpdateMark'>$update_pic</td>";
252
a1a8a2be
AD
253 print "<td class='headlineUpdated'>".$line["updated"]."</td>";
254 print "<td class='headlineTitle'>$content_link</td>";
d76a3b03 255
a1a8a2be 256 print "</tr>";
d76a3b03 257
a1a8a2be
AD
258 ++$lnum;
259 }
d76a3b03 260
ac53063a
AD
261 if ($lnum == 0) {
262 print "<tr><td align='center'>No entries found.</td></tr>";
263
264 }
265
b197f117 266 print "<tr><td colspan=\"3\" class=\"headlineToolbar\">";
d76a3b03 267
a1a8a2be
AD
268 $next_skip = $skip + HEADLINES_PER_PAGE;
269 $prev_skip = $skip - HEADLINES_PER_PAGE;
83fe4d6d
AD
270
271 print "Navigate: ";
e6d1c0a0
AD
272
273 if ($prev_skip >= 0) {
274 print "<a class=\"button\"
275 href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
276 } else {
277 print "<a class=\"disabledButton\">Previous Page</a>";
278 }
a1a8a2be 279 print "&nbsp;";
e6d1c0a0 280
a2015351
AD
281 // start unholy navbar block
282
283
e6d1c0a0
AD
284 if ($next_skip < $total_entries) {
285 print "<a class=\"button\"
286 href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
287 } else {
288 print "<a class=\"disabledButton\">Next Page</a>";
289 }
2127761f 290 print "&nbsp;&nbsp;Feed: ";
2127761f 291
ac53063a 292 print "<a class=\"button\"
2127761f 293 href=\"javascript:viewfeed($feed, 0, 'ForceUpdate');\">Update</a>";
175847de 294
83fe4d6d 295 print "&nbsp;&nbsp;Mark as read: ";
175847de 296
83fe4d6d 297 print "<a class=\"button\"
175847de 298 href=\"javascript:catchupPage($feed);\">This Page</a>";
83fe4d6d 299 print "&nbsp;";
a1a8a2be 300 print "<a class=\"button\"
83fe4d6d 301 href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
d76a3b03 302
a1a8a2be 303 print "</td></tr>";
a2015351
AD
304
305 // end unholy navbar block
306
a1a8a2be 307 print "</table>";
d76a3b03 308
a1a8a2be
AD
309 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries
310 WHERE feed_id = ttrss_feeds.id) AS total,
311 (SELECT count(id) FROM ttrss_entries
312 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
313 FROM ttrss_feeds WHERE id = '$feed'");
d76a3b03 314
a1a8a2be
AD
315 $total = pg_fetch_result($result, 0, "total");
316 $unread = pg_fetch_result($result, 0, "unread");
d76a3b03 317
a1a8a2be
AD
318 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
319 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
320 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
d76a3b03 321
1cd17194
AD
322 }
323
0e091d38 324 if ($op == "pref-rpc") {
331900c6 325
0e091d38 326 $subop = $_GET["subop"];
331900c6 327
83fe4d6d
AD
328 if ($subop == "unread") {
329 $ids = split(",", $_GET["ids"]);
330 foreach ($ids as $id) {
331 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
332 }
0e091d38
AD
333
334 print "Marked selected feeds as read.";
83fe4d6d
AD
335 }
336
337 if ($subop == "read") {
338 $ids = split(",", $_GET["ids"]);
339 foreach ($ids as $id) {
b197f117
AD
340 pg_query("UPDATE ttrss_entries
341 SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
83fe4d6d 342 }
0e091d38
AD
343
344 print "Marked selected feeds as unread.";
345
346 }
347
348 }
349
350 if ($op == "pref-feeds") {
351
352 $subop = $_GET["subop"];
353
354 if ($subop == "edit") {
355 print "<p>[Edit feed placeholder]</p>";
83fe4d6d
AD
356 }
357
331900c6 358 if ($subop == "remove") {
331900c6 359
b0b4abcf 360 if (!WEB_DEMO_MODE) {
331900c6 361
b0b4abcf
AD
362 $ids = split(",", $_GET["ids"]);
363
364 foreach ($ids as $id) {
365 pg_query("BEGIN");
366 pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
367 pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
368 pg_query("COMMIT");
369 }
331900c6
AD
370 }
371 }
372
373 if ($subop == "add") {
b0b4abcf
AD
374
375 if (!WEB_DEMO_MODE) {
331900c6 376
b0b4abcf
AD
377 $feed_link = pg_escape_string($_GET["link"]);
378
379 $result = pg_query(
380 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
331900c6 381
b0b4abcf 382 $result = pg_query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
331900c6 383
b0b4abcf 384 $feed_id = pg_fetch_result($result, 0, "id");
331900c6 385
b0b4abcf
AD
386 if ($feed_id) {
387 update_rss_feed($link, $feed_link, $feed_id);
388 }
389 }
331900c6
AD
390 }
391
007bda35 392 $result = pg_query("SELECT * FROM ttrss_feeds ORDER by title");
1cd17194 393
331900c6 394 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
007bda35
AD
395 print "<tr class=\"title\">
396 <td>Select</td><td>Title</td><td>Link</td><td>Last Updated</td></tr>";
397
398 $lnum = 0;
399
400 while ($line = pg_fetch_assoc($result)) {
401
402 $class = ($lnum % 2) ? "even" : "odd";
331900c6
AD
403
404 $feed_id = $line["id"];
405
406 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
007bda35
AD
407
408 print "<td><input onclick='toggleSelectRow(this);'
331900c6
AD
409 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
410 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
411 $line["title"] . "</td>";
412 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
413 $line["feed_url"] . "</td>";
414
007bda35
AD
415 print "<td>" . $line["last_updated"] . "</td>";
416 print "</tr>";
417
418 ++$lnum;
419 }
420
421 print "</table>";
422
423 }
424
425 pg_close($link);
1cd17194 426?>