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