]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
2dac4205fcbdf8b1751499d31efb05c9f674c2ac
[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                 $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 && $line["update_timeout"] > MIN_UPDATE_TIME) {
115                                 
116                                 update_rss_feed($link, $line["feed_url"], $feed);
117
118                         } else {
119
120                                 if ($ext == "MarkAllRead")  {
121
122                                         pg_query("UPDATE ttrss_entries SET unread = false 
123                                                 WHERE feed_id = '$feed'");
124                                 }
125
126                                 if ($ext == "MarkPageRead")  {
127
128 //                                      pg_query("UPDATE ttrss_entries SET unread = false 
129 //                                              WHERE feed_id = '$feed' ORDER BY updated OFFSET $skip LIMIT 1");
130                                 }
131
132                         }
133                 }
134
135                 print "<table class=\"headlines\" width=\"100%\">";
136
137                 print "<tr><td class=\"search\" colspan=\"2\">
138                         Search: <input onchange=\"javascript:search($feed,this);\"></td></tr>"; 
139                 print "<tr><td colspan=\"2\" class=\"title\">" . $line["title"] . "</td></tr>"; 
140
141                 if ($ext == "SEARCH") {
142                         $search = $_GET["search"];
143                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
144                                 OR content LIKE '%$search%') AND";
145                 }
146
147                 $result = pg_query("SELECT id,title,updated,unread,feed_id FROM
148                         ttrss_entries WHERE
149                         $search_query_part
150                         feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
151
152                 $lnum = 0;
153
154                 while ($line = pg_fetch_assoc($result)) {
155
156                         $class = ($lnum % 2) ? "even" : "odd";
157
158                         if ($line["unread"] == "t") 
159                                 $class .= "Unread";
160
161                         $content_link = "<a href=\"javascript:view(".$line["id"].",".$line["feed_id"].");\">".$line["title"]."</a>";
162                         
163                         print "<tr class='$class' id='RROW-".$line["id"]."'>";
164                         print "<td class='headlineUpdated'>".$line["updated"]."</td>";
165                         print "<td class='headlineTitle'>$content_link</td>";
166
167                         print "</tr>";
168
169                         ++$lnum;
170                 }
171
172                 if ($lnum == 0) {
173                         print "<tr><td align='center'>No entries found.</td></tr>";
174
175                 }
176
177                 print "<tr><td colspan=\"2\" class=\"headlineToolbar\">";
178
179                 $next_skip = $skip + HEADLINES_PER_PAGE;
180                 $prev_skip = $skip - HEADLINES_PER_PAGE;
181         
182                 print "Navigate: ";
183                 print "<a class=\"button\" 
184                         href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
185                 print "&nbsp;";
186                 print "<a class=\"button\" 
187                         href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
188                 print "&nbsp;";
189                 print "<a class=\"button\" 
190                         href=\"javascript:viewfeed($feed, $skip, '');\">Refresh</a>";
191                 print "&nbsp;&nbsp;Mark as read: ";
192                 print "<a class=\"button\" 
193                         href=\"javascript:viewfeed($feed, $skip, 'MarkPageRead');\">This Page</a>";
194                 print "&nbsp;";
195                 print "<a class=\"button\" 
196                         href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
197
198                 print "</td></tr>";
199                 print "</table>";
200
201                 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries 
202                         WHERE feed_id = ttrss_feeds.id) AS total,
203                 (SELECT count(id) FROM ttrss_entries
204                         WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
205                 FROM ttrss_feeds WHERE id = '$feed'");                  
206
207                 $total = pg_fetch_result($result, 0, "total");
208                 $unread = pg_fetch_result($result, 0, "unread");
209
210                 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
211                 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
212                 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
213
214         }
215
216         if ($op == "pref-rpc") {
217
218                 $subop = $_GET["subop"];
219
220                 if ($subop == "unread") {
221                         $ids = split(",", $_GET["ids"]);
222                         foreach ($ids as $id) {
223                                 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
224                         }
225
226                         print "Marked selected feeds as read.";
227                 }
228
229                 if ($subop == "read") {
230                         $ids = split(",", $_GET["ids"]);
231                         foreach ($ids as $id) {
232                                 pg_query("UPDATE ttrss_entries SET unread = false WHERE feed_id = '$id'");
233                         }
234
235                         print "Marked selected feeds as unread.";
236
237                 }
238
239         }
240
241         if ($op == "pref-feeds") {
242         
243                 $subop = $_GET["subop"];
244
245                 if ($subop == "edit") {
246                         print "<p>[Edit feed placeholder]</p>";
247                 }
248
249                 if ($subop == "remove") {
250                         $ids = split(",", $_GET["ids"]);
251
252                         foreach ($ids as $id) {
253                                 pg_query("BEGIN");
254                                 pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
255                                 pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
256                                 pg_query("COMMIT");
257
258                         }
259                 }
260
261                 if ($subop == "add") {
262                         $feed_link = pg_escape_string($_GET["link"]);
263                                 
264                         $result = pg_query(
265                                 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
266
267                         $result = pg_query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
268
269                         $feed_id = pg_fetch_result($result, 0, "id");
270
271                         if ($feed_id) {
272                                 update_rss_feed($link, $feed_link, $feed_id);
273                         }
274
275                 }
276         
277                 $result = pg_query("SELECT * FROM ttrss_feeds ORDER by title");
278
279                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
280                 print "<tr class=\"title\">
281                                         <td>Select</td><td>Title</td><td>Link</td><td>Last Updated</td></tr>";
282                 
283                 $lnum = 0;
284                 
285                 while ($line = pg_fetch_assoc($result)) {
286
287                         $class = ($lnum % 2) ? "even" : "odd";
288                         
289                         $feed_id = $line["id"];
290                         
291                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
292
293                         print "<td><input onclick='toggleSelectRow(this);' 
294                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
295                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
296                                 $line["title"] . "</td>";               
297                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
298                                 $line["feed_url"] . "</td>";            
299                                 
300                         print "<td>" . $line["last_updated"] . "</td>";
301                         print "</tr>";
302
303                         ++$lnum;
304                 }
305
306                 print "</table>";
307
308         }
309
310         pg_close($link);
311 ?>