]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
update NEWS, TODO
[tt-rss.git] / backend.php
1 <?
2         $op = $_GET["op"];
3
4         if ($op == "rpc") {
5                 header("Content-Type: application/xml");
6         }
7
8         require_once "config.php";
9         require_once "db.php";
10         require_once "functions.php";
11         require_once "magpierss/rss_fetch.inc";
12
13         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
14
15         if (!$link) {
16                 if (DB_TYPE == "mysql") {
17                         print mysql_error();
18                 }
19                 // PG seems to display its own errors just fine by default.             
20                 return;
21         }
22
23         if (DB_TYPE == "pgsql") {
24                 pg_query("set client_encoding = 'utf-8'");
25         }
26         
27         $fetch = $_GET["fetch"];
28
29         /* FIXME this needs reworking */
30
31         function getTagCounters($link) {
32                 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
33                         FROM ttrss_tags,ttrss_entries WHERE
34                         post_id = ttrss_entries.id AND unread = true GROUP BY tag_name 
35                 UNION
36                         select tag_name,0 as count FROM ttrss_tags");
37
38                 $tags = array();
39
40                 while ($line = db_fetch_assoc($result)) {
41                         $tags[$line["tag_name"]] += $line["count"];
42                 }
43
44                 foreach (array_keys($tags) as $tag) {
45                         $unread = $tags[$tag];                  
46
47                         $tag = htmlspecialchars($tag);
48                         print "<tag id=\"$tag\" counter=\"$unread\"/>";
49                 } 
50         }
51
52         function getLabelCounters($link) {
53
54                 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
55                         WHERE marked = true AND unread = true");
56
57                 $count = db_fetch_result($result, 0, "count");
58
59                 print "<label id=\"-1\" counter=\"$count\"/>";
60
61                 $result = db_query($link, "SELECT id,sql_exp,description FROM
62                         ttrss_labels ORDER by description");
63         
64                 while ($line = db_fetch_assoc($result)) {
65
66                         $id = -$line["id"] - 11;
67
68                         error_reporting (0);
69
70                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
71                                 WHERE (" . $line["sql_exp"] . ") AND unread = true");
72
73                         $count = db_fetch_result($tmp_result, 0, "count");
74
75                         print "<feed id=\"$id\" counter=\"$count\"/>";
76
77                         error_reporting (E_ERROR | E_WARNING | E_PARSE);
78         
79                 }
80         }
81
82         function getFeedCounter($link, $id) {
83         
84                 $result = db_query($link, "SELECT 
85                                 count(id) as count FROM ttrss_entries
86                         WHERE feed_id = '$id'   AND unread = true");
87         
88                         $count = db_fetch_result($result, 0, "count");
89                         
90                         print "<feed id=\"$id\" counter=\"$count\"/>";          
91         }
92
93         function getFeedCounters($link) {
94         
95                 $result = db_query($link, "SELECT id,
96                         (SELECT count(id) FROM ttrss_entries WHERE feed_id = ttrss_feeds.id 
97                                 AND unread = true) as count
98                         FROM ttrss_feeds");
99         
100                 while ($line = db_fetch_assoc($result)) {
101                 
102                         $id = $line["id"];
103                         $count = $line["count"];
104
105                         print "<feed id=\"$id\" counter=\"$count\"/>";
106                 }
107         }
108
109         function outputFeedList($link, $tags = false) {
110
111                 print "<html><head>
112                         <title>Tiny Tiny RSS : Feedlist</title>
113                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
114                         <script type=\"text/javascript\" src=\"functions.js\"></script>
115                         <script type=\"text/javascript\" src=\"feedlist.js\"></script>
116                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
117                         </head><body>";
118
119                 print "<ul class=\"feedList\" id=\"feedList\">";
120
121                 if (!$tags) {
122
123                         /* virtual feeds */
124
125                         $result = db_query($link, "SELECT count(id) as num_starred 
126                                 FROM ttrss_entries WHERE marked = true AND unread = true");
127                         $num_starred = db_fetch_result($result, 0, "num_starred");
128
129                         $class = "odd";
130
131                         if ($num_starred > 0) $class .= "Unread";
132
133                         printFeedEntry(-1, $class, "Starred articles", $num_starred, 
134                                 "images/mark_set.png");
135
136                         if (ENABLE_LABELS) {
137         
138                                 $result = db_query($link, "SELECT id,sql_exp,description FROM
139                                         ttrss_labels ORDER by description");
140                 
141                                 if (db_num_rows($result) > 0) {
142                                         print "<li><hr></li>";
143                                 }
144                 
145                                 while ($line = db_fetch_assoc($result)) {
146         
147                                         error_reporting (0);
148                 
149                                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
150                                                 WHERE (" . $line["sql_exp"] . ") AND unread = true");
151         
152                                         $count = db_fetch_result($tmp_result, 0, "count");
153         
154                                         $class = "odd";
155         
156                                         if ($count > 0) {
157                                                 $class .= "Unread";
158                                         }
159                                         
160                                         error_reporting (E_ERROR | E_WARNING | E_PARSE);
161         
162                                         printFeedEntry(-$line["id"]-11, 
163                                                 $class, $line["description"], $count, "images/label.png");
164                 
165                                 }
166                         }
167         
168                         print "<li><hr></li>";
169
170                         $result = db_query($link, "SELECT *,
171                                 (SELECT count(id) FROM ttrss_entries 
172                                         WHERE feed_id = ttrss_feeds.id) AS total,
173                                 (SELECT count(id) FROM ttrss_entries
174                                         WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
175                                 FROM ttrss_feeds ORDER BY title");                      
176         
177                         $actid = $_GET["actid"];
178         
179                         /* real feeds */
180         
181                         $lnum = 0;
182         
183                         $total_unread = 0;
184         
185                         while ($line = db_fetch_assoc($result)) {
186                         
187                                 $feed = $line["title"];
188                                 $feed_id = $line["id"];   
189         
190                                 $subop = $_GET["subop"];
191                                 
192                                 $total = $line["total"];
193                                 $unread = $line["unread"];
194                                 
195         //                      $class = ($lnum % 2) ? "even" : "odd";
196         
197                                 $class = "odd";
198         
199                                 if ($unread > 0) $class .= "Unread";
200         
201                                 if ($actid == $feed_id) {
202                                         $class .= "Selected";
203                                 }
204         
205                                 $total_unread += $unread;
206         
207                                 printFeedEntry($feed_id, $class, $feed, $unread, "icons/$feed_id.ico");
208         
209                                 ++$lnum;
210                         }
211                 } else {
212
213                         // tags
214
215                         $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
216                                 FROM ttrss_tags,ttrss_entries WHERE
217                                 post_id = ttrss_entries.id AND unread = true GROUP BY tag_name 
218                         UNION
219                                 select tag_name,0 as count FROM ttrss_tags");
220         
221                         $tags = array();
222         
223                         while ($line = db_fetch_assoc($result)) {
224                                 $tags[$line["tag_name"]] += $line["count"];
225                         }
226         
227                         foreach (array_keys($tags) as $tag) {
228         
229                                 $unread = $tags[$tag];
230         
231                                 $class = "odd";
232         
233                                 if ($unread > 0) {
234                                         $class .= "Unread";
235                                 }
236         
237                                 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png");
238         
239                         } 
240
241                 }
242
243                 print "</ul>";
244
245                 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
246
247         }
248
249
250         if ($op == "rpc") {
251
252                 $subop = $_GET["subop"];
253
254                 if ($subop == "getLabelCounters") {
255                         $aid = $_GET["aid"];            
256                         print "<rpc-reply>";
257                         getLabelCounters($link);
258                         if ($aid) {
259                                 getFeedCounter($link, $aid);
260                         }
261                         print "</rpc-reply>";
262                 }
263
264                 if ($subop == "getFeedCounters") {
265                         print "<rpc-reply>";
266                         getFeedCounters($link);
267                         print "</rpc-reply>";
268                 }
269
270                 if ($subop == "getAllCounters") {
271                         print "<rpc-reply>";
272                         getLabelCounters($link);
273                         getFeedCounters($link);
274                         getTagCounters($link);
275                         print "</rpc-reply>";
276
277                 }
278
279                 if ($subop == "mark") {
280                         $mark = $_GET["mark"];
281                         $id = db_escape_string($_GET["id"]);
282
283                         if ($mark == "1") {
284                                 $mark = "true";
285                         } else {
286                                 $mark = "false";
287                         }
288
289                         $result = db_query($link, "UPDATE ttrss_entries SET marked = $mark
290                                 WHERE id = '$id'");
291                 }
292
293                 if ($subop == "updateFeed") {
294                         $feed_id = db_escape_string($_GET["feed"]);
295
296                         $result = db_query($link, 
297                                 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'");
298
299                         if (db_num_rows($result) > 0) {                 
300                                 $feed_url = db_fetch_result($result, 0, "feed_url");
301 //                              update_rss_feed($link, $feed_url, $feed_id);
302                         }
303
304                         print "DONE-$feed_id";
305
306                         return;
307                 }
308
309                 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
310                         update_all_feeds($link, true);                  
311
312                         print "<rpc-reply>";
313                         getLabelCounters($link);
314                         getFeedCounters($link);
315                         getTagCounters($link);
316                         print "</rpc-reply>";
317                 }
318                 
319                 if ($subop == "catchupPage") {
320
321                         $ids = split(",", $_GET["ids"]);
322
323                         foreach ($ids as $id) {
324
325                                 db_query($link, "UPDATE ttrss_entries SET unread=false,last_read = NOW()
326                                         WHERE id = '$id'");
327
328                         }
329
330                         print "Marked active page as read.";
331                 }
332         }
333         
334         if ($op == "feeds") {
335
336                 $tags = $_GET["tags"];
337
338                 $subop = $_GET["subop"];
339
340                 if ($subop == "catchupAll") {
341                         db_query($link, "UPDATE ttrss_entries SET last_read = NOW(),unread = false");
342                 }
343
344                 outputFeedList($link, $tags);
345
346         }
347
348         if ($op == "view") {
349
350                 $id = $_GET["id"];
351                 $feed_id = $_GET["feed"];
352
353                 $result = db_query($link, "UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
354
355                 $addheader = $_GET["addheader"];
356
357                 $result = db_query($link, "SELECT title,link,content,feed_id,comments,
358                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url 
359                         FROM ttrss_entries
360                         WHERE   id = '$id'");
361
362                 if ($addheader) {
363                         print "<html><head>
364                                 <title>Tiny Tiny RSS : Article $id</title>
365                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
366                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
367                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
368                                 </head><body>";
369                 }
370
371                 if ($result) {
372
373                         $line = db_fetch_assoc($result);
374
375                         if ($line["icon_url"]) {
376                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
377                         } else {
378                                 $feed_icon = "&nbsp;";
379                         }
380
381                         if ($line["comments"] && $line["link"] != $line["comments"]) {
382                                 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
383                         } else {
384                                 $entry_comments = "";
385                         }
386
387                         print "<div class=\"postReply\">";
388
389                         print "<div class=\"postHeader\"><table>";
390
391                         print "<tr><td><b>Title:</b></td>
392                                 <td width='100%'>" . $line["title"] . "</td></tr>";
393                                 
394                         print "<tr><td><b>Link:</b></td>
395                                 <td width='100%'>
396                                 <a href=\"" . $line["link"] . "\">".$line["link"]."</a>
397                                 $entry_comments</td></tr>";
398                                         
399                         print "</table></div>";
400
401                         print "<div class=\"postIcon\">" . $feed_icon . "</div>";
402                         print "<div class=\"postContent\">" . $line["content"] . "</div>";
403                         
404                         print "</div>";
405
406                         print "<script type=\"text/javascript\">
407                                 update_label_counters('$feed_id');
408                         </script>";
409                 }
410
411                 if ($addheader) {
412                         print "</body></html>";
413                 }
414         }
415
416         if ($op == "viewfeed") {
417
418                 $feed = $_GET["feed"];
419                 $skip = $_GET["skip"];
420                 $subop = $_GET["subop"];
421                 $view_mode = $_GET["view"];
422                 $addheader = $_GET["addheader"];
423                 $limit = $_GET["limit"];
424
425                 if (!$feed) {
426                         print "Error: no feed to display.";
427                         return;
428                 }
429
430                 if (!$skip) $skip = 0;
431
432                 if ($subop == "undefined") $subop = "";
433
434                 if ($addheader) {
435                         print "<html><head>
436                                 <title>Tiny Tiny RSS : Feed $feed</title>
437                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
438                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
439                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
440                                 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
441                                 </head><body>";
442                 }
443
444                 if (sprintf("%d", $feed) != 0 && $feed >= 0) {
445
446                         $result = db_query($link, 
447                                 "SELECT *,SUBSTRING(last_updated,1,16) as last_updated_s
448                                 FROM ttrss_feeds WHERE id = '$feed'");
449                 
450                         if ($result) {
451         
452                                 $line = db_fetch_assoc($result);
453         
454                                 update_rss_feed($link, $line["feed_url"], $feed);
455                                         
456                                 if ($subop == "MarkAllRead")  {
457         
458                                         db_query($link, "UPDATE ttrss_entries SET unread = false,last_read = NOW() 
459                                                 WHERE feed_id = '$feed'");
460                                 }
461                         }
462                 }
463
464                 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
465
466                 $search = $_GET["search"];
467
468                 if ($search) {
469                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
470                                 OR content LIKE '%$search%') AND";
471                 } else {
472                         $search_query_part = "";
473                 }
474
475                 $view_query_part = "";
476
477                 if ($view_mode == "Starred") {
478                         $view_query_part = " marked = true AND ";
479                 }
480
481                 if ($view_mode == "Unread") {
482                         $view_query_part = " unread = true AND ";
483                 }
484
485 /*              $result = db_query($link, "SELECT count(id) AS total_entries 
486                         FROM ttrss_entries WHERE 
487                         $search_query_part
488                         feed_id = '$feed'");
489
490                 $total_entries = db_fetch_result($result, 0, "total_entries"); */
491
492 /*              $result = db_query("SELECT count(id) AS unread_entries 
493                         FROM ttrss_entries WHERE 
494                         $search_query_part
495                         unread = true AND
496                         feed_id = '$feed'");
497
498                 $unread_entries = db_fetch_result($result, 0, "unread_entries"); */
499
500                 if ($limit && $limit != "All") {
501                         $limit_query_part = "LIMIT " . $limit;
502                 } 
503
504                 $vfeed_query_part = "";
505
506                 if (sprintf("%d", $feed) == 0) {
507                         $query_strategy_part = "ttrss_entries.id > 0";
508                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
509                                 id = feed_id) as feed_title,";
510                 } else if ($feed >= 0) {
511                         $query_strategy_part = "feed_id = '$feed'";
512                 } else if ($feed == -1) { // starred virtual feed
513                         $query_strategy_part = "marked = true";
514                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
515                                 id = feed_id) as feed_title,";
516                 } else if ($feed <= -10) { // labels
517                         $label_id = -$feed - 11;
518
519                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
520                                 WHERE id = '$label_id'");
521                 
522                         $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
523         
524                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
525                                 id = feed_id) as feed_title,";
526                 } else {
527                         $query_strategy_part = "id > 0"; // dumb
528                 }
529
530                 $order_by = "updated DESC";
531
532 //              if ($feed < -10) {
533 //                      $order_by = "feed_id,updated DESC";
534 //              }
535
536                 if ($feed < -10) error_reporting (0);
537
538                 if (sprintf("%d", $feed) != 0) {
539
540                         $result = db_query($link, "SELECT 
541                                         id,title,updated,unread,feed_id,marked,link,last_read,
542                                         SUBSTRING(last_read,1,19) as last_read_noms,
543                                         $vfeed_query_part
544                                         SUBSTRING(updated,1,19) as updated_noms
545                                 FROM
546                                         ttrss_entries 
547                                 WHERE
548                                 $search_query_part
549                                 $view_query_part
550                                 $query_strategy_part ORDER BY $order_by
551                                 $limit_query_part");
552
553                 } else {
554                         // browsing by tag
555
556                         $result = db_query($link, "SELECT
557                                 ttrss_entries.id as id,title,updated,unread,feed_id,
558                                 marked,link,last_read,
559                                 SUBSTRING(last_read,1,19) as last_read_noms,
560                                 $vfeed_query_part
561                                 SUBSTRING(updated,1,19) as updated_noms
562                                 FROM
563                                         ttrss_entries,ttrss_tags
564                                 WHERE
565                                         post_id = ttrss_entries.id AND tag_name = '$feed' AND
566                                         $view_query_part
567                                         $search_query_part
568                                         $query_strategy_part ORDER BY $order_by
569                                 $limit_query_part");    
570                 }
571
572                 if (!$result) {
573                         print "<tr><td colspan='4' align='center'>
574                                 Could not display feed (query failed). Please check match syntax or local configuration.</td></tr>";
575                         return;
576                 }
577
578                 $lnum = 0;
579
580                 error_reporting (E_ERROR | E_WARNING | E_PARSE);
581
582                 $num_unread = 0;
583
584                 while ($line = db_fetch_assoc($result)) {
585
586                         $class = ($lnum % 2) ? "even" : "odd";
587
588                         $id = $line["id"];
589                         $feed_id = $line["feed_id"];
590
591 //                      printf("L %d (%s) &gt; U %d (%s) = %d<br>", 
592 //                              strtotime($line["last_read_noms"]), $line["last_read_noms"],
593 //                              strtotime($line["updated"]), $line["updated"],
594 //                              strtotime($line["last_read"]) >= strtotime($line["updated"]));
595
596                         if ($line["last_read"] != "" && $line["updated"] != "" &&
597                                 strtotime($line["last_read_noms"]) < strtotime($line["updated_noms"])) {
598
599                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
600                                         alt=\"Updated\">";
601
602                         } else {
603
604                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
605                                         alt=\"Updated\">";
606
607                         }
608
609                         if ($line["unread"] == "t" || $line["unread"] == "1") {
610                                 $class .= "Unread";
611                                 ++$num_unread;
612                         }
613
614                         if ($line["marked"] == "t" || $line["marked"] == "1") {
615                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\" 
616                                         alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
617                         } else {
618                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\" 
619                                         alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
620                         }
621
622                         $content_link = "<a id=\"FTITLE-$id\" href=\"javascript:view($id,$feed_id);\">" .
623                                 $line["title"] . "</a>";
624                                 
625                         print "<tr class='$class' id='RROW-$id'>";
626                         // onclick=\"javascript:view($id,$feed_id)\">
627
628                         print "<td valign='center' align='center'>$update_pic</td>";
629                         print "<td valign='center' align='center'>$marked_pic</td>";
630
631                         print "<td width='25%'>
632                                 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
633
634                         if ($line["feed_title"]) {                      
635                                 print "<td width='50%'>$content_link</td>";
636                                 print "<td width='20%'>".$line["feed_title"]."</td>";
637                         } else {
638                                 print "<td width='70%'>$content_link</td>";
639                         }
640
641                         print "</tr>";
642
643                         ++$lnum;
644                 }
645
646                 if ($lnum == 0) {
647                         print "<tr><td align='center'>No articles found.</td></tr>";
648                 }
649                 
650                 print "</table>";
651                 
652                 print "<script type=\"text/javascript\">
653                         document.onkeydown = hotkey_handler;
654                         update_label_counters('$feed');
655                 </script>";
656
657                 if ($addheader) {
658                         print "</body></html>";
659                 }
660
661         }
662
663         if ($op == "pref-rpc") {
664
665                 $subop = $_GET["subop"];
666
667                 if ($subop == "unread") {
668                         $ids = split(",", $_GET["ids"]);
669                         foreach ($ids as $id) {
670                                 db_query($link, "UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
671                         }
672
673                         print "Marked selected feeds as read.";
674                 }
675
676                 if ($subop == "read") {
677                         $ids = split(",", $_GET["ids"]);
678                         foreach ($ids as $id) {
679                                 db_query($link, "UPDATE ttrss_entries 
680                                         SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
681                         }
682
683                         print "Marked selected feeds as unread.";
684
685                 }
686
687         }
688
689         if ($op == "pref-feeds") {
690         
691                 $subop = $_GET["subop"];
692
693                 if ($subop == "editSave") {
694                         $feed_title = db_escape_string($_GET["t"]);
695                         $feed_link = db_escape_string($_GET["l"]);
696                         $feed_id = $_GET["id"];
697
698                         $result = db_query($link, "UPDATE ttrss_feeds SET 
699                                 title = '$feed_title', feed_url = '$feed_link' WHERE id = '$feed_id'");                 
700
701                 }
702
703                 if ($subop == "remove") {
704
705                         if (!WEB_DEMO_MODE) {
706
707                                 $ids = split(",", $_GET["ids"]);
708
709                                 foreach ($ids as $id) {
710                                         db_query($link, "DELETE FROM ttrss_feeds WHERE id = '$id'");
711                                         
712                                         if (file_exists(ICONS_DIR . "/$id.ico")) {
713                                                 unlink(ICONS_DIR . "/$id.ico");
714                                         }
715                                 }
716                         }
717                 }
718
719                 if ($subop == "add") {
720                 
721                         if (!WEB_DEMO_MODE) {
722
723                                 $feed_link = db_escape_string($_GET["link"]);
724                                         
725                                 $result = db_query($link,
726                                         "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
727
728                                 $result = db_query($link,
729                                         "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
730
731                                 $feed_id = db_fetch_result($result, 0, "id");
732
733                                 if ($feed_id) {
734                                         update_rss_feed($link, $feed_link, $feed_id);
735                                 }
736                         }
737                 }
738
739                 print "<table class=\"prefAddFeed\"><tr>
740                         <td><input id=\"fadd_link\"></td>
741                         <td colspan=\"4\" align=\"right\">
742                                 <a class=\"button\" href=\"javascript:addFeed()\">Add feed</a></td></tr>
743                 </table>";
744
745                 $result = db_query($link, "SELECT 
746                                 id,title,feed_url,substring(last_updated,1,16) as last_updated
747                         FROM 
748                                 ttrss_feeds ORDER by title");
749
750                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
751                 print "<tr class=\"title\">
752                                         <td>&nbsp;</td><td>Select</td><td width=\"40%\">Title</td>
753                                         <td width=\"40%\">Link</td><td>Last updated</td></tr>";
754                 
755                 $lnum = 0;
756                 
757                 while ($line = db_fetch_assoc($result)) {
758
759                         $class = ($lnum % 2) ? "even" : "odd";
760
761                         $feed_id = $line["id"];
762
763                         $edit_feed_id = $_GET["id"];
764
765                         if ($subop == "edit" && $feed_id != $edit_feed_id) {
766                                 $class .= "Grayed";
767                         }
768
769                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
770
771                         $icon_file = ICONS_DIR . "/$feed_id.ico";
772
773                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
774                                         $feed_icon = "<img width=\"16\" height=\"16\"
775                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
776                         } else {
777                                 $feed_icon = "&nbsp;";
778                         }
779                         print "<td align='center'>$feed_icon</td>";             
780
781                         if (!$edit_feed_id || $subop != "edit") {
782
783                                 print "<td><input onclick='toggleSelectRow(this);' 
784                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
785
786                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
787                                         $line["title"] . "</td>";               
788                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
789                                         $line["feed_url"] . "</td>";            
790                         
791
792                         } else if ($feed_id != $edit_feed_id) {
793
794                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
795                                         id=\"FRCHK-".$line["id"]."\"></td>";
796
797                                 print "<td>".$line["title"]."</td>";            
798                                 print "<td>".$line["feed_url"]."</td>";         
799
800                         } else {
801
802                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
803
804                                 print "<td><input id=\"iedit_title\" value=\"".$line["title"]."\"></td>";
805                                 print "<td><input id=\"iedit_link\" value=\"".$line["feed_url"]."\"></td>";
806                                                 
807                         }
808
809                         if (!$line["last_updated"]) $line["last_updated"] = "Never";
810
811                         print "<td>" . $line["last_updated"] . "</td>";
812                         
813                         print "</tr>";
814
815                         ++$lnum;
816                 }
817
818                 if ($lnum == 0) {
819                         print "<tr><td colspan=\"5\" align=\"center\">No feeds defined.</td></tr>";
820                 }
821
822                 print "</table>";
823
824                 print "<p>";
825
826                 if ($subop == "edit") {
827                         print "Edit feed:&nbsp;
828                                 <input type=\"submit\" class=\"button\" 
829                                         onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
830                                 <input type=\"submit\" class=\"button\" 
831                                         onclick=\"javascript:feedEditSave()\" value=\"Save\">";
832                         } else {
833
834                         print "
835                                 Selection:&nbsp;
836                         <input type=\"submit\" class=\"button\" 
837                                 onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
838                         <input type=\"submit\" class=\"button\" 
839                                 onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
840                                 
841                         if (ENABLE_PREFS_CATCHUP_UNCATCHUP) {
842                                 print "
843                                 <input type=\"submit\" class=\"button\" 
844                                         onclick=\"javascript:readSelectedFeeds()\" value=\"Mark as read\">
845                                 <input type=\"submit\" class=\"button\" 
846                                         onclick=\"javascript:unreadSelectedFeeds()\" value=\"Mark as unread\">&nbsp;";
847                         }
848                         print "
849                         All feeds: 
850                                 <input type=\"submit\" 
851                                         class=\"button\" onclick=\"gotoExportOpml()\" value=\"Export OPML\">";
852                 
853                         }
854
855         }
856
857         if ($op == "pref-filters") {
858
859                 $subop = $_GET["subop"];
860
861                 if ($subop == "editSave") {
862
863                         $regexp = db_escape_string($_GET["r"]);
864                         $descr = db_escape_string($_GET["d"]);
865                         $match = db_escape_string($_GET["m"]);
866                         $filter_id = db_escape_string($_GET["id"]);
867                         
868                         $result = db_query($link, "UPDATE ttrss_filters SET 
869                                 reg_exp = '$regexp', 
870                                 description = '$descr',
871                                 filter_type = (SELECT id FROM ttrss_filter_types WHERE
872                                         description = '$match')
873                                 WHERE id = '$filter_id'");
874                 }
875
876                 if ($subop == "remove") {
877
878                         if (!WEB_DEMO_MODE) {
879
880                                 $ids = split(",", $_GET["ids"]);
881
882                                 foreach ($ids as $id) {
883                                         db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
884                                         
885                                 }
886                         }
887                 }
888
889                 if ($subop == "add") {
890                 
891                         if (!WEB_DEMO_MODE) {
892
893                                 $regexp = db_escape_string($_GET["regexp"]);
894                                 $match = db_escape_string($_GET["match"]);
895                                         
896                                 $result = db_query($link,
897                                         "INSERT INTO ttrss_filters (reg_exp,filter_type) VALUES 
898                                                 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
899                                                         description = '$match'))");
900                         } 
901                 }
902
903                 $result = db_query($link, "SELECT description 
904                         FROM ttrss_filter_types ORDER BY description");
905
906                 $filter_types = array();
907
908                 while ($line = db_fetch_assoc($result)) {
909                         array_push($filter_types, $line["description"]);
910                 }
911
912                 print "<table class=\"prefAddFeed\"><tr>
913                         <td><input id=\"fadd_regexp\"></td>
914                         <td>";
915                         print_select("fadd_match", "Title", $filter_types);     
916         
917                 print"</td><td colspan=\"4\" align=\"right\">
918                                 <a class=\"button\" href=\"javascript:addFilter()\">Add filter</a></td></tr>
919                 </table>";
920
921                 $result = db_query($link, "SELECT 
922                                 id,reg_exp,description,
923                                 (SELECT name FROM ttrss_filter_types WHERE 
924                                         id = filter_type) as filter_type_name,
925                                 (SELECT description FROM ttrss_filter_types 
926                                         WHERE id = filter_type) as filter_type_descr
927                         FROM 
928                                 ttrss_filters ORDER by reg_exp");
929
930                 print "<p><table width=\"100%\" class=\"prefFilterList\" id=\"prefFilterList\">";
931
932                 print "<tr class=\"title\">
933                                         <td width=\"5%\">Select</td><td width=\"40%\">Filter expression</td>
934                                         <td width=\"40%\">Description</td><td width=\"10%\">Match</td></tr>";
935                 
936                 $lnum = 0;
937                 
938                 while ($line = db_fetch_assoc($result)) {
939
940                         $class = ($lnum % 2) ? "even" : "odd";
941
942                         $filter_id = $line["id"];
943                         $edit_filter_id = $_GET["id"];
944
945                         if ($subop == "edit" && $filter_id != $edit_filter_id) {
946                                 $class .= "Grayed";
947                         }
948
949                         print "<tr class=\"$class\" id=\"FILRR-$filter_id\">";
950
951                         $line["regexp"] = htmlspecialchars($line["reg_exp"]);
952                         $line["description"] = htmlspecialchars($line["description"]);
953
954                         if (!$edit_filter_id || $subop != "edit") {
955
956                                 if (!$line["description"]) $line["description"] = "[No description]";
957
958                                 print "<td><input onclick='toggleSelectRow(this);' 
959                                 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
960
961                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
962                                         $line["reg_exp"] . "</td>";             
963                                         
964                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
965                                         $line["description"] . "</td>";                 
966
967                                 print "<td>".$line["filter_type_descr"]."</td>";
968
969                         } else if ($filter_id != $edit_filter_id) {
970
971                                 if (!$line["description"]) $line["description"] = "[No description]";
972
973                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
974                                         id=\"FICHK-".$line["id"]."\"></td>";
975
976                                 print "<td>".$line["reg_exp"]."</td>";          
977                                 print "<td>".$line["description"]."</td>";              
978                                 print "<td>".$line["filter_type_descr"]."</td>";
979
980                         } else {
981
982                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
983
984                                 print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
985                                         "\"></td>";
986
987                                 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
988                                         "\"></td>";
989
990                                 print "<td>";
991                                 print_select("iedit_match", $line["filter_type_descr"], $filter_types);
992                                 print "</td>";
993                                                 
994                         }
995                                 
996                         
997                         print "</tr>";
998
999                         ++$lnum;
1000                 }
1001
1002                 if ($lnum == 0) {
1003                         print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
1004                 }
1005
1006                 print "</table>";
1007
1008                 print "<p>";
1009
1010                 if ($subop == "edit") {
1011                         print "Edit feed:
1012                                 <input type=\"submit\" class=\"button\" 
1013                                         onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">
1014                                 <input type=\"submit\" class=\"button\" 
1015                                         onclick=\"javascript:filterEditSave()\" value=\"Save\">";
1016                                         
1017                 } else {
1018
1019                         print "
1020                                 Selection:
1021                         <input type=\"submit\" class=\"button\" 
1022                                 onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
1023                         <input type=\"submit\" class=\"button\" 
1024                                 onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
1025                 }
1026         }
1027
1028         if ($op == "pref-labels") {
1029
1030                 $subop = $_GET["subop"];
1031
1032                 if ($subop == "editSave") {
1033
1034                         $sql_exp = $_GET["s"];
1035                         $descr = $_GET["d"];
1036                         $label_id = db_escape_string($_GET["id"]);
1037                         
1038 //                      print "$sql_exp : $descr : $label_id";
1039                         
1040                         $result = db_query($link, "UPDATE ttrss_labels SET 
1041                                 sql_exp = '$sql_exp', 
1042                                 description = '$descr'
1043                                 WHERE id = '$label_id'");
1044                 }
1045
1046                 if ($subop == "remove") {
1047
1048                         if (!WEB_DEMO_MODE) {
1049
1050                                 $ids = split(",", $_GET["ids"]);
1051
1052                                 foreach ($ids as $id) {
1053                                         db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
1054                                         
1055                                 }
1056                         }
1057                 }
1058
1059                 if ($subop == "add") {
1060                 
1061                         if (!WEB_DEMO_MODE) {
1062
1063                                 $exp = $_GET["exp"];
1064                                         
1065                                 $result = db_query($link,
1066                                         "INSERT INTO ttrss_labels (sql_exp,description) 
1067                                                 VALUES ('$exp', '$exp')");
1068                         } 
1069                 }
1070
1071                 print "<table class=\"prefAddFeed\"><tr>
1072                         <td><input id=\"ladd_expr\"></td>";
1073                         
1074                 print"<td colspan=\"4\" align=\"right\">
1075                                 <a class=\"button\" href=\"javascript:addLabel()\">Add label</a></td></tr>
1076                 </table>";
1077
1078                 $result = db_query($link, "SELECT 
1079                                 id,sql_exp,description
1080                         FROM 
1081                                 ttrss_labels ORDER by description");
1082
1083                 print "<p><table width=\"100%\" class=\"prefLabelList\" id=\"prefLabelList\">";
1084
1085                 print "<tr class=\"title\">
1086                                         <td width=\"5%\">Select</td><td width=\"40%\">SQL expression</td>
1087                                         <td width=\"40%\">Caption</td></tr>";
1088                 
1089                 $lnum = 0;
1090                 
1091                 while ($line = db_fetch_assoc($result)) {
1092
1093                         $class = ($lnum % 2) ? "even" : "odd";
1094
1095                         $label_id = $line["id"];
1096                         $edit_label_id = $_GET["id"];
1097
1098                         if ($subop == "edit" && $label_id != $edit_label_id) {
1099                                 $class .= "Grayed";
1100                         }
1101
1102                         print "<tr class=\"$class\" id=\"LILRR-$label_id\">";
1103
1104                         $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
1105                         $line["description"] = htmlspecialchars($line["description"]);
1106
1107                         if (!$edit_label_id || $subop != "edit") {
1108
1109                                 if (!$line["description"]) $line["description"] = "[No caption]";
1110
1111                                 print "<td><input onclick='toggleSelectRow(this);' 
1112                                 type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
1113
1114                                 print "<td><a href=\"javascript:editLabel($label_id);\">" . 
1115                                         $line["sql_exp"] . "</td>";             
1116                                         
1117                                 print "<td><a href=\"javascript:editLabel($label_id);\">" . 
1118                                         $line["description"] . "</td>";                 
1119
1120                         } else if ($label_id != $edit_label_id) {
1121
1122                                 if (!$line["description"]) $line["description"] = "[No description]";
1123
1124                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
1125                                         id=\"LICHK-".$line["id"]."\"></td>";
1126
1127                                 print "<td>".$line["sql_exp"]."</td>";          
1128                                 print "<td>".$line["description"]."</td>";              
1129
1130                         } else {
1131
1132                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
1133
1134                                 print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
1135                                         "\"></td>";
1136
1137                                 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1138                                         "\"></td>";
1139                                                 
1140                         }
1141                                 
1142                         
1143                         print "</tr>";
1144
1145                         ++$lnum;
1146                 }
1147
1148                 if ($lnum == 0) {
1149                         print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
1150                 }
1151
1152                 print "</table>";
1153
1154                 print "<p>";
1155
1156                 if ($subop == "edit") {
1157                         print "Edit label:
1158                                 <input type=\"submit\" class=\"button\" 
1159                                         onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">
1160                                 <input type=\"submit\" class=\"button\" 
1161                                         onclick=\"javascript:labelEditSave()\" value=\"Save\">";
1162                                         
1163                 } else {
1164
1165                         print "
1166                                 Selection:
1167                         <input type=\"submit\" class=\"button\" 
1168                                 onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
1169                         <input type=\"submit\" class=\"button\" 
1170                                 onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
1171                 }
1172         }
1173
1174         if ($op == "error") {
1175                 print "<div width=\"100%\" align='center'>";
1176                 $msg = $_GET["msg"];
1177                 print $msg;
1178                 print "</div>";
1179         }
1180
1181         db_close($link);
1182 ?>