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