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