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