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