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