]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
add db-prefs.php
[tt-rss.git] / backend.php
1 <?
2         define(SCHEMA_VERSION, 2);
3
4         $op = $_GET["op"];
5
6         if ($op == "rpc" || $op == "updateAllFeeds") {
7                 header("Content-Type: application/xml");
8         }
9
10         require_once "config.php";
11         require_once "db.php";
12         require_once "db-prefs.php";
13         require_once "functions.php";
14         require_once "magpierss/rss_fetch.inc";
15
16         $script_started = getmicrotime();
17
18         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
19         $dbprefs_link = $link;
20
21         if (!$link) {
22                 if (DB_TYPE == "mysql") {
23                         print mysql_error();
24                 }
25                 // PG seems to display its own errors just fine by default.             
26                 return;
27         }
28
29         if (DB_TYPE == "pgsql") {
30                 pg_query("set client_encoding = 'utf-8'");
31         }
32
33 /*
34         $result = db_query($link, "SELECT schema_version FROM ttrss_version");
35
36         $schema_version = db_fetch_result($result, 0, "schema_version");
37
38         if ($schema_version != SCHEMA_VERSION) {
39                 print "Error: database schema is invalid
40                         (got version $schema_version; expected ".SCHEMA_VERSION.")";
41                 return;
42         }
43 */
44
45         $fetch = $_GET["fetch"];
46
47         /* FIXME this needs reworking */
48
49         function getGlobalCounters($link) {
50                 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries
51                         WHERE unread = true");
52                 $c_id = db_fetch_result($result, 0, "c_id");
53                 print "<counter id='global-unread' counter='$c_id'/>";
54         }
55
56         function getTagCounters($link) {
57                 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
58                         FROM ttrss_tags,ttrss_entries WHERE
59                         post_id = ttrss_entries.id AND unread = true GROUP BY tag_name 
60                 UNION
61                         select tag_name,0 as count FROM ttrss_tags");
62
63                 $tags = array();
64
65                 while ($line = db_fetch_assoc($result)) {
66                         $tags[$line["tag_name"]] += $line["count"];
67                 }
68
69                 foreach (array_keys($tags) as $tag) {
70                         $unread = $tags[$tag];                  
71
72                         $tag = htmlspecialchars($tag);
73                         print "<tag id=\"$tag\" counter=\"$unread\"/>";
74                 } 
75         }
76
77         function getLabelCounters($link) {
78
79                 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
80                         WHERE marked = true AND unread = true");
81
82                 $count = db_fetch_result($result, 0, "count");
83
84                 print "<label id=\"-1\" counter=\"$count\"/>";
85
86                 $result = db_query($link, "SELECT id,sql_exp,description FROM
87                         ttrss_labels ORDER by description");
88         
89                 while ($line = db_fetch_assoc($result)) {
90
91                         $id = -$line["id"] - 11;
92
93                         error_reporting (0);
94
95                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
96                                 WHERE (" . $line["sql_exp"] . ") AND unread = true");
97
98                         $count = db_fetch_result($tmp_result, 0, "count");
99
100                         print "<label id=\"$id\" counter=\"$count\"/>";
101
102                         error_reporting (E_ERROR | E_WARNING | E_PARSE);
103         
104                 }
105         }
106
107         function getFeedCounter($link, $id) {
108         
109                 $result = db_query($link, "SELECT 
110                                 count(id) as count FROM ttrss_entries
111                         WHERE feed_id = '$id'   AND unread = true");
112         
113                         $count = db_fetch_result($result, 0, "count");
114                         
115                         print "<feed id=\"$id\" counter=\"$count\"/>";          
116         }
117
118         function getFeedCounters($link) {
119         
120                 $result = db_query($link, "SELECT id,
121                         (SELECT count(id) FROM ttrss_entries WHERE feed_id = ttrss_feeds.id 
122                                 AND unread = true) as count
123                         FROM ttrss_feeds");
124         
125                 while ($line = db_fetch_assoc($result)) {
126                 
127                         $id = $line["id"];
128                         $count = $line["count"];
129
130                         print "<feed id=\"$id\" counter=\"$count\"/>";
131                 }
132         }
133
134         function outputFeedList($link, $tags = false) {
135
136                 print "<html><head>
137                         <title>Tiny Tiny RSS : Feedlist</title>
138                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
139
140                 if (USE_COMPACT_STYLESHEET) {
141                         print "<link rel=\"stylesheet\" type=\"text/css\" 
142                                 href=\"tt-rss_compact.css\"/>";
143                 } else {
144                         print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
145                                         type=\"text/css\" href=\"tt-rss_compact.css\"/>";
146                 }
147
148                 print "<script type=\"text/javascript\" src=\"functions.js\"></script>
149                         <script type=\"text/javascript\" src=\"feedlist.js\"></script>
150                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
151                         </head><body onload=\"init()\">";
152
153                 print "<ul class=\"feedList\" id=\"feedList\">";
154
155                 if (!$tags) {
156
157                         /* virtual feeds */
158
159                         $result = db_query($link, "SELECT count(id) as num_starred 
160                                 FROM ttrss_entries WHERE marked = true AND unread = true");
161                         $num_starred = db_fetch_result($result, 0, "num_starred");
162
163                         $class = "virt";
164
165                         if ($num_starred > 0) $class .= "Unread";
166
167                         printFeedEntry(-1, $class, "Starred articles", $num_starred, 
168                                 "images/mark_set.png");
169
170                         if (ENABLE_LABELS) {
171         
172                                 $result = db_query($link, "SELECT id,sql_exp,description FROM
173                                         ttrss_labels ORDER by description");
174                 
175                                 if (db_num_rows($result) > 0) {
176                                         print "<li><hr></li>";
177                                 }
178                 
179                                 while ($line = db_fetch_assoc($result)) {
180         
181                                         error_reporting (0);
182                 
183                                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
184                                                 WHERE (" . $line["sql_exp"] . ") AND unread = true");
185         
186                                         $count = db_fetch_result($tmp_result, 0, "count");
187         
188                                         $class = "label";
189         
190                                         if ($count > 0) {
191                                                 $class .= "Unread";
192                                         }
193                                         
194                                         error_reporting (E_ERROR | E_WARNING | E_PARSE);
195         
196                                         printFeedEntry(-$line["id"]-11, 
197                                                 $class, $line["description"], $count, "images/label.png");
198                 
199                                 }
200                         }
201         
202                         print "<li><hr></li>";
203
204                         $result = db_query($link, "SELECT *,
205                                 (SELECT count(id) FROM ttrss_entries 
206                                         WHERE feed_id = ttrss_feeds.id) AS total,
207                                 (SELECT count(id) FROM ttrss_entries
208                                         WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
209                                 FROM ttrss_feeds ORDER BY title");                      
210         
211                         $actid = $_GET["actid"];
212         
213                         /* real feeds */
214         
215                         $lnum = 0;
216         
217                         $total_unread = 0;
218         
219                         while ($line = db_fetch_assoc($result)) {
220                         
221                                 $feed = $line["title"];
222                                 $feed_id = $line["id"];   
223         
224                                 $subop = $_GET["subop"];
225                                 
226                                 $total = $line["total"];
227                                 $unread = $line["unread"];
228                                 
229         //                      $class = ($lnum % 2) ? "even" : "odd";
230         
231                                 $class = "feed";
232         
233                                 if ($unread > 0) $class .= "Unread";
234         
235                                 if ($actid == $feed_id) {
236                                         $class .= "Selected";
237                                 }
238         
239                                 $total_unread += $unread;
240         
241                                 printFeedEntry($feed_id, $class, $feed, $unread, "icons/$feed_id.ico");
242         
243                                 ++$lnum;
244                         }
245                 } else {
246
247                         // tags
248
249                         $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
250                                 FROM ttrss_tags,ttrss_entries WHERE
251                                 post_id = ttrss_entries.id AND unread = true GROUP BY tag_name 
252                         UNION
253                                 select tag_name,0 as count FROM ttrss_tags");
254         
255                         $tags = array();
256         
257                         while ($line = db_fetch_assoc($result)) {
258                                 $tags[$line["tag_name"]] += $line["count"];
259                         }
260         
261                         foreach (array_keys($tags) as $tag) {
262         
263                                 $unread = $tags[$tag];
264         
265                                 $class = "odd";
266         
267                                 if ($unread > 0) {
268                                         $class .= "Unread";
269                                 }
270         
271                                 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png");
272         
273                         } 
274
275                 }
276
277                 if (db_num_rows($result) == 0) {
278                         print "<li>No tags to display.</li>";
279                 }
280
281                 print "</ul>";
282
283                 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
284
285         }
286
287
288         if ($op == "rpc") {
289
290                 $subop = $_GET["subop"];
291
292                 if ($subop == "getLabelCounters") {
293                         $aid = $_GET["aid"];            
294                         print "<rpc-reply>";
295                         getLabelCounters($link);
296                         if ($aid) {
297                                 getFeedCounter($link, $aid);
298                         }
299                         print "</rpc-reply>";
300                 }
301
302                 if ($subop == "getFeedCounters") {
303                         print "<rpc-reply>";
304                         getFeedCounters($link);
305                         print "</rpc-reply>";
306                 }
307
308                 if ($subop == "getAllCounters") {
309                         print "<rpc-reply>";
310                         getLabelCounters($link);
311                         getFeedCounters($link);
312                         getTagCounters($link);
313                         getGlobalCounters($link);
314                         print "</rpc-reply>";
315                 }
316
317                 if ($subop == "mark") {
318                         $mark = $_GET["mark"];
319                         $id = db_escape_string($_GET["id"]);
320
321                         if ($mark == "1") {
322                                 $mark = "true";
323                         } else {
324                                 $mark = "false";
325                         }
326
327                         $result = db_query($link, "UPDATE ttrss_entries SET marked = $mark
328                                 WHERE id = '$id'");
329                 }
330
331                 if ($subop == "updateFeed") {
332                         $feed_id = db_escape_string($_GET["feed"]);
333
334                         $result = db_query($link, 
335                                 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'");
336
337                         if (db_num_rows($result) > 0) {                 
338                                 $feed_url = db_fetch_result($result, 0, "feed_url");
339 //                              update_rss_feed($link, $feed_url, $feed_id);
340                         }
341
342                         print "DONE-$feed_id";
343
344                         return;
345                 }
346
347                 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
348                 
349                         update_all_feeds($link, true);                  
350
351                         $omode = $_GET["omode"];
352
353                         if (!$omode) $omode = "tfl";
354
355                         print "<rpc-reply>";
356                         if (strchr($omode, "l")) getLabelCounters($link);
357                         if (strchr($omode, "f")) getFeedCounters($link);
358                         if (strchr($omode, "t")) getTagCounters($link);
359                         getGlobalCounters($link);
360                         print "</rpc-reply>";
361                 }
362                 
363                 if ($subop == "catchupPage") {
364
365                         $ids = split(",", $_GET["ids"]);
366
367                         foreach ($ids as $id) {
368
369                                 db_query($link, "UPDATE ttrss_entries SET unread=false,last_read = NOW()
370                                         WHERE id = '$id'");
371
372                         }
373
374                         print "Marked active page as read.";
375                 }
376
377                 if ($subop == "sanityCheck") {
378
379                         $error_code = 0;
380
381                         $result = db_query($link, "SELECT schema_version FROM ttrss_version");
382
383                         $schema_version = db_fetch_result($result, 0, "schema_version");
384
385                         if ($schema_version != SCHEMA_VERSION) {
386                                 $error_code = 5;
387                         }
388
389                         print "<error code='$error_code'/>";
390                 }
391         }
392         
393         if ($op == "feeds") {
394
395                 $tags = $_GET["tags"];
396
397                 $subop = $_GET["subop"];
398
399                 if ($subop == "catchupAll") {
400                         db_query($link, "UPDATE ttrss_entries SET last_read = NOW(),unread = false");
401                 }
402
403                 outputFeedList($link, $tags);
404
405         }
406
407         if ($op == "view") {
408
409                 $id = $_GET["id"];
410                 $feed_id = $_GET["feed"];
411
412                 $result = db_query($link, "UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
413
414                 $addheader = $_GET["addheader"];
415
416                 $result = db_query($link, "SELECT title,link,content,feed_id,comments,
417                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url 
418                         FROM ttrss_entries
419                         WHERE   id = '$id'");
420
421                 if ($addheader) {
422                         print "<html><head>
423                                 <title>Tiny Tiny RSS : Article $id</title>
424                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
425                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
426                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
427                                 </head><body>";
428                 }
429
430                 if ($result) {
431
432                         $line = db_fetch_assoc($result);
433
434                         if ($line["icon_url"]) {
435                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
436                         } else {
437                                 $feed_icon = "&nbsp;";
438                         }
439
440                         if ($line["comments"] && $line["link"] != $line["comments"]) {
441                                 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
442                         } else {
443                                 $entry_comments = "";
444                         }
445
446                         print "<div class=\"postReply\">";
447
448                         print "<div class=\"postHeader\"><table>";
449
450                         print "<tr><td><b>Title:</b></td>
451                                 <td width='100%'>" . $line["title"] . "</td></tr>";
452                                 
453                         print "<tr><td><b>Link:</b></td>
454                                 <td width='100%'>
455                                 <a href=\"" . $line["link"] . "\">".$line["link"]."</a>
456                                 $entry_comments</td></tr>";
457                                         
458                         print "</table></div>";
459
460                         print "<div class=\"postIcon\">" . $feed_icon . "</div>";
461                         print "<div class=\"postContent\">" . $line["content"] . "</div>";
462                         
463                         print "</div>";
464
465                         print "<script type=\"text/javascript\">
466                                 update_label_counters('$feed_id');
467                         </script>";
468                 }
469
470                 if ($addheader) {
471                         print "</body></html>";
472                 }
473         }
474
475         if ($op == "viewfeed") {
476
477                 $feed = $_GET["feed"];
478                 $skip = $_GET["skip"];
479                 $subop = $_GET["subop"];
480                 $view_mode = $_GET["view"];
481                 $addheader = $_GET["addheader"];
482                 $limit = $_GET["limit"];
483
484                 if (!$feed) {
485                         print "Error: no feed to display.";
486                         return;
487                 }
488
489                 if (!$skip) $skip = 0;
490
491                 if ($subop == "undefined") $subop = "";
492
493                 if ($addheader) {
494                         print "<html><head>
495                                 <title>Tiny Tiny RSS : Feed $feed</title>
496                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
497
498                         if (USE_COMPACT_STYLESHEET) {
499                                 print "<link rel=\"stylesheet\" 
500                                                 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
501
502                         } else {
503                                 print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
504                                                 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
505                         }
506                         print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">  
507                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
508                                 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
509                                 </head><body onload='init()'>";
510                 }
511
512                 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
513
514                         $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
515                                 WHERE id = '$feed'");
516
517                         $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
518
519                         update_rss_feed($link, $feed_url, $feed);
520
521                 }
522
523                 if ($subop == "MarkAllRead")  {
524
525                         if (sprintf("%d", $feed) != 0) {
526                         
527                                 if ($feed > 0) {
528                                         db_query($link, "UPDATE ttrss_entries 
529                                                 SET unread = false,last_read = NOW() 
530                                                 WHERE feed_id = '$feed'");
531                                                 
532                                 } else if ($feed < 0 && $feed > -10) { // special, like starred
533
534                                         if ($feed == -1) {
535                                                 db_query($link, "UPDATE ttrss_entries 
536                                                         SET unread = false,last_read = NOW()
537                                                         WHERE marked = true");
538                                         }
539                         
540                                 } else if ($feed < -10) { // label
541
542                                         $label_id = -$feed - 11;
543
544                                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
545                                                 WHERE id = '$label_id'");
546
547                                         if ($tmp_result) {
548                                                 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
549
550                                                 db_query($link, "UPDATE ttrss_entries 
551                                                         SET unread = false,last_read = NOW()
552                                                         WHERE $sql_exp");
553                                         }
554                                 }
555                         } else { // tag
556                                 // FIXME, implement catchup for tags
557                         }
558
559                 }
560
561                 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
562
563                 $search = $_GET["search"];
564
565                 $search_mode = $_GET["smode"];
566
567                 if ($search) {
568                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
569                                 OR content LIKE '%$search%') AND";
570                 } else {
571                         $search_query_part = "";
572                 }
573
574                 $view_query_part = "";
575
576                 if ($view_mode == "Starred") {
577                         $view_query_part = " marked = true AND ";
578                 }
579
580                 if ($view_mode == "Unread") {
581                         $view_query_part = " unread = true AND ";
582                 }
583
584                 if ($view_mode == "Unread or Starred") {
585                         $view_query_part = " (unread = true OR marked = true) AND ";
586                 }
587
588                 if ($view_mode == "Unread or Updated") {
589                         $view_query_part = " (unread = true OR last_read is NULL) AND ";
590                 }
591
592 /*              $result = db_query($link, "SELECT count(id) AS total_entries 
593                         FROM ttrss_entries WHERE 
594                         $search_query_part
595                         feed_id = '$feed'");
596
597                 $total_entries = db_fetch_result($result, 0, "total_entries"); */
598
599 /*              $result = db_query("SELECT count(id) AS unread_entries 
600                         FROM ttrss_entries WHERE 
601                         $search_query_part
602                         unread = true AND
603                         feed_id = '$feed'");
604
605                 $unread_entries = db_fetch_result($result, 0, "unread_entries"); */
606
607                 if ($limit && $limit != "All") {
608                         $limit_query_part = "LIMIT " . $limit;
609                 } 
610
611                 $vfeed_query_part = "";
612
613                 // override query strategy and enable feed display when searching globally
614                 if ($search_mode == "All feeds") {
615                         $query_strategy_part = "id > 0";
616                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
617                                 id = feed_id) as feed_title,";
618                 } else if (sprintf("%d", $feed) == 0) {
619                         $query_strategy_part = "ttrss_entries.id > 0";
620                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
621                                 id = feed_id) as feed_title,";
622                 } else if ($feed >= 0) {
623                         $query_strategy_part = "feed_id = '$feed'";
624                 } else if ($feed == -1) { // starred virtual feed
625                         $query_strategy_part = "marked = true";
626                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
627                                 id = feed_id) as feed_title,";
628                 } else if ($feed <= -10) { // labels
629                         $label_id = -$feed - 11;
630
631                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
632                                 WHERE id = '$label_id'");
633                 
634                         $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
635         
636                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
637                                 id = feed_id) as feed_title,";
638                 } else {
639                         $query_strategy_part = "id > 0"; // dumb
640                 }
641
642
643                 $order_by = "updated DESC";
644
645 //              if ($feed < -10) {
646 //                      $order_by = "feed_id,updated DESC";
647 //              }
648
649                 if ($feed < -10) error_reporting (0);
650
651                 if (sprintf("%d", $feed) != 0) {
652
653                         $result = db_query($link, "SELECT 
654                                         id,title,updated,unread,feed_id,marked,link,last_read,
655                                         SUBSTRING(last_read,1,19) as last_read_noms,
656                                         $vfeed_query_part
657                                         SUBSTRING(updated,1,19) as updated_noms
658                                 FROM
659                                         ttrss_entries 
660                                 WHERE
661                                 $search_query_part
662                                 $view_query_part
663                                 $query_strategy_part ORDER BY $order_by
664                                 $limit_query_part");
665
666                 } else {
667                         // browsing by tag
668
669                         $result = db_query($link, "SELECT
670                                 ttrss_entries.id as id,title,updated,unread,feed_id,
671                                 marked,link,last_read,
672                                 SUBSTRING(last_read,1,19) as last_read_noms,
673                                 $vfeed_query_part
674                                 SUBSTRING(updated,1,19) as updated_noms
675                                 FROM
676                                         ttrss_entries,ttrss_tags
677                                 WHERE
678                                         post_id = ttrss_entries.id AND tag_name = '$feed' AND
679                                         $view_query_part
680                                         $search_query_part
681                                         $query_strategy_part ORDER BY $order_by
682                                 $limit_query_part");    
683                 }
684
685                 if (!$result) {
686                         print "<tr><td colspan='4' align='center'>
687                                 Could not display feed (query failed). Please check match syntax or local configuration.</td></tr>";
688                         return;
689                 }
690
691                 $lnum = 0;
692
693                 error_reporting (E_ERROR | E_WARNING | E_PARSE);
694
695                 $num_unread = 0;
696
697                 while ($line = db_fetch_assoc($result)) {
698
699                         $class = ($lnum % 2) ? "even" : "odd";
700
701                         $id = $line["id"];
702                         $feed_id = $line["feed_id"];
703
704 //                      printf("L %d (%s) &gt; U %d (%s) = %d<br>", 
705 //                              strtotime($line["last_read_noms"]), $line["last_read_noms"],
706 //                              strtotime($line["updated"]), $line["updated"],
707 //                              strtotime($line["last_read"]) >= strtotime($line["updated"]));
708
709 /*                      if ($line["last_read"] != "" && $line["updated"] != "" &&
710                                 strtotime($line["last_read_noms"]) < strtotime($line["updated_noms"])) {
711
712                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
713                                         alt=\"Updated\">";
714
715                         } else {
716
717                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
718                                         alt=\"Updated\">";
719
720                         } */
721
722                         if ($line["last_read"] == "" && 
723                                         ($line["unread"] != "t" && $line["unread"] != "1")) {
724
725                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
726                                         alt=\"Updated\">";
727                         } else {
728                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
729                                         alt=\"Updated\">";
730                         }
731
732                         if ($line["unread"] == "t" || $line["unread"] == "1") {
733                                 $class .= "Unread";
734                                 ++$num_unread;
735                         }
736
737                         if ($line["marked"] == "t" || $line["marked"] == "1") {
738                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\" 
739                                         alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
740                         } else {
741                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\" 
742                                         alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
743                         }
744
745                         $content_link = "<a id=\"FTITLE-$id\" href=\"javascript:view($id,$feed_id);\">" .
746                                 $line["title"] . "</a>";
747                                 
748                         print "<tr class='$class' id='RROW-$id'>";
749                         // onclick=\"javascript:view($id,$feed_id)\">
750
751                         print "<td valign='center' align='center'>$update_pic</td>";
752                         print "<td valign='center' align='center'>$marked_pic</td>";
753
754                         print "<td width='25%'>
755                                 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
756
757                         if ($line["feed_title"]) {                      
758                                 print "<td width='50%'>$content_link</td>";
759                                 print "<td width='20%'>
760                                         <a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a></td>";
761                         } else {
762                                 print "<td width='70%'>$content_link</td>";
763                         }
764
765                         print "</tr>";
766
767                         ++$lnum;
768                 }
769
770                 if ($lnum == 0) {
771                         print "<tr><td align='center'>No articles found.</td></tr>";
772                 }
773                 
774                 print "</table>";
775                 
776                 print "<script type=\"text/javascript\">
777                         document.onkeydown = hotkey_handler;
778                         update_label_counters('$feed');
779                 </script>";
780
781                 if ($addheader) {
782                         print "</body></html>";
783                 }
784
785         }
786
787         if ($op == "pref-rpc") {
788
789                 $subop = $_GET["subop"];
790
791                 if ($subop == "unread") {
792                         $ids = split(",", $_GET["ids"]);
793                         foreach ($ids as $id) {
794                                 db_query($link, "UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
795                         }
796
797                         print "Marked selected feeds as read.";
798                 }
799
800                 if ($subop == "read") {
801                         $ids = split(",", $_GET["ids"]);
802                         foreach ($ids as $id) {
803                                 db_query($link, "UPDATE ttrss_entries 
804                                         SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
805                         }
806
807                         print "Marked selected feeds as unread.";
808
809                 }
810
811         }
812
813         if ($op == "pref-feeds") {
814         
815                 $subop = $_GET["subop"];
816
817                 if ($subop == "editSave") {
818                         $feed_title = db_escape_string($_GET["t"]);
819                         $feed_link = db_escape_string($_GET["l"]);
820                         $upd_intl = db_escape_string($_GET["ui"]);
821                         $purge_intl = db_escape_string($_GET["pi"]);
822                         $feed_id = $_GET["id"];
823
824                         if (strtoupper($upd_intl) == "DEFAULT")
825                                 $upd_intl = 0;
826
827                         if (strtoupper($purge_intl) == "DEFAULT")
828                                 $purge_intl = 0;
829
830                         if (strtoupper($purge_intl) == "DISABLED")
831                                 $purge_intl = -1;
832
833                         $result = db_query($link, "UPDATE ttrss_feeds SET 
834                                 title = '$feed_title', feed_url = '$feed_link',
835                                 update_interval = '$upd_intl',
836                                 purge_interval = '$purge_intl' 
837                                 WHERE id = '$feed_id'");                        
838
839                 }
840
841                 if ($subop == "remove") {
842
843                         if (!WEB_DEMO_MODE) {
844
845                                 $ids = split(",", $_GET["ids"]);
846
847                                 foreach ($ids as $id) {
848                                         db_query($link, "DELETE FROM ttrss_feeds WHERE id = '$id'");
849                                         
850                                         if (file_exists(ICONS_DIR . "/$id.ico")) {
851                                                 unlink(ICONS_DIR . "/$id.ico");
852                                         }
853                                 }
854                         }
855                 }
856
857                 if ($subop == "add") {
858                 
859                         if (!WEB_DEMO_MODE) {
860
861                                 $feed_link = db_escape_string($_GET["link"]);
862                                         
863                                 $result = db_query($link,
864                                         "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
865
866                                 $result = db_query($link,
867                                         "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
868
869                                 $feed_id = db_fetch_result($result, 0, "id");
870
871                                 if ($feed_id) {
872                                         update_rss_feed($link, $feed_link, $feed_id);
873                                 }
874                         }
875                 }
876
877                 $result = db_query($link, "SELECT id,title,feed_url,last_error 
878                         FROM ttrss_feeds WHERE last_error != ''");
879
880                 if (db_num_rows($result) > 0) {
881                 
882                         print "<div class=\"warning\">";
883                 
884                         print "<b>Feeds with update errors:</b>";
885
886                         print "<ul class=\"nomarks\">";
887                                                 
888                         while ($line = db_fetch_assoc($result)) {
889                                 print "<li>" . $line["title"] . " (" . $line["feed_url"] . "): " . 
890                                         $line["last_error"];
891                         }
892
893                         print "</ul>";
894                         print "</div>";
895
896                 }
897
898                 print "<table class=\"prefAddFeed\"><tr>
899                         <td><input id=\"fadd_link\"></td>
900                         <td colspan=\"4\" align=\"right\">
901                                 <a class=\"button\" href=\"javascript:addFeed()\">Add feed</a></td></tr>
902                 </table>";
903
904                 $result = db_query($link, "SELECT 
905                                 id,title,feed_url,substring(last_updated,1,16) as last_updated,
906                                 update_interval,purge_interval
907                         FROM 
908                                 ttrss_feeds ORDER by title");
909
910                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
911                 print "<tr class=\"title\">
912                                         <td>&nbsp;</td><td>Select</td><td width=\"30%\">Title</td>
913                                         <td width=\"30%\">Link</td>
914                                         <td width=\"10%\">Update Interval</td>
915                                         <td width=\"10%\">Purge Days</td>
916                                         <td>Last updated</td></tr>";
917                 
918                 $lnum = 0;
919                 
920                 while ($line = db_fetch_assoc($result)) {
921
922                         $class = ($lnum % 2) ? "even" : "odd";
923
924                         $feed_id = $line["id"];
925
926                         $edit_feed_id = $_GET["id"];
927
928                         if ($subop == "edit" && $feed_id != $edit_feed_id) {
929                                 $class .= "Grayed";
930                         }
931
932                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
933
934                         $icon_file = ICONS_DIR . "/$feed_id.ico";
935
936                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
937                                         $feed_icon = "<img width=\"16\" height=\"16\"
938                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
939                         } else {
940                                 $feed_icon = "&nbsp;";
941                         }
942                         print "<td align='center'>$feed_icon</td>";             
943
944                         $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
945                         $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
946
947                         if (!$edit_feed_id || $subop != "edit") {
948
949                                 print "<td><input onclick='toggleSelectRow(this);' 
950                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
951
952                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
953                                         $edit_title . "</a></td>";              
954                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
955                                         $edit_link . "</a></td>";               
956
957                                 if ($line["update_interval"] == "0")
958                                         $line["update_interval"] = "Default";
959
960                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
961                                         $line["update_interval"] . "</a></td>";
962
963                                 if ($line["purge_interval"] == "0")
964                                         $line["purge_interval"] = "Default";
965
966                                 if ($line["purge_interval"] < 0)
967                                         $line["purge_interval"] = "Disabled";
968
969                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
970                                         $line["purge_interval"] . "</a></td>";
971
972                         } else if ($feed_id != $edit_feed_id) {
973
974                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
975                                         id=\"FRCHK-".$line["id"]."\"></td>";
976
977                                 print "<td>$edit_title</td>";           
978                                 print "<td>$edit_link</td>";            
979
980                                 if ($line["update_interval"] == "0")
981                                         $line["update_interval"] = "Default";
982
983                                 print "<td>" . $line["update_interval"] . "</td>";
984
985                                 if ($line["purge_interval"] == "0")
986                                         $line["purge_interval"] = "Default";
987
988                                 if ($line["purge_interval"] < 0)
989                                         $line["purge_interval"] = "Disabled";
990
991                                 print "<td>" . $line["purge_interval"] . "</td>";
992
993                         } else {
994
995                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
996
997                                 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
998                                 print "<td><input id=\"iedit_link\" value=\"$edit_link\"></td>";
999                                 print "<td><input id=\"iedit_updintl\" value=\"".$line["update_interval"]."\"></td>";
1000                                 print "<td><input id=\"iedit_purgintl\" value=\"".$line["purge_interval"]."\"></td>";
1001                                         
1002                         }
1003
1004                         if (!$line["last_updated"]) $line["last_updated"] = "Never";
1005
1006                         print "<td>" . $line["last_updated"] . "</td>";
1007                         
1008                         print "</tr>";
1009
1010                         ++$lnum;
1011                 }
1012
1013                 if ($lnum == 0) {
1014                         print "<tr><td colspan=\"5\" align=\"center\">No feeds defined.</td></tr>";
1015                 }
1016
1017                 print "</table>";
1018
1019                 print "<p>";
1020
1021                 if ($subop == "edit") {
1022                         print "Edit feed:&nbsp;
1023                                 <input type=\"submit\" class=\"button\" 
1024                                         onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
1025                                 <input type=\"submit\" class=\"button\" 
1026                                         onclick=\"javascript:feedEditSave()\" value=\"Save\">";
1027                         } else {
1028
1029                         print "
1030                                 Selection:&nbsp;
1031                         <input type=\"submit\" class=\"button\" 
1032                                 onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
1033                         <input type=\"submit\" class=\"button\" 
1034                                 onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
1035                                 
1036                         if (ENABLE_PREFS_CATCHUP_UNCATCHUP) {
1037                                 print "
1038                                 <input type=\"submit\" class=\"button\" 
1039                                         onclick=\"javascript:readSelectedFeeds()\" value=\"Mark as read\">
1040                                 <input type=\"submit\" class=\"button\" 
1041                                         onclick=\"javascript:unreadSelectedFeeds()\" value=\"Mark as unread\">&nbsp;";
1042                         }
1043                         print "
1044                         All feeds: 
1045                                 <input type=\"submit\" 
1046                                         class=\"button\" onclick=\"gotoExportOpml()\" value=\"Export OPML\">";
1047                 
1048                         }
1049
1050                 print "<h3>OPML Import</h3>
1051                 <form   enctype=\"multipart/form-data\" method=\"POST\" action=\"opml.php\">
1052                         File: <input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
1053                         <input class=\"button\" name=\"op\" onclick=\"return validateOpmlImport();\"
1054                                 type=\"submit\" value=\"Import\">
1055                         </form>";
1056
1057         }
1058
1059         if ($op == "pref-filters") {
1060
1061                 $subop = $_GET["subop"];
1062
1063                 if ($subop == "editSave") {
1064
1065                         $regexp = db_escape_string($_GET["r"]);
1066                         $descr = db_escape_string($_GET["d"]);
1067                         $match = db_escape_string($_GET["m"]);
1068                         $filter_id = db_escape_string($_GET["id"]);
1069                         
1070                         $result = db_query($link, "UPDATE ttrss_filters SET 
1071                                 reg_exp = '$regexp', 
1072                                 description = '$descr',
1073                                 filter_type = (SELECT id FROM ttrss_filter_types WHERE
1074                                         description = '$match')
1075                                 WHERE id = '$filter_id'");
1076                 }
1077
1078                 if ($subop == "remove") {
1079
1080                         if (!WEB_DEMO_MODE) {
1081
1082                                 $ids = split(",", $_GET["ids"]);
1083
1084                                 foreach ($ids as $id) {
1085                                         db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
1086                                         
1087                                 }
1088                         }
1089                 }
1090
1091                 if ($subop == "add") {
1092                 
1093                         if (!WEB_DEMO_MODE) {
1094
1095                                 $regexp = db_escape_string($_GET["regexp"]);
1096                                 $match = db_escape_string($_GET["match"]);
1097                                         
1098                                 $result = db_query($link,
1099                                         "INSERT INTO ttrss_filters (reg_exp,filter_type) VALUES 
1100                                                 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
1101                                                         description = '$match'))");
1102                         } 
1103                 }
1104
1105                 $result = db_query($link, "SELECT description 
1106                         FROM ttrss_filter_types ORDER BY description");
1107
1108                 $filter_types = array();
1109
1110                 while ($line = db_fetch_assoc($result)) {
1111                         array_push($filter_types, $line["description"]);
1112                 }
1113
1114                 print "<table class=\"prefAddFeed\"><tr>
1115                         <td><input id=\"fadd_regexp\"></td>
1116                         <td>";
1117                         print_select("fadd_match", "Title", $filter_types);     
1118         
1119                 print"</td><td colspan=\"4\" align=\"right\">
1120                                 <a class=\"button\" href=\"javascript:addFilter()\">Add filter</a></td></tr>
1121                 </table>";
1122
1123                 $result = db_query($link, "SELECT 
1124                                 id,reg_exp,description,
1125                                 (SELECT name FROM ttrss_filter_types WHERE 
1126                                         id = filter_type) as filter_type_name,
1127                                 (SELECT description FROM ttrss_filter_types 
1128                                         WHERE id = filter_type) as filter_type_descr
1129                         FROM 
1130                                 ttrss_filters ORDER by reg_exp");
1131
1132                 print "<p><table width=\"100%\" class=\"prefFilterList\" id=\"prefFilterList\">";
1133
1134                 print "<tr class=\"title\">
1135                                         <td width=\"5%\">Select</td><td width=\"40%\">Filter expression</td>
1136                                         <td width=\"40%\">Description</td><td width=\"10%\">Match</td></tr>";
1137                 
1138                 $lnum = 0;
1139                 
1140                 while ($line = db_fetch_assoc($result)) {
1141
1142                         $class = ($lnum % 2) ? "even" : "odd";
1143
1144                         $filter_id = $line["id"];
1145                         $edit_filter_id = $_GET["id"];
1146
1147                         if ($subop == "edit" && $filter_id != $edit_filter_id) {
1148                                 $class .= "Grayed";
1149                         }
1150
1151                         print "<tr class=\"$class\" id=\"FILRR-$filter_id\">";
1152
1153                         $line["regexp"] = htmlspecialchars($line["reg_exp"]);
1154                         $line["description"] = htmlspecialchars($line["description"]);
1155
1156                         if (!$edit_filter_id || $subop != "edit") {
1157
1158                                 if (!$line["description"]) $line["description"] = "[No description]";
1159
1160                                 print "<td><input onclick='toggleSelectRow(this);' 
1161                                 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
1162
1163                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
1164                                         $line["reg_exp"] . "</td>";             
1165                                         
1166                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
1167                                         $line["description"] . "</td>";                 
1168
1169                                 print "<td>".$line["filter_type_descr"]."</td>";
1170
1171                         } else if ($filter_id != $edit_filter_id) {
1172
1173                                 if (!$line["description"]) $line["description"] = "[No description]";
1174
1175                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
1176                                         id=\"FICHK-".$line["id"]."\"></td>";
1177
1178                                 print "<td>".$line["reg_exp"]."</td>";          
1179                                 print "<td>".$line["description"]."</td>";              
1180                                 print "<td>".$line["filter_type_descr"]."</td>";
1181
1182                         } else {
1183
1184                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
1185
1186                                 print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
1187                                         "\"></td>";
1188
1189                                 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1190                                         "\"></td>";
1191
1192                                 print "<td>";
1193                                 print_select("iedit_match", $line["filter_type_descr"], $filter_types);
1194                                 print "</td>";
1195                                                 
1196                         }
1197                                 
1198                         
1199                         print "</tr>";
1200
1201                         ++$lnum;
1202                 }
1203
1204                 if ($lnum == 0) {
1205                         print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
1206                 }
1207
1208                 print "</table>";
1209
1210                 print "<p>";
1211
1212                 if ($subop == "edit") {
1213                         print "Edit feed:
1214                                 <input type=\"submit\" class=\"button\" 
1215                                         onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">
1216                                 <input type=\"submit\" class=\"button\" 
1217                                         onclick=\"javascript:filterEditSave()\" value=\"Save\">";
1218                                         
1219                 } else {
1220
1221                         print "
1222                                 Selection:
1223                         <input type=\"submit\" class=\"button\" 
1224                                 onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
1225                         <input type=\"submit\" class=\"button\" 
1226                                 onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
1227                 }
1228         }
1229
1230         if ($op == "pref-labels") {
1231
1232                 $subop = $_GET["subop"];
1233
1234                 if ($subop == "editSave") {
1235
1236                         $sql_exp = $_GET["s"];
1237                         $descr = $_GET["d"];
1238                         $label_id = db_escape_string($_GET["id"]);
1239                         
1240 //                      print "$sql_exp : $descr : $label_id";
1241                         
1242                         $result = db_query($link, "UPDATE ttrss_labels SET 
1243                                 sql_exp = '$sql_exp', 
1244                                 description = '$descr'
1245                                 WHERE id = '$label_id'");
1246                 }
1247
1248                 if ($subop == "remove") {
1249
1250                         if (!WEB_DEMO_MODE) {
1251
1252                                 $ids = split(",", $_GET["ids"]);
1253
1254                                 foreach ($ids as $id) {
1255                                         db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
1256                                         
1257                                 }
1258                         }
1259                 }
1260
1261                 if ($subop == "add") {
1262                 
1263                         if (!WEB_DEMO_MODE) {
1264
1265                                 $exp = $_GET["exp"];
1266                                         
1267                                 $result = db_query($link,
1268                                         "INSERT INTO ttrss_labels (sql_exp,description) 
1269                                                 VALUES ('$exp', '$exp')");
1270                         } 
1271                 }
1272
1273                 print "<table class=\"prefAddFeed\"><tr>
1274                         <td><input id=\"ladd_expr\"></td>";
1275                         
1276                 print"<td colspan=\"4\" align=\"right\">
1277                                 <a class=\"button\" href=\"javascript:addLabel()\">Add label</a></td></tr>
1278                 </table>";
1279
1280                 $result = db_query($link, "SELECT 
1281                                 id,sql_exp,description
1282                         FROM 
1283                                 ttrss_labels ORDER by description");
1284
1285                 print "<p><table width=\"100%\" class=\"prefLabelList\" id=\"prefLabelList\">";
1286
1287                 print "<tr class=\"title\">
1288                                         <td width=\"5%\">Select</td><td width=\"40%\">SQL expression
1289                                         <a class=\"helpLink\" href=\"javascript:popupHelp(1)\">(?)</a>
1290                                         </td>
1291                                         <td width=\"40%\">Caption</td></tr>";
1292                 
1293                 $lnum = 0;
1294                 
1295                 while ($line = db_fetch_assoc($result)) {
1296
1297                         $class = ($lnum % 2) ? "even" : "odd";
1298
1299                         $label_id = $line["id"];
1300                         $edit_label_id = $_GET["id"];
1301
1302                         if ($subop == "edit" && $label_id != $edit_label_id) {
1303                                 $class .= "Grayed";
1304                         }
1305
1306                         print "<tr class=\"$class\" id=\"LILRR-$label_id\">";
1307
1308                         $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
1309                         $line["description"] = htmlspecialchars($line["description"]);
1310
1311                         if (!$edit_label_id || $subop != "edit") {
1312
1313                                 if (!$line["description"]) $line["description"] = "[No caption]";
1314
1315                                 print "<td><input onclick='toggleSelectRow(this);' 
1316                                 type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
1317
1318                                 print "<td><a href=\"javascript:editLabel($label_id);\">" . 
1319                                         $line["sql_exp"] . "</td>";             
1320                                         
1321                                 print "<td><a href=\"javascript:editLabel($label_id);\">" . 
1322                                         $line["description"] . "</td>";                 
1323
1324                         } else if ($label_id != $edit_label_id) {
1325
1326                                 if (!$line["description"]) $line["description"] = "[No description]";
1327
1328                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
1329                                         id=\"LICHK-".$line["id"]."\"></td>";
1330
1331                                 print "<td>".$line["sql_exp"]."</td>";          
1332                                 print "<td>".$line["description"]."</td>";              
1333
1334                         } else {
1335
1336                                 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
1337
1338                                 print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
1339                                         "\"></td>";
1340
1341                                 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1342                                         "\"></td>";
1343                                                 
1344                         }
1345                                 
1346                         
1347                         print "</tr>";
1348
1349                         ++$lnum;
1350                 }
1351
1352                 if ($lnum == 0) {
1353                         print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
1354                 }
1355
1356                 print "</table>";
1357
1358                 print "<p>";
1359
1360                 if ($subop == "edit") {
1361                         print "Edit label:
1362                                 <input type=\"submit\" class=\"button\" 
1363                                         onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">
1364                                 <input type=\"submit\" class=\"button\" 
1365                                         onclick=\"javascript:labelEditSave()\" value=\"Save\">";
1366                                         
1367                 } else {
1368
1369                         print "
1370                                 Selection:
1371                         <input type=\"submit\" class=\"button\" 
1372                                 onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
1373                         <input type=\"submit\" class=\"button\" 
1374                                 onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
1375                 }
1376         }
1377
1378         if ($op == "error") {
1379                 print "<div width=\"100%\" align='center'>";
1380                 $msg = $_GET["msg"];
1381                 print $msg;
1382                 print "</div>";
1383         }
1384
1385         if ($op == "help") {
1386                 print "<html><head>
1387                         <title>Tiny Tiny RSS : Help</title>
1388                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
1389                         <script type=\"text/javascript\" src=\"functions.js\"></script>
1390                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
1391                         </head><body>";
1392
1393                 $tid = sprintf("%d", $_GET["tid"]);
1394
1395                 /* FIXME this badly needs real implementation */
1396
1397                 print "<div class='helpResponse'>";
1398
1399                 ?>
1400
1401                 <h1>Help for SQL expressions</h1>
1402
1403                 <h2>Description</h2>
1404
1405                 <p>The &laquo;SQL expression&raquo; is added to WHERE clause of
1406                         view feed query. You can match on ttrss_entries table fields
1407                         and even use subselect to query additional information. This 
1408                         functionality is considered to be advanced and requires basic
1409                         understanding of SQL.</p>
1410                         
1411                 <h2>Examples</h2>
1412
1413                 <pre>unread = true</pre>
1414
1415                 Matches all unread articles
1416
1417                 <pre>title like '%Linux%'</pre>
1418
1419                 Matches all articles which mention Linux in the title. You get the idea.
1420
1421                 <p>See the database schema included in the distribution package for gruesome
1422                 details.</p>
1423
1424                 <?
1425
1426                 print "<div align='center'>
1427                         <a class=\"helpLink\"
1428                         href=\"javascript:window.close()\">(Close this window)</a></div>";
1429
1430                 print "</div>";
1431
1432                 print "</body></html>";
1433
1434         }
1435
1436         if ($op == "dlg") {
1437                 $id = $_GET["id"];
1438                 $param = $_GET["param"];
1439
1440                 if ($id == "quickAddFeed") {
1441                         print "Feed URL: <input 
1442                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1443                         id=\"qafInput\">
1444                         <input class=\"button\"
1445                                 type=\"submit\" onclick=\"javascript:qafAdd()\" value=\"Add feed\">
1446                         <input class=\"button\"
1447                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
1448                                 value=\"Cancel\">";
1449                 }
1450
1451                 if ($id == "quickDelFeed") {
1452
1453                         $param = db_escape_string($param);
1454
1455                         $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$param'");
1456
1457                         if ($result) {
1458
1459                                 $f_title = db_fetch_result($result, 0, "title");
1460                 
1461                                 print "Remove current feed ($f_title)?&nbsp;
1462                                 <input class=\"button\"
1463                                         type=\"submit\" onclick=\"javascript:qfdDelete($param)\" value=\"Remove\">
1464                                 <input class=\"button\"
1465                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
1466                                         value=\"Cancel\">";
1467                         } else {
1468                                 print "Error: Feed $param not found.&nbsp;
1469                                 <input class=\"button\"
1470                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
1471                                         value=\"Cancel\">";             
1472                         }
1473                 }
1474
1475                 if ($id == "search") {
1476
1477                         print "<input id=\"searchbox\" class=\"extSearch\"                      
1478                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1479                         onchange=\"javascript:search()\">
1480                         <select id=\"searchmodebox\">
1481                                 <option selected>All feeds</option>
1482                                 <option>This feed</option>
1483                         </select>               
1484                         <input type=\"submit\" 
1485                                 class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
1486                         <input class=\"button\"
1487                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
1488                                 value=\"Close\">";
1489
1490                 }
1491
1492         }
1493
1494         if ($op == "updateAllFeeds") {
1495                 update_all_feeds($link, true);                  
1496
1497                 print "<rpc-reply>";
1498                 getLabelCounters($link);
1499                 getFeedCounters($link);
1500                 getTagCounters($link);
1501                 getGlobalCounters($link);
1502                 print "</rpc-reply>";
1503
1504         }
1505
1506         db_close($link);
1507 ?>
1508
1509 <!-- <?= sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
1510