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