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