]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
store previous value of counters and send only diffs
[tt-rss.git] / backend.php
1 <?
2         session_start();
3
4         if ($_GET["debug"]) {
5                 define('DEFAULT_ERROR_LEVEL', E_ALL);
6         } else {
7                 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
8         }
9
10         error_reporting(DEFAULT_ERROR_LEVEL);
11
12         $op = $_REQUEST["op"];
13
14         if ((!$op || $op == "rpc" || $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
15                 header("Content-Type: application/xml");
16         }
17
18         if (!$_SESSION["uid"] && $op != "globalUpdateFeeds") {
19
20                 if ($op == "rpc") {
21                         print "<error error-code=\"6\"/>";
22                 }
23                 exit;
24         }
25
26         if (!$op) {
27                 print "<error error-code=\"7\"/>";
28                 exit;
29         }
30
31         define('SCHEMA_VERSION', 2);
32
33         require_once "sanity_check.php";
34         require_once "config.php";
35         require_once "db.php";
36         require_once "db-prefs.php";
37         require_once "functions.php";
38         require_once "magpierss/rss_fetch.inc";
39
40         $script_started = getmicrotime();
41
42         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
43
44         if (!$link) {
45                 if (DB_TYPE == "mysql") {
46                         print mysql_error();
47                 }
48                 // PG seems to display its own errors just fine by default.             
49                 return;
50         }
51
52         if (DB_TYPE == "pgsql") {
53                 pg_query("set client_encoding = 'utf-8'");
54         }
55
56         $fetch = $_GET["fetch"];
57
58         function getAllCounters($link) {
59                 getLabelCounters($link);
60                 getFeedCounters($link);
61                 getTagCounters($link);
62                 getGlobalCounters($link);
63         }       
64
65         /* FIXME this needs reworking */
66
67         function getGlobalCounters($link) {
68                 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
69                         WHERE unread = true AND 
70                         ttrss_user_entries.ref_id = ttrss_entries.id AND 
71                         owner_uid = " . $_SESSION["uid"]);
72                 $c_id = db_fetch_result($result, 0, "c_id");
73                 print "<counter id='global-unread' counter='$c_id'/>";
74         }
75
76         function getTagCounters($link, $smart_mode = true) {
77
78                 if ($smart_mode) {
79                         if (!$_SESSION["tctr_last_value"]) {
80                                 $_SESSION["tctr_last_value"] = array();
81                         }
82                 }
83
84                 $old_counters = $_SESSION["tctr_last_value"];
85
86                 $tctrs_modified = false;
87
88                 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
89                         FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
90                         ttrss_user_entries.ref_id = ttrss_entries.id AND 
91                         ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
92                         post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name 
93                 UNION
94                         select tag_name,0 as count FROM ttrss_tags
95                         WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]);
96
97                 $tags = array();
98
99                 while ($line = db_fetch_assoc($result)) {
100                         $tags[$line["tag_name"]] += $line["count"];
101                 }
102
103                 foreach (array_keys($tags) as $tag) {
104                         $unread = $tags[$tag];                  
105
106                         $tag = htmlspecialchars($tag);
107
108                         if (!$smart_mode || $old_counters[$tag] != $unread) {                   
109                                 $old_counters[$tag] = $unread;
110                                 $tctrs_modified = true;
111                                 print "<tag id=\"$tag\" counter=\"$unread\"/>";
112                         }
113
114                 } 
115
116                 if ($smart_mode && $tctrs_modified) {
117                         $_SESSION["tctr_last_value"] = $old_counters;
118                 }
119
120         }
121
122         function getLabelCounters($link) {
123
124                 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
125                         WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND 
126                         unread = true AND owner_uid = ".$_SESSION["uid"]);
127
128                 $count = db_fetch_result($result, 0, "count");
129
130                 print "<label id=\"-1\" counter=\"$count\"/>";
131
132                 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
133                         ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
134         
135                 while ($line = db_fetch_assoc($result)) {
136
137                         $id = -$line["id"] - 11;
138
139                         error_reporting (0);
140
141                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
142                                 WHERE (" . $line["sql_exp"] . ") AND unread = true AND 
143                                 ttrss_user_entries.ref_id = ttrss_entries.id AND 
144                                 owner_uid = ".$_SESSION["uid"]);
145
146                         $count = db_fetch_result($tmp_result, 0, "count");
147
148                         print "<label id=\"$id\" counter=\"$count\"/>";
149
150                         error_reporting (DEFAULT_ERROR_LEVEL);
151         
152                 }
153         }
154
155         function getFeedCounter($link, $id) {
156         
157                 $result = db_query($link, "SELECT 
158                                 count(id) as count FROM ttrss_entries,ttrss_user_entries
159                         WHERE feed_id = '$id' AND unread = true
160                         AND ttrss_user_entries.ref_id = ttrss_entries.id");
161         
162                         $count = db_fetch_result($result, 0, "count");
163                         
164                         print "<feed id=\"$id\" counter=\"$count\"/>";          
165         }
166
167         function getFeedCounters($link, $smart_mode = true) {
168
169                 if ($smart_mode) {
170                         if (!$_SESSION["fctr_last_value"]) {
171                                 $_SESSION["fctr_last_value"] = array();
172                         }
173                 }
174
175                 $old_counters = $_SESSION["fctr_last_value"];
176
177                 $result = db_query($link, "SELECT id,
178                         (SELECT count(id) 
179                                 FROM ttrss_entries,ttrss_user_entries 
180                                 WHERE feed_id = ttrss_feeds.id AND ttrss_user_entries.ref_id = ttrss_entries.id
181                                 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
182                         FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
183
184                 $fctrs_modified = false;
185
186                 while ($line = db_fetch_assoc($result)) {
187                 
188                         $id = $line["id"];
189                         $count = $line["count"];
190
191                         if (!$smart_mode || $old_counters[$id] != $count) {
192                                 $old_counters[$id] = $count;
193                                 $fctrs_modified = true;
194                                 print "<feed id=\"$id\" counter=\"$count\"/>";
195                         }
196                 }
197
198                 if ($smart_mode && $fctrs_modified) {
199                         $_SESSION["fctr_last_value"] = $old_counters;
200                 }
201         }
202
203         function outputFeedList($link, $tags = false) {
204
205                 print "<html><head>
206                         <title>Tiny Tiny RSS : Feedlist</title>
207                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
208
209                 $user_theme = $_SESSION["theme"];
210                 if ($user_theme) { 
211                         print "<link rel=\"stylesheet\" type=\"text/css\" 
212                                 href=\"themes/$user_theme/theme.css\">";
213                 }
214
215                 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
216                         print "<link rel=\"stylesheet\" type=\"text/css\" 
217                                 href=\"tt-rss_compact.css\"/>";
218                 } else {
219                         print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
220                                         type=\"text/css\" href=\"tt-rss_compact.css\"/>";
221                 }
222
223                 print "<script type=\"text/javascript\" src=\"functions.js\"></script>
224                         <script type=\"text/javascript\" src=\"feedlist.js\"></script>
225                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
226                         </head><body onload=\"init()\">";
227
228                 print "<ul class=\"feedList\" id=\"feedList\">";
229
230                 $owner_uid = $_SESSION["uid"];
231
232                 if (!$tags) {
233
234                         /* virtual feeds */
235
236                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
237                                 print "<li class=\"feedCat\">Special</li>";
238                                 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
239                         }
240
241                         $result = db_query($link, "SELECT count(id) as num_starred 
242                                 FROM ttrss_entries,ttrss_user_entries 
243                                 WHERE marked = true AND 
244                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
245                                 unread = true AND owner_uid = '$owner_uid'");
246                         $num_starred = db_fetch_result($result, 0, "num_starred");
247
248                         $class = "virt";
249
250                         if ($num_starred > 0) $class .= "Unread";
251
252                         printFeedEntry(-1, $class, "Starred articles", $num_starred, 
253                                 "images/mark_set.png", $link);
254
255                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
256                                 print "</li></ul>";
257                         }
258
259                         if (get_pref($link, 'ENABLE_LABELS')) {
260         
261                                 $result = db_query($link, "SELECT id,sql_exp,description FROM
262                                         ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
263                 
264                                 if (db_num_rows($result) > 0) {
265                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
266                                                 print "<li class=\"feedCat\">Labels</li>";
267                                                 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
268                                         } else {
269                                                 print "<li><hr></li>";
270                                         }
271                                 }
272                 
273                                 while ($line = db_fetch_assoc($result)) {
274         
275                                         error_reporting (0);
276                 
277                                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
278                                                 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
279                                                 ttrss_user_entries.ref_id = ttrss_entries.id
280                                                 AND owner_uid = '$owner_uid'");
281         
282                                         $count = db_fetch_result($tmp_result, 0, "count");
283         
284                                         $class = "label";
285         
286                                         if ($count > 0) {
287                                                 $class .= "Unread";
288                                         }
289                                         
290                                         error_reporting (DEFAULT_ERROR_LEVEL);
291         
292                                         printFeedEntry(-$line["id"]-11, 
293                                                 $class, $line["description"], $count, "images/label.png", $link);
294                 
295                                 }
296
297                                 if (db_num_rows($result) > 0) {
298                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
299                                                 print "</li></ul>";
300                                         }
301                                 }
302
303                         }
304
305 //                      if (!get_pref($link, 'ENABLE_FEED_CATS')) {
306                                 print "<li><hr></li>";
307 //                      }
308
309                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
310                                 $order_by_qpart = "category,title";
311                         } else {
312                                 $order_by_qpart = "title";
313                         }
314
315                         $result = db_query($link, "SELECT *,
316                                 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
317                                         WHERE feed_id = ttrss_feeds.id AND 
318                                         ttrss_user_entries.ref_id = ttrss_entries.id AND
319                                         owner_uid = '$owner_uid') AS total,
320                                 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
321                                         WHERE feed_id = ttrss_feeds.id AND unread = true
322                                                 AND ttrss_user_entries.ref_id = ttrss_entries.id
323                                                 AND owner_uid = '$owner_uid') as unread,
324                                 (SELECT title FROM ttrss_feed_categories 
325                                         WHERE id = cat_id) AS category
326                                 FROM ttrss_feeds WHERE owner_uid = '$owner_uid' ORDER BY $order_by_qpart");                     
327         
328                         $actid = $_GET["actid"];
329         
330                         /* real feeds */
331         
332                         $lnum = 0;
333         
334                         $total_unread = 0;
335
336                         $category = "";
337         
338                         while ($line = db_fetch_assoc($result)) {
339                         
340                                 $feed = $line["title"];
341                                 $feed_id = $line["id"];   
342         
343                                 $subop = $_GET["subop"];
344                                 
345                                 $total = $line["total"];
346                                 $unread = $line["unread"];
347
348                                 $tmp_category = $line["category"];
349
350                                 if (!$tmp_category) {
351                                         $tmp_category = "Uncategorized";
352                                 }
353                                 
354         //                      $class = ($lnum % 2) ? "even" : "odd";
355         
356                                 $class = "feed";
357         
358                                 if ($unread > 0) $class .= "Unread";
359         
360                                 if ($actid == $feed_id) {
361                                         $class .= "Selected";
362                                 }
363         
364                                 $total_unread += $unread;
365
366                                 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
367                                 
368                                         if ($category) {
369                                                 print "</li></ul></li>";
370                                         }
371                                 
372                                         $category = $tmp_category;
373                                         
374                                         print "<li class=\"feedCat\">$category</li>";
375                                         print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
376                                 }
377         
378                                 printFeedEntry($feed_id, $class, $feed, $unread, 
379                                         "icons/$feed_id.ico", $link);
380         
381                                 ++$lnum;
382                         }
383
384                 } else {
385
386                         // tags
387
388                         $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
389                                 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
390                                 post_int_id = ttrss_user_entries.int_id AND 
391                                 unread = true AND ref_id = ttrss_entries.id
392                                 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name       
393                         UNION
394                                 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
395                         ORDER BY tag_name");
396         
397                         $tags = array();
398         
399                         while ($line = db_fetch_assoc($result)) {
400                                 $tags[$line["tag_name"]] += $line["count"];
401                         }
402         
403                         foreach (array_keys($tags) as $tag) {
404         
405                                 $unread = $tags[$tag];
406         
407                                 $class = "tag";
408         
409                                 if ($unread > 0) {
410                                         $class .= "Unread";
411                                 }
412         
413                                 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
414         
415                         } 
416
417                 }
418
419                 if (db_num_rows($result) == 0) {
420                         if ($tags) {
421                                 $what = "tags";
422                         } else {
423                                 $what = "feeds";
424                         }
425                         print "<li>No $what to display.</li>";
426                 }
427
428                 print "</ul>";
429
430         }
431
432
433         if ($op == "rpc") {
434
435                 $subop = $_GET["subop"];
436
437                 if ($subop == "getLabelCounters") {
438                         $aid = $_GET["aid"];            
439                         print "<rpc-reply>";
440                         getLabelCounters($link);
441                         if ($aid) {
442                                 getFeedCounter($link, $aid);
443                         }
444                         print "</rpc-reply>";
445                 }
446
447                 if ($subop == "getFeedCounters") {
448                         print "<rpc-reply>";
449                         getFeedCounters($link);
450                         print "</rpc-reply>";
451                 }
452
453                 if ($subop == "getAllCounters") {
454                         print "<rpc-reply>";
455                         getAllCounters($link);
456                         print "</rpc-reply>";
457                 }
458
459                 if ($subop == "mark") {
460                         $mark = $_GET["mark"];
461                         $id = db_escape_string($_GET["id"]);
462
463                         if ($mark == "1") {
464                                 $mark = "true";
465                         } else {
466                                 $mark = "false";
467                         }
468
469                         // FIXME this needs collision testing
470
471                         $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
472                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
473                 }
474
475                 if ($subop == "updateFeed") {
476                         $feed_id = db_escape_string($_GET["feed"]);
477
478                         $result = db_query($link, 
479                                 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
480                                         AND owner_uid = " . $_SESSION["uid"]);
481
482                         if (db_num_rows($result) > 0) {                 
483                                 $feed_url = db_fetch_result($result, 0, "feed_url");
484                                 update_rss_feed($link, $feed_url, $feed_id);
485                         }
486
487                         print "<rpc-reply>";
488                         getFeedCounter($link, $feed_id);
489                         print "</rpc-reply>";
490                         
491                         return;
492                 }
493
494                 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
495                 
496                         update_all_feeds($link, $subop == "forceUpdateAllFeeds");                       
497
498                         $omode = $_GET["omode"];
499
500                         if (!$omode) $omode = "tfl";
501
502                         print "<rpc-reply>";
503                         if (strchr($omode, "l")) getLabelCounters($link);
504                         if (strchr($omode, "f")) getFeedCounters($link);
505                         if (strchr($omode, "t")) getTagCounters($link);
506                         getGlobalCounters($link);
507                         print "</rpc-reply>";
508                 }
509         
510                 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
511                 if ($subop == "catchupSelected") {
512
513                         $ids = split(",", db_escape_string($_GET["ids"]));
514
515                         $cmode = sprintf("%d", $_GET["cmode"]);
516
517                         foreach ($ids as $id) {
518
519                                 if ($cmode == 0) {
520                                         db_query($link, "UPDATE ttrss_user_entries SET 
521                                         unread = false,last_read = NOW()
522                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
523                                 } else if ($cmode == 1) {
524                                         db_query($link, "UPDATE ttrss_user_entries SET 
525                                         unread = true
526                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
527                                 } else {
528                                         db_query($link, "UPDATE ttrss_user_entries SET 
529                                         unread = NOT unread,last_read = NOW()
530                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
531                                 }
532                         }
533                         print "<rpc-reply>";
534                         getAllCounters($link);
535                         print "</rpc-reply>";
536                 }
537
538                 if ($subop == "markSelected") {
539
540                         $ids = split(",", db_escape_string($_GET["ids"]));
541
542                         $cmode = sprintf("%d", $_GET["cmode"]);
543
544                         foreach ($ids as $id) {
545
546                                 if ($cmode == 0) {
547                                         db_query($link, "UPDATE ttrss_user_entries SET 
548                                         marked = false
549                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
550                                 } else if ($cmode == 1) {
551                                         db_query($link, "UPDATE ttrss_user_entries SET 
552                                         marked = true
553                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
554                                 } else {
555                                         db_query($link, "UPDATE ttrss_user_entries SET 
556                                         marked = NOT marked
557                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
558                                 }
559                         }
560                         print "<rpc-reply>";
561                         getAllCounters($link);
562                         print "</rpc-reply>";
563                 }
564
565                 if ($subop == "sanityCheck") {
566
567                         $error_code = 0;
568
569                         $result = db_query($link, "SELECT schema_version FROM ttrss_version");
570
571                         $schema_version = db_fetch_result($result, 0, "schema_version");
572
573                         if ($schema_version != SCHEMA_VERSION) {
574                                 $error_code = 5;
575                         }
576
577                         print "<error error-code='$error_code'/>";
578                 }
579
580                 if ($subop == "globalPurge") {
581
582                         print "<rpc-reply>";
583                         global_purge_old_posts($link, true);
584                         print "</rpc-reply>";
585
586                 }
587
588         }
589         
590         if ($op == "feeds") {
591
592                 $tags = $_GET["tags"];
593
594                 $subop = $_GET["subop"];
595
596                 if ($subop == "catchupAll") {
597                         db_query($link, "UPDATE ttrss_user_entries SET 
598                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
599                 }
600
601                 outputFeedList($link, $tags);
602
603         }
604
605         if ($op == "view") {
606
607                 $id = $_GET["id"];
608                 $feed_id = $_GET["feed"];
609
610                 $result = db_query($link, "UPDATE ttrss_user_entries 
611                         SET unread = false,last_read = NOW() 
612                         WHERE ref_id = '$id' AND feed_id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
613
614                 $addheader = $_GET["addheader"];
615
616                 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
617                         SUBSTRING(updated,1,16) as updated,
618                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url
619                         FROM ttrss_entries,ttrss_user_entries
620                         WHERE   id = '$id' AND ref_id = id");
621
622                 if ($addheader) {
623                         print "<html><head>
624                                 <title>Tiny Tiny RSS : Article $id</title>
625                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
626
627                         $user_theme = $_SESSION["theme"];
628                         if ($user_theme) { 
629                                 print "<link rel=\"stylesheet\" type=\"text/css\" 
630                                         href=\"themes/$user_theme/theme.css\">";
631                         }
632
633                         if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
634                                 print "<link rel=\"stylesheet\" type=\"text/css\" 
635                                         href=\"tt-rss_compact.css\"/>";
636                         } else {
637                                 print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
638                                                 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
639                         }
640
641                         print "<script type=\"text/javascript\" src=\"functions.js\"></script>
642                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
643                                 </head><body>";
644                 }
645
646                 if ($result) {
647
648                         $line = db_fetch_assoc($result);
649
650                         if ($line["icon_url"]) {
651                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
652                         } else {
653                                 $feed_icon = "&nbsp;";
654                         }
655
656                         if ($line["comments"] && $line["link"] != $line["comments"]) {
657                                 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
658                         } else {
659                                 $entry_comments = "";
660                         }
661
662                         print "<div class=\"postReply\">";
663
664                         print "<div class=\"postHeader\"><table width=\"100%\">";
665
666                         print "<tr><td>" . $line["title"] . "</td>";
667
668                         $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), 
669                                 strtotime($line["updated"]));
670                 
671                         print "<td class=\"postDate\">$parsed_updated</td>";
672                                                 
673                         print "</tr>";
674
675                         $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
676                                 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
677                                 ORDER BY tag_name");
678         
679                         $tags_str = "";
680                         $f_tags_str = "";
681
682                         $num_tags = 0;
683
684                         while ($tmp_line = db_fetch_assoc($tmp_result)) {
685                                 $num_tags++;
686                                 $tag = $tmp_line["tag_name"];                           
687                                 $tag_str = "<a href=\"javascript:parent.viewfeed('$tag')\">$tag</a>, "; 
688                                 
689                                 if ($num_tags == 5) {
690                                         $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
691                                 } else if ($num_tags < 5) {
692                                         $tags_str .= $tag_str;
693                                 }
694                                 $f_tags_str .= $tag_str;
695                         }
696
697                         $tags_str = preg_replace("/, $/", "", $tags_str);
698                         $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
699
700                         print "<tr><td width='50%'>
701                                 <a href=\"" . $line["link"] . "\">".$line["link"]."</a>
702                                 $entry_comments</td>
703                                 <td align=\"right\">$tags_str</td></tr>";
704
705 /*                      if ($tags_str) {
706                                 print "<tr><td><b>Tags:</b></td>
707                                         <td width='100%'>$tags_str</td></tr>";
708                         } */
709
710                         print "</table></div>";
711
712                         print "<div class=\"postIcon\">" . $feed_icon . "</div>";
713                         print "<div class=\"postContent\">";
714                         
715                         if (db_num_rows($tmp_result) > 5) {
716                                 print "<div id=\"allEntryTags\">Tags: $f_tags_str</div>";
717                         }
718
719                         print $line["content"] . "</div>";
720                         
721                         print "</div>";
722
723                         print "<script type=\"text/javascript\">
724                                 update_all_counters('$feed_id');
725                         </script>";
726                 }
727
728                 if ($addheader) {
729                         print "</body></html>";
730                 }
731         }
732
733         if ($op == "viewfeed") {
734
735                 $feed = $_GET["feed"];
736                 $skip = $_GET["skip"];
737                 $subop = $_GET["subop"];
738                 $view_mode = $_GET["view"];
739                 $addheader = $_GET["addheader"];
740                 $limit = $_GET["limit"];
741                 $omode = $_GET["omode"];
742
743                 if ($omode == "xml") {
744                         header("Content-Type: application/xml");
745                 }
746
747                 if (!$feed) {
748                         return;
749                 }
750
751                 if (!$skip) $skip = 0;
752
753                 if ($subop == "undefined") $subop = "";
754
755                 if ($addheader) {
756                         print "<html><head>
757                                 <title>Tiny Tiny RSS : Feed $feed</title>
758                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
759
760                         $user_theme = $_SESSION["theme"];
761                         if ($user_theme) { 
762                                 print "<link rel=\"stylesheet\" type=\"text/css\" 
763                                         href=\"themes/$user_theme/theme.css\">";
764                         }
765
766                         if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
767                                 print "<link rel=\"stylesheet\" 
768                                                 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
769
770                         } else {
771                                 print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
772                                                 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
773                         }
774
775                         print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">  
776                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
777                                 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
778                                 </head><body onload='init()'>";
779                 }
780
781                 if ($subop == "ForceUpdate" && is_int($feed)) {
782
783                         $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
784                                 WHERE id = '$feed'");
785
786                         $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
787
788                         update_rss_feed($link, $feed_url, $feed);
789
790                 }
791
792                 if ($subop == "MarkAllRead")  {
793
794                         if (is_int($feed)) {
795                         
796                                 if ($feed > 0) {
797                                         db_query($link, "UPDATE ttrss_user_entries 
798                                                 SET unread = false,last_read = NOW() 
799                                                 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
800                                                 
801                                 } else if ($feed < 0 && $feed > -10) { // special, like starred
802
803                                         if ($feed == -1) {
804                                                 db_query($link, "UPDATE ttrss_user_entries 
805                                                         SET unread = false,last_read = NOW()
806                                                         WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
807                                         }
808                         
809                                 } else if ($feed < -10) { // label
810
811                                         // TODO make this more efficient
812
813                                         $label_id = -$feed - 11;
814
815                                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
816                                                 WHERE id = '$label_id'");                                       
817
818                                         if ($tmp_result) {
819                                                 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
820
821                                                 db_query($link, "BEGIN");
822
823                                                 $tmp2_result = db_query($link,
824                                                         "SELECT 
825                                                                 int_id 
826                                                         FROM 
827                                                                 ttrss_user_entries,ttrss_entries 
828                                                         WHERE
829                                                                 ref_id = id AND 
830                                                                 $sql_exp AND
831                                                                 owner_uid = " . $_SESSION["uid"]);
832
833                                                 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
834                                                         db_query($link, "UPDATE 
835                                                                 ttrss_user_entries 
836                                                         SET 
837                                                                 unread = false, last_read = NOW()
838                                                         WHERE
839                                                                 int_id = " . $tmp_line["int_id"]);
840                                                 }
841                                                                 
842                                                 db_query($link, "COMMIT");
843
844 /*                                              db_query($link, "UPDATE ttrss_user_entries,ttrss_entries 
845                                                         SET unread = false,last_read = NOW()
846                                                         WHERE $sql_exp
847                                                         AND ref_id = id
848                                                         AND owner_uid = ".$_SESSION["uid"]); */
849                                         }
850                                 }
851                         } else { // tag
852                                 db_query($link, "BEGIN");
853
854                                 $tag_name = db_escape_string($feed);
855
856                                 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
857                                         WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
858
859                                 while ($line = db_fetch_assoc($result)) {
860                                         db_query($link, "UPDATE ttrss_user_entries SET
861                                                 unread = false, last_read = NOW() 
862                                                 WHERE int_id = " . $line["post_int_id"]);
863                                 }
864                                 db_query($link, "COMMIT");
865                         }
866
867                 }
868
869                 $search = db_escape_string($_GET["search"]);
870                 $search_mode = db_escape_string($_GET["smode"]);
871
872                 if ($search) {
873                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
874                                 OR content LIKE '%$search%') AND";
875                 } else {
876                         $search_query_part = "";
877                 }
878
879                 $view_query_part = "";
880
881                 if ($view_mode == "Starred") {
882                         $view_query_part = " marked = true AND ";
883                 }
884
885                 if ($view_mode == "Unread") {
886                         $view_query_part = " unread = true AND ";
887                 }
888
889                 if ($view_mode == "Unread or Starred") {
890                         $view_query_part = " (unread = true OR marked = true) AND ";
891                 }
892
893                 if ($view_mode == "Unread or Updated") {
894                         $view_query_part = " (unread = true OR last_read is NULL) AND ";
895                 }
896
897 /*              $result = db_query($link, "SELECT count(id) AS total_entries 
898                         FROM ttrss_entries WHERE 
899                         $search_query_part
900                         feed_id = '$feed'");
901
902                 $total_entries = db_fetch_result($result, 0, "total_entries"); */
903
904 /*              $result = db_query("SELECT count(id) AS unread_entries 
905                         FROM ttrss_entries WHERE 
906                         $search_query_part
907                         unread = true AND
908                         feed_id = '$feed'");
909
910                 $unread_entries = db_fetch_result($result, 0, "unread_entries"); */
911
912                 if ($limit && $limit != "All") {
913                         $limit_query_part = "LIMIT " . $limit;
914                 } 
915
916                 $vfeed_query_part = "";
917
918                 // override query strategy and enable feed display when searching globally
919                 if ($search && $search_mode == "All feeds") {
920                         $query_strategy_part = "id > 0";
921                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
922                                 id = feed_id) as feed_title,";
923                 } else if (is_int($feed)) {
924                         $query_strategy_part = "ttrss_entries.id > 0";
925                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
926                                 id = feed_id) as feed_title,";
927                 } else if ($feed >= 0) {
928                         $query_strategy_part = "feed_id = '$feed'";
929                 } else if ($feed == -1) { // starred virtual feed
930                         $query_strategy_part = "marked = true";
931                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
932                                 id = feed_id) as feed_title,";
933                 } else if ($feed <= -10) { // labels
934                         $label_id = -$feed - 11;
935
936                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
937                                 WHERE id = '$label_id'");
938                 
939                         $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
940         
941                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
942                                 id = feed_id) as feed_title,";
943                 } else {
944                         $query_strategy_part = "id > 0"; // dumb
945                 }
946
947                 $order_by = "updated DESC";
948
949 //              if ($feed < -10) {
950 //                      $order_by = "feed_id,updated DESC";
951 //              }
952
953                 $feed_title = "";
954
955                 if ($search && $search_mode == "All feeds") {
956                         $feed_title = "Search results";
957                 } else if (is_int($feed)) {
958                         $feed_title = $feed;
959                 } else if ($feed > 0) {
960                         $result = db_query($link, "SELECT title,site_url FROM ttrss_feeds 
961                                 WHERE id = '$feed'");
962
963                         $feed_title = db_fetch_result($result, 0, "title");
964                         $feed_site_url = db_fetch_result($result, 0, "site_url");
965
966                 } else if ($feed == -1) {
967                         $feed_title = "Starred articles";
968                 } else if ($feed < -10) {
969                         $label_id = -$feed - 11;
970                         $result = db_query($link, "SELECT description FROM ttrss_labels
971                                 WHERE id = '$label_id'");
972                         $feed_title = db_fetch_result($result, 0, "description");
973                 } else {
974                         $feed_title = "?";
975                 }
976
977                 if ($feed < -10) error_reporting (0);
978
979                 if (is_int($feed) != 0) {
980
981                         if ($feed > 0) {                        
982                                 $feed_kind = "Feeds";
983                         } else {
984                                 $feed_kind = "Labels";
985                         }
986
987                         if (!$vfeed_query_part) {
988                                 $content_query_part = "content as content_preview,";
989                         } else {
990                                 $content_query_part = "";
991                         }
992
993                         $result = db_query($link, "SELECT 
994                                         id,title,
995                                         SUBSTRING(updated,1,16) as updated,
996                                         unread,feed_id,marked,link,last_read,
997                                         SUBSTRING(last_read,1,19) as last_read_noms,
998                                         $vfeed_query_part
999                                         $content_query_part
1000                                         SUBSTRING(updated,1,19) as updated_noms
1001                                 FROM
1002                                         ttrss_entries,ttrss_user_entries
1003                                 WHERE
1004                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
1005                                 owner_uid = '".$_SESSION["uid"]."' AND
1006                                 $search_query_part
1007                                 $view_query_part
1008                                 $query_strategy_part ORDER BY $order_by
1009                                 $limit_query_part");
1010
1011                 } else {
1012                         // browsing by tag
1013
1014                         $feed_kind = "Tags";
1015
1016                         $result = db_query($link, "SELECT
1017                                 ttrss_entries.id as id,title,
1018                                 SUBSTRING(updated,1,16) as updated,
1019                                 unread,feed_id,
1020                                 marked,link,last_read,
1021                                 SUBSTRING(last_read,1,19) as last_read_noms,
1022                                 $vfeed_query_part
1023                                 $content_query_part
1024                                 SUBSTRING(updated,1,19) as updated_noms
1025                                 FROM
1026                                         ttrss_entries,ttrss_user_entries,ttrss_tags
1027                                 WHERE
1028                                         ref_id = ttrss_entries.id AND
1029                                         ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
1030                                         post_int_id = int_id AND tag_name = '$feed' AND
1031                                         $view_query_part
1032                                         $search_query_part
1033                                         $query_strategy_part ORDER BY $order_by
1034                                 $limit_query_part");    
1035                 }
1036
1037                 if (!$result) {
1038                         if ($omode != "xml") {
1039                                 print "<div align='center'>
1040                                         Could not display feed (query failed). Please check label match syntax or local configuration.</div>";
1041                                 return;
1042                         } else {
1043                                 print "<error error-code=\"8\"/>";
1044
1045                         }
1046                 }
1047         
1048                 if (db_num_rows($result) > 0) {
1049
1050                         if ($omode != "xml") {
1051
1052                                 print "<table class=\"headlinesSubToolbar\" 
1053                                         width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
1054                                 
1055                                 print "<td class=\"headlineActions\">
1056                                         Select: 
1057                                                         <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 
1058                                                                 'RROW-', 'RCHK-', true)\">All</a>,
1059                                                         <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 
1060                                                                 'RROW-', 'RCHK-', true, 'Unread')\">Unread</a>,
1061                                                         <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 
1062                                                                 'RROW-', 'RCHK-', false)\">None</a>
1063                                         &nbsp;&nbsp;
1064                                         Toggle: <a href=\"javascript:toggleUnread()\">Unread</a>,
1065                                                         <a href=\"javascript:toggleStarred()\">Starred</a>";
1066                 
1067                                 print "</td>";
1068                 
1069                                 print "<td class=\"headlineTitle\">";
1070                 
1071                                 if ($feed_site_url) {
1072                                         print "<a target=\"_blank\" href=\"$feed_site_url\">$feed_title</a>";
1073                                 } else {
1074                                         print $feed_title;
1075                                 }
1076                                 
1077                                 print "</td>";
1078                                 print "</tr></table>";
1079                 
1080                                 print "<table class=\"headlinesList\" id=\"headlinesList\" 
1081                                         cellspacing=\"0\" width=\"100%\">";
1082
1083                         } else {
1084                                 print "<headlines feed=\"$feed\" title=\"$feed_title\" site_url=\"$feed_site_url\">";
1085                         }
1086         
1087                         $lnum = 0;
1088         
1089                         error_reporting (DEFAULT_ERROR_LEVEL);
1090         
1091                         $num_unread = 0;
1092         
1093                         while ($line = db_fetch_assoc($result)) {
1094
1095                                 $class = ($lnum % 2) ? "even" : "odd";
1096         
1097                                 $id = $line["id"];
1098                                 $feed_id = $line["feed_id"];
1099         
1100                                 if ($line["last_read"] == "" && 
1101                                                 ($line["unread"] != "t" && $line["unread"] != "1")) {
1102         
1103                                         $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
1104                                                 alt=\"Updated\">";
1105                                 } else {
1106                                         $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
1107                                                 alt=\"Updated\">";
1108                                 }
1109         
1110                                 if ($line["unread"] == "t" || $line["unread"] == "1") {
1111                                         $class .= "Unread";
1112                                         ++$num_unread;
1113                                         $is_unread = 'true';
1114                                 } else {
1115                                         $is_unread = 'false';
1116                                 }
1117         
1118                                 if ($line["marked"] == "t" || $line["marked"] == "1") {
1119                                         $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\" 
1120                                                 alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
1121                                 } else {
1122                                         $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\" 
1123                                                 alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
1124                                 }
1125         
1126                                 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
1127                                         $line["title"] . "</a>";
1128
1129                                 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1130                                         $updated_fmt = smart_date_time(strtotime($line["updated"]));
1131                                 } else {
1132                                         $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1133                                         $updated_fmt = date($short_date, strtotime($line["updated"]));
1134                                 }                               
1135
1136                                 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
1137                                         $content_preview = truncate_string(strip_tags($line["content_preview"]), 101);
1138                                 }
1139
1140                                 if ($omode != "xml") {
1141                                         
1142                                         print "<tr class='$class' id='RROW-$id'>";
1143                                         // onclick=\"javascript:view($id,$feed_id)\">
1144                 
1145                                         print "<td class='hlUpdatePic'>$update_pic</td>";
1146                 
1147                                         print "<td class='hlSelectRow'>
1148                                                 <input type=\"checkbox\" onclick=\"toggleSelectRow(this)\"
1149                                                         class=\"feedCheckBox\" id=\"RCHK-$id\">
1150                                                 </td>";
1151                 
1152                                         print "<td class='hlMarkedPic'>$marked_pic</td>";
1153                 
1154                                         if ($line["feed_title"]) {                      
1155                                                 print "<td class='hlContent'>$content_link</td>";
1156                                                 print "<td class='hlFeed'>
1157                                                         <a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>&nbsp;</td>";
1158                                         } else {                        
1159                                                 print "<td class='hlContent'>";
1160                 
1161                                                 print "<a href=\"javascript:view($id,$feed_id);\">" .
1162                                                         $line["title"];
1163                 
1164                                                 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
1165                                                                 
1166                                                         if ($content_preview) {
1167                                                                 print "<span class=\"contentPreview\"> - $content_preview</span>";
1168                                                         }
1169                                                 }
1170                 
1171                                                 print "</a>";
1172                                                 print "</td>";
1173                                         }
1174                                         
1175                                         print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
1176                 
1177                                         print "</tr>";
1178
1179                                 } else {
1180
1181                                         print "<entry unread='$is_unread' id='$id'>";
1182                                         print "<title><![CDATA[" . $line["title"] . "]]></title>";
1183                                         print "<link>" . $line["link"] . "</link>";
1184                                         print "<updated>$updated_fmt</updated>";
1185                                         if ($content_preview) {
1186                                                 print "<preview><![CDATA[ $content_preview ]]></preview>";
1187                                         }                                       
1188
1189                                         if ($line["feed_title"]) {
1190                                         print "<feed id='$feed_id'><![CDATA[" . $line["feed_title"] . "]]></feed>";
1191                                         }
1192                                         print "</entry>";
1193
1194                                 }
1195                                 
1196         
1197                                 ++$lnum;
1198                         }
1199
1200                         if ($omode != "xml") {                  
1201                                 print "</table>";
1202                         } else {
1203                                 print "</headlines>";
1204                         }
1205
1206                 } else {
1207                         print "<div width='100%' align='center'>No articles found.</div>";
1208                 }
1209
1210                 if ($omode != "xml") {
1211
1212                         print "<script type=\"text/javascript\">
1213                                 document.onkeydown = hotkey_handler;
1214                                 update_all_counters('$feed');
1215                         </script>";
1216         
1217                         if ($addheader) {
1218                                 print "</body></html>";
1219                         }
1220                 }
1221         }
1222
1223         if ($op == "pref-rpc") {
1224
1225                 $subop = $_GET["subop"];
1226
1227                 if ($subop == "unread") {
1228                         $ids = split(",", db_escape_string($_GET["ids"]));
1229                         foreach ($ids as $id) {
1230                                 db_query($link, "UPDATE ttrss_user_entries SET unread = true 
1231                                         WHERE feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
1232                         }
1233
1234                         print "Marked selected feeds as unread.";
1235                 }
1236
1237                 if ($subop == "read") {
1238                         $ids = split(",", db_escape_string($_GET["ids"]));
1239                         foreach ($ids as $id) {
1240                                 db_query($link, "UPDATE ttrss_user_entries 
1241                                         SET unread = false,last_read = NOW() WHERE 
1242                                                 feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
1243                         }
1244
1245                         print "Marked selected feeds as read.";
1246
1247                 }
1248
1249         }
1250
1251         if ($op == "pref-feeds") {
1252         
1253                 $subop = $_GET["subop"];
1254
1255                 if ($subop == "editSave") {
1256                         $feed_title = db_escape_string($_GET["t"]);
1257                         $feed_link = db_escape_string($_GET["l"]);
1258                         $upd_intl = db_escape_string($_GET["ui"]);
1259                         $purge_intl = db_escape_string($_GET["pi"]);
1260                         $feed_id = db_escape_string($_GET["id"]);
1261                         $cat_id = db_escape_string($_GET["catid"]);
1262
1263                         if (strtoupper($upd_intl) == "DEFAULT")
1264                                 $upd_intl = 0;
1265
1266                         if (strtoupper($upd_intl) == "DISABLED")
1267                                 $upd_intl = -1;
1268
1269                         if (strtoupper($purge_intl) == "DEFAULT")
1270                                 $purge_intl = 0;
1271
1272                         if (strtoupper($purge_intl) == "DISABLED")
1273                                 $purge_intl = -1;
1274
1275                         if ($cat_id != 0) {
1276                                 $category_qpart = "cat_id = '$cat_id'";
1277                         } else {
1278                                 $category_qpart = 'cat_id = NULL';
1279                         }
1280
1281                         $result = db_query($link, "UPDATE ttrss_feeds SET 
1282                                 $category_qpart,
1283                                 title = '$feed_title', feed_url = '$feed_link',
1284                                 update_interval = '$upd_intl',
1285                                 purge_interval = '$purge_intl'
1286                                 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);                    
1287
1288                 }
1289
1290                 if ($subop == "remove") {
1291
1292                         if (!WEB_DEMO_MODE) {
1293
1294                                 $ids = split(",", db_escape_string($_GET["ids"]));
1295
1296                                 foreach ($ids as $id) {
1297                                         db_query($link, "DELETE FROM ttrss_feeds 
1298                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
1299
1300                                         $icons_dir = ICONS_DIR;
1301                                         
1302                                         if (file_exists($icons_dir . "/$id.ico")) {
1303                                                 unlink($icons_dir . "/$id.ico");
1304                                         }
1305                                 }
1306                         }
1307                 }
1308
1309                 if ($subop == "add") {
1310                 
1311                         if (!WEB_DEMO_MODE) {
1312
1313                                 $feed_link = db_escape_string(trim($_GET["link"]));
1314
1315                                 $result = db_query($link,
1316                                         "SELECT id FROM ttrss_feeds 
1317                                         WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1318
1319                                 if (db_num_rows($result) == 0) {
1320                                         
1321                                         $result = db_query($link,
1322                                                 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title) 
1323                                                 VALUES ('".$_SESSION["uid"]."', '$feed_link', '')");
1324
1325                                         $result = db_query($link,
1326                                         "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' 
1327                                                 AND owner_uid = " . $_SESSION["uid"]);
1328
1329                                         $feed_id = db_fetch_result($result, 0, "id");
1330
1331                                         if ($feed_id) {
1332                                                 update_rss_feed($link, $feed_link, $feed_id, true);
1333                                         }
1334                                 } else {
1335
1336                                         print "<div class=\"warning\">
1337                                                 Feed <b>$feed_link</b> already exists in the database.
1338                                         </div>";
1339                                 }
1340                         }
1341                 }
1342
1343                 if ($subop == "addCat") {
1344
1345                         if (!WEB_DEMO_MODE) {
1346
1347                                 $feed_cat = db_escape_string(trim($_GET["cat"]));
1348
1349                                 $result = db_query($link,
1350                                         "SELECT id FROM ttrss_feed_categories
1351                                         WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
1352
1353                                 if (db_num_rows($result) == 0) {
1354                                         
1355                                         $result = db_query($link,
1356                                                 "INSERT INTO ttrss_feed_categories (owner_uid,title) 
1357                                                 VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
1358
1359                                 } else {
1360
1361                                         print "<div class=\"warning\">
1362                                                 Category <b>$feed_cat</b> already exists in the database.
1363                                         </div>";
1364                                 }
1365
1366
1367                         }
1368                 }
1369
1370                 if ($subop == "removeCats") {
1371
1372                         if (!WEB_DEMO_MODE) {
1373
1374                                 $ids = split(",", db_escape_string($_GET["ids"]));
1375
1376                                 foreach ($ids as $id) {
1377
1378                                         db_query($link, "BEGIN");
1379
1380                                         $result = db_query($link, 
1381                                                 "SELECT count(id) as num_feeds FROM ttrss_feeds 
1382                                                         WHERE cat_id = '$id'");
1383
1384                                         $num_feeds = db_fetch_result($result, 0, "num_feeds");
1385
1386                                         if ($num_feeds == 0) {
1387                                                 db_query($link, "DELETE FROM ttrss_feed_categories
1388                                                         WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
1389                                         } else {
1390
1391                                                 print "<div class=\"warning\">
1392                                                         Unable to delete non empty feed categories.</div>";
1393                                                         
1394                                         }
1395
1396                                         db_query($link, "COMMIT");
1397                                 }
1398                         }
1399                 }
1400
1401                 if ($subop == "categorize") {
1402
1403                         if (!WEB_DEMO_MODE) {
1404
1405                                 $ids = split(",", db_escape_string($_GET["ids"]));
1406
1407                                 $cat_id = db_escape_string($_GET["cat_id"]);
1408
1409                                 if ($cat_id == 0) {
1410                                         $cat_id_qpart = 'NULL';
1411                                 } else {
1412                                         $cat_id_qpart = "'$cat_id'";
1413                                 }
1414
1415                                 db_query($link, "BEGIN");
1416
1417                                 foreach ($ids as $id) {
1418                                 
1419                                         db_query($link, "UPDATE ttrss_feeds SET cat_id = $cat_id_qpart
1420                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
1421                                 }
1422
1423                                 db_query($link, "COMMIT");
1424                         }
1425
1426                 }
1427
1428 //              print "<h3>Edit Feeds</h3>";
1429
1430                 $result = db_query($link, "SELECT id,title,feed_url,last_error 
1431                         FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
1432
1433                 if (db_num_rows($result) > 0) {
1434                 
1435                         print "<div class=\"warning\">";
1436                 
1437                         print "<a href=\"javascript:showBlockElement('feedUpdateErrors')\">
1438                                 <b>Feeds with update errors</b> (click to expand)</a>";
1439
1440                         print "<ul id=\"feedUpdateErrors\" class=\"nomarks\">";
1441                                                 
1442                         while ($line = db_fetch_assoc($result)) {
1443                                 print "<li>" . $line["title"] . " (" . $line["feed_url"] . "): " . 
1444                                         $line["last_error"];
1445                         }
1446
1447                         print "</ul>";
1448                         print "</div>";
1449
1450                 }
1451
1452                 $feed_search = db_escape_string($_GET["search"]);
1453
1454                 if (array_key_exists("search", $_GET)) {
1455                         $_SESSION["prefs_feed_search"] = $feed_search;
1456                 } else {
1457                         $feed_search = $_SESSION["prefs_feed_search"];
1458                 }
1459
1460                 print "<table width='100%' class=\"prefGenericAddBox\" 
1461                         cellspacing='0' cellpadding='0'><tr>
1462                         <td>
1463                                 <input id=\"fadd_link\" 
1464                                 onchange=\"javascript:addFeed()\"
1465                                 size=\"40\">
1466                                 <input type=\"submit\" class=\"button\"
1467                                 onclick=\"javascript:addFeed()\" value=\"Add feed\">
1468                         </td><td align='right'>
1469                                 <input id=\"feed_search\" size=\"20\"  
1470                                 onchange=\"javascript:updateFeedList()\"
1471                                 value=\"$feed_search\">
1472                                 <input type=\"submit\" class=\"button\" 
1473                                 onclick=\"javascript:updateFeedList()\" value=\"Search\">
1474                         </td>                   
1475                         </tr></table>";
1476
1477                 $feeds_sort = db_escape_string($_GET["sort"]);
1478
1479                 if (!$feeds_sort || $feeds_sort == "undefined") {
1480                         $feeds_sort = $_SESSION["pref_sort_feeds"];                     
1481                         if (!$feeds_sort) $feeds_sort = "title";
1482                 }
1483
1484                 $_SESSION["pref_sort_feeds"] = $feeds_sort;
1485
1486                 if ($feed_search) {
1487                         $search_qpart = "UPPER(title) LIKE UPPER('%$feed_search%') AND";
1488                 } else {
1489                         $search_qpart = "";
1490                 }
1491
1492                 $result = db_query($link, "SELECT 
1493                                 id,title,feed_url,substring(last_updated,1,16) as last_updated,
1494                                 update_interval,purge_interval,
1495                                 (SELECT title FROM ttrss_feed_categories 
1496                                         WHERE id = cat_id) AS category
1497                         FROM 
1498                                 ttrss_feeds 
1499                         WHERE 
1500                                 $search_qpart owner_uid = '".$_SESSION["uid"]."'                        
1501                         ORDER by $feeds_sort,title");
1502
1503                 if (db_num_rows($result) != 0) {
1504
1505                         print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
1506
1507                         print "<p><table width=\"100%\" cellspacing=\"0\" 
1508                                 class=\"prefFeedList\" id=\"prefFeedList\">";
1509                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
1510                                 Select: 
1511                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedList', 
1512                                                 'FEEDR-', 'FRCHK-', true)\">All</a>,
1513                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedList', 
1514                                                 'FEEDR-', 'FRCHK-', false)\">None</a>
1515                                 </td</tr>";
1516
1517                         print "<tr class=\"title\">
1518                                                 <td width=\"3%\">&nbsp;</td>
1519                                                 <td width=\"3%\">Select</td>
1520                                                 <td width=\"20%\">
1521                                                         <a href=\"javascript:updateFeedList('title')\">Title</a></td>
1522                                                 <td width=\"20%\">
1523                                                         <a href=\"javascript:updateFeedList('feed_url')\">Link</a>
1524                                                 </td>";
1525         
1526                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
1527                                 print "<td width=\"10%\">
1528                                         <a href=\"javascript:updateFeedList('category')\">Category</a></td>";
1529                         }
1530                         
1531                         print "
1532                                 <td width=\"10%\">
1533                                         <a href=\"javascript:updateFeedList('update_interval')\">Update Interval</a>
1534                                 </td>
1535                                 <td width=\"10%\">
1536                                         <a href=\"javascript:updateFeedList('purge_interval')\">Purge Days</a>
1537                                 </td>
1538                         </tr>";
1539                         
1540                         $lnum = 0;
1541                         
1542                         while ($line = db_fetch_assoc($result)) {
1543         
1544                                 $class = ($lnum % 2) ? "even" : "odd";
1545         
1546                                 $feed_id = $line["id"];
1547         
1548                                 $edit_feed_id = $_GET["id"];
1549         
1550                                 if ($subop == "edit" && $feed_id != $edit_feed_id) {
1551                                         $class .= "Grayed";
1552                                         $this_row_id = "";
1553                                 } else {
1554                                         $this_row_id = "id=\"FEEDR-$feed_id\"";
1555                                 }
1556         
1557                                 print "<tr class=\"$class\" $this_row_id>";
1558         
1559                                 $icon_file = ICONS_DIR . "/$feed_id.ico";
1560         
1561                                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1562                                                 $feed_icon = "<img width=\"16\" height=\"16\"
1563                                                         src=\"" . ICONS_URL . "/$feed_id.ico\">";
1564                                 } else {
1565                                         $feed_icon = "&nbsp;";
1566                                 }
1567                                 print "<td align='center'>$feed_icon</td>";             
1568         
1569                                 $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
1570                                 $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
1571                                 $edit_cat = htmlspecialchars(db_unescape_string($line["category"]));
1572         
1573                                 if (!$edit_cat) $edit_cat = "Uncategorized";
1574         
1575                                 if (!$edit_feed_id || $subop != "edit") {
1576         
1577                                         print "<td><input onclick='toggleSelectRow(this);' 
1578                                         type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
1579
1580                                         $edit_title = truncate_string($edit_title, 40);
1581                                         $edit_link = truncate_string($edit_link, 60);
1582         
1583                                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1584                                                 $edit_title . "</a></td>";              
1585                                                 
1586                                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1587                                                 $edit_link . "</a></td>";               
1588         
1589                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
1590                                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1591                                                         $edit_cat . "</a></td>";                
1592                                         }
1593         
1594                                         if ($line["update_interval"] == "0")
1595                                                 $line["update_interval"] = "Default";
1596         
1597                                         if ($line["update_interval"] == "-1")
1598                                                 $line["update_interval"] = "Disabled";
1599
1600                                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1601                                                 $line["update_interval"] . "</a></td>";
1602         
1603                                         if ($line["purge_interval"] == "0")
1604                                                 $line["purge_interval"] = "Default";
1605         
1606                                         if ($line["purge_interval"] < 0)
1607                                                 $line["purge_interval"] = "Disabled";
1608         
1609                                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1610                                                 $line["purge_interval"] . "</a></td>";
1611         
1612                                 } else if ($feed_id != $edit_feed_id) {
1613         
1614                                         print "<td><input disabled=\"true\" type=\"checkbox\" 
1615                                                 id=\"FRCHK-".$line["id"]."\"></td>";
1616
1617                                         $edit_title = truncate_string($edit_title, 40);
1618                                         $edit_link = truncate_string($edit_link, 60);
1619
1620                                         print "<td>$edit_title</td>";           
1621                                         print "<td>$edit_link</td>";            
1622         
1623                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
1624                                                 print "<td>$edit_cat</td>";             
1625                                         }
1626         
1627                                         if ($line["update_interval"] == "0")
1628                                                 $line["update_interval"] = "Default";
1629         
1630                                         print "<td>" . $line["update_interval"] . "</td>";
1631         
1632                                         if ($line["purge_interval"] == "0")
1633                                                 $line["purge_interval"] = "Default";
1634         
1635                                         if ($line["purge_interval"] < 0)
1636                                                 $line["purge_interval"] = "Disabled";
1637         
1638                                         print "<td>" . $line["purge_interval"] . "</td>";
1639         
1640                                 } else {
1641         
1642                                         print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1643         
1644                                         print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
1645                                         print "<td><input id=\"iedit_link\" value=\"$edit_link\"></td>";
1646         
1647                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
1648         
1649                                                 print "<td>";
1650                                                 print "<select id=\"iedit_fcat\">";
1651                                                 print "<option id=\"0\">Uncategorized</option>";
1652                 
1653                                                 $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1654                                                         WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1655                 
1656                                                 if (db_num_rows($tmp_result) > 0) {
1657                                                         print "<option disabled>--------</option>";
1658                                                 }
1659
1660                                                 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1661                                                         if ($tmp_line["id"] == $line["cat_id"]) {
1662                                                                 $is_selected = "selected";
1663                                                         } else {
1664                                                                 $is_selected = "";
1665                                                         }
1666                                                         printf("<option $is_selected id='%d'>%s</option>", 
1667                                                                 $tmp_line["id"], $tmp_line["title"]);
1668                                                 }
1669                 
1670                                                 print "</select></td>";
1671                                                 print "</td>";
1672         
1673                                         }
1674                                         
1675                                         print "<td><input id=\"iedit_updintl\" 
1676                                                 value=\"".$line["update_interval"]."\"></td>";
1677                                         print "<td><input id=\"iedit_purgintl\" 
1678                                                 value=\"".$line["purge_interval"]."\"></td>";
1679                                                 
1680                                 }
1681         
1682 /*                              if (!$line["last_updated"]) $line["last_updated"] = "Never";
1683         
1684                                 print "<td>" . $line["last_updated"] . "</td>"; */
1685                                 
1686                                 print "</tr>";
1687         
1688                                 ++$lnum;
1689                         }
1690         
1691                         print "</table>";
1692
1693                         print "<p>";
1694         
1695                         if ($subop == "edit") {
1696                                 print "Edit feed:&nbsp;
1697                                         <input type=\"submit\" class=\"button\" 
1698                                                 onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
1699                                         <input type=\"submit\" class=\"button\" 
1700                                                 onclick=\"javascript:feedEditSave()\" value=\"Save\">";
1701                         } else {
1702         
1703                                 print "
1704                                         Selection:&nbsp;
1705                                 <input type=\"submit\" class=\"button\" 
1706                                         onclick=\"javascript:selectedFeedDetails()\" value=\"Details\">
1707                                 <input type=\"submit\" class=\"button\" 
1708                                         onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
1709                                 <input type=\"submit\" class=\"button\" 
1710                                         onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
1711
1712                                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1713
1714                                         print "&nbsp;&nbsp;";                           
1715
1716                                         $result = db_query($link, "SELECT title,id FROM ttrss_feed_categories
1717                                                 WHERE owner_uid = ".$_SESSION["uid"]."
1718                                                 ORDER BY title");
1719
1720                                         print "<select id=\"sfeed_set_fcat\">";
1721                                         print "<option id=\"0\">Uncategorized</option>";
1722
1723                                         if (db_num_rows($result) != 0) {
1724                 
1725                                                 print "<option disabled>--------</option>";
1726
1727                                                 while ($line = db_fetch_assoc($result)) {
1728                                                         printf("<option id='%d'>%s</option>", 
1729                                                                 $line["id"], $line["title"]);
1730                                                 }               
1731                                         }
1732
1733                                         print "</select>";
1734
1735                                         print " <input type=\"submit\" class=\"button\" 
1736                                         onclick=\"javascript:categorizeSelectedFeeds()\" value=\"Set category\">";
1737
1738                                 }
1739
1740                                 if (get_pref($link, 'ENABLE_PREFS_CATCHUP_UNCATCHUP')) {
1741                                         print "
1742                                         <input type=\"submit\" class=\"button\" 
1743                                                 onclick=\"javascript:readSelectedFeeds(true)\" value=\"Mark as read\">
1744                                         <input type=\"submit\" class=\"button\" 
1745                                                 onclick=\"javascript:readSelectedFeeds(false)\" 
1746                                                 value=\"Mark as unread\">&nbsp;";
1747                                 }
1748                                 
1749                                 print "
1750                                         &nbsp;All feeds: <input type=\"submit\" 
1751                                                         class=\"button\" onclick=\"gotoExportOpml()\" 
1752                                                         value=\"Export OPML\">";                        
1753                                 }
1754                 } else {
1755
1756                         print "<p>No feeds defined.</p>";
1757
1758                 }
1759
1760                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1761
1762                         print "<h3>Edit Categories</h3>";
1763
1764         //              print "<h3>Categories</h3>";
1765
1766                         print "<div class=\"prefGenericAddBox\">
1767                                 <input id=\"fadd_cat\" 
1768                                         onchange=\"javascript:addFeedCat()\"
1769                                         size=\"40\">&nbsp;
1770                                 <input 
1771                                         type=\"submit\" class=\"button\" 
1772                                         onclick=\"javascript:addFeedCat()\" value=\"Add category\"></div>";
1773         
1774                         $result = db_query($link, "SELECT title,id FROM ttrss_feed_categories
1775                                 WHERE owner_uid = ".$_SESSION["uid"]."
1776                                 ORDER BY title");
1777
1778                         if (db_num_rows($result) != 0) {
1779         
1780                                 print "<p><table width=\"100%\" class=\"prefFeedCatList\" 
1781                                         cellspacing=\"0\" id=\"prefFeedCatList\">";
1782
1783                                 print "<tr><td class=\"selectPrompt\" colspan=\"8\">
1784                                 Select: 
1785                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedCatList', 
1786                                                 'FCATR-', 'FCCHK-', true)\">All</a>,
1787                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedCatList', 
1788                                                 'FCATR-', 'FCCHK-', false)\">None</a>
1789                                 </td</tr>";
1790
1791                                 print "<tr class=\"title\">
1792                                                         <td width=\"10%\">Select</td><td width=\"80%\">Title</td>
1793                                                 </tr>";
1794                                                 
1795                                 $lnum = 0;
1796                                 
1797                                 while ($line = db_fetch_assoc($result)) {
1798                 
1799                                         $class = ($lnum % 2) ? "even" : "odd";
1800                 
1801                                         $cat_id = $line["id"];
1802                 
1803                                         $edit_cat_id = $_GET["id"];
1804                 
1805                                         if ($subop == "editCat" && $cat_id != $edit_cat_id) {
1806                                                 $class .= "Grayed";
1807                                                 $this_row_id = "";
1808                                         } else {
1809                                                 $this_row_id = "id=\"FCATR-$cat_id\"";
1810                                         }
1811                 
1812                                         print "<tr class=\"$class\" $this_row_id>";
1813                 
1814                                         $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
1815                 
1816                                         if (!$edit_cat_id || $subop != "editCat") {
1817                 
1818                                                 print "<td><input onclick='toggleSelectRow(this);' 
1819                                                 type=\"checkbox\" id=\"FCCHK-".$line["id"]."\"></td>";
1820                 
1821                                                 print "<td><a href=\"javascript:editFeedCat($cat_id);\">" . 
1822                                                         $edit_title . "</a></td>";              
1823                 
1824                                         } else if ($cat_id != $edit_cat_id) {
1825                 
1826                                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
1827                                                         id=\"FRCHK-".$line["id"]."\"></td>";
1828                 
1829                                                 print "<td>$edit_title</td>";           
1830                 
1831                                         } else {
1832                 
1833                                                 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1834                 
1835                                                 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
1836                                                 
1837                                         }
1838                                         
1839                                         print "</tr>";
1840                 
1841                                         ++$lnum;
1842                                 }
1843         
1844                                 print "</table>";
1845         
1846                                 print "<p>";
1847         
1848                                 if ($subop == "editCat") {
1849                                         print "Edit category:&nbsp;
1850                                                 <input type=\"submit\" class=\"button\" 
1851                                                         onclick=\"javascript:feedCatEditCancel()\" value=\"Cancel\">
1852                                                 <input type=\"submit\" class=\"button\" 
1853                                                         onclick=\"javascript:feedCatEditSave()\" value=\"Save\">";
1854                                         } else {
1855                 
1856                                         print "
1857                                                 Selection:&nbsp;
1858                                         <input type=\"submit\" class=\"button\" 
1859                                                 onclick=\"javascript:editSelectedFeedCat()\" value=\"Edit\">
1860                                         <input type=\"submit\" class=\"button\" 
1861                                                 onclick=\"javascript:removeSelectedFeedCats()\" value=\"Remove\">";
1862         
1863                                 }
1864         
1865                         } else {
1866                                 print "<p>No feed categories defined.</p>";
1867                         }
1868                 }
1869
1870                 print "<h3>Import OPML</h3>
1871                 <form   enctype=\"multipart/form-data\" method=\"POST\" action=\"opml.php\">
1872                         File: <input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
1873                         <input class=\"button\" name=\"op\" onclick=\"return validateOpmlImport();\"
1874                                 type=\"submit\" value=\"Import\">
1875                         </form>";
1876
1877         }
1878
1879         if ($op == "pref-filters") {
1880
1881                 $subop = $_GET["subop"];
1882
1883                 if ($subop == "editSave") {
1884
1885                         $regexp = db_escape_string($_GET["r"]);
1886                         $descr = db_escape_string($_GET["d"]);
1887                         $match = db_escape_string($_GET["m"]);
1888                         $filter_id = db_escape_string($_GET["id"]);
1889                         $feed_id = db_escape_string($_GET["fid"]);
1890
1891                         if (!$feed_id) {
1892                                 $feed_id = 'NULL';
1893                         } else {
1894                                 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
1895                         }
1896                         
1897                         $result = db_query($link, "UPDATE ttrss_filters SET 
1898                                 reg_exp = '$regexp', 
1899                                 description = '$descr',
1900                                 feed_id = $feed_id,
1901                                 filter_type = (SELECT id FROM ttrss_filter_types WHERE
1902                                         description = '$match')
1903                                 WHERE id = '$filter_id'");
1904                 }
1905
1906                 if ($subop == "remove") {
1907
1908                         if (!WEB_DEMO_MODE) {
1909
1910                                 $ids = split(",", db_escape_string($_GET["ids"]));
1911
1912                                 foreach ($ids as $id) {
1913                                         db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
1914                                         
1915                                 }
1916                         }
1917                 }
1918
1919                 if ($subop == "add") {
1920                 
1921                         if (!WEB_DEMO_MODE) {
1922
1923                                 $regexp = db_escape_string(trim($_GET["regexp"]));
1924                                 $match = db_escape_string(trim($_GET["match"]));
1925                                 $feed_id = db_escape_string($_GET["fid"]);
1926
1927                                 if (!$feed_id) {
1928                                         $feed_id = 'NULL';
1929                                 } else {
1930                                         $feed_id = sprintf("'%s'", db_escape_string($feed_id));
1931                                 }
1932
1933                                 $result = db_query($link,
1934                                         "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id) VALUES 
1935                                                 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
1936                                                         description = '$match'),'".$_SESSION["uid"]."', $feed_id)");
1937                         } 
1938                 }
1939
1940                 $result = db_query($link, "SELECT description 
1941                         FROM ttrss_filter_types ORDER BY description");
1942
1943                 $filter_types = array();
1944
1945                 while ($line = db_fetch_assoc($result)) {
1946                         array_push($filter_types, $line["description"]);
1947                 }
1948
1949                 print "<div class=\"prefGenericAddBox\">
1950                 <input id=\"fadd_regexp\" onchange=\"javascript:addFilter()\" size=\"40\">&nbsp;";
1951                 
1952                 print_select("fadd_match", "Title", $filter_types);     
1953
1954                 print "&nbsp;<select id=\"fadd_feed\">";
1955
1956                 print "<option selected id=\"0\">All feeds</option>";
1957
1958                 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1959                         WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1960
1961                 if (db_num_rows($result) > 0) {
1962                         print "<option disabled>--------</option>";
1963                 }
1964
1965                 while ($line = db_fetch_assoc($result)) {
1966                         printf("<option id='%d'>%s</option>", $line["id"], $line["title"]);
1967                 }
1968
1969                 print "</select>&nbsp;";
1970         
1971                 print "<input type=\"submit\" 
1972                         class=\"button\" onclick=\"javascript:addFilter()\" 
1973                         value=\"Add filter\">";
1974
1975                 $result = db_query($link, "SELECT 
1976                                 ttrss_filters.id AS id,reg_exp,
1977                                 ttrss_filters.description AS description,
1978                                 ttrss_filter_types.name AS filter_type_name,
1979                                 ttrss_filter_types.description AS filter_type_descr,
1980                                 feed_id,
1981                                 (SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
1982                         FROM 
1983                                 ttrss_filters,ttrss_filter_types
1984                         WHERE
1985                                 filter_type = ttrss_filter_types.id AND
1986                                 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
1987                         ORDER by reg_exp");
1988
1989                 if (db_num_rows($result) != 0) {
1990
1991                         print "<p><table width=\"100%\" cellspacing=\"0\" class=\"prefFilterList\" 
1992                                 id=\"prefFilterList\">";
1993
1994                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
1995                                 Select: 
1996                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFilterList', 
1997                                                 'FILRR-', 'FICHK-', true)\">All</a>,
1998                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFilterList', 
1999                                                 'FILRR-', 'FICHK-', false)\">None</a>
2000                                 </td</tr>";
2001
2002                         print "<tr class=\"title\">
2003                                                 <td width=\"5%\">Select</td><td width=\"30%\">Filter expression</td>
2004                                                 <td width=\"30%\">Feed</td><td width=\"10%\">Match</td>
2005                                                 <td width=\"30%\">Description</td></tr>";
2006                 
2007                         $lnum = 0;
2008                         
2009                         while ($line = db_fetch_assoc($result)) {
2010         
2011                                 $class = ($lnum % 2) ? "even" : "odd";
2012         
2013                                 $filter_id = $line["id"];
2014                                 $edit_filter_id = $_GET["id"];
2015         
2016                                 if ($subop == "edit" && $filter_id != $edit_filter_id) {
2017                                         $class .= "Grayed";
2018                                         $this_row_id = "";
2019                                 } else {
2020                                         $this_row_id = "id=\"FILRR-$filter_id\"";
2021                                 }
2022         
2023                                 print "<tr class=\"$class\" $this_row_id>";
2024         
2025                                 $line["regexp"] = htmlspecialchars($line["reg_exp"]);
2026                                 $line["description"] = htmlspecialchars($line["description"]);
2027         
2028                                 if (!$line["feed_title"]) $line["feed_title"] = "All feeds";
2029         
2030                                 if (!$edit_filter_id || $subop != "edit") {
2031         
2032                                         if (!$line["description"]) $line["description"] = "[No description]";
2033         
2034                                         print "<td><input onclick='toggleSelectRow(this);' 
2035                                         type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
2036         
2037                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2038                                                 $line["reg_exp"] . "</td>";             
2039         
2040                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2041                                                 $line["feed_title"] . "</td>";                  
2042         
2043                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2044                                                 $line["filter_type_descr"] . "</td>";           
2045                                                 
2046                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2047                                                 $line["description"] . "</td>";                 
2048         
2049                                 } else if ($filter_id != $edit_filter_id) {
2050         
2051                                         if (!$line["description"]) $line["description"] = "[No description]";
2052         
2053                                         print "<td><input disabled=\"true\" type=\"checkbox\" 
2054                                                 id=\"FICHK-".$line["id"]."\"></td>";
2055         
2056                                         print "<td>".$line["reg_exp"]."</td>";          
2057                                         print "<td>".$line["feed_title"]."</td>";
2058                                         print "<td>".$line["filter_type_descr"]."</td>";
2059                                         print "<td>".$line["description"]."</td>";              
2060         
2061                                 } else {
2062         
2063                                         print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
2064         
2065                                         print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
2066                                                 "\"></td>";
2067         
2068                                         print "<td>";
2069         
2070                                         print "<select id=\"iedit_feed\">";
2071         
2072                                         print "<option id=\"0\">All feeds</option>";
2073         
2074                                         if (db_num_rows($result) > 0) {
2075                                                 print "<option disabled>--------</option>";
2076                                         }
2077         
2078                                         $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feeds
2079                                                 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2080         
2081                                         while ($tmp_line = db_fetch_assoc($tmp_result)) {
2082                                                 if ($tmp_line["id"] == $line["feed_id"]) {
2083                                                         $is_selected = "selected";
2084                                                 } else {
2085                                                         $is_selected = "";
2086                                                 }
2087                                                 printf("<option $is_selected id='%d'>%s</option>", 
2088                                                         $tmp_line["id"], $tmp_line["title"]);
2089                                         }
2090         
2091                                         print "</select></td>";
2092         
2093                                         print "<td>";
2094                                         print_select("iedit_match", $line["filter_type_descr"], $filter_types);
2095                                         print "</td>";
2096         
2097                                         print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
2098                                                 "\"></td>";
2099         
2100                                         print "</td>";
2101                                 }
2102                                 
2103                                 print "</tr>";
2104         
2105                                 ++$lnum;
2106                         }
2107         
2108                         if ($lnum == 0) {
2109                                 print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
2110                         }
2111         
2112                         print "</table>";
2113         
2114                         print "<p>";
2115         
2116                         if ($subop == "edit") {
2117                                 print "Edit feed:
2118                                         <input type=\"submit\" class=\"button\" 
2119                                                 onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">
2120                                         <input type=\"submit\" class=\"button\" 
2121                                                 onclick=\"javascript:filterEditSave()\" value=\"Save\">";
2122                                                 
2123                         } else {
2124         
2125                                 print "
2126                                         Selection:
2127                                 <input type=\"submit\" class=\"button\" 
2128                                         onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
2129                                 <input type=\"submit\" class=\"button\" 
2130                                         onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
2131                         }
2132
2133                 } else {
2134
2135                         print "<p>No filters defined.</p>";
2136
2137                 }
2138         }
2139
2140         if ($op == "pref-labels") {
2141
2142                 $subop = $_GET["subop"];
2143
2144                 if ($subop == "test") {
2145
2146                         $expr = $_GET["expr"];
2147                         $descr = $_GET["descr"];
2148
2149                         print "<div class='infoBoxContents'>";
2150                 
2151                         print "<h1>Label &laquo;$descr&raquo;</h1>";
2152
2153 //                      print "<p><b>Expression</b>: $expr</p>";
2154
2155                         $result = db_query($link, 
2156                                 "SELECT count(id) AS num_matches
2157                                         FROM ttrss_entries,ttrss_user_entries
2158                                         WHERE ($expr) AND 
2159                                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
2160                                                 owner_uid = " . $_SESSION["uid"]);
2161
2162                         $num_matches = db_fetch_result($result, 0, "num_matches");;
2163                         
2164                         if ($num_matches > 0) { 
2165
2166                                 print "<p>Query returned <b>$num_matches</b> matches, first 5 follow:</p>";
2167
2168                                 $result = db_query($link, 
2169                                         "SELECT title, 
2170                                                 (SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
2171                                         FROM ttrss_entries,ttrss_user_entries
2172                                                         WHERE ($expr) AND 
2173                                                         ttrss_user_entries.ref_id = ttrss_entries.id
2174                                                         AND owner_uid = " . $_SESSION["uid"] . " 
2175                                                         ORDER BY date_entered DESC LIMIT 5");
2176
2177                                 print "<ul class=\"nomarks\">";
2178                                 while ($line = db_fetch_assoc($result)) {
2179                                         print "<li>".$line["title"].
2180                                                 " <span class=\"insensitive\">(".$line["feed_title"].")</span></li>";
2181                                 }
2182                                 print "</ul>";
2183
2184                         } else {
2185                                 print "<p>Query didn't return any matches.</p>";
2186                         }
2187
2188                         print "</div>";
2189
2190                         print "<div align='center'>
2191                                 <input type='submit' class='button'                     
2192                                 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
2193                         return;
2194                 }
2195
2196                 if ($subop == "editSave") {
2197
2198                         $sql_exp = $_GET["s"];
2199                         $descr = $_GET["d"];
2200                         $label_id = db_escape_string($_GET["id"]);
2201                         
2202 //                      print "$sql_exp : $descr : $label_id";
2203                         
2204                         $result = db_query($link, "UPDATE ttrss_labels SET 
2205                                 sql_exp = '$sql_exp', 
2206                                 description = '$descr'
2207                                 WHERE id = '$label_id'");
2208                 }
2209
2210                 if ($subop == "remove") {
2211
2212                         if (!WEB_DEMO_MODE) {
2213
2214                                 $ids = split(",", db_escape_string($_GET["ids"]));
2215
2216                                 foreach ($ids as $id) {
2217                                         db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
2218                                         
2219                                 }
2220                         }
2221                 }
2222
2223                 if ($subop == "add") {
2224                 
2225                         if (!WEB_DEMO_MODE) {
2226
2227                                 // no escaping is done here on purpose
2228                                 $exp = trim($_GET["exp"]);
2229                                         
2230                                 $result = db_query($link,
2231                                         "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
2232                                                 VALUES ('$exp', '$exp', '".$_SESSION["uid"]."')");
2233                         } 
2234                 }
2235
2236                 print "<div class=\"prefGenericAddBox\">
2237                         <input size=\"40\" id=\"ladd_expr\">&nbsp;";
2238                         
2239                 print"<input type=\"submit\" class=\"button\" 
2240                         onclick=\"javascript:addLabel()\" value=\"Add label\"></div>";
2241
2242                 $result = db_query($link, "SELECT 
2243                                 id,sql_exp,description
2244                         FROM 
2245                                 ttrss_labels 
2246                         WHERE 
2247                                 owner_uid = ".$_SESSION["uid"]."
2248                         ORDER by description");
2249
2250                 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
2251
2252                 if (db_num_rows($result) != 0) {
2253
2254                         print "<p><table width=\"100%\" cellspacing=\"0\" 
2255                                 class=\"prefLabelList\" id=\"prefLabelList\">";
2256
2257                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
2258                                 Select: 
2259                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefLabelList', 
2260                                                 'LILRR-', 'LICHK-', true)\">All</a>,
2261                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefLabelList', 
2262                                                 'LILRR-', 'LICHK-', false)\">None</a>
2263                                 </td</tr>";
2264
2265                         print "<tr class=\"title\">
2266                                                 <td width=\"5%\">Select</td><td width=\"40%\">SQL expression
2267                                                 <a class=\"helpLink\" href=\"javascript:displayHelpInfobox(1)\">(?)</a>
2268                                                 </td>
2269                                                 <td width=\"40%\">Caption</td></tr>";
2270                         
2271                         $lnum = 0;
2272                         
2273                         while ($line = db_fetch_assoc($result)) {
2274         
2275                                 $class = ($lnum % 2) ? "even" : "odd";
2276         
2277                                 $label_id = $line["id"];
2278                                 $edit_label_id = $_GET["id"];
2279         
2280                                 if ($subop == "edit" && $label_id != $edit_label_id) {
2281                                         $class .= "Grayed";
2282                                         $this_row_id = "";
2283                                 } else {
2284                                         $this_row_id = "id=\"LILRR-$label_id\"";
2285                                 }
2286         
2287                                 print "<tr class=\"$class\" $this_row_id>";
2288         
2289                                 $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
2290                                 $line["description"] = htmlspecialchars($line["description"]);
2291         
2292                                 if (!$edit_label_id || $subop != "edit") {
2293         
2294                                         if (!$line["description"]) $line["description"] = "[No caption]";
2295         
2296                                         print "<td><input onclick='toggleSelectRow(this);' 
2297                                         type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
2298         
2299                                         print "<td><a href=\"javascript:editLabel($label_id);\">" . 
2300                                                 $line["sql_exp"] . "</td>";             
2301                                                 
2302                                         print "<td><a href=\"javascript:editLabel($label_id);\">" . 
2303                                                 $line["description"] . "</td>";                 
2304         
2305                                 } else if ($label_id != $edit_label_id) {
2306         
2307                                         if (!$line["description"]) $line["description"] = "[No description]";
2308         
2309                                         print "<td><input disabled=\"true\" type=\"checkbox\" 
2310                                                 id=\"LICHK-".$line["id"]."\"></td>";
2311         
2312                                         print "<td>".$line["sql_exp"]."</td>";          
2313                                         print "<td>".$line["description"]."</td>";              
2314         
2315                                 } else {
2316         
2317                                         print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
2318         
2319                                         print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
2320                                                 "\"></td>";
2321         
2322                                         print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
2323                                                 "\"></td>";
2324                                                         
2325                                 }
2326                                         
2327                                 
2328                                 print "</tr>";
2329         
2330                                 ++$lnum;
2331                         }
2332         
2333                         if ($lnum == 0) {
2334                                 print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
2335                         }
2336         
2337                         print "</table>";
2338         
2339                         print "<p>";
2340         
2341                         if ($subop == "edit") {
2342                                 print "Edit label:
2343                                         <input type=\"submit\" class=\"button\" 
2344                                                 onclick=\"javascript:labelTest()\" value=\"Test\">
2345                                         <input type=\"submit\" class=\"button\" 
2346                                                 onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">
2347                                         <input type=\"submit\" class=\"button\" 
2348                                                 onclick=\"javascript:labelEditSave()\" value=\"Save\">";
2349                                                 
2350                         } else {
2351         
2352                                 print "
2353                                         Selection:
2354                                 <input type=\"submit\" class=\"button\" 
2355                                         onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
2356                                 <input type=\"submit\" class=\"button\" 
2357                                         onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
2358                         }
2359                 } else {
2360                         print "<p>No labels defined.</p>";
2361                 }
2362         }
2363
2364         if ($op == "error") {
2365                 print "<div width=\"100%\" align='center'>";
2366                 $msg = $_GET["msg"];
2367                 print $msg;
2368                 print "</div>";
2369         }
2370
2371         if ($op == "help") {
2372                 if (!$_GET["noheaders"]) {
2373                         print "<html><head>
2374                                 <title>Tiny Tiny RSS : Help</title>
2375                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
2376                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
2377                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
2378                                 </head><body>";
2379                 }
2380
2381                 $tid = sprintf("%d", $_GET["tid"]);
2382
2383                 print "<div class='infoBoxContents'>";
2384
2385                 if (file_exists("help/$tid.php")) {
2386                         include("help/$tid.php");
2387                 } else {
2388                         print "<p>Help topic not found.</p>";
2389                 }
2390
2391                 print "</div>";
2392
2393                 print "<div align='center'>
2394                         <input type='submit' class='button'                     
2395                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
2396
2397                 if (!$_GET["noheaders"]) { 
2398                         print "</body></html>";
2399                 }
2400
2401         }
2402
2403         if ($op == "dlg") {
2404                 $id = $_GET["id"];
2405                 $param = $_GET["param"];
2406
2407                 if ($id == "quickAddFeed") {
2408                         print "
2409                         Feed URL: <input 
2410                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
2411                         id=\"qafInput\">
2412                         <input class=\"button\"
2413                                 type=\"submit\" onclick=\"javascript:qafAdd()\" value=\"Add feed\">
2414                         <input class=\"button\"
2415                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
2416                                 value=\"Cancel\">";
2417                 }
2418
2419                 if ($id == "quickDelFeed") {
2420
2421                         $param = db_escape_string($param);
2422
2423                         $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$param'");
2424
2425                         if ($result) {
2426
2427                                 $f_title = db_fetch_result($result, 0, "title");
2428                 
2429                                 print "Remove current feed (<b>$f_title</b>)?&nbsp;
2430                                 <input class=\"button\"
2431                                         type=\"submit\" onclick=\"javascript:qfdDelete($param)\" value=\"Remove\">
2432                                 <input class=\"button\"
2433                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
2434                                         value=\"Cancel\">";
2435                         } else {
2436                                 print "Error: Feed $param not found.&nbsp;
2437                                 <input class=\"button\"
2438                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
2439                                         value=\"Cancel\">";             
2440                         }
2441                 }
2442
2443                 if ($id == "search") {
2444
2445                         print "<input id=\"searchbox\" class=\"extSearch\"                      
2446                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
2447                         onchange=\"javascript:search()\">
2448                         <select id=\"searchmodebox\">
2449                                 <option selected>All feeds</option>
2450                                 <option>This feed</option>
2451                         </select>               
2452                         <input type=\"submit\" 
2453                                 class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
2454                         <input class=\"button\"
2455                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
2456                                 value=\"Close\">";
2457
2458                 }
2459
2460         }
2461
2462         // update feeds of all users, may be used anonymously
2463         if ($op == "globalUpdateFeeds") {
2464
2465                 $result = db_query($link, "SELECT id FROM ttrss_users");
2466
2467                 while ($line = db_fetch_assoc($result)) {
2468                         $user_id = $line["id"];
2469 //                      print "<!-- updating feeds of uid $user_id -->";
2470                         update_all_feeds($link, false, $user_id);
2471                 }
2472
2473                 print "<rpc-reply>
2474                         <message msg=\"All feeds updated\"/>
2475                 </rpc-reply>";
2476
2477         }
2478
2479         if ($op == "pref-prefs") {
2480
2481                 $subop = $_REQUEST["subop"];
2482
2483                 if ($subop == "Save configuration") {
2484
2485                         if (WEB_DEMO_MODE) {
2486                                 header("Location: prefs.php");
2487                                 return;
2488                         }
2489
2490                         $_SESSION["prefs_op_result"] = "save-config";
2491
2492                         foreach (array_keys($_POST) as $pref_name) {
2493                         
2494                                 $pref_name = db_escape_string($pref_name);
2495                                 $value = db_escape_string($_POST[$pref_name]);
2496
2497                                 $result = db_query($link, "SELECT type_name 
2498                                         FROM ttrss_prefs,ttrss_prefs_types 
2499                                         WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
2500
2501                                 if (db_num_rows($result) > 0) {
2502
2503                                         $type_name = db_fetch_result($result, 0, "type_name");
2504
2505 //                                      print "$pref_name : $type_name : $value<br>";
2506
2507                                         if ($type_name == "bool") {
2508                                                 if ($value == "1") {
2509                                                         $value = "true";
2510                                                 } else {
2511                                                         $value = "false";
2512                                                 }
2513                                         } else if ($type_name == "integer") {
2514                                                 $value = sprintf("%d", $value);
2515                                         }
2516
2517 //                                      print "$pref_name : $type_name : $value<br>";
2518
2519                                         db_query($link, "UPDATE ttrss_user_prefs SET value = '$value' 
2520                                                 WHERE pref_name = '$pref_name' AND owner_uid = ".$_SESSION["uid"]);
2521
2522                                 }
2523
2524                                 header("Location: prefs.php");
2525
2526                         }
2527
2528                 } else if ($subop == "getHelp") {
2529
2530                         $pref_name = db_escape_string($_GET["pn"]);
2531
2532                         $result = db_query($link, "SELECT help_text FROM ttrss_prefs
2533                                 WHERE pref_name = '$pref_name'");
2534
2535                         if (db_num_rows($result) > 0) {
2536                                 $help_text = db_fetch_result($result, 0, "help_text");
2537                                 print $help_text;
2538                         } else {
2539                                 print "Unknown option: $pref_name";
2540                         }
2541
2542                 } else if ($subop == "Change password") {
2543
2544                         if (WEB_DEMO_MODE) {
2545                                 header("Location: prefs.php");
2546                                 return;
2547                         }
2548
2549                         $old_pw = $_POST["OLD_PASSWORD"];
2550                         $new_pw = $_POST["OLD_PASSWORD"];
2551
2552                         $old_pw_hash = 'SHA1:' . sha1($_POST["OLD_PASSWORD"]);
2553                         $new_pw_hash = 'SHA1:' . sha1($_POST["NEW_PASSWORD"]);
2554
2555                         $active_uid = $_SESSION["uid"];
2556
2557                         if ($old_pw && $new_pw) {
2558
2559                                 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
2560
2561                                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
2562                                         id = '$active_uid' AND (pwd_hash = '$old_pw' OR 
2563                                                 pwd_hash = '$old_pw_hash')");
2564
2565                                 if (db_num_rows($result) == 1) {
2566                                         db_query($link, "UPDATE ttrss_users SET pwd_hash = '$new_pw_hash' 
2567                                                 WHERE id = '$active_uid'");                             
2568
2569                                         $_SESSION["pwd_change_result"] = "ok";
2570                                 } else {
2571                                         $_SESSION["pwd_change_result"] = "failed";                                      
2572                                 }
2573                         }
2574
2575                         header("Location: prefs.php");
2576
2577                 } else if ($subop == "Reset to defaults") {
2578
2579                         if (WEB_DEMO_MODE) {
2580                                 header("Location: prefs.php");
2581                                 return;
2582                         }
2583
2584                         $_SESSION["prefs_op_result"] = "reset-to-defaults";
2585
2586                         if (DB_TYPE == "pgsql") {
2587                                 db_query($link,"UPDATE ttrss_user_prefs 
2588                                         SET value = ttrss_prefs.def_value 
2589                                         WHERE owner_uid = '".$_SESSION["uid"]."' AND
2590                                         ttrss_prefs.pref_name = ttrss_user_prefs.pref_name");
2591                         } else {
2592                                 db_query($link, "DELETE FROM ttrss_user_prefs 
2593                                         WHERE owner_uid = ".$_SESSION["uid"]);
2594                                 initialize_user_prefs($link, $_SESSION["uid"]);
2595                         }
2596
2597                         header("Location: prefs.php");
2598
2599                 } else if ($subop == "Change theme") {
2600
2601                         $theme = db_escape_string($_POST["theme"]);
2602
2603                         if ($theme == "Default") {
2604                                 $theme_qpart = 'NULL';
2605                         } else {
2606                                 $theme_qpart = "'$theme'";
2607                         }
2608
2609                         $result = db_query($link, "SELECT id,theme_path FROM ttrss_themes
2610                                 WHERE theme_name = '$theme'");
2611
2612                         if (db_num_rows($result) == 1) {
2613                                 $theme_id = db_fetch_result($result, 0, "id");
2614                                 $theme_path = db_fetch_result($result, 0, "theme_path");
2615                         } else {
2616                                 $theme_id = "NULL";
2617                                 $theme_path = "";
2618                         }
2619
2620                         db_query($link, "UPDATE ttrss_users SET
2621                                 theme_id = $theme_id WHERE id = " . $_SESSION["uid"]);
2622
2623                         $_SESSION["theme"] = $theme_path;
2624
2625                         header("Location: prefs.php");
2626
2627                 } else {
2628
2629                         if (!SINGLE_USER_MODE) {
2630
2631                                 $result = db_query($link, "SELECT id FROM ttrss_users
2632                                         WHERE id = ".$_SESSION["uid"]." AND (pwd_hash = 'password' OR
2633                                                 pwd_hash = 'SHA1:".sha1("password")."')");
2634
2635                                 if (db_num_rows($result) != 0) {
2636                                         print "<div class=\"warning\"> 
2637                                                 Your password is at default value, please change it.
2638                                         </div>";
2639                                 }
2640
2641                                 if ($_SESSION["pwd_change_result"] == "failed") {
2642                                         print "<div class=\"warning\"> 
2643                                                         There was an error while changing your password.
2644                                                 </div>";
2645                                 }
2646
2647                                 if ($_SESSION["pwd_change_result"] == "ok") {
2648                                         print "<div class=\"notice\"> 
2649                                                         Password changed successfully.
2650                                                 </div>";
2651                                 }
2652
2653                                 $_SESSION["pwd_change_result"] = "";
2654
2655                                 if ($_SESSION["prefs_op_result"] == "reset-to-defaults") {
2656                                         print "<div class=\"notice\"> 
2657                                                         Your configuration was reset to defaults.
2658                                                 </div>";
2659                                 }
2660
2661                                 if ($_SESSION["prefs_op_result"] == "save-config") {
2662                                         print "<div class=\"notice\"> 
2663                                                         Your configuration was saved successfully.
2664                                                 </div>";
2665                                 }
2666
2667                                 $_SESSION["prefs_op_result"] = "";
2668
2669                                 print "<form action=\"backend.php\" method=\"POST\">";
2670         
2671                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
2672                                 print "<tr><td colspan='3'><h3>Authentication</h3></tr></td>";
2673         
2674                                 print "<tr><td width=\"40%\">Old password</td>";
2675                                 print "<td><input class=\"editbox\" type=\"password\"
2676                                         name=\"OLD_PASSWORD\"></td></tr>";
2677         
2678                                 print "<tr><td width=\"40%\">New password</td>";
2679                                 
2680                                 print "<td><input class=\"editbox\" type=\"password\"
2681                                         name=\"NEW_PASSWORD\"></td></tr>";
2682         
2683                                 print "</table>";
2684         
2685                                 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
2686         
2687                                 print "<p><input class=\"button\" type=\"submit\" 
2688                                         value=\"Change password\" name=\"subop\">";
2689         
2690                                 print "</form>";
2691
2692                         }
2693
2694                         $result = db_query($link, "SELECT
2695                                 theme_id FROM ttrss_users WHERE id = " . $_SESSION["uid"]);
2696
2697                         $user_theme_id = db_fetch_result($result, 0, "theme_id");
2698
2699                         $result = db_query($link, "SELECT
2700                                 id,theme_name FROM ttrss_themes ORDER BY theme_name");
2701
2702                         if (db_num_rows($result) > 0) {
2703
2704                                 print "<form action=\"backend.php\" method=\"POST\">";
2705                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
2706                                 print "<tr><td colspan='3'><h3>Themes</h3></tr></td>";
2707                                 print "<tr><td width=\"40%\">Select theme</td>";
2708                                 print "<td><select name=\"theme\">";
2709                                 print "<option>Default</option>";
2710                                 print "<option disabled>--------</option>";                             
2711                                 
2712                                 while ($line = db_fetch_assoc($result)) {       
2713                                         if ($line["id"] == $user_theme_id) {
2714                                                 $selected = "selected";
2715                                         } else {
2716                                                 $selected = "";
2717                                         }
2718                                         print "<option $selected>" . $line["theme_name"] . "</option>";
2719                                 }
2720                                 print "</select></td></tr>";
2721                                 print "</table>";
2722                                 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
2723                                 print "<p><input class=\"button\" type=\"submit\" 
2724                                         value=\"Change theme\" name=\"subop\">";
2725                                 print "</form>";
2726                         }
2727
2728                         $result = db_query($link, "SELECT 
2729                                 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
2730                                 section_name,def_value
2731                                 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
2732                                 WHERE type_id = ttrss_prefs_types.id AND 
2733                                         section_id = ttrss_prefs_sections.id AND
2734                                         ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
2735                                         owner_uid = ".$_SESSION["uid"]."
2736                                 ORDER BY section_id,short_desc");
2737
2738                         print "<form action=\"backend.php\" method=\"POST\">";
2739
2740                         $lnum = 0;
2741
2742                         $active_section = "";
2743         
2744                         while ($line = db_fetch_assoc($result)) {
2745
2746                                 if ($active_section != $line["section_name"]) {
2747
2748                                         if ($active_section != "") {
2749                                                 print "</table>";
2750                                         }
2751
2752                                         print "<p><table width=\"100%\" class=\"prefPrefsList\">";
2753                                 
2754                                         $active_section = $line["section_name"];                                
2755                                         
2756                                         print "<tr><td colspan=\"3\"><h3>$active_section</h3></td></tr>";
2757 //                                      print "<tr class=\"title\">
2758 //                                              <td width=\"25%\">Option</td><td>Value</td></tr>";
2759
2760                                         $lnum = 0;
2761                                 }
2762
2763 //                              $class = ($lnum % 2) ? "even" : "odd";
2764
2765                                 print "<tr>";
2766
2767                                 $type_name = $line["type_name"];
2768                                 $pref_name = $line["pref_name"];
2769                                 $value = $line["value"];
2770                                 $def_value = $line["def_value"];
2771                                 $help_text = $line["help_text"];
2772
2773                                 print "<td width=\"40%\" id=\"$pref_name\">" . $line["short_desc"];
2774
2775                                 if ($help_text) print "<div class=\"prefHelp\">$help_text</div>";
2776                                 
2777                                 print "</td>";
2778
2779                                 print "<td>";
2780
2781                                 if ($type_name == "bool") {
2782 //                                      print_select($pref_name, $value, array("true", "false"));
2783
2784                                         if ($value == "true") {
2785                                                 $value = "Yes";
2786                                         } else {
2787                                                 $value = "No";
2788                                         }
2789
2790                                         print_radio($pref_name, $value, array("Yes", "No"));
2791                         
2792                                 } else {
2793                                         print "<input class=\"editbox\" name=\"$pref_name\" value=\"$value\">";
2794                                 }
2795
2796                                 print "</td>";
2797
2798                                 print "</tr>";
2799
2800                                 $lnum++;
2801                         }
2802
2803                         print "</table>";
2804
2805                         print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
2806
2807                         print "<p><input class=\"button\" type=\"submit\" 
2808                                 name=\"subop\" value=\"Save configuration\">";
2809                                 
2810                         print "&nbsp;<input class=\"button\" type=\"submit\" 
2811                                 name=\"subop\" value=\"Reset to defaults\"></p>";
2812
2813                         print "</form>";
2814
2815                 }
2816
2817         }
2818
2819         if ($op == "pref-users") {
2820
2821                 $subop = $_GET["subop"];
2822
2823                 if ($subop == "editSave") {
2824         
2825                         if (!WEB_DEMO_MODE) {
2826
2827                                 $login = db_escape_string($_GET["l"]);
2828                                 $uid = db_escape_string($_GET["id"]);
2829                                 $access_level = sprintf("%d", $_GET["al"]);
2830
2831                                 db_query($link, "UPDATE ttrss_users SET login = '$login', access_level = '$access_level' WHERE id = '$uid'");
2832
2833                         }
2834                 } else if ($subop == "remove") {
2835
2836                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
2837
2838                                 $ids = split(",", db_escape_string($_GET["ids"]));
2839
2840                                 foreach ($ids as $id) {
2841                                         db_query($link, "DELETE FROM ttrss_users WHERE id = '$id' AND id != " . $_SESSION["uid"]);
2842                                         
2843                                 }
2844                         }
2845                 } else if ($subop == "add") {
2846                 
2847                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
2848
2849                                 $login = db_escape_string(trim($_GET["login"]));
2850                                 $tmp_user_pwd = make_password(8);
2851                                 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
2852
2853                                 db_query($link, "INSERT INTO ttrss_users (login,pwd_hash,access_level)
2854                                         VALUES ('$login', '$pwd_hash', 0)");
2855
2856
2857                                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
2858                                         login = '$login' AND pwd_hash = '$pwd_hash'");
2859
2860                                 if (db_num_rows($result) == 1) {
2861
2862                                         $new_uid = db_fetch_result($result, 0, "id");
2863
2864                                         print "<div class=\"notice\">Added user <b>".$_GET["login"].
2865                                                 "</b> with password <b>$tmp_user_pwd</b>.</div>";
2866
2867                                         initialize_user($link, $new_uid);
2868
2869                                 } else {
2870                                 
2871                                         print "<div class=\"warning\">Error while adding user <b>".
2872                                         $_GET["login"].".</b></div>";
2873
2874                                 }
2875                         } 
2876                 } else if ($subop == "resetPass") {
2877
2878                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
2879
2880                                 $uid = db_escape_string($_GET["id"]);
2881
2882                                 $result = db_query($link, "SELECT login FROM ttrss_users WHERE id = '$uid'");
2883
2884                                 $login = db_fetch_result($result, 0, "login");
2885                                 $tmp_user_pwd = make_password(8);
2886                                 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
2887
2888                                 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$pwd_hash'
2889                                         WHERE id = '$uid'");
2890
2891                                 print "<div class=\"notice\">Changed password of 
2892                                         user <b>$login</b> to <b>$tmp_user_pwd</b>.</div>";                             
2893
2894                         }
2895                 }
2896
2897                 print "<div class=\"prefGenericAddBox\">
2898                         <input id=\"uadd_box\" onchange=\"javascript:addUser()\" size=\"40\">&nbsp;";
2899                         
2900                 print"<input type=\"submit\" class=\"button\" 
2901                         onclick=\"javascript:addUser()\" value=\"Add user\"></div>";
2902
2903                 $result = db_query($link, "SELECT 
2904                                 id,login,access_level,
2905                                 SUBSTRING(last_login,1,16) as last_login
2906                         FROM 
2907                                 ttrss_users
2908                         ORDER by login");
2909
2910                 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
2911
2912                 print "<p><table width=\"100%\" cellspacing=\"0\" 
2913                         class=\"prefUserList\" id=\"prefUserList\">";
2914
2915                 print "<tr><td class=\"selectPrompt\" colspan=\"8\">
2916                                 Select: 
2917                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefUserList', 
2918                                                 'UMRR-', 'UMCHK-', true)\">All</a>,
2919                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefUserList', 
2920                                                 'UMRR-', 'UMCHK-', false)\">None</a>
2921                                 </td</tr>";
2922
2923                 print "<tr class=\"title\">
2924                                         <td width=\"5%\">Select</td>
2925                                         <td width='30%'>Username</td>
2926                                         <td width='30%'>Access Level</td>
2927                                         <td width='30%'>Last login</td></tr>";
2928                 
2929                 $lnum = 0;
2930                 
2931                 while ($line = db_fetch_assoc($result)) {
2932
2933                         $class = ($lnum % 2) ? "even" : "odd";
2934
2935                         $uid = $line["id"];
2936                         $edit_uid = $_GET["id"];
2937
2938                         if ($uid == $_SESSION["uid"] || ($subop == "edit" && $uid != $edit_uid)) {
2939                                 $class .= "Grayed";
2940                                 $this_row_id = "";
2941                         } else {
2942                                 $this_row_id = "id=\"UMRR-$uid\"";
2943                         }               
2944                         
2945                         print "<tr class=\"$class\" $this_row_id>";
2946
2947                         $line["login"] = htmlspecialchars($line["login"]);
2948
2949                         if ($uid == $_SESSION["uid"]) {
2950
2951                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
2952                                         id=\"UMCHK-".$line["id"]."\"></td>";
2953
2954                                 print "<td>".$line["login"]."</td>";            
2955                                 print "<td>".$line["access_level"]."</td>";             
2956
2957                         } else if (!$edit_uid || $subop != "edit") {
2958
2959                                 print "<td><input onclick='toggleSelectRow(this);' 
2960                                 type=\"checkbox\" id=\"UMCHK-$uid\"></td>";
2961
2962                                 print "<td><a href=\"javascript:editUser($uid);\">" . 
2963                                         $line["login"] . "</td>";               
2964                                         
2965                                 print "<td><a href=\"javascript:editUser($uid);\">" . 
2966                                         $line["access_level"] . "</td>";                        
2967
2968                         } else if ($uid != $edit_uid) {
2969
2970                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
2971                                         id=\"UMCHK-".$line["id"]."\"></td>";
2972
2973                                 print "<td>".$line["login"]."</td>";            
2974                                 print "<td>".$line["access_level"]."</td>";             
2975
2976                         } else {
2977
2978                                 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
2979
2980                                 print "<td><input id=\"iedit_ulogin\" value=\"".$line["login"].
2981                                         "\"></td>";
2982
2983                                 print "<td><input id=\"iedit_ulevel\" value=\"".$line["access_level"].
2984                                         "\"></td>";
2985                                                 
2986                         }
2987                                 
2988                         print "<td>".$line["last_login"]."</td>";               
2989                 
2990                         print "</tr>";
2991
2992                         ++$lnum;
2993                 }
2994
2995                 print "</table>";
2996
2997                 print "<p>";
2998
2999                 if ($subop == "edit") {
3000                         print "Edit label:
3001                                 <input type=\"submit\" class=\"button\" 
3002                                         onclick=\"javascript:userEditCancel()\" value=\"Cancel\">
3003                                 <input type=\"submit\" class=\"button\" 
3004                                         onclick=\"javascript:userEditSave()\" value=\"Save\">";
3005                                         
3006                 } else {
3007
3008                         print "
3009                                 Selection:
3010                         <input type=\"submit\" class=\"button\" 
3011                                 onclick=\"javascript:selectedUserDetails()\" value=\"User details\">
3012                         <input type=\"submit\" class=\"button\" 
3013                                 onclick=\"javascript:editSelectedUser()\" value=\"Edit\">
3014                         <input type=\"submit\" class=\"button\" 
3015                                 onclick=\"javascript:removeSelectedUsers()\" value=\"Remove\">
3016                         <input type=\"submit\" class=\"button\" 
3017                                 onclick=\"javascript:resetSelectedUserPass()\" value=\"Reset password\">";
3018
3019                 }
3020         }
3021
3022         if ($op == "user-details") {
3023
3024                 if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
3025                         return;
3026                 }
3027                           
3028 /*              print "<html><head>
3029                         <title>Tiny Tiny RSS : User Details</title>
3030                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
3031                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
3032                         </head><body>"; */
3033
3034                 $uid = sprintf("%d", $_GET["id"]);
3035
3036                 print "<div class='infoBoxContents'>";
3037
3038                 $result = db_query($link, "SELECT login,
3039                         SUBSTRING(last_login,1,16) AS last_login,
3040                         access_level,
3041                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
3042                                 WHERE owner_uid = id) AS stored_articles
3043                         FROM ttrss_users 
3044                         WHERE id = '$uid'");
3045                         
3046                 if (db_num_rows($result) == 0) {
3047                         print "<h1>User not found</h1>";
3048                         return;
3049                 }
3050                 
3051                 print "<h1>User Details</h1>";
3052
3053                 print "<table width='100%'>";
3054
3055                 $login = db_fetch_result($result, 0, "login");
3056                 $last_login = db_fetch_result($result, 0, "last_login");
3057                 $access_level = db_fetch_result($result, 0, "access_level");
3058                 $stored_articles = db_fetch_result($result, 0, "stored_articles");
3059
3060                 print "<tr><td>Username</td><td>$login</td></tr>";
3061                 print "<tr><td>Access level</td><td>$access_level</td></tr>";
3062                 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
3063                 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
3064
3065                 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
3066                         WHERE owner_uid = '$uid'");
3067
3068                 $num_feeds = db_fetch_result($result, 0, "num_feeds");
3069
3070                 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
3071
3072 /*              $result = db_query($link, "SELECT 
3073                         SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size 
3074                         FROM ttrss_user_entries,ttrss_entries 
3075                                 WHERE owner_uid = '$uid' AND ref_id = id");
3076
3077                 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
3078
3079                 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>";  */
3080
3081                 print "</table>";
3082
3083                 print "<h1>Subscribed feeds</h1>";
3084
3085                 $result = db_query($link, "SELECT id,title,feed_url FROM ttrss_feeds
3086                         WHERE owner_uid = '$uid' ORDER BY title LIMIT 20");
3087
3088                 print "<ul class=\"nomarks\">";
3089
3090                 while ($line = db_fetch_assoc($result)) {
3091
3092                         $icon_file = ICONS_URL."/".$line["id"].".ico";
3093
3094                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
3095                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
3096                         } else {
3097                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
3098                         }
3099
3100                         print "<li>$feed_icon&nbsp;<a href=\"".$line["feed_url"]."\">".$line["title"]."</a></li>";
3101                 }
3102
3103                 if (db_num_rows($result) < $num_feeds) {
3104                          // FIXME - add link to show ALL subscribed feeds here somewhere
3105                         print "<li><img 
3106                                 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
3107                 }
3108                 
3109                 print "</ul>";
3110
3111                 print "</div>";
3112
3113                 print "<div align='center'>
3114                         <input type='submit' class='button'                     
3115                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
3116
3117 //              print "</body></html>"; 
3118
3119         }
3120
3121         if ($op == "feed-details") {
3122
3123                 $feed_id = $_GET["id"];
3124
3125                 $result = db_query($link, 
3126                         "SELECT 
3127                                 title,feed_url,last_updated,icon_url,site_url,
3128                                 (SELECT COUNT(int_id) FROM ttrss_user_entries 
3129                                         WHERE feed_id = id) AS total,
3130                                 (SELECT COUNT(int_id) FROM ttrss_user_entries 
3131                                         WHERE feed_id = id AND unread = true) AS unread,
3132                                 (SELECT COUNT(int_id) FROM ttrss_user_entries 
3133                                         WHERE feed_id = id AND marked = true) AS marked
3134                         FROM ttrss_feeds
3135                         WHERE id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
3136
3137                 if (db_num_rows($result) == 0) return;
3138
3139                 $title = db_fetch_result($result, 0, "title");
3140                 $last_updated = db_fetch_result($result, 0, "last_updated");
3141                 $feed_url = db_fetch_result($result, 0, "feed_url");
3142                 $icon_url = db_fetch_result($result, 0, "icon_url");
3143                 $total = db_fetch_result($result, 0, "total");
3144                 $unread = db_fetch_result($result, 0, "unread");
3145                 $marked = db_fetch_result($result, 0, "marked");
3146                 $site_url = db_fetch_result($result, 0, "site_url");
3147
3148                 $result = db_query($link, "SELECT COUNT(id) AS subscribed
3149                                         FROM ttrss_feeds WHERE feed_url = '$feed_url'");
3150
3151                 $subscribed = db_fetch_result($result, 0, "subscribed");
3152
3153                 print "<div class=\"infoBoxContents\">";
3154
3155                 $icon_file = ICONS_DIR . "/$feed_id.ico";
3156
3157                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
3158                                 $feed_icon = "<img width=\"16\" height=\"16\"
3159                                         src=\"" . ICONS_URL . "/$feed_id.ico\">";
3160                 } else {
3161                         $feed_icon = "";
3162                 }
3163
3164                 print "<h1>$feed_icon $title</h1>";
3165
3166                 print "<table width='100%'>";
3167
3168                 if ($site_url) {
3169                         print "<tr><td width='30%'>Link</td>
3170                                 <td><a href=\"$site_url\">$site_url</a>
3171                                 <a href=\"$feed_url\">(feed)</a></td>
3172                                 </td></tr>";
3173                 } else {
3174                         print "<tr><td width='30%'>Feed URL</td>
3175                                 <td><a href=\"$feed_url\">$feed_url</a></td></tr>";
3176                 }
3177                 print "<tr><td>Last updated</td><td>$last_updated</td></tr>";
3178                 print "<tr><td>Total articles</td><td>$total</td></tr>";
3179                 print "<tr><td>Unread articles</td><td>$unread</td></tr>";
3180                 print "<tr><td>Starred articles</td><td>$marked</td></tr>";
3181                 print "<tr><td>Subscribed users</td><td>$subscribed</td></tr>";
3182
3183                 print "</table>";
3184
3185                 $result = db_query($link, "SELECT title,
3186                         SUBSTRING(updated,1,16) AS updated,unread
3187                         FROM ttrss_entries,ttrss_user_entries
3188                         WHERE ref_id = id AND feed_id = '$feed_id' 
3189                         ORDER BY date_entered DESC LIMIT 5");
3190
3191                 if (db_num_rows($result) > 0) {
3192
3193                         print "<h1>Latest headlines</h1>";
3194
3195                         print "<ul class=\"nomarks\">";
3196         
3197                         while ($line = db_fetch_assoc($result)) {
3198                                 if ($line["unread"] == "t" || $line["unread"] == "1") {
3199                                         $line["title"] = "<b>" . $line["title"] . "</b>";
3200                                 }                               
3201                                 print "<li>" . $line["title"].
3202                                 "&nbsp;<span class=\"insensitive\">(" .$line["updated"].")</span></li>";
3203                         }
3204         
3205                         print "</ul>";
3206         
3207                         print "</div>";
3208         
3209                         print "<div align='center'>
3210                                 <input type='submit' class='button'                     
3211                                 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
3212                 }
3213         }
3214
3215         db_close($link);
3216 ?>
3217
3218 <!-- <?= sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
3219