]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
updated TODO
[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 "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
95
96 /*
97                 print "<p align=\"center\">All feeds: 
98                         <a class=\"button\" 
99                                 href=\"javascript:scheduleFeedUpdate(true)\">Update</a>";
100
101                 print "&nbsp;<a class=\"button\" 
102                                 href=\"javascript:catchupAllFeeds()\">Mark as read</a></p>";
103
104                 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
105 */
106
107
108         }
109
110
111         if ($op == "rpc") {
112
113                 $subop = $_GET["subop"];
114
115                 if ($subop == "mark") {
116                         $mark = $_GET["mark"];
117                         $id = pg_escape_string($_GET["id"]);
118
119                         if ($mark == "1") {
120                                 $mark = "true";
121                         } else {
122                                 $mark = "false";
123                         }
124
125                         $result = pg_query("UPDATE ttrss_entries SET marked = $mark
126                                 WHERE id = '$id'");
127                 }
128
129                 if ($subop == "updateFeed") {
130                         $feed_id = pg_escape_string($_GET["feed"]);
131
132                         $result = pg_query($link, 
133                                 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'");
134
135                         if (pg_num_rows($result) > 0) {                 
136                                 $feed_url = pg_fetch_result($result, 0, "feed_url");
137 //                              update_rss_feed($link, $feed_url, $feed_id);
138                         }
139
140                         print "DONE-$feed_id";
141
142                         return;
143                 }
144
145                 if ($subop == "forceUpdateAllFeeds") {
146                         update_all_feeds($link, true);                  
147                         outputFeedList($link);
148                 }
149
150                 if ($subop == "updateAllFeeds") {
151                         update_all_feeds($link, false);
152                         outputFeedList($link);
153                 }
154                 
155                 if ($subop == "catchupPage") {
156
157                         $ids = split(",", $_GET["ids"]);
158
159                         foreach ($ids as $id) {
160
161                                 pg_query("UPDATE ttrss_entries SET unread=false,last_read = NOW()
162                                         WHERE id = '$id'");
163
164                         }
165
166                         print "Marked active page as read.";
167                 }
168
169         }
170         
171         if ($op == "feeds") {
172
173                 $subop = $_GET["subop"];
174
175                 if ($subop == "catchupAll") {
176                         pg_query("UPDATE ttrss_entries SET last_read = NOW(),unread = false");
177                 }
178
179                 outputFeedList($link);
180
181         }
182
183         if ($op == "view") {
184
185                 $id = $_GET["id"];
186
187                 $result = pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
188
189                 $addheader = $_GET["addheader"];
190
191                 $result = pg_query("SELECT title,link,content,feed_id,comments,
192                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url 
193                         FROM ttrss_entries
194                         WHERE   id = '$id'");
195
196                 if ($addheader) {
197                         print "<html><head>
198                                 <title>Tiny Tiny RSS : Article $id</title>
199                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
200                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
201                                 </head><body>";
202                 }
203
204                 if ($result) {
205
206                         $line = pg_fetch_assoc($result);
207
208                         if ($line["icon_url"]) {
209                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
210                         } else {
211                                 $feed_icon = "&nbsp;";
212                         }
213
214                         print "<table class=\"postTable\" width=\"100%\" cellspacing=\"0\" 
215                                 cellpadding=\"0\">";
216                                 
217                         print "<tr class=\"titleTop\"><td align=\"right\"><b>Title:</b></td>
218                                 <td width=\"100%\">".$line["title"]."</td>
219                                 <td>&nbsp;</td></tr>";
220
221                         if ($line["comments"] && $line["comments"] != $line["link"]) {
222 //                              print "<tr class=\"titleInner\"><td align=\"right\"><b>Comments:</b></td>
223 //                                      <td><a href=\"".$line["comments"]."\">".$line["comments"]."</a></td>
224 //                                      <td>&nbsp;</td> </tr>";
225
226                                 $comments_prompt = "(<a href=\"".$line["comments"]."\">Comments</a>)";
227                         }
228                         
229                         print "<tr class=\"titleBottom\"><td align=\"right\"><b>Link:</b></td>
230                                 <td><a href=\"".$line["link"]."\">".$line["link"]."</a> $comments_prompt</td>
231                                 <td>&nbsp;</td></tr>";
232                         print "<tr><td valign=\"top\" class=\"post\" 
233                                 colspan=\"2\">" . $line["content"] . "</td>
234                                 <td valign=\"top\">$feed_icon</td>
235                         </tr>";
236                         print "</table>";        
237
238                 }
239
240                 if ($addheader) {
241                         print "</body></html>";
242                 }
243         }
244
245         if ($op == "viewfeed") {
246
247                 $feed = $_GET["feed"];
248                 $skip = $_GET["skip"];
249                 $subop = $_GET["subop"];
250                 $view_mode = $_GET["view"];
251                 $addheader = $_GET["addheader"];
252
253                 if (!$skip) $skip = 0;
254
255                 if ($subop == "undefined") $subop = "";
256
257                 if ($addheader) {
258                         print "<html><head>
259                                 <title>Tiny Tiny RSS : Article $id</title>
260                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
261                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
262                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
263                                 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
264                                 </head><body>";
265                 }
266
267                 // FIXME: check for null value here
268
269                 $result = pg_query("SELECT *,SUBSTRING(last_updated,1,16) as last_updated,
270                         EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) as update_timeout
271                         FROM ttrss_feeds WHERE id = '$feed'");
272
273                 if ($result) {
274
275                         $line = pg_fetch_assoc($result);
276
277                         if ($subop == "ForceUpdate" ||
278                                 (!$subop && $line["update_timeout"] > MIN_UPDATE_TIME)) {                               
279
280                                 update_rss_feed($link, $line["feed_url"], $feed);
281                                 
282                         } else {
283
284                                 if ($subop == "MarkAllRead")  {
285
286                                         pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() 
287                                                 WHERE feed_id = '$feed'");
288                                 }
289                         }
290                 }
291
292                 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
293
294                 $feed_last_updated = "Updated: " . $line["last_updated"];
295
296                 if (!$addheader) {
297
298                         print "<tr><td class=\"search\" colspan=\"4\">
299                                 Search: <input id=\"searchbox\"
300                                 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
301                                 onchange=\"javascript:search($feed);\"> ";
302
303                         print " <a class=\"button\" href=\"javascript:resetSearch()\">Reset</a>";
304         
305                         print "&nbsp;&nbsp;View: ";
306         
307                         print_select("viewbox", $view_mode, array("All Posts", "Starred"),
308                                 "onchange=\"javascript:viewfeed('$feed', '0', '');\"");
309
310                         print "</td></tr>"; 
311                 
312                         print "<tr>
313                         <td colspan=\"4\" class=\"title\">" . $line["title"] . "</td></tr>"; 
314
315                 }
316
317                 $search = $_GET["search"];
318
319                 if ($search) {
320                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
321                                 OR content LIKE '%$search%') AND";
322                 } else {
323                         $search_query_part = "";
324                 }
325
326                 $view_query_part = "";
327
328                 if ($view_mode == "Starred") {
329                         $view_query_part = " marked = true AND ";
330                 }
331
332                 $result = pg_query("SELECT count(id) AS total_entries 
333                         FROM ttrss_entries WHERE 
334                         $search_query_part
335                         feed_id = '$feed'");
336
337                 $total_entries = pg_fetch_result($result, 0, "total_entries");
338
339                 if (!$addheader) {
340                         $limit_query_part = "LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip";
341                 }
342
343                 $result = pg_query("SELECT 
344                                 id,title,updated,unread,feed_id,marked,
345                                 EXTRACT(EPOCH FROM last_read) AS last_read_ts,
346                                 EXTRACT(EPOCH FROM updated) AS updated_ts
347                         FROM
348                                 ttrss_entries 
349                         WHERE
350                         $search_query_part
351                         $view_query_part
352                         feed_id = '$feed' ORDER BY updated DESC 
353                         $limit_query_part");
354
355                 $lnum = 0;
356                 
357                 $num_unread = 0;
358
359                 while ($line = pg_fetch_assoc($result)) {
360
361                         $class = ($lnum % 2) ? "even" : "odd";
362
363                         if ($line["last_read_ts"] < $line["updated_ts"] && $line["unread"] == "f") {
364                                 $update_pic = "<img src=\"images/updated.png\" alt=\"Updated\">";
365                                 ++$num_unread;
366                         } else {
367                                 $update_pic = "&nbsp;";
368                         }
369
370                         if ($line["unread"] == "t") {
371                                 $class .= "Unread";
372                                 ++$num_unread;
373                         }
374
375                         $id = $line["id"];
376                         $feed_id = $line["feed_id"];
377
378                         if ($line["marked"] == "t") {
379                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\" 
380                                         alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
381                         } else {
382                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\" 
383                                         alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
384                         }
385
386                         $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
387                                 $line["title"] . "</a>";
388                                 
389                         print "<tr class='$class' id='RROW-$id'>";
390
391                         print "<td id='FUPDPIC-$id' valign='center' 
392                                 class='headlineUpdateMark'>$update_pic</td>";
393
394                         print "<td valign='center' 
395                                 class='headlineUpdateMark'>$marked_pic</td>";
396
397                         print "<td class='headlineUpdated'>
398                                 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
399                         print "<td class='headlineTitle'>$content_link</td>";
400
401                         print "</tr>";
402
403                         ++$lnum;
404                 }
405
406                 if ($lnum == 0) {
407                         print "<tr><td align='center'>No entries found.</td></tr>";
408                 }
409
410                 while ($lnum < HEADLINES_PER_PAGE) {
411                         ++$lnum;
412                         print "<tr><td>&nbsp;</td></tr>";
413                 }
414
415                 // start unholy navbar block
416
417                 if (!$addheader) {
418
419                         print "<tr><td colspan=\"4\" class=\"headlineToolbar\">";
420                         
421                         $next_skip = $skip + HEADLINES_PER_PAGE;
422                         $prev_skip = $skip - HEADLINES_PER_PAGE;
423         
424                         print "Navigate: ";
425         
426                         if ($prev_skip >= 0) {
427                                 print "<a class=\"button\" 
428                                         href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
429                         } else {
430                                 print "<a class=\"disabledButton\">Previous Page</a>";
431                         }
432                         print "&nbsp;";
433         
434                         if ($next_skip < $total_entries) {              
435                                 print "<a class=\"button\" 
436                                         href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
437                         } else {
438                                 print "<a class=\"disabledButton\">Next Page</a>";
439                         }                       
440                         print "&nbsp;&nbsp;Feed: ";
441         
442                         print "<a class=\"button\" 
443                                 href=\"javascript:viewfeed($feed, 0, 'ForceUpdate');\">Update</a>";
444                         
445                         print "&nbsp;&nbsp;Mark as read: ";
446                         
447                         if ($num_unread > 0) {
448                                 print "<a class=\"button\" id=\"btnCatchupPage\" 
449                                         href=\"javascript:catchupPage($feed);\">This Page</a>";
450                                 print "&nbsp;";
451                         } else {
452                                 print "<a class=\"disabledButton\">This Page</a>";
453                                 print "&nbsp;";
454                         }
455                 
456                         print "<a class=\"button\" 
457                                 href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
458
459                 }
460
461 /*              print "&nbsp;&nbsp;Unmark: ";
462
463                 print "<a class=\"button\" 
464                         href=\"javascript:unmarkPosts(false);\">This Page</a>";
465                 print "&nbsp;";
466
467                 print "<a class=\"button\" 
468                         href=\"javascript:unmarkPosts(true);\">All Posts</a>"; */
469
470                 print "</td></tr>";
471
472                 // end unholy navbar block
473                 
474                 print "</table>";
475
476                 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries 
477                         WHERE feed_id = ttrss_feeds.id) AS total,
478                 (SELECT count(id) FROM ttrss_entries
479                         WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
480                 FROM ttrss_feeds WHERE id = '$feed'");                  
481
482                 $total = pg_fetch_result($result, 0, "total");
483                 $unread = pg_fetch_result($result, 0, "unread");
484
485                 // update unread/total counters and status for active feed in the feedlist 
486                 // kludge, because iframe doesn't seem to support onload() 
487                 
488                 print "<script type=\"text/javascript\">
489                         var feedr = parent.document.getElementById(\"FEEDR-\" + $feed);
490                         var feedt = parent.document.getElementById(\"FEEDT-\" + $feed);
491                         var feedu = parent.document.getElementById(\"FEEDU-\" + $feed);
492
493                         feedt.innerHTML = \"$total\";
494                         feedu.innerHTML = \"$unread\";
495
496                         if ($unread > 0 && !feedr.className.match(\"Unread\")) {
497                                         feedr.className = feedr.className + \"Unread\";
498                         } else if ($unread <= 0) {      
499                                         feedr.className = feedr.className.replace(\"Unread\", \"\");
500                         }       
501                 </script>";
502
503                 if ($addheader) {
504                         print "</body></html>";
505                 }
506
507         }
508
509         if ($op == "pref-rpc") {
510
511                 $subop = $_GET["subop"];
512
513                 if ($subop == "unread") {
514                         $ids = split(",", $_GET["ids"]);
515                         foreach ($ids as $id) {
516                                 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
517                         }
518
519                         print "Marked selected feeds as read.";
520                 }
521
522                 if ($subop == "read") {
523                         $ids = split(",", $_GET["ids"]);
524                         foreach ($ids as $id) {
525                                 pg_query("UPDATE ttrss_entries 
526                                         SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
527                         }
528
529                         print "Marked selected feeds as unread.";
530
531                 }
532
533         }
534
535         if ($op == "pref-feeds") {
536         
537                 $subop = $_GET["subop"];
538
539                 if ($subop == "editSave") {
540                         $feed_title = pg_escape_string($_GET["t"]);
541                         $feed_link = pg_escape_string($_GET["l"]);
542                         $feed_id = $_GET["id"];
543
544                         $result = pg_query("UPDATE ttrss_feeds SET 
545                                 title = '$feed_title', feed_url = '$feed_link' WHERE id = '$feed_id'");                 
546
547                 }
548
549                 if ($subop == "remove") {
550
551                         if (!WEB_DEMO_MODE) {
552
553                                 $ids = split(",", $_GET["ids"]);
554
555                                 foreach ($ids as $id) {
556                                         pg_query("BEGIN");
557                                         pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
558                                         pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
559                                         pg_query("COMMIT");
560                                         
561                                         if (file_exists(ICONS_DIR . "/$id.ico")) {
562                                                 unlink(ICONS_DIR . "/$id.ico");
563                                         }
564                                 }
565                         }
566                 }
567
568                 if ($subop == "add") {
569                 
570                         if (!WEB_DEMO_MODE) {
571
572                                 $feed_link = pg_escape_string($_GET["link"]);
573                                         
574                                 $result = pg_query(
575                                         "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
576
577                                 $result = pg_query(
578                                         "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
579
580                                 $feed_id = pg_fetch_result($result, 0, "id");
581
582                                 if ($feed_id) {
583                                         update_rss_feed($link, $feed_link, $feed_id);
584                                 }
585                         }
586                 }
587
588                 print "<table class=\"prefAddFeed\"><tr>
589                         <td><input id=\"fadd_link\"></td>
590                         <td colspan=\"4\" align=\"right\">
591                                 <a class=\"button\" href=\"javascript:addFeed()\">Add feed</a></td></tr>
592                 </table>";
593
594                 $result = pg_query("SELECT 
595                                 id,title,feed_url,substring(last_updated,1,16) as last_updated
596                         FROM 
597                                 ttrss_feeds ORDER by title");
598
599                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
600                 print "<tr class=\"title\">
601                                         <td>&nbsp;</td><td>Select</td><td width=\"40%\">Title</td>
602                                         <td width=\"40%\">Link</td><td>Last updated</td></tr>";
603                 
604                 $lnum = 0;
605                 
606                 while ($line = pg_fetch_assoc($result)) {
607
608                         $class = ($lnum % 2) ? "even" : "odd";
609
610                         $feed_id = $line["id"];
611
612                         $edit_feed_id = $_GET["id"];
613
614                         if ($subop == "edit" && $feed_id != $edit_feed_id) {
615                                 $class .= "Grayed";
616                         }
617
618                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
619
620                         $icon_file = ICONS_DIR . "/$feed_id.ico";
621
622                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
623                                         $feed_icon = "<img width=\"16\" height=\"16\"
624                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
625                         } else {
626                                 $feed_icon = "&nbsp;";
627                         }
628                         print "<td align='center'>$feed_icon</td>";             
629
630                         if (!$edit_feed_id || $subop != "edit") {
631
632                                 print "<td><input onclick='toggleSelectRow(this);' 
633                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
634
635                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
636                                         $line["title"] . "</td>";               
637                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
638                                         $line["feed_url"] . "</td>";            
639                         
640
641                         } else if ($feed_id != $edit_feed_id) {
642
643                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
644                                         id=\"FRCHK-".$line["id"]."\"></td>";
645
646                                 print "<td>".$line["title"]."</td>";            
647                                 print "<td>".$line["feed_url"]."</td>";         
648
649                         } else {
650
651                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
652
653                                 print "<td><input id=\"iedit_title\" value=\"".$line["title"]."\"></td>";
654                                 print "<td><input id=\"iedit_link\" value=\"".$line["feed_url"]."\"></td>";
655                                                 
656                         }
657
658                         if (!$line["last_updated"]) $line["last_updated"] = "Never";
659
660                         print "<td>" . $line["last_updated"] . "</td>";
661                         
662                         print "</tr>";
663
664                         ++$lnum;
665                 }
666
667                 if ($lnum == 0) {
668                         print "<tr><td colspan=\"5\" align=\"center\">No feeds defined.</td></tr>";
669                 }
670
671                 print "</table>";
672
673                 print "<p>";
674
675                 if ($subop == "edit") {
676                         print "Edit feed:&nbsp;
677                                         <a class=\"button\" href=\"javascript:feedEditCancel()\">Cancel</a>&nbsp;
678                                         <a class=\"button\" href=\"javascript:feedEditSave()\">Save</a>";
679                         } else {
680
681                         print "
682                                 Selection:&nbsp;
683                         <a class=\"button\" 
684                                 href=\"javascript:editSelectedFeed()\">Edit</a>&nbsp;
685                         <a class=\"buttonWarn\" 
686                                 href=\"javascript:removeSelectedFeeds()\">Remove</a>&nbsp;";
687                         if (ENABLE_PREFS_CATCHUP_UNCATCHUP) {
688                                 print "
689                                 <a class=\"button\" 
690                                         href=\"javascript:readSelectedFeeds()\">Mark as read</a>&nbsp;
691                                 <a class=\"button\" 
692                                         href=\"javascript:unreadSelectedFeeds()\">Mark as unread</a>&nbsp;";
693                         }
694                         print "
695                         All feeds:&nbsp; 
696                                 <a class=\"button\" href=\"opml.php?op=Export\">Export OPML</a>";
697                 
698                         }
699
700         }
701
702         if ($op == "pref-filters") {
703
704                 $subop = $_GET["subop"];
705
706                 if ($subop == "editSave") {
707
708                         $regexp = pg_escape_string($_GET["r"]);
709                         $descr = pg_escape_string($_GET["d"]);
710                         $match = pg_escape_string($_GET["m"]);
711                         $filter_id = pg_escape_string($_GET["id"]);
712                         
713                         $result = pg_query("UPDATE ttrss_filters SET 
714                                 regexp = '$regexp', 
715                                 description = '$descr',
716                                 filter_type = (SELECT id FROM ttrss_filter_types WHERE
717                                         description = '$match')
718                                 WHERE id = '$filter_id'");
719                 }
720
721                 if ($subop == "remove") {
722
723                         if (!WEB_DEMO_MODE) {
724
725                                 $ids = split(",", $_GET["ids"]);
726
727                                 foreach ($ids as $id) {
728                                         pg_query("DELETE FROM ttrss_filters WHERE id = '$id'");
729                                         
730                                 }
731                         }
732                 }
733
734                 if ($subop == "add") {
735                 
736                         if (!WEB_DEMO_MODE) {
737
738                                 $regexp = pg_escape_string($_GET["regexp"]);
739                                 $match = pg_escape_string($_GET["match"]);
740                                         
741                                 $result = pg_query(
742                                         "INSERT INTO ttrss_filters (regexp,filter_type) VALUES 
743                                                 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
744                                                         description = '$match'))");
745                         } 
746                 }
747
748                 $result = pg_query("SELECT description 
749                         FROM ttrss_filter_types ORDER BY description");
750
751                 $filter_types = array();
752
753                 while ($line = pg_fetch_assoc($result)) {
754                         array_push($filter_types, $line["description"]);
755                 }
756
757                 print "<table class=\"prefAddFeed\"><tr>
758                         <td><input id=\"fadd_regexp\"></td>
759                         <td>";
760                         print_select("fadd_match", "Title", $filter_types);     
761         
762                 print"</td><td colspan=\"4\" align=\"right\">
763                                 <a class=\"button\" href=\"javascript:addFilter()\">Add filter</a></td></tr>
764                 </table>";
765
766                 $result = pg_query("SELECT 
767                                 id,regexp,description,
768                                 (SELECT name FROM ttrss_filter_types WHERE 
769                                         id = filter_type) as filter_type_name,
770                                 (SELECT description FROM ttrss_filter_types 
771                                         WHERE id = filter_type) as filter_type_descr
772                         FROM 
773                                 ttrss_filters ORDER by regexp");
774
775                 print "<p><table width=\"100%\" class=\"prefFilterList\" id=\"prefFilterList\">";
776
777                 print "<tr class=\"title\">
778                                         <td width=\"5%\">Select</td><td width=\"40%\">Filter expression</td>
779                                         <td width=\"40%\">Description</td><td width=\"10%\">Match</td></tr>";
780                 
781                 $lnum = 0;
782                 
783                 while ($line = pg_fetch_assoc($result)) {
784
785                         $class = ($lnum % 2) ? "even" : "odd";
786
787                         $filter_id = $line["id"];
788                         $edit_filter_id = $_GET["id"];
789
790                         if ($subop == "edit" && $filter_id != $edit_filter_id) {
791                                 $class .= "Grayed";
792                         }
793
794                         print "<tr class=\"$class\" id=\"FILRR-$filter_id\">";
795
796                         $line["regexp"] = htmlspecialchars($line["regexp"]);
797                         $line["description"] = htmlspecialchars($line["description"]);
798
799                         if (!$edit_filter_id || $subop != "edit") {
800
801                                 if (!$line["description"]) $line["description"] = "[No description]";
802
803                                 print "<td><input onclick='toggleSelectRow(this);' 
804                                 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
805
806                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
807                                         $line["regexp"] . "</td>";              
808                                         
809                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
810                                         $line["description"] . "</td>";                 
811
812                                 print "<td>".$line["filter_type_descr"]."</td>";
813
814                         } else if ($filter_id != $edit_filter_id) {
815
816                                 if (!$line["description"]) $line["description"] = "[No description]";
817
818                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
819                                         id=\"FICHK-".$line["id"]."\"></td>";
820
821                                 print "<td>".$line["regexp"]."</td>";           
822                                 print "<td>".$line["description"]."</td>";              
823                                 print "<td>".$line["filter_type_descr"]."</td>";
824
825                         } else {
826
827                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
828
829                                 print "<td><input id=\"iedit_regexp\" value=\"".$line["regexp"].
830                                         "\"></td>";
831
832                                 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
833                                         "\"></td>";
834
835                                 print "<td>";
836                                 print_select("iedit_match", $line["filter_type_descr"], $filter_types);
837                                 print "</td>";
838                                                 
839                         }
840                                 
841                         
842                         print "</tr>";
843
844                         ++$lnum;
845                 }
846
847                 if ($lnum == 0) {
848                         print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
849                 }
850
851                 print "</table>";
852
853                 print "<p>";
854
855                 if ($subop == "edit") {
856                         print "Edit feed:&nbsp;
857                                         <a class=\"button\" href=\"javascript:filterEditCancel()\">Cancel</a>&nbsp;
858                                         <a class=\"button\" href=\"javascript:filterEditSave()\">Save</a>";
859                                         
860                 } else {
861
862                         print "
863                                 Selection:&nbsp;
864                         <a class=\"button\" 
865                                 href=\"javascript:editSelectedFilter()\">Edit</a>&nbsp;
866                         <a class=\"buttonWarn\" 
867                                 href=\"javascript:removeSelectedFilters()\">Remove</a>&nbsp;";
868                 }
869         }
870
871         pg_close($link);
872 ?>