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