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