]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
e34cde94459db24eeb65edb6d382b6839b2c0b46
[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         error_reporting(0);
9
10         $link = pg_connect(DB_CONN);    
11
12         error_reporting (E_ERROR | E_WARNING | E_PARSE);
13
14         if (!$link) {
15                 print "Could not connect to database. Please check local configuration.";
16                 return;
17         }
18
19         pg_query("set client_encoding = 'utf-8'");
20
21         $op = $_GET["op"];
22         $fetch = $_GET["fetch"];
23
24         function outputFeedList($link) {
25
26                 $result = pg_query($link, "SELECT *,
27                         (SELECT count(id) FROM ttrss_entries 
28                                 WHERE feed_id = ttrss_feeds.id) AS total,
29                         (SELECT count(id) FROM ttrss_entries
30                                 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
31                         FROM ttrss_feeds ORDER BY title");                      
32
33                 print "<table width=\"100%\" class=\"feeds\" id=\"feedsList\">";
34
35                 $lnum = 0;
36
37                 $total_unread = 0;
38
39                 while ($line = pg_fetch_assoc($result)) {
40                 
41                         $feed = $line["title"];
42                         $feed_id = $line["id"];   
43
44                         $subop = $_GET["subop"];
45                         
46                         $total = $line["total"];
47                         $unread = $line["unread"];
48                         
49                         $class = ($lnum % 2) ? "even" : "odd";
50
51                         if ($unread > 0) $class .= "Unread";
52
53                         $total_unread += $unread;
54
55                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
56
57                         $icon_file = ICONS_DIR . "/$feed_id.ico";
58
59                         if ($subop != "piggie") {
60
61                                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
62                                                 $feed_icon = "<img width=\"16\" height=\"16\"
63                                                         src=\"" . ICONS_URL . "/$feed_id.ico\">";
64                                 } else {
65                                         $feed_icon = "&nbsp;";
66                                 }
67                         } else {
68                                 $feed_icon = "<img width=\"16\" height=\"16\"
69                                         src=\"http://madoka.spb.ru/stuff/fox/tiny_piggie.png\">";
70                         }
71                 
72                         $feed = "<a href=\"javascript:viewfeed($feed_id, 0);\">$feed</a>";
73                         if (ENABLE_FEED_ICONS) {
74                                 print "<td>$feed_icon</td>";
75                         }
76                         print "<td id=\"FEEDN-$feed_id\">$feed</td>";
77                         print "<td>";
78                         print "<span id=\"FEEDU-$feed_id\">$unread</span>&nbsp;/&nbsp;";
79                         print "<span id=\"FEEDT-$feed_id\">$total</span>";
80                         print "</td>";
81
82                         print "</tr>";
83                         ++$lnum;
84                 }
85
86 //              print "<tr><td class=\"footer\" colspan=\"3\">
87 //                      <a href=\"javascript:update_feed_list(false,true)\">Update all feeds</a></td></tr>";
88
89 //              print "<tr><td class=\"footer\" colspan=\"2\">&nbsp;";
90 //              print "</td></tr>";
91
92                 print "</table>";
93
94                 print "<p align=\"center\">All feeds: 
95                         <a class=\"button\" 
96                                 href=\"javascript:scheduleFeedUpdate(true)\">Update</a>";
97
98                 print "&nbsp;<a class=\"button\" 
99                                 href=\"javascript:catchupAllFeeds()\">Mark as read</a></p>";
100
101                 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
102
103
104
105         }
106
107
108         if ($op == "rpc") {
109
110                 $subop = $_GET["subop"];
111
112                 if ($subop == "getRelativeId") {
113                         $mode = $_GET["mode"];
114                         $id = $_GET["id"];
115                         $feed_id = $_GET["feed"];
116
117                         if ($id != 'false' && $feed_id != 'false') {
118
119                                 if ($mode == 'next') {
120                                         $check_qpart = "updated >= ";
121                                 } else {
122                                         $idcheck_qpart = "id < '$id'";
123                                 }
124
125                                 $result = pg_query("SELECT id FROM ttrss_entries WHERE
126                                         $check_qpart AND
127                                         feed_id = '$feed_id'
128                                         ORDER BY updated DESC LIMIT 1");
129
130                                 $result_id = pg_fetch_result($result, 0, "id");
131
132                                 print "M $mode : P $id -> P $result_id : F $feed_id";
133
134                         }
135                 }
136
137                 if ($subop == "forceUpdateAllFeeds") {
138                         update_all_feeds($link, true);                  
139                         outputFeedList($link);
140                 }
141
142                 if ($subop == "updateAllFeeds") {
143                         update_all_feeds($link, false);
144                         outputFeedList($link);
145                 }
146                 
147                 if ($subop == "catchupPage") {
148
149                         $ids = split(",", $_GET["ids"]);
150
151                         foreach ($ids as $id) {
152
153                                 pg_query("UPDATE ttrss_entries SET unread=false,last_read = NOW()
154                                         WHERE id = '$id'");
155
156                         }
157
158                         print "Marked active page as read.";
159                 }
160
161         }
162         
163         if ($op == "feeds") {
164
165                 $subop = $_GET["subop"];
166
167                 if ($subop == "catchupAll") {
168                         pg_query("UPDATE ttrss_entries SET last_read = NOW(),unread = false");
169                 }
170
171                 outputFeedList($link);
172
173         }
174
175         if ($op == "view") {
176
177                 $id = $_GET["id"];
178
179                 $result = pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
180
181                 $result = pg_query("SELECT title,link,content,feed_id,
182                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url 
183                         FROM ttrss_entries
184                         WHERE   id = '$id'");
185
186                 if ($result) {
187
188                         $line = pg_fetch_assoc($result);
189
190                         if ($line["icon_url"]) {
191                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
192                         } else {
193                                 $feed_icon = "&nbsp;";
194                         }
195
196                         print "<table class=\"postTable\" width=\"100%\" cellspacing=\"0\" 
197                                 cellpadding=\"0\">";
198                                 
199                         print "<tr class=\"titleTop\"><td align=\"right\"><b>Title:</b></td>
200                                 <td width=\"100%\">".$line["title"]."</td>
201                                 <td>&nbsp;</td></tr>";
202                                 
203                         print "<tr class=\"titleBottom\"><td align=\"right\"><b>Link:</b></td>
204                                 <td><a href=\"".$line["link"]."\">".$line["link"]."</a></td>
205                                 <td>&nbsp;</td> </tr>";
206                         print "<tr><td class=\"post\" colspan=\"2\">" . $line["content"] . "</td>                       
207                                 <td valign=\"top\">$feed_icon</td>
208                         </tr>";
209                         print "</table>";        
210
211                 }
212         }
213
214         if ($op == "viewfeed") {
215
216                 $feed = $_GET["feed"];
217                 $skip = $_GET["skip"];
218                 $subop = $_GET["subop"];
219
220                 if (!$skip) $skip = 0;
221
222                 if ($subop == "undefined") $subop = "";
223
224                 // FIXME: check for null value here
225
226                 $result = pg_query("SELECT *,SUBSTRING(last_updated,1,16) as last_updated,
227                         EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) as update_timeout
228                         FROM ttrss_feeds WHERE id = '$feed'");
229
230                 if ($result) {
231
232                         $line = pg_fetch_assoc($result);
233
234                         if ($subop == "ForceUpdate" || 
235                                 (!$subop && $line["update_timeout"] > MIN_UPDATE_TIME)) {
236                                 
237                                 update_rss_feed($link, $line["feed_url"], $feed);
238
239                         } else {
240
241                                 if ($subop == "MarkAllRead")  {
242
243                                         pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() 
244                                                 WHERE feed_id = '$feed'");
245                                 }
246                         }
247                 }
248
249                 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
250
251                 $feed_last_updated = "Updated: " . $line["last_updated"];
252
253                 print "<tr><td class=\"search\" colspan=\"3\">
254                         Search: <input id=\"searchbox\"
255                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
256                         onchange=\"javascript:search($feed);\">
257                         <a class=\"button\" href=\"javascript:resetSearch()\">Reset</a>
258                         </td></tr>"; 
259                 print "<tr>
260                 <td colspan=\"3\" class=\"title\">" . $line["title"] . "</td></tr>"; 
261
262                 $search = $_GET["search"];
263
264                 if (search) {
265                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
266                                 OR content LIKE '%$search%') AND";
267                 }
268
269                 $result = pg_query("SELECT count(id) AS total_entries 
270                         FROM ttrss_entries WHERE 
271                         $search_query_part
272                         feed_id = '$feed'");
273
274                 $total_entries = pg_fetch_result($result, 0, "total_entries");
275
276                 $result = pg_query("SELECT 
277                                 id,title,updated,unread,feed_id,
278                                 EXTRACT(EPOCH FROM last_read) AS last_read_ts,
279                                 EXTRACT(EPOCH FROM updated) AS updated_ts
280                         FROM
281                                 ttrss_entries 
282                         WHERE
283                         $search_query_part
284                         feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
285
286                 $lnum = 0;
287
288                 while ($line = pg_fetch_assoc($result)) {
289
290                         $class = ($lnum % 2) ? "even" : "odd";
291
292                         if ($line["last_read_ts"] < $line["updated_ts"] && $line["unread"] == "f") {
293                                 $update_pic = "<img src=\"images/updated.png\" alt=\"Updated\">";
294                         } else {
295                                 $update_pic = "&nbsp;";
296                         }
297
298                         if ($line["unread"] == "t") 
299                                 $class .= "Unread";
300
301                         $id = $line["id"];
302                         $feed_id = $line["feed_id"];
303
304                         $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
305                                 $line["title"] . "</a>";
306                                 
307                         print "<tr class='$class' id='RROW-$id'>";
308
309                         print "<td id='FUPDPIC-$id' valign='center' class='headlineUpdateMark'>$update_pic</td>";
310
311                         print "<td class='headlineUpdated'>
312                                 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
313                         print "<td class='headlineTitle'>$content_link</td>";
314
315                         print "</tr>";
316
317                         ++$lnum;
318                 }
319
320                 if ($lnum == 0) {
321                         print "<tr><td align='center'>No entries found.</td></tr>";
322                 }
323
324                 while ($lnum < HEADLINES_PER_PAGE) {
325                         ++$lnum;
326                         print "<tr><td>&nbsp;</td></tr>";
327                 }
328
329                 // start unholy navbar block
330
331                 print "<tr><td colspan=\"3\" class=\"headlineToolbar\">";
332                 
333                 $next_skip = $skip + HEADLINES_PER_PAGE;
334                 $prev_skip = $skip - HEADLINES_PER_PAGE;
335
336                 print "Navigate: ";
337
338                 if ($prev_skip >= 0) {
339                         print "<a class=\"button\" 
340                                 href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
341                 } else {
342                         print "<a class=\"disabledButton\">Previous Page</a>";
343                 }
344                 print "&nbsp;";
345
346                 if ($next_skip < $total_entries) {              
347                         print "<a class=\"button\" 
348                                 href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
349                 } else {
350                         print "<a class=\"disabledButton\">Next Page</a>";
351                 }                       
352                 print "&nbsp;&nbsp;Feed: ";
353
354                 print "<a class=\"button\" 
355                         href=\"javascript:viewfeed($feed, 0, 'ForceUpdate');\">Update</a>";
356                 
357                 print "&nbsp;&nbsp;Mark as read: ";
358                 
359                 print "<a class=\"button\" 
360                         href=\"javascript:catchupPage($feed);\">This Page</a>";
361                 print "&nbsp;";
362                 print "<a class=\"button\" 
363                         href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
364
365                 print "</td></tr>";
366
367                 // end unholy navbar block
368                 
369                 print "</table>";
370
371                 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries 
372                         WHERE feed_id = ttrss_feeds.id) AS total,
373                 (SELECT count(id) FROM ttrss_entries
374                         WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
375                 FROM ttrss_feeds WHERE id = '$feed'");                  
376
377                 $total = pg_fetch_result($result, 0, "total");
378                 $unread = pg_fetch_result($result, 0, "unread");
379
380                 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
381                 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
382                 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
383
384         }
385
386         if ($op == "pref-rpc") {
387
388                 $subop = $_GET["subop"];
389
390                 if ($subop == "unread") {
391                         $ids = split(",", $_GET["ids"]);
392                         foreach ($ids as $id) {
393                                 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
394                         }
395
396                         print "Marked selected feeds as read.";
397                 }
398
399                 if ($subop == "read") {
400                         $ids = split(",", $_GET["ids"]);
401                         foreach ($ids as $id) {
402                                 pg_query("UPDATE ttrss_entries 
403                                         SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
404                         }
405
406                         print "Marked selected feeds as unread.";
407
408                 }
409
410         }
411
412         if ($op == "pref-feeds") {
413         
414                 $subop = $_GET["subop"];
415
416                 if ($subop == "edit") {
417
418                         $feed_id = $_GET["id"];
419
420                         $result = pg_query("SELECT title,feed_url 
421                                 FROM ttrss_feeds WHERE id = '$feed_id'");
422
423                         $fedit_link = pg_fetch_result($result, 0, "feed_url");
424                         $fedit_title = pg_fetch_result($result, 0, "title");
425                 
426                         print "<table class=\"prefAddFeed\">
427                         <td>Title:</td><td><input id=\"fedit_title\" value=\"$fedit_title\"></td></tr>
428                         <td>Link:</td><td><input id=\"fedit_link\" value=\"$fedit_link\"></td></tr>             
429                         <tr><td colspan=\"2\" align=\"right\">
430                                 <a class=\"button\" href=\"javascript:feedEditCancel()\">Cancel</a>
431                                 <a class=\"button\" href=\"javascript:feedEditSave($feed_id)\">Save</a>
432                                 </td></tr>
433                         </table>";
434
435                 } else {
436
437                         print "<table class=\"prefAddFeed\">
438                         <td><input id=\"fadd_link\"></td>
439                         <td colspan=\"4\" align=\"right\">
440                                 <a class=\"button\" href=\"javascript:addFeed()\">Add feed</a></td></tr>
441                         </table>";
442
443                 }
444
445                 if ($subop == "editSave") {
446                         $feed_title = pg_escape_string($_GET["t"]);
447                         $feed_link = pg_escape_string($_GET["l"]);
448                         $feed_id = $_GET["id"];
449
450                         $result = pg_query("UPDATE ttrss_feeds SET 
451                                 title = '$feed_title', feed_url = '$feed_link' WHERE id = '$feed_id'");                 
452
453                 }
454
455                 if ($subop == "remove") {
456
457                         if (!WEB_DEMO_MODE) {
458
459                                 $ids = split(",", $_GET["ids"]);
460
461                                 foreach ($ids as $id) {
462                                         pg_query("BEGIN");
463                                         pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
464                                         pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
465                                         pg_query("COMMIT");
466                                 }
467                         }
468                 }
469
470                 if ($subop == "add") {
471                 
472                         if (!WEB_DEMO_MODE) {
473
474                                 $feed_link = pg_escape_string($_GET["link"]);
475                                         
476                                 $result = pg_query(
477                                         "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
478
479                                 $result = pg_query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
480
481                                 $feed_id = pg_fetch_result($result, 0, "id");
482
483                                 if ($feed_id) {
484                                         update_rss_feed($link, $feed_link, $feed_id);
485                                 }
486                         }
487                 }
488         
489                 $result = pg_query("SELECT 
490                                 id,title,feed_url,substring(last_updated,1,16) as last_updated
491                         FROM 
492                                 ttrss_feeds ORDER by title");
493
494                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
495                 print "<tr class=\"title\">
496                                         <td>&nbsp;</td><td>Select</td><td>Title</td><td>Link</td><td>Last Updated</td></tr>";
497                 
498                 $lnum = 0;
499                 
500                 while ($line = pg_fetch_assoc($result)) {
501
502                         $class = ($lnum % 2) ? "even" : "odd";
503                         
504                         $feed_id = $line["id"];
505                         
506                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
507
508                         $icon_file = ICONS_DIR . "/$feed_id.ico";
509
510                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
511                                         $feed_icon = "<img width=\"16\" height=\"16\"
512                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
513                         } else {
514                                 $feed_icon = "&nbsp;";
515                         }
516                         print "<td align='center'>$feed_icon</td>";             
517
518                         print "<td><input onclick='toggleSelectRow(this);' 
519                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
520                 
521                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
522                                 $line["title"] . "</td>";               
523                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
524                                 $line["feed_url"] . "</td>";            
525                                 
526                         print "<td>" . $line["last_updated"] . "</td>";
527                         print "</tr>";
528
529                         ++$lnum;
530                 }
531
532                 print "</table>";
533
534         }
535
536         pg_close($link);
537 ?>