]> git.wh0rd.org - tt-rss.git/blob - backend.php
cute icon for updated posts
[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,last_read = NOW() 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,last_read = NOW()
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=\"3\" 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
149 id,title,updated,unread,feed_id,
150 EXTRACT(EPOCH FROM last_read) AS last_read_ts,
151 EXTRACT(EPOCH FROM updated) AS updated_ts
152 FROM
153 ttrss_entries
154 WHERE
155 $search_query_part
156 feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
157
158 $lnum = 0;
159
160 while ($line = pg_fetch_assoc($result)) {
161
162 $class = ($lnum % 2) ? "even" : "odd";
163
164 if ($line["last_read_ts"] < $line["updated_ts"] && $line["unread"] == "f") {
165 $update_pic = "<img src=\"updated.png\" alt=\"Updated\">";
166 } else {
167 $update_pic = "&nbsp;";
168 }
169
170 if ($line["unread"] == "t")
171 $class .= "Unread";
172
173 $id = $line["id"];
174 $feed_id = $line["feed_id"];
175
176 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
177 $line["title"] . "</a>";
178
179 print "<tr class='$class' id='RROW-$id'>";
180
181 print "<td id='FUPDPIC-$id' valign='center' class='headlineUpdateMark'>$update_pic</td>";
182
183 print "<td class='headlineUpdated'>".$line["updated"]."</td>";
184 print "<td class='headlineTitle'>$content_link</td>";
185
186 print "</tr>";
187
188 ++$lnum;
189 }
190
191 if ($lnum == 0) {
192 print "<tr><td align='center'>No entries found.</td></tr>";
193
194 }
195
196 print "<tr><td colspan=\"3\" class=\"headlineToolbar\">";
197
198 $next_skip = $skip + HEADLINES_PER_PAGE;
199 $prev_skip = $skip - HEADLINES_PER_PAGE;
200
201 print "Navigate: ";
202 print "<a class=\"button\"
203 href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
204 print "&nbsp;";
205 print "<a class=\"button\"
206 href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
207 print "&nbsp;";
208 print "<a class=\"button\"
209 href=\"javascript:viewfeed($feed, $skip, '');\">Refresh Page</a>";
210 print "&nbsp;";
211 print "<a class=\"button\"
212 href=\"javascript:viewfeed($feed, 0, 'ForceUpdate');\">Update</a>";
213 print "&nbsp;&nbsp;Mark as read: ";
214 print "<a class=\"button\"
215 href=\"javascript:viewfeed($feed, $skip, 'MarkPageRead');\">This Page</a>";
216 print "&nbsp;";
217 print "<a class=\"button\"
218 href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
219
220 print "</td></tr>";
221 print "</table>";
222
223 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries
224 WHERE feed_id = ttrss_feeds.id) AS total,
225 (SELECT count(id) FROM ttrss_entries
226 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
227 FROM ttrss_feeds WHERE id = '$feed'");
228
229 $total = pg_fetch_result($result, 0, "total");
230 $unread = pg_fetch_result($result, 0, "unread");
231
232 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
233 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
234 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
235
236 }
237
238 if ($op == "pref-rpc") {
239
240 $subop = $_GET["subop"];
241
242 if ($subop == "unread") {
243 $ids = split(",", $_GET["ids"]);
244 foreach ($ids as $id) {
245 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
246 }
247
248 print "Marked selected feeds as read.";
249 }
250
251 if ($subop == "read") {
252 $ids = split(",", $_GET["ids"]);
253 foreach ($ids as $id) {
254 pg_query("UPDATE ttrss_entries
255 SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
256 }
257
258 print "Marked selected feeds as unread.";
259
260 }
261
262 }
263
264 if ($op == "pref-feeds") {
265
266 $subop = $_GET["subop"];
267
268 if ($subop == "edit") {
269 print "<p>[Edit feed placeholder]</p>";
270 }
271
272 if ($subop == "remove") {
273 $ids = split(",", $_GET["ids"]);
274
275 foreach ($ids as $id) {
276 pg_query("BEGIN");
277 pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
278 pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
279 pg_query("COMMIT");
280
281 }
282 }
283
284 if ($subop == "add") {
285 $feed_link = pg_escape_string($_GET["link"]);
286
287 $result = pg_query(
288 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
289
290 $result = pg_query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
291
292 $feed_id = pg_fetch_result($result, 0, "id");
293
294 if ($feed_id) {
295 update_rss_feed($link, $feed_link, $feed_id);
296 }
297
298 }
299
300 $result = pg_query("SELECT * FROM ttrss_feeds ORDER by title");
301
302 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
303 print "<tr class=\"title\">
304 <td>Select</td><td>Title</td><td>Link</td><td>Last Updated</td></tr>";
305
306 $lnum = 0;
307
308 while ($line = pg_fetch_assoc($result)) {
309
310 $class = ($lnum % 2) ? "even" : "odd";
311
312 $feed_id = $line["id"];
313
314 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
315
316 print "<td><input onclick='toggleSelectRow(this);'
317 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
318 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
319 $line["title"] . "</td>";
320 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
321 $line["feed_url"] . "</td>";
322
323 print "<td>" . $line["last_updated"] . "</td>";
324 print "</tr>";
325
326 ++$lnum;
327 }
328
329 print "</table>";
330
331 }
332
333 pg_close($link);
334 ?>