]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
some UI frontend cookies respect session cookie lifetime, misc cookie cleanups (close...
[tt-rss.git] / backend.php
1 <?
2         require_once "sessions.php";
3         
4         header("Cache-Control: no-cache, must-revalidate");
5         header("Pragma: no-cache");
6         header("Expires: -1");
7         
8         if ($_GET["debug"]) {
9                 define('DEFAULT_ERROR_LEVEL', E_ALL);
10         } else {
11                 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
12         }
13
14         error_reporting(DEFAULT_ERROR_LEVEL);
15
16         $op = $_REQUEST["op"];
17
18         if ((!$op || $op == "rpc" || $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
19                 header("Content-Type: application/xml");
20         }
21
22         if (!$_SESSION["uid"] && $op != "globalUpdateFeeds") {
23
24                 if ($op == "rpc") {
25                         print "<error error-code=\"6\"/>";
26                 } else {
27                         print "
28                         <html><body>
29                                 <p>Error: Not logged in.</p>
30                                 <script type=\"text/javascript\">
31                                         if (parent.window != 'undefined') {
32                                                 parent.window.location = \"login.php\";         
33                                         } else {
34                                                 window.location = \"login.php\";
35                                         }
36                                 </script>
37                         </body></html>
38                         ";
39                 }
40                 exit;
41         }
42
43         if (!$op) {
44                 print "<error error-code=\"7\"/>";
45                 exit;
46         }
47
48         define('SCHEMA_VERSION', 7);
49
50         require_once "sanity_check.php";
51         require_once "config.php";
52         require_once "db.php";
53         require_once "db-prefs.php";
54         require_once "functions.php";
55         require_once "magpierss/rss_fetch.inc";
56
57         $purge_intervals = array(
58                 0  => "Default",
59                 -1 => "Never purge",
60                 5  => "1 week",
61                 14 => "2 weeks",
62                 31 => "1 month",
63                 60 => "2 months",
64                 90 => "3 months");
65
66         $update_intervals = array(
67                 0   => "Default",
68                 -1  => "Disable updates",
69                 30  => "30 minutes",
70                 60  => "1 hour",
71                 240 => "4 hours",
72                 720 => "12 hours",
73                 1440 => "Daily",
74                 10080 => "Weekly");
75
76         $script_started = getmicrotime();
77
78         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
79
80         if (!$link) {
81                 if (DB_TYPE == "mysql") {
82                         print mysql_error();
83                 }
84                 // PG seems to display its own errors just fine by default.             
85                 return;
86         }
87
88         if (DB_TYPE == "pgsql") {
89                 pg_query("set client_encoding = 'utf-8'");
90         }
91
92         if ($_SESSION["uid"]) {
93                 if (get_pref($link, "HIDE_READ_FEEDS") == "true") {
94                         setcookie("ttrss_vf_hreadf", 1);
95                 } else {
96                         setcookie("ttrss_vf_hreadf", 0);
97                 }  
98
99                 setcookie('ttrss_vf_refresh', FEEDS_FRAME_REFRESH);
100                 setcookie('ttrss_vf_daemon', ENABLE_UPDATE_DAEMON);
101         }
102
103         $fetch = $_GET["fetch"];
104
105 //      setcookie("ttrss_icons_url", ICONS_URL);
106
107         if (!sanity_check($link)) { return; }
108
109         function getAllCounters($link) {
110                 getLabelCounters($link);
111                 getFeedCounters($link);
112                 getTagCounters($link);
113                 getGlobalCounters($link);
114                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
115                         getCategoryCounters($link);
116                 }
117         }       
118
119         function getCategoryCounters($link) {
120                 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) 
121                                 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id 
122                                         AND unread = true)) AS unread FROM ttrss_feeds 
123                         WHERE 
124                                 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
125
126                 while ($line = db_fetch_assoc($result)) {
127                         $line["cat_id"] = sprintf("%d", $line["cat_id"]);
128                         print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
129                                 $line["unread"]."\"/>";
130                 }
131         }
132
133         function getFeedUnread($link, $feed) {
134                 $n_feed = sprintf("%d", $feed);
135         
136                 if ($n_feed == -1) {
137                         $match_part = "marked = true";
138                 } else if ($feed > 0) {
139                         $match_part = "feed_id = '$n_feed'";
140                 } else if ($feed < -10) {
141                         $label_id = -$feed - 11;
142
143                         $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
144                                 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
145
146                         $match_part = db_fetch_result($result, 0, "sql_exp");
147                 }
148
149                 if ($match_part) {
150                 
151                         $result = db_query($link, "SELECT count(int_id) AS unread 
152                                 FROM ttrss_user_entries 
153                                 WHERE   unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
154                                 
155                 } else {
156                 
157                         $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
158                                 FROM ttrss_tags,ttrss_user_entries 
159                                 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
160                                         ttrss_tags.owner_uid = " . $_SESSION["uid"]);
161                 }
162                 
163                 $unread = db_fetch_result($result, 0, "unread");
164                 return $unread;
165         }
166
167         /* FIXME this needs reworking */
168
169         function getGlobalUnread($link) {
170                 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
171                         WHERE unread = true AND 
172                         ttrss_user_entries.ref_id = ttrss_entries.id AND 
173                         owner_uid = " . $_SESSION["uid"]);
174                 $c_id = db_fetch_result($result, 0, "c_id");
175                 return $c_id;
176         }
177
178         function getGlobalCounters($link, $global_unread = -1) {
179                 if ($global_unread == -1) {     
180                         $global_unread = getGlobalUnread($link);
181                 }
182                 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
183         }
184
185         function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
186
187                 if ($smart_mode) {
188                         if (!$_SESSION["tctr_last_value"]) {
189                                 $_SESSION["tctr_last_value"] = array();
190                         }
191                 }
192
193                 $old_counters = $_SESSION["tctr_last_value"];
194
195                 $tctrs_modified = false;
196
197 /*              $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
198                         FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
199                         ttrss_user_entries.ref_id = ttrss_entries.id AND 
200                         ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
201                         post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name 
202                 UNION
203                         select tag_name,0 as count FROM ttrss_tags
204                         WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
205
206                 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) 
207                         FROM ttrss_user_entries WHERE int_id = post_int_id 
208                                 AND unread = true)) AS count FROM ttrss_tags 
209                         WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
210                         
211                 $tags = array();
212
213                 while ($line = db_fetch_assoc($result)) {
214                         $tags[$line["tag_name"]] += $line["count"];
215                 }
216
217                 foreach (array_keys($tags) as $tag) {
218                         $unread = $tags[$tag];                  
219
220                         $tag = htmlspecialchars($tag);
221
222                         if (!$smart_mode || $old_counters[$tag] != $unread) {                   
223                                 $old_counters[$tag] = $unread;
224                                 $tctrs_modified = true;
225                                 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
226                         }
227
228                 } 
229
230                 if ($smart_mode && $tctrs_modified) {
231                         $_SESSION["tctr_last_value"] = $old_counters;
232                 }
233
234         }
235
236         function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
237
238                 if ($smart_mode) {
239                         if (!$_SESSION["lctr_last_value"]) {
240                                 $_SESSION["lctr_last_value"] = array();
241                         }
242                 }
243
244                 $old_counters = $_SESSION["lctr_last_value"];
245                 $lctrs_modified = false;
246
247                 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
248                         WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND 
249                         unread = true AND owner_uid = ".$_SESSION["uid"]);
250
251                 $count = db_fetch_result($result, 0, "count");
252
253                 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
254
255                 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
256                         ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
257         
258                 while ($line = db_fetch_assoc($result)) {
259
260                         $id = -$line["id"] - 11;
261
262                         error_reporting (0);
263
264                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
265                                 WHERE (" . $line["sql_exp"] . ") AND unread = true AND 
266                                 ttrss_user_entries.ref_id = ttrss_entries.id AND 
267                                 owner_uid = ".$_SESSION["uid"]);
268
269                         $count = db_fetch_result($tmp_result, 0, "count");
270
271                         if (!$smart_mode || $old_counters[$id] != $count) {     
272                                 $old_counters[$id] = $count;
273                                 $lctrs_modified = true;
274                                 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
275                         }
276
277                         error_reporting (DEFAULT_ERROR_LEVEL);
278                 }
279
280                 if ($smart_mode && $lctrs_modified) {
281                         $_SESSION["lctr_last_value"] = $old_counters;
282                 }
283         }
284
285 /*      function getFeedCounter($link, $id) {
286         
287                 $result = db_query($link, "SELECT 
288                                 count(id) as count,last_error
289                         FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
290                         WHERE feed_id = '$id' AND unread = true
291                         AND ttrss_user_entries.feed_id = ttrss_feeds.id
292                         AND ttrss_user_entries.ref_id = ttrss_entries.id");
293         
294                         $count = db_fetch_result($result, 0, "count");
295                         $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
296                         
297                         print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";           
298         } */
299
300         function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
301
302                 if ($smart_mode) {
303                         if (!$_SESSION["fctr_last_value"]) {
304                                 $_SESSION["fctr_last_value"] = array();
305                         }
306                 }
307
308                 $old_counters = $_SESSION["fctr_last_value"];
309
310                 $result = db_query($link, "SELECT id,last_error,parent_feed,
311                         (SELECT count(id) 
312                                 FROM ttrss_entries,ttrss_user_entries 
313                                 WHERE feed_id = ttrss_feeds.id AND 
314                                         ttrss_user_entries.ref_id = ttrss_entries.id
315                                 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
316                         FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
317                                 AND parent_feed IS NULL");
318
319                 $fctrs_modified = false;
320
321                 while ($line = db_fetch_assoc($result)) {
322                 
323                         $id = $line["id"];
324                         $count = $line["count"];
325                         $last_error = htmlspecialchars($line["last_error"]);
326         
327                         $has_img = is_file(ICONS_DIR . "/$id.ico");
328
329                         $tmp_result = db_query($link,
330                                 "SELECT id,COUNT(unread) AS unread
331                                 FROM ttrss_feeds LEFT JOIN ttrss_user_entries 
332                                         ON (ttrss_feeds.id = ttrss_user_entries.feed_id) 
333                                 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
334                         
335                         if (db_num_rows($tmp_result) > 0) {                             
336                                 while ($l = db_fetch_assoc($tmp_result)) {
337                                         $count += $l["unread"];
338                                 }
339                         }
340
341                         if (!$smart_mode || $old_counters[$id] != $count) {
342                                 $old_counters[$id] = $count;
343                                 $fctrs_modified = true;
344
345                                 if ($last_error) {
346                                         $error_part = "error=\"$last_error\"";
347                                 } else {
348                                         $error_part = "";
349                                 }
350
351                                 if ($has_img) {
352                                         $has_img_part = "hi=\"$has_img\"";
353                                 } else {
354                                         $has_img_part = "";
355                                 }                               
356
357                                 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part/>";
358                         }
359                 }
360
361                 if ($smart_mode && $fctrs_modified) {
362                         $_SESSION["fctr_last_value"] = $old_counters;
363                 }
364         }
365
366         function outputFeedList($link, $tags = false) {
367
368                 print "<html><head>
369                         <title>Tiny Tiny RSS : Feedlist</title>
370                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
371
372                 $user_theme = $_SESSION["theme"];
373                 if ($user_theme) { 
374                         print "<link rel=\"stylesheet\" type=\"text/css\" 
375                                 href=\"themes/$user_theme/theme.css\">";
376                 }
377
378                 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
379                         print "<link rel=\"stylesheet\" type=\"text/css\" 
380                                 href=\"tt-rss_compact.css\"/>";
381                 } else {
382                         print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
383                                         type=\"text/css\" href=\"tt-rss_compact.css\"/>";
384                 }
385
386                 print "<script type=\"text/javascript\" src=\"functions.js\"></script>
387                         <script type=\"text/javascript\" src=\"feedlist.js\"></script>
388                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
389                         <!--[if gte IE 5.5000]>
390                         <script type=\"text/javascript\" src=\"pngfix.js\"></script>
391                         <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
392                         <![endif]-->
393                         </head><body>
394                         <script type=\"text/javascript\">
395                                 if (document.addEventListener) {
396                                         document.addEventListener(\"DOMContentLoaded\", init, null);
397                                 }
398                                 window.onload = init;
399                         </script>";
400
401                 print "<ul class=\"feedList\" id=\"feedList\">\n";
402
403                 $owner_uid = $_SESSION["uid"];
404
405                 if (!$tags) {
406
407                         /* virtual feeds */
408
409                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
410                                 print "<li class=\"feedCat\">Special</li>";
411                                 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
412                         }
413
414                         $result = db_query($link, "SELECT count(id) as num_starred 
415                                 FROM ttrss_entries,ttrss_user_entries 
416                                 WHERE marked = true AND 
417                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
418                                 unread = true AND owner_uid = '$owner_uid'");
419                         $num_starred = db_fetch_result($result, 0, "num_starred");
420
421                         $class = "virt";
422
423                         if ($num_starred > 0) $class .= "Unread";
424
425                         printFeedEntry(-1, $class, "Starred articles", $num_starred, 
426                                 "images/mark_set.png", $link);
427
428                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
429                                 print "</ul>\n";
430                         }
431
432                         if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
433         
434                                 $result = db_query($link, "SELECT id,sql_exp,description FROM
435                                         ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
436                 
437                                 if (db_num_rows($result) > 0) {
438                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
439                                                 print "<li class=\"feedCat\">Labels</li>";
440                                                 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
441                                         } else {
442                                                 print "<li><hr></li>";
443                                         }
444                                 }
445                 
446                                 while ($line = db_fetch_assoc($result)) {
447         
448                                         error_reporting (0);
449                 
450                                         $tmp_result = db_query($link, "SELECT count(id) as count 
451                                                 FROM ttrss_entries,ttrss_user_entries
452                                                 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
453                                                 ttrss_user_entries.ref_id = ttrss_entries.id
454                                                 AND owner_uid = '$owner_uid'");
455         
456                                         $count = db_fetch_result($tmp_result, 0, "count");
457         
458                                         $class = "label";
459         
460                                         if ($count > 0) {
461                                                 $class .= "Unread";
462                                         }
463                                         
464                                         error_reporting (DEFAULT_ERROR_LEVEL);
465         
466                                         printFeedEntry(-$line["id"]-11, 
467                                                 $class, $line["description"], $count, "images/label.png", $link);
468                 
469                                 }
470
471                                 if (db_num_rows($result) > 0) {
472                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
473                                                 print "</ul>";
474                                         }
475                                 }
476
477                         }
478
479 //                      if (!get_pref($link, 'ENABLE_FEED_CATS')) {
480                                 print "<li><hr></li>";
481 //                      }
482
483                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
484                                 $order_by_qpart = "category,title";
485                         } else {
486                                 $order_by_qpart = "title";
487                         }
488
489                         $result = db_query($link, "SELECT ttrss_feeds.*,
490                                 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
491                                         WHERE feed_id = ttrss_feeds.id AND 
492                                         ttrss_user_entries.ref_id = ttrss_entries.id AND
493                                         owner_uid = '$owner_uid') AS total,
494                                 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
495                                         WHERE feed_id = ttrss_feeds.id AND unread = true
496                                                 AND ttrss_user_entries.ref_id = ttrss_entries.id
497                                                 AND owner_uid = '$owner_uid') as unread,
498                                 cat_id,last_error,
499                                 ttrss_feed_categories.title AS category,
500                                 ttrss_feed_categories.collapsed 
501                                 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories 
502                                         ON (ttrss_feed_categories.id = cat_id)                          
503                                 WHERE 
504                                         ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
505                                 ORDER BY $order_by_qpart"); 
506
507                         $actid = $_GET["actid"];
508         
509                         /* real feeds */
510         
511                         $lnum = 0;
512         
513                         $total_unread = 0;
514
515                         $category = "";
516         
517                         while ($line = db_fetch_assoc($result)) {
518                         
519                                 $feed = db_unescape_string($line["title"]);
520                                 $feed_id = $line["id"];   
521         
522                                 $subop = $_GET["subop"];
523                                 
524                                 $total = $line["total"];
525                                 $unread = $line["unread"];
526
527                                 $tmp_result = db_query($link,
528                                         "SELECT id,COUNT(unread) AS unread
529                                         FROM ttrss_feeds LEFT JOIN ttrss_user_entries 
530                                                 ON (ttrss_feeds.id = ttrss_user_entries.feed_id) 
531                                         WHERE parent_feed = '$feed_id' AND unread = true 
532                                         GROUP BY ttrss_feeds.id");
533                         
534                                 if (db_num_rows($tmp_result) > 0) {                             
535                                         while ($l = db_fetch_assoc($tmp_result)) {
536                                                 $unread += $l["unread"];
537                                         }
538                                 }
539
540                                 $cat_id = $line["cat_id"];
541
542                                 $tmp_category = $line["category"];
543
544                                 if (!$tmp_category) {
545                                         $tmp_category = "Uncategorized";
546                                 }
547                                 
548         //                      $class = ($lnum % 2) ? "even" : "odd";
549
550                                 if ($line["last_error"]) {
551                                         $class = "error";
552                                 } else {
553                                         $class = "feed";
554                                 }
555         
556                                 if ($unread > 0) $class .= "Unread";
557         
558                                 if ($actid == $feed_id) {
559                                         $class .= "Selected";
560                                 }
561         
562                                 $total_unread += $unread;
563
564                                 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
565                                 
566                                         if ($category) {
567                                                 print "</ul></li>";
568                                         }
569                                 
570                                         $category = $tmp_category;
571
572                                         $collapsed = $line["collapsed"];
573
574                                         // workaround for NULL category
575                                         if ($category == "Uncategorized") {
576                                                 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
577                                                         $collapsed = "t";
578                                                 }
579                                         }
580
581                                         if ($collapsed == "t" || $collapsed == "1") {
582                                                 $holder_class = "invisible";
583                                                 $ellipsis = "...";
584                                         } else {
585                                                 $holder_class = "";
586                                                 $ellipsis = "";
587                                         }
588
589                                         if ($cat_id) {
590                                                 $cat_id_qpart = "cat_id = '$cat_id'";
591                                         } else {
592                                                 $cat_id_qpart = "cat_id IS NULL";
593                                         }
594
595                                         $tmp_result = db_query($link, "SELECT count(int_id) AS unread
596                                                 FROM ttrss_user_entries,ttrss_feeds WHERE
597                                                         unread = true AND
598                                                         feed_id = ttrss_feeds.id AND $cat_id_qpart AND
599                                                         ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
600
601                                         $cat_unread = db_fetch_result($tmp_result, 0, "unread");
602
603                                         $cat_id = sprintf("%d", $cat_id);
604                                         
605                                         print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
606                                                 <a href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
607                                                         <a href=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
608                                                         <span id=\"FCATCTR-$cat_id\" 
609                                                         class=\"$catctr_class\">($cat_unread unread)$ellipsis</span></a>
610                                                         <!-- <div style=\"float : right\">
611                                                                 <a href=\"javascript:viewCategory($cat_id)\">[view]</a>
612                                                         </div> -->
613                                                 </li>";
614
615                                         // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
616                                         // -> keyboard navigation, etc.
617                                         print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
618                                 }
619         
620                                 printFeedEntry($feed_id, $class, $feed, $unread, 
621                                         "icons/$feed_id.ico", $link);
622         
623                                 ++$lnum;
624                         }
625
626                 } else {
627
628                         // tags
629
630 /*                      $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
631                                 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
632                                 post_int_id = ttrss_user_entries.int_id AND 
633                                 unread = true AND ref_id = ttrss_entries.id
634                                 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name       
635                         UNION
636                                 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
637                         ORDER BY tag_name"); */
638
639                         $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) 
640                                 FROM ttrss_user_entries WHERE int_id = post_int_id 
641                                         AND unread = true)) AS count FROM ttrss_tags 
642                                 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
643
644                         $tags = array();
645         
646                         while ($line = db_fetch_assoc($result)) {
647                                 $tags[$line["tag_name"]] += $line["count"];
648                         }
649         
650                         foreach (array_keys($tags) as $tag) {
651         
652                                 $unread = $tags[$tag];
653         
654                                 $class = "tag";
655         
656                                 if ($unread > 0) {
657                                         $class .= "Unread";
658                                 }
659         
660                                 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
661         
662                         } 
663
664                 }
665
666                 if (db_num_rows($result) == 0) {
667                         if ($tags) {
668                                 $what = "tags";
669                         } else {
670                                 $what = "feeds";
671                         }
672                         print "<li>No $what to display.</li>";
673                 }
674
675                 print "</ul>";
676
677                 print '
678                         <script type="text/javascript">
679                                 /* for IE */
680                                 function statechange() {
681                                         if (document.readyState == "interactive") init();
682                                 }
683                         
684                                 if (document.readyState) {      
685                                         if (document.readyState == "interactive" || document.readyState == "complete") {
686                                                 init();
687                                         } else {
688                                                 document.onreadystatechange = statechange;
689                                         }
690                                 }
691                         </script></body></html>';
692         }
693
694
695         if ($op == "rpc") {
696
697                 $subop = $_GET["subop"];
698
699                 if ($subop == "setpref") {
700                         if (WEB_DEMO_MODE) {
701                                 return;
702                         }
703
704                         print "<rpc-reply>";
705
706                         $key = db_escape_string($_GET["key"]);
707                         $value = db_escape_string($_GET["value"]);
708
709                         set_pref($link, $key, $value);
710
711                         print "<param-set key=\"$key\" value=\"$value\"/>";
712
713                         print "</rpc-reply>";
714
715                 }
716
717                 if ($subop == "getLabelCounters") {
718                         $aid = $_GET["aid"];            
719                         print "<rpc-reply>";
720                         getLabelCounters($link);
721                         if ($aid) {
722                                 getFeedCounter($link, $aid);
723                         }
724                         print "</rpc-reply>";
725                 }
726
727                 if ($subop == "getFeedCounters") {
728                         print "<rpc-reply>";
729                         getFeedCounters($link);
730                         print "</rpc-reply>";
731                 }
732
733                 if ($subop == "getAllCounters") {
734                         print "<rpc-reply>";
735                         getAllCounters($link);
736                         print "</rpc-reply>";
737                 }
738
739                 if ($subop == "mark") {
740                         $mark = $_GET["mark"];
741                         $id = db_escape_string($_GET["id"]);
742
743                         if ($mark == "1") {
744                                 $mark = "true";
745                         } else {
746                                 $mark = "false";
747                         }
748
749                         // FIXME this needs collision testing
750
751                         $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
752                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
753                 }
754
755                 if ($subop == "updateFeed") {
756                         $feed_id = db_escape_string($_GET["feed"]);
757
758                         $result = db_query($link, 
759                                 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
760                                         AND owner_uid = " . $_SESSION["uid"]);
761
762                         if (db_num_rows($result) > 0) {                 
763                                 $feed_url = db_fetch_result($result, 0, "feed_url");
764                                 update_rss_feed($link, $feed_url, $feed_id);
765                         }
766
767                         print "<rpc-reply>";
768                         getFeedCounter($link, $feed_id);
769                         print "</rpc-reply>";
770                         
771                         return;
772                 }
773
774                 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
775         
776                         if (ENABLE_UPDATE_DAEMON) {
777
778                                 if ($subop == "forceUpdateAllFeeds") {                          
779
780                                         $result = db_query($link, "SELECT count(id) AS cid FROM
781                                                 ttrss_scheduled_updates WHERE feed_id IS NULL AND
782                                                         owner_uid = " . $_SESSION["uid"]);
783         
784                                         $cid = db_fetch_result($result, 0, "cid");
785         
786 #                                       print "<rpc-reply>";
787         
788                                         if ($cid == 0) {
789         
790                                                 db_query($link, "INSERT INTO ttrss_scheduled_updates
791                                                         (owner_uid, feed_id, entered) VALUES
792                                                         (".$_SESSION["uid"].", NULL, NOW())");
793                                                         
794 //                                              print "<!-- ScheduledOK -->";
795                                                 
796                                         } else {
797 //                                              print "<!-- RequestAlreadyInQueue -->";
798                                         }
799         
800 #                                       print "</rpc-reply>";
801                                 }
802                                 
803                         } else {        
804                                 update_all_feeds($link, $subop == "forceUpdateAllFeeds");
805                         }
806
807                         $global_unread_caller = sprintf("%d", $_GET["uctr"]);
808                         $global_unread = getGlobalUnread($link);
809
810                         print "<rpc-reply>";
811
812                         if ($global_unread_caller != $global_unread) {
813
814                                 $omode = $_GET["omode"];
815          
816                                 if (!$omode) $omode = "tflc";
817          
818                                 if (strchr($omode, "l")) getLabelCounters($link);
819                                 if (strchr($omode, "f")) getFeedCounters($link);
820                                 if (strchr($omode, "t")) getTagCounters($link);
821                                 if (strchr($omode, "c")) {                      
822                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
823                                                 getCategoryCounters($link);
824                                         }
825                                 }
826                         }
827
828                         getGlobalCounters($link, $global_unread);
829
830                         print "</rpc-reply>";
831
832                 }
833         
834                 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
835                 if ($subop == "catchupSelected") {
836
837                         $ids = split(",", db_escape_string($_GET["ids"]));
838
839                         $cmode = sprintf("%d", $_GET["cmode"]);
840
841                         foreach ($ids as $id) {
842
843                                 if ($cmode == 0) {
844                                         db_query($link, "UPDATE ttrss_user_entries SET 
845                                         unread = false,last_read = NOW()
846                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
847                                 } else if ($cmode == 1) {
848                                         db_query($link, "UPDATE ttrss_user_entries SET 
849                                         unread = true
850                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
851                                 } else {
852                                         db_query($link, "UPDATE ttrss_user_entries SET 
853                                         unread = NOT unread,last_read = NOW()
854                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
855                                 }
856                         }
857                         print "<rpc-reply>";
858                         getAllCounters($link);
859                         print "</rpc-reply>";
860                 }
861
862                 if ($subop == "markSelected") {
863
864                         $ids = split(",", db_escape_string($_GET["ids"]));
865
866                         $cmode = sprintf("%d", $_GET["cmode"]);
867
868                         foreach ($ids as $id) {
869
870                                 if ($cmode == 0) {
871                                         db_query($link, "UPDATE ttrss_user_entries SET 
872                                         marked = false
873                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
874                                 } else if ($cmode == 1) {
875                                         db_query($link, "UPDATE ttrss_user_entries SET 
876                                         marked = true
877                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
878                                 } else {
879                                         db_query($link, "UPDATE ttrss_user_entries SET 
880                                         marked = NOT marked
881                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
882                                 }
883                         }
884                         print "<rpc-reply>";
885                         getAllCounters($link);
886                         print "</rpc-reply>";
887                 }
888
889                 if ($subop == "sanityCheck") {
890                         if (sanity_check($link)) {
891                                 print "<error error-code=\"0\"/>";
892                         }
893                 }
894
895                 if ($subop == "globalPurge") {
896
897                         print "<rpc-reply>";
898                         global_purge_old_posts($link, true);
899                         print "</rpc-reply>";
900
901                 }
902
903         }
904         
905         if ($op == "feeds") {
906
907                 $tags = $_GET["tags"];
908
909                 $subop = $_GET["subop"];
910
911                 if ($subop == "catchupAll") {
912                         db_query($link, "UPDATE ttrss_user_entries SET 
913                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
914                 }
915
916                 if ($subop == "collapse") {
917                         $cat_id = db_escape_string($_GET["cid"]);
918
919                         db_query($link, "UPDATE ttrss_feed_categories SET
920                                 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " . 
921                                 $_SESSION["uid"]);
922                         return;
923                 }
924
925                 outputFeedList($link, $tags);
926
927         }
928
929         if ($op == "view") {
930
931                 $id = db_escape_string($_GET["id"]);
932                 $feed_id = db_escape_string($_GET["feed"]);
933
934                 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
935                         WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
936
937                 if (db_num_rows($result) == 1) {
938                         $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
939                 } else {
940                         $rtl_content = false;
941                 }
942
943                 if ($rtl_content) {
944                         $rtl_tag = "dir=\"RTL\"";
945                 } else {
946                         $rtl_tag = "";
947                 }
948
949                 $result = db_query($link, "UPDATE ttrss_user_entries 
950                         SET unread = false,last_read = NOW() 
951                         WHERE ref_id = '$id' AND feed_id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
952
953                 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
954                         SUBSTRING(updated,1,16) as updated,
955                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
956                         num_comments,
957                         author
958                         FROM ttrss_entries,ttrss_user_entries
959                         WHERE   id = '$id' AND ref_id = id");
960
961                 print "<html><head>
962                         <title>Tiny Tiny RSS : Article $id</title>
963                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
964
965                 $user_theme = $_SESSION["theme"];
966                 if ($user_theme) { 
967                         print "<link rel=\"stylesheet\" type=\"text/css\" 
968                                 href=\"themes/$user_theme/theme.css\">";
969                 }
970
971                 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
972                         print "<link rel=\"stylesheet\" type=\"text/css\" 
973                                 href=\"tt-rss_compact.css\"/>";
974                 } else {
975                         print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
976                                         type=\"text/css\" href=\"tt-rss_compact.css\"/>";
977                 }
978
979                 print "<script type=\"text/javascript\" src=\"functions.js\"></script>
980                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
981                         </head><body $rtl_tag>";
982
983                 if ($result) {
984
985                         $line = db_fetch_assoc($result);
986
987                         if ($line["icon_url"]) {
988                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
989                         } else {
990                                 $feed_icon = "&nbsp;";
991                         }
992
993 /*                      if ($line["comments"] && $line["link"] != $line["comments"]) {
994                                 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
995                         } else {
996                                 $entry_comments = "";
997                         } */
998
999                         $num_comments = $line["num_comments"];
1000                         $entry_comments = "";
1001
1002                         if ($num_comments > 0) {
1003                                 if ($line["comments"]) {
1004                                         $comments_url = $line["comments"];
1005                                 } else {
1006                                         $comments_url = $line["link"];
1007                                 }
1008                                 $entry_comments = "<a href=\"$comments_url\">$num_comments comments</a>";
1009                         } else {
1010                                 if ($line["comments"] && $line["link"] != $line["comments"]) {
1011                                         $entry_comments = "<a href=\"".$line["comments"]."\">comments</a>";
1012                                 }                               
1013                         }
1014
1015                         print "<div class=\"postReply\">";
1016
1017                         print "<div class=\"postHeader\"><table width=\"100%\">";
1018
1019                         $entry_author = $line["author"];
1020
1021                         if ($entry_author) {
1022                                 $entry_author = " - by $entry_author";
1023                         }
1024
1025                         print "<tr><td><a href=\"" . $line["link"] . "\">" . $line["title"] . 
1026                                 "</a>$entry_author</td>";
1027
1028                         $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), 
1029                                 strtotime($line["updated"]));
1030                 
1031                         print "<td class=\"postDate\">$parsed_updated</td>";
1032                                                 
1033                         print "</tr>";
1034
1035                         $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
1036                                 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
1037                                 ORDER BY tag_name");
1038         
1039                         $tags_str = "";
1040                         $f_tags_str = "";
1041
1042                         $num_tags = 0;
1043
1044                         while ($tmp_line = db_fetch_assoc($tmp_result)) {
1045                                 $num_tags++;
1046                                 $tag = $tmp_line["tag_name"];                           
1047                                 $tag_str = "<a href=\"javascript:parent.viewfeed('$tag')\">$tag</a>, "; 
1048                                 
1049                                 if ($num_tags == 5) {
1050                                         $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
1051                                 } else if ($num_tags < 5) {
1052                                         $tags_str .= $tag_str;
1053                                 }
1054                                 $f_tags_str .= $tag_str;
1055                         }
1056
1057                         $tags_str = preg_replace("/, $/", "", $tags_str);
1058                         $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
1059
1060 //                      $truncated_link = truncate_string($line["link"], 60);
1061
1062                         if ($tags_str || $entry_comments) {
1063                                 print "<tr><td width='50%'>
1064                                         $entry_comments</td>
1065                                         <td align=\"right\">$tags_str</td></tr>";
1066                         }
1067
1068                         print "</table></div>";
1069
1070                         print "<div class=\"postIcon\">" . $feed_icon . "</div>";
1071                         print "<div class=\"postContent\">";
1072                         
1073                         if (db_num_rows($tmp_result) > 5) {
1074                                 print "<div id=\"allEntryTags\">Tags: $f_tags_str</div>";
1075                         }
1076
1077                         if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
1078                                 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
1079                         }
1080
1081                         print $line["content"] . "</div>";
1082                         
1083                         print "</div>";
1084
1085                         print "<script type=\"text/javascript\">
1086                                 update_all_counters('$feed_id');
1087                         </script>";
1088                 }
1089
1090                 print "</body></html>";
1091         }
1092
1093         if ($op == "viewfeed") {
1094
1095                 $feed = $_GET["feed"];
1096                 $skip = $_GET["skip"];
1097                 $subop = $_GET["subop"];
1098                 $view_mode = $_GET["view"];
1099                 $limit = $_GET["limit"];
1100                 $cat_view = $_GET["cat"];
1101
1102                 if (!$skip) $skip = 0;
1103
1104                 if ($subop == "undefined") $subop = "";
1105
1106                 print "<html><head>
1107                         <title>Tiny Tiny RSS : Feed $feed</title>
1108                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
1109
1110                 $user_theme = $_SESSION["theme"];
1111                 if ($user_theme) { 
1112                         print "<link rel=\"stylesheet\" type=\"text/css\" 
1113                                 href=\"themes/$user_theme/theme.css\">";
1114                 }
1115
1116                 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
1117                         print "<link rel=\"stylesheet\" 
1118                                         type=\"text/css\" href=\"tt-rss_compact.css\"/>";
1119
1120                 } else {
1121                         print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
1122                                         type=\"text/css\" href=\"tt-rss_compact.css\"/>";
1123                 }
1124
1125                 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
1126                         WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1127
1128                 if (db_num_rows($result) == 1) {
1129                         $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
1130                 } else {
1131                         $rtl_content = false;
1132                 }
1133
1134                 if ($rtl_content) {
1135                         $rtl_tag = "dir=\"RTL\"";
1136                 } else {
1137                         $rtl_tag = "";
1138                 }
1139
1140                 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">  
1141                         <script type=\"text/javascript\" src=\"functions.js\"></script>
1142                         <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
1143                         <!--[if gte IE 5.5000]>
1144                         <script type=\"text/javascript\" src=\"pngfix.js\"></script>
1145                         <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
1146                         <![endif]-->
1147                         </head><body>
1148                         <script type=\"text/javascript\">
1149                         if (document.addEventListener) {
1150                                 document.addEventListener(\"DOMContentLoaded\", init, null);
1151                         }
1152                         window.onload = init;
1153                         </script>";
1154
1155                 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
1156
1157 /*                      if (ENABLE_UPDATE_DAEMON) {
1158
1159                                 if ($cid == 0) {
1160
1161                                         db_query($link, "INSERT INTO ttrss_scheduled_updates
1162                                                 (owner_uid, feed_id, entered) VALUES
1163                                                 (".$_SESSION["uid"].", '$feed', NOW())");
1164                                 }
1165
1166                         } else { 
1167                                 // fixme update_rss_feed...
1168                         } */
1169
1170                         if ($cat_view) {
1171
1172                                 if ($feed > 0) {
1173                                         $cat_qpart = "cat_id = '$feed'";
1174                                 } else {
1175                                         $cat_qpart = "cat_id IS NULL";
1176                                 }
1177                                 
1178                                 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1179                                         WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1180
1181                                 while ($tmp_line = db_fetch_assoc($tmp_result)) {                                       
1182                                         $feed_url = $tmp_line["feed_url"];
1183                                         update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1184                                 }
1185
1186                         } else {
1187                                 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1188                                         WHERE id = '$feed'");
1189                                 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");                                
1190                                 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1191                         }
1192                 }
1193
1194                 if ($subop == "MarkAllRead")  {
1195
1196                         if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1197                         
1198                                 if ($cat_view) {
1199
1200                                         if ($feed > 0) {
1201                                                 $cat_qpart = "cat_id = '$feed'";
1202                                         } else {
1203                                                 $cat_qpart = "cat_id IS NULL";
1204                                         }
1205                                         
1206                                         $tmp_result = db_query($link, "SELECT id 
1207                                                 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . 
1208                                                 $_SESSION["uid"]);
1209
1210                                         while ($tmp_line = db_fetch_assoc($tmp_result)) {
1211
1212                                                 $tmp_feed = $tmp_line["id"];
1213
1214                                                 db_query($link, "UPDATE ttrss_user_entries 
1215                                                         SET unread = false,last_read = NOW() 
1216                                                         WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1217                                         }
1218
1219                                 } else if ($feed > 0) {
1220
1221                                         $tmp_result = db_query($link, "SELECT id 
1222                                                 FROM ttrss_feeds WHERE parent_feed = '$feed'
1223                                                 ORDER BY cat_id,title");
1224
1225                                         $parent_ids = array();
1226
1227                                         if (db_num_rows($tmp_result) > 0) {
1228                                                 while ($p = db_fetch_assoc($tmp_result)) {
1229                                                         array_push($parent_ids, "feed_id = " . $p["id"]);
1230                                                 }
1231
1232                                                 $children_qpart = implode(" OR ", $parent_ids);
1233                                                 
1234                                                 db_query($link, "UPDATE ttrss_user_entries 
1235                                                         SET unread = false,last_read = NOW() 
1236                                                         WHERE (feed_id = '$feed' OR $children_qpart) 
1237                                                         AND owner_uid = " . $_SESSION["uid"]);
1238
1239                                         } else {                                                
1240                                                 db_query($link, "UPDATE ttrss_user_entries 
1241                                                         SET unread = false,last_read = NOW() 
1242                                                         WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1243                                         }
1244                                                 
1245                                 } else if ($feed < 0 && $feed > -10) { // special, like starred
1246
1247                                         if ($feed == -1) {
1248                                                 db_query($link, "UPDATE ttrss_user_entries 
1249                                                         SET unread = false,last_read = NOW()
1250                                                         WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1251                                         }
1252                         
1253                                 } else if ($feed < -10) { // label
1254
1255                                         // TODO make this more efficient
1256
1257                                         $label_id = -$feed - 11;
1258
1259                                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1260                                                 WHERE id = '$label_id'");                                       
1261
1262                                         if ($tmp_result) {
1263                                                 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1264
1265                                                 db_query($link, "BEGIN");
1266
1267                                                 $tmp2_result = db_query($link,
1268                                                         "SELECT 
1269                                                                 int_id 
1270                                                         FROM 
1271                                                                 ttrss_user_entries,ttrss_entries 
1272                                                         WHERE
1273                                                                 ref_id = id AND 
1274                                                                 $sql_exp AND
1275                                                                 owner_uid = " . $_SESSION["uid"]);
1276
1277                                                 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1278                                                         db_query($link, "UPDATE 
1279                                                                 ttrss_user_entries 
1280                                                         SET 
1281                                                                 unread = false, last_read = NOW()
1282                                                         WHERE
1283                                                                 int_id = " . $tmp_line["int_id"]);
1284                                                 }
1285                                                                 
1286                                                 db_query($link, "COMMIT");
1287
1288 /*                                              db_query($link, "UPDATE ttrss_user_entries,ttrss_entries 
1289                                                         SET unread = false,last_read = NOW()
1290                                                         WHERE $sql_exp
1291                                                         AND ref_id = id
1292                                                         AND owner_uid = ".$_SESSION["uid"]); */
1293                                         }
1294                                 }
1295                         } else { // tag
1296                                 db_query($link, "BEGIN");
1297
1298                                 $tag_name = db_escape_string($feed);
1299
1300                                 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1301                                         WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1302
1303                                 while ($line = db_fetch_assoc($result)) {
1304                                         db_query($link, "UPDATE ttrss_user_entries SET
1305                                                 unread = false, last_read = NOW() 
1306                                                 WHERE int_id = " . $line["post_int_id"]);
1307                                 }
1308                                 db_query($link, "COMMIT");
1309                         }
1310
1311                 }
1312
1313                 $search = db_escape_string($_GET["search"]);
1314                 $search_mode = db_escape_string($_GET["smode"]);
1315
1316                 if ($search) {
1317                         $search_query_part = "(upper(ttrss_entries.title) LIKE upper('%$search%') 
1318                                 OR ttrss_entries.content LIKE '%$search%') AND";
1319                 } else {
1320                         $search_query_part = "";
1321                 }
1322
1323                 $view_query_part = "";
1324
1325                 if ($view_mode == "Adaptive") {
1326                         if ($search) {
1327                                 $view_query_part = " ";
1328                         } else if ($feed != -1) {
1329                                 $unread = getFeedUnread($link, $feed);
1330                                 if ($unread > 0) {
1331                                         $view_query_part = " unread = true AND ";
1332                                 }
1333                         }
1334                 }
1335
1336                 if ($view_mode == "Starred") {
1337                         $view_query_part = " marked = true AND ";
1338                 }
1339
1340                 if ($view_mode == "Unread") {
1341                         $view_query_part = " unread = true AND ";
1342                 }
1343
1344                 if ($limit && $limit != "All") {
1345                         $limit_query_part = "LIMIT " . $limit;
1346                 } 
1347
1348                 $vfeed_query_part = "";
1349
1350                 // override query strategy and enable feed display when searching globally
1351                 if ($search && $search_mode == "All feeds") {
1352                         $query_strategy_part = "ttrss_entries.id > 0";
1353                         $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
1354                 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
1355                         $query_strategy_part = "ttrss_entries.id > 0";
1356                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
1357                                 id = feed_id) as feed_title,";
1358                 } else if ($feed >= 0 && $search && $search_mode == "This category") {
1359
1360                         $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
1361
1362                         $tmp_result = db_query($link, "SELECT id 
1363                                 FROM ttrss_feeds WHERE cat_id = 
1364                                         (SELECT cat_id FROM ttrss_feeds WHERE id = '$feed') AND id != '$feed'");
1365
1366                         $cat_siblings = array();
1367
1368                         if (db_num_rows($tmp_result) > 0) {
1369                                 while ($p = db_fetch_assoc($tmp_result)) {
1370                                         array_push($cat_siblings, "feed_id = " . $p["id"]);
1371                                 }
1372
1373                                 $query_strategy_part = sprintf("(feed_id = %d OR %s)", 
1374                                         $feed, implode(" OR ", $cat_siblings));
1375
1376                         } else {
1377                                 $query_strategy_part = "ttrss_entries.id > 0";
1378                         }
1379                         
1380                 } else if ($feed >= 0) {
1381
1382                         if ($cat_view) {
1383
1384                                 if ($feed > 0) {
1385                                         $query_strategy_part = "cat_id = '$feed'";
1386                                 } else {
1387                                         $query_strategy_part = "cat_id IS NULL";
1388                                 }
1389
1390                                 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1391
1392                         } else {                
1393                                 $tmp_result = db_query($link, "SELECT id 
1394                                         FROM ttrss_feeds WHERE parent_feed = '$feed'
1395                                         ORDER BY cat_id,title");
1396         
1397                                 $parent_ids = array();
1398         
1399                                 if (db_num_rows($tmp_result) > 0) {
1400                                         while ($p = db_fetch_assoc($tmp_result)) {
1401                                                 array_push($parent_ids, "feed_id = " . $p["id"]);
1402                                         }
1403         
1404                                         $query_strategy_part = sprintf("(feed_id = %d OR %s)", 
1405                                                 $feed, implode(" OR ", $parent_ids));
1406         
1407                                         $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1408                                 } else {
1409                                         $query_strategy_part = "feed_id = '$feed'";
1410                                 }
1411                         }
1412                 } else if ($feed == -1) { // starred virtual feed
1413                         $query_strategy_part = "marked = true";
1414                         $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1415                 } else if ($feed <= -10) { // labels
1416                         $label_id = -$feed - 11;
1417
1418                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1419                                 WHERE id = '$label_id'");
1420                 
1421                         $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
1422         
1423                         $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1424                 } else {
1425                         $query_strategy_part = "id > 0"; // dumb
1426                 }
1427
1428                 $order_by = "updated DESC";
1429
1430 //              if ($feed < -10) {
1431 //                      $order_by = "feed_id,updated DESC";
1432 //              }
1433
1434                 $feed_title = "";
1435
1436                 if ($search && $search_mode == "All feeds") {
1437                         $feed_title = "Global search results ($search)";
1438                 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
1439                         $feed_title = "Feed search results ($search, $feed)";
1440                 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
1441                         $feed_title = $feed;
1442                 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
1443
1444                         if ($cat_view) {
1445
1446                                 if ($feed != 0) {                       
1447                                         $result = db_query($link, "SELECT title FROM ttrss_feed_categories
1448                                                 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1449                                         $feed_title = db_fetch_result($result, 0, "title");
1450                                 } else {
1451                                         $feed_title = "Uncategorized";
1452                                 }
1453                         } else {
1454                                 
1455                                 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds 
1456                                         WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1457         
1458                                 $feed_title = db_fetch_result($result, 0, "title");
1459                                 $feed_site_url = db_fetch_result($result, 0, "site_url");
1460                                 $last_error = db_fetch_result($result, 0, "last_error");
1461
1462                         }
1463
1464                 } else if ($feed == -1) {
1465                         $feed_title = "Starred articles";
1466                 } else if ($feed < -10) {
1467                         $label_id = -$feed - 11;
1468                         $result = db_query($link, "SELECT description FROM ttrss_labels
1469                                 WHERE id = '$label_id'");
1470                         $feed_title = db_fetch_result($result, 0, "description");
1471                 } else {
1472                         $feed_title = "?";
1473                 }
1474
1475                 if ($feed < -10) error_reporting (0);
1476
1477                 print "<div id=\"headlinesContainer\">";
1478
1479                 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1480
1481                         if ($feed >= 0) {
1482                                 $feed_kind = "Feeds";
1483                         } else {
1484                                 $feed_kind = "Labels";
1485                         }
1486
1487 //                      if (!$vfeed_query_part) {
1488                         $content_query_part = "content as content_preview,";
1489 //                      } else {
1490 //                              $content_query_part = "";
1491 //                      }
1492
1493                         $result = db_query($link, "SELECT 
1494                                         ttrss_entries.id,ttrss_entries.title,
1495                                         SUBSTRING(updated,1,16) as updated,
1496                                         unread,feed_id,marked,link,last_read,
1497                                         SUBSTRING(last_read,1,19) as last_read_noms,
1498                                         $vfeed_query_part
1499                                         $content_query_part
1500                                         SUBSTRING(updated,1,19) as updated_noms
1501                                 FROM
1502                                         ttrss_entries,ttrss_user_entries,ttrss_feeds
1503                                 WHERE
1504                                 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1505                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
1506                                 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
1507                                 $search_query_part
1508                                 $view_query_part
1509                                 $query_strategy_part ORDER BY $order_by
1510                                 $limit_query_part");
1511
1512                 } else {
1513                         // browsing by tag
1514
1515                         $feed_kind = "Tags";
1516
1517                         $result = db_query($link, "SELECT
1518                                 ttrss_entries.id as id,title,
1519                                 SUBSTRING(updated,1,16) as updated,
1520                                 unread,feed_id,
1521                                 marked,link,last_read,                          
1522                                 SUBSTRING(last_read,1,19) as last_read_noms,
1523                                 $vfeed_query_part
1524                                 $content_query_part
1525                                 SUBSTRING(updated,1,19) as updated_noms
1526                                 FROM
1527                                         ttrss_entries,ttrss_user_entries,ttrss_tags
1528                                 WHERE
1529                                         ref_id = ttrss_entries.id AND
1530                                         ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
1531                                         post_int_id = int_id AND tag_name = '$feed' AND
1532                                         $view_query_part
1533                                         $search_query_part
1534                                         $query_strategy_part ORDER BY $order_by
1535                                 $limit_query_part");    
1536                 }
1537
1538                 if (!$result) {
1539                         print "<div align='center'>
1540                                 Could not display feed (query failed). Please check label match syntax or local configuration.</div>";
1541                         return;
1542                 }
1543
1544                 function print_headline_subtoolbar($link, $feed_site_url, $feed_title, 
1545                                                 $bottom = false) {
1546
1547                         if (!$bottom) {
1548                                 $class = "headlinesSubToolbar";
1549                                 $tid = "headlineActionsTop";
1550                         } else {
1551                                 $class = "invisible";
1552                                 $tid = "headlineActionsBottom";
1553                         }
1554
1555                         print "<table class=\"$class\" id=\"$tid\"
1556                                 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
1557
1558                         if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
1559
1560                                 print "<td class=\"headlineActions\">
1561                                         Select: 
1562                                                                 <a href='#' onclick=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>,
1563                                                                 <a href='#' onclick=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>,
1564                                                                 <a href='#' onclick=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a>
1565                                                 &nbsp;&nbsp;
1566                                                 Toggle: <a href='#' onclick=\"javascript:selectionToggleUnread()\">Unread</a>,
1567                                                                 <a href='#' onclick=\"javascript:selectionToggleMarked()\">Starred</a>";
1568                                 print "</td>";
1569
1570                         } else {
1571
1572                                 print "<td class=\"headlineActions\">
1573                                         Select: 
1574                                                                 <a href=\"#\" onclick=\"javascript:cdmSelectArticles('all')\">All</a>,
1575                                                                 <a href=\"#\" onclick=\"javascript:cdmSelectArticles('unread')\">Unread</a>,
1576                                                                 <a href=\"#\" onclick=\"javascript:cdmSelectArticles('none')\">None</a>
1577                                                 &nbsp;&nbsp;
1578                                                 Toggle: <a href=\"#\" onclick=\"javascript:selectionToggleUnread(true)\">Unread</a>,
1579                                                                 <a href=\"#\" onclick=\"javascript:selectionToggleMarked(true)\">Starred</a>";
1580                         
1581                                 print "</td>";
1582
1583                         }
1584
1585                         if ($last_error) {
1586                                         print "<td align='center' class='small'>
1587                                                 <a class=\"warning\" href=\"javascript:alert('TT-RSS encountered an error while trying to update this feed.\\n\\n$last_error')\">Could not update this feed.</a></td>";
1588                         }       
1589
1590                         print "<td class=\"headlineTitle\">";
1591                 
1592                         if ($feed_site_url) {
1593                                 if (!$bottom) {
1594                                         $target = "target=\"_blank\"";
1595                                 }
1596                                 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
1597                         } else {
1598                                 print $feed_title;
1599                         }
1600                                 
1601                         print "</td>";
1602                         print "</tr></table>";
1603
1604                 }
1605         
1606                 if (db_num_rows($result) > 0) {
1607
1608                         print_headline_subtoolbar($link, $feed_site_url, $feed_title);
1609
1610                         if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
1611                                 print "<table class=\"headlinesList\" id=\"headlinesList\" 
1612                                         cellspacing=\"0\" width=\"100%\" $rtl_tag>";
1613                         }
1614
1615                         $lnum = 0;
1616         
1617                         error_reporting (DEFAULT_ERROR_LEVEL);
1618         
1619                         $num_unread = 0;
1620         
1621                         while ($line = db_fetch_assoc($result)) {
1622
1623                                 $class = ($lnum % 2) ? "even" : "odd";
1624         
1625                                 $id = $line["id"];
1626                                 $feed_id = $line["feed_id"];
1627         
1628                                 if ($line["last_read"] == "" && 
1629                                                 ($line["unread"] != "t" && $line["unread"] != "1")) {
1630         
1631                                         $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
1632                                                 alt=\"Updated\">";
1633                                 } else {
1634                                         $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
1635                                                 alt=\"Updated\">";
1636                                 }
1637         
1638                                 if ($line["unread"] == "t" || $line["unread"] == "1") {
1639                                         $class .= "Unread";
1640                                         ++$num_unread;
1641                                         $is_unread = true;
1642                                 } else {
1643                                         $is_unread = false;
1644                                 }
1645         
1646                                 if ($line["marked"] == "t" || $line["marked"] == "1") {
1647                                         $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\" 
1648                                                 alt=\"Reset mark\" onclick='javascript:toggleMark($id)'>";
1649                                 } else {
1650                                         $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\" 
1651                                                 alt=\"Set mark\" onclick='javascript:toggleMark($id)'>";
1652                                 }
1653         
1654                                 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
1655                                         $line["title"] . "</a>";
1656
1657                                 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1658                                         $updated_fmt = smart_date_time(strtotime($line["updated"]));
1659                                 } else {
1660                                         $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1661                                         $updated_fmt = date($short_date, strtotime($line["updated"]));
1662                                 }                               
1663
1664                                 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
1665                                         $content_preview = truncate_string(strip_tags($line["content_preview"]), 
1666                                                 100);
1667                                 }
1668
1669                                 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
1670                                         
1671                                         print "<tr class='$class' id='RROW-$id'>";
1672                 
1673                                         print "<td class='hlUpdatePic'>$update_pic</td>";
1674                 
1675                                         print "<td class='hlSelectRow'>
1676                                                 <input type=\"checkbox\" onclick=\"toggleSelectRow(this)\"
1677                                                         class=\"feedCheckBox\" id=\"RCHK-$id\">
1678                                                 </td>";
1679                 
1680                                         print "<td class='hlMarkedPic'>$marked_pic</td>";
1681                 
1682                                         if ($line["feed_title"]) {                      
1683                                                 print "<td class='hlContent'>$content_link</td>";
1684                                                 print "<td class='hlFeed'>
1685                                                         <a href='javascript:viewfeed($feed_id)'>".
1686                                                                 $line["feed_title"]."</a>&nbsp;</td>";
1687                                         } else {                        
1688                                                 print "<td class='hlContent' valign='middle'>";
1689                 
1690                                                 print "<a href=\"javascript:view($id,$feed_id);\">" .
1691                                                         $line["title"];
1692                 
1693                                                 if (get_pref($link, 'SHOW_CONTENT_PREVIEW') && !$rtl_tag) {
1694                                                         if ($content_preview) {
1695                                                                 print "<span class=\"contentPreview\"> - $content_preview</span>";
1696                                                         }
1697                                                 }
1698                 
1699                                                 print "</a>";
1700                                                 print "</td>";
1701                                         }
1702                                         
1703                                         print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
1704                 
1705                                         print "</tr>";
1706
1707                                 } else {
1708                                         
1709                                         if ($is_unread) {
1710                                                 $add_class = "Unread";
1711                                         } else {
1712                                                 $add_class = "";
1713                                         }       
1714                                         
1715                                         print "<div class=\"cdmArticle$add_class\" id=\"RROW-$id\">";
1716
1717                                         print "<div class=\"cdmHeader\">";
1718
1719                                         print "<div style=\"float : right\">$updated_fmt</div>";
1720                                         
1721                                         print "<a target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
1722
1723                                         if ($line["feed_title"]) {      
1724                                                 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
1725                                         }
1726
1727                                         print "</div>";
1728
1729                                         print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div>";
1730         
1731                                         print "<div style=\"float : right\">$marked_pic</div>
1732                                                 <div class=\"cdmFooter\">
1733                                                         <input type=\"checkbox\" onclick=\"toggleSelectRowById(this, 
1734                                                         'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\"></div>";
1735
1736                                         print "</div>"; 
1737
1738                                 }                               
1739         
1740                                 ++$lnum;
1741                         }
1742
1743                         if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {                        
1744                                 print "</table>";
1745                         }
1746
1747                         print_headline_subtoolbar($link, 
1748                                 "javascript:catchupPage()", "Mark page as read", true);
1749
1750
1751                 } else {
1752                         print "<div width='100%' align='center'>No articles found.</div>";
1753                 }
1754
1755                 print "</div>";
1756
1757                 print "<script type=\"text/javascript\">
1758                         document.onkeydown = hotkey_handler;
1759                         // if (parent.daemon_enabled) parent.updateTitle('$feed_title');
1760                         update_all_counters('$feed');
1761                 </script>";
1762         
1763                 print '
1764                         <script type="text/javascript">
1765                                 /* for IE */
1766                                 function statechange() {
1767                                         if (document.readyState == "interactive") init();
1768                                 }
1769                         
1770                                 if (document.readyState) {      
1771                                         if (document.readyState == "interactive" || document.readyState == "complete") {
1772                                                 init();
1773                                         } else {
1774                                                 document.onreadystatechange = statechange;
1775                                         }
1776                                 }
1777                         </script>';
1778
1779                 print "</body></html>";
1780         }
1781
1782         if ($op == "pref-rpc") {
1783
1784                 $subop = $_GET["subop"];
1785
1786                 if ($subop == "unread") {
1787                         $ids = split(",", db_escape_string($_GET["ids"]));
1788                         foreach ($ids as $id) {
1789                                 db_query($link, "UPDATE ttrss_user_entries SET unread = true 
1790                                         WHERE feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
1791                         }
1792
1793                         print "Marked selected feeds as unread.";
1794                 }
1795
1796                 if ($subop == "read") {
1797                         $ids = split(",", db_escape_string($_GET["ids"]));
1798                         foreach ($ids as $id) {
1799                                 db_query($link, "UPDATE ttrss_user_entries 
1800                                         SET unread = false,last_read = NOW() WHERE 
1801                                                 feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
1802                         }
1803
1804                         print "Marked selected feeds as read.";
1805
1806                 }
1807
1808         }
1809
1810         if ($op == "pref-feeds") {
1811         
1812                 $subop = $_REQUEST["subop"];
1813                 $quiet = $_REQUEST["quiet"];
1814
1815                 if ($subop == "massSubscribe") {
1816                         $ids = split(",", db_escape_string($_GET["ids"]));
1817
1818                         $subscribed = array();
1819
1820                         foreach ($ids as $id) {
1821                                 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
1822                                         WHERE id = '$id'");
1823
1824                                 $feed_url = db_fetch_result($result, 0, "feed_url");
1825                                 $title = db_fetch_result($result, 0, "title");
1826
1827                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
1828                                         feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
1829
1830                                 if (db_num_rows($result) == 0) {                        
1831                                         $result = db_query($link,
1832                                                 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) 
1833                                                 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
1834
1835                                         array_push($subscribed, $title);
1836                                 }
1837                         }
1838
1839                         if (count($subscribed) > 0) {
1840                                 print "<div class=\"notice\">";
1841                                 print "<b>Subscribed to feeds:</b>";
1842                                 print "<ul class=\"nomarks\">";
1843                                 foreach ($subscribed as $title) {
1844                                         print "<li>$title</li>";
1845                                 }
1846                                 print "</ul>";
1847                                 print "</div>";
1848                         }
1849                 }               
1850
1851                 if ($subop == "browse") {
1852
1853                         if (!ENABLE_FEED_BROWSER) {
1854                                 print "Feed browser is administratively disabled.";
1855                                 return;
1856                         }
1857                         
1858                         print "<div class=\"infoBoxContents\">";
1859
1860                         print "<h1>Feed browser</h1>";
1861
1862                         print "<p>Showing top 50 registered feeds, sorted by popularity:</p>";
1863
1864                         $result = db_query($link, "SELECT feed_url,count(id) AS subscribers 
1865                                 FROM ttrss_feeds 
1866                                 WHERE auth_login = '' AND auth_pass = '' AND private = false
1867                                 GROUP BY feed_url ORDER BY subscribers DESC LIMIT 50");
1868                         
1869                         print "<ul class='browseFeedList' id='browseFeedList'>";
1870
1871                         $feedctr = 0;
1872                         
1873                         while ($line = db_fetch_assoc($result)) {
1874                                 $feed_url = $line["feed_url"];
1875                                 $subscribers = $line["subscribers"];
1876
1877                                 $sub_result = db_query($link, "SELECT id
1878                                         FROM ttrss_feeds WHERE feed_url = '$feed_url' AND owner_uid =" . 
1879                                         $_SESSION["uid"]);
1880
1881                                 if (db_num_rows($sub_result) > 0) {
1882                                         continue; // already subscribed
1883                                 }
1884                         
1885                                 $det_result = db_query($link, "SELECT site_url,title,id 
1886                                         FROM ttrss_feeds WHERE feed_url = '$feed_url' LIMIT 1");
1887
1888                                 $details = db_fetch_assoc($det_result);
1889                         
1890                                 $icon_file = ICONS_DIR . "/" . $details["id"] . ".ico";
1891
1892                                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1893                                                 $feed_icon = "<img class=\"tinyFeedIcon\"       src=\"" . ICONS_URL . 
1894                                                         "/".$details["id"].".ico\">";
1895                                 } else {
1896                                         $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
1897                                 }
1898
1899                                 $check_box = "<input onclick='toggleSelectListRow(this)' class='feedBrowseCB' 
1900                                         type=\"checkbox\" id=\"FBCHK-" . $details["id"] . "\">";
1901
1902                                 $class = ($feedctr % 2) ? "even" : "odd";
1903
1904                                 print "<li class='$class' id=\"FBROW-".$details["id"]."\">$check_box".
1905                                         "$feed_icon " . db_unescape_string($details["title"]) . 
1906                                         "&nbsp;<span class='subscribers'>($subscribers)</span></li>";
1907
1908                                         ++$feedctr;
1909                         }
1910
1911                         if ($feedctr == 0) {
1912                                 print "<li>No feeds found to subscribe.</li>";
1913                         }
1914
1915                         print "</ul>";
1916
1917                         print "<div align='center'>
1918                                 <input type=\"submit\" class=\"button\" 
1919                                 onclick=\"feedBrowserSubscribe()\" value=\"Subscribe\">
1920                                 <input type='submit' class='button'                     
1921                                 onclick=\"closeInfoBox()\" value=\"Cancel\"></div>";
1922
1923                         print "</div>";
1924                         return;
1925                 }
1926
1927                 if ($subop == "editfeed") {
1928                         $feed_id = db_escape_string($_GET["id"]);
1929
1930                         $result = db_query($link, 
1931                                 "SELECT * FROM ttrss_feeds WHERE id = '$feed_id' AND
1932                                         owner_uid = " . $_SESSION["uid"]);
1933
1934                         $title = htmlspecialchars(db_unescape_string(db_fetch_result($result,
1935                                 0, "title")));
1936
1937                         print "<div class=\"infoBoxContents\">";
1938
1939                         $icon_file = ICONS_DIR . "/$feed_id.ico";
1940         
1941                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
1942                                         $feed_icon = "<img width=\"16\" height=\"16\"
1943                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
1944                         } else {
1945                                 $feed_icon = "";
1946                         }
1947         
1948                         print "<h1>$feed_icon $title</h1>";
1949
1950                         print "<table width='100%'>";
1951
1952                         $row_class = "odd";
1953
1954                         print "<tr class='$row_class'><td>Title:</td>";
1955                         print "<td><input id=\"iedit_title\" value=\"$title\"></td></tr>";
1956
1957                         $feed_url = db_fetch_result($result, 0, "feed_url");
1958                         $feed_url = htmlspecialchars(db_unescape_string(db_fetch_result($result,
1959                                 0, "feed_url")));
1960                         $row_class = toggleEvenOdd($row_class);
1961
1962                         print "<tr class='$row_class'><td>Feed URL:</td>";
1963                         print "<td><input id=\"iedit_link\" value=\"$feed_url\"></td></tr>";
1964
1965                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
1966
1967                                 $cat_id = db_fetch_result($result, 0, "cat_id");
1968
1969                                 $row_class = toggleEvenOdd($row_class);
1970
1971                                 print "<tr class='$row_class'><td>Category:</td>";
1972                                 print "<td>";
1973                                 print "<select id=\"iedit_fcat\">";
1974                                 print "<option id=\"0\">Uncategorized</option>";
1975
1976                                 $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1977                                         WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1978
1979                                 if (db_num_rows($tmp_result) > 0) {
1980                                         print "<option disabled>--------</option>";
1981                                 }
1982
1983                                 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1984                                         if ($tmp_line["id"] == $cat_id) {
1985                                                 $is_selected = "selected";
1986                                         } else {
1987                                                 $is_selected = "";
1988                                         }
1989                                         printf("<option $is_selected id='%d'>%s</option>", 
1990                                                 $tmp_line["id"], $tmp_line["title"]);
1991                                 }
1992
1993                                 print "</select></td>";
1994                                 print "</td></tr>";
1995         
1996                         }
1997
1998                         $update_interval = db_fetch_result($result, 0, "update_interval");
1999                         $row_class = toggleEvenOdd($row_class);
2000
2001                         print "<tr class='$row_class'><td>Update Interval:</td>";
2002 //                      print "<td><input id=\"iedit_updintl\" 
2003 //                              value=\"$update_interval\"></td></tr>";
2004
2005                         print "<td>";
2006
2007                         print "<select id=\"iedit_updintl\">";
2008                         
2009                         foreach (array_keys($update_intervals) as $i) {
2010                         
2011                                 if ($i == $update_interval) {
2012                                         $selected = "selected";
2013                                 } else {
2014                                         $selected = "";
2015                                 }                                       
2016                                 print "<option $selected id=\"$i\">" . $update_intervals[$i] . "</option>";
2017                         }
2018                                 
2019                         print "</select>";
2020
2021                         print "</td>";
2022
2023                         $row_class = toggleEvenOdd($row_class);
2024                         print "<tr class='$row_class'><td>Link to:</td>";
2025
2026                         $tmp_result = db_query($link, "SELECT COUNT(id) AS count
2027                                 FROM ttrss_feeds WHERE parent_feed = '$feed_id'");
2028
2029                         $linked_count = db_fetch_result($tmp_result, 0, "count");
2030
2031                         $parent_feed = db_fetch_result($result, 0, "parent_feed");
2032
2033                         if ($linked_count > 0) {
2034                                 $disabled = "disabled";
2035                         }
2036
2037                         print "<select $disabled  id=\"iedit_parent_feed\">";
2038                         
2039                         print "<option id=\"0\">Not linked</option>";
2040
2041                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
2042                                 if ($cat_id) {
2043                                         $cat_qpart = "AND cat_id = '$cat_id'";
2044                                 } else {
2045                                         $cat_qpart = "AND cat_id IS NULL";
2046                                 }
2047                         }
2048
2049                         $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feeds
2050                                 WHERE id != '$feed_id' AND owner_uid = ".$_SESSION["uid"]." 
2051                                         $cat_qpart ORDER BY title");
2052
2053                                 if (db_num_rows($tmp_result) > 0) {
2054                                         print "<option disabled>--------</option>";
2055                                 }
2056
2057                                 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2058                                         if ($tmp_line["id"] == $parent_feed) {
2059                                                 $is_selected = "selected";
2060                                         } else {
2061                                                 $is_selected = "";
2062                                         }
2063                                         printf("<option $is_selected id='%d'>%s</option>", 
2064                                                 $tmp_line["id"], $tmp_line["title"]);
2065                                 }
2066
2067                                 print "</select></td>";
2068                                 print "</td></tr>";
2069
2070                         $purge_interval = db_fetch_result($result, 0, "purge_interval");
2071                         $row_class = toggleEvenOdd($row_class);
2072
2073                         print "<tr class='$row_class'><td>Purge Days:</td>";
2074 //                      print "<td><input id=\"iedit_purgintl\" 
2075 //                              value=\"$purge_interval\"></td></tr>";
2076
2077                         print "<td>";
2078
2079                         print "<select id=\"iedit_purgintl\">";
2080                         
2081                         foreach (array_keys($purge_intervals) as $i) {
2082                         
2083                                 if ($i == $purge_interval) {
2084                                         $selected = "selected";
2085                                 } else {
2086                                         $selected = "";
2087                                 }                                       
2088                                 print "<option $selected id=\"$i\">" . $purge_intervals[$i] . "</option>";
2089                         }
2090                                 
2091                         print "</select>";
2092
2093                         print "</td>";
2094
2095 //                      print "<tr><td colspan=\"2\"><b>Authentication</b></td></tr>";
2096
2097                         $row_class = toggleEvenOdd($row_class);
2098                         $auth_login = db_fetch_result($result, 0, "auth_login");
2099
2100                         print "<tr class='$row_class'><td>Login:</td>";
2101                         print "<td><input id=\"iedit_login\" 
2102                                 value=\"$auth_login\"></td></tr>";
2103
2104                         $row_class = toggleEvenOdd($row_class);
2105                         $auth_pass = db_fetch_result($result, 0, "auth_pass");
2106
2107                         print "<tr class='$row_class'><td>Password:</td>";
2108                         print "<td><input type=\"password\" id=\"iedit_pass\" 
2109                                 value=\"$auth_pass\"></td></tr>";
2110
2111                         $row_class = toggleEvenOdd($row_class);
2112                         $private = sql_bool_to_bool(db_fetch_result($result, 0, "private"));
2113
2114                         if ($private) {
2115                                 $checked = "checked";
2116                         } else {
2117                                 $checked = "";
2118                         }
2119
2120                         print "<tr class='$row_class'><td valign='top'>Options:</td>";
2121                         print "<td><input type=\"checkbox\" id=\"iedit_private\" 
2122                                 $checked><label for=\"iedit_private\">Hide from feed browser</label>";
2123
2124                         $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
2125
2126                         if ($rtl_content) {
2127                                 $checked = "checked";
2128                         } else {
2129                                 $checked = "";
2130                         }
2131
2132                         print "<br><input type=\"checkbox\" id=\"iedit_rtl\" 
2133                                 $checked><label for=\"iedit_rtl\">Right-to-left content</label>";
2134                         
2135                         print "</td></tr>";
2136
2137                         print "</table>";
2138                         print "</div>";
2139
2140                         print "<div align='center'>
2141                                 <input type=\"submit\" class=\"button\" 
2142                                 onclick=\"feedEditSave()\" value=\"Save\">
2143                                 <input type='submit' class='button'                     
2144                                 onclick=\"feedEditCancel()\" value=\"Cancel\"></div>";
2145                         return;
2146                 }
2147
2148                 if ($subop == "editSave") {
2149                         $feed_title = db_escape_string($_POST["t"]);
2150                         $feed_link = db_escape_string($_POST["l"]);
2151                         $upd_intl = db_escape_string($_POST["ui"]);
2152                         $purge_intl = db_escape_string($_POST["pi"]);
2153                         $feed_id = db_escape_string($_POST["id"]);
2154                         $cat_id = db_escape_string($_POST["catid"]);
2155                         $auth_login = db_escape_string($_POST["login"]);
2156                         $auth_pass = db_escape_string($_POST["pass"]);
2157                         $parent_feed = db_escape_string($_POST["pfeed"]);
2158                         $private = db_escape_string($_POST["is_pvt"]);
2159                         $rtl_content = db_escape_string($_POST["is_rtl"]);
2160
2161                         if (strtoupper($upd_intl) == "DEFAULT")
2162                                 $upd_intl = 0;
2163
2164                         if (strtoupper($upd_intl) == "DISABLED")
2165                                 $upd_intl = -1;
2166
2167                         if (strtoupper($purge_intl) == "DEFAULT")
2168                                 $purge_intl = 0;
2169
2170                         if (strtoupper($purge_intl) == "DISABLED")
2171                                 $purge_intl = -1;
2172
2173                         if ($cat_id != 0) {
2174                                 $category_qpart = "cat_id = '$cat_id'";
2175                         } else {
2176                                 $category_qpart = 'cat_id = NULL';
2177                         }
2178
2179                         if ($parent_feed != 0) {
2180                                 $parent_qpart = "parent_feed = '$parent_feed'";
2181                         } else {
2182                                 $parent_qpart = 'parent_feed = NULL';
2183                         }
2184
2185                         $result = db_query($link, "UPDATE ttrss_feeds SET 
2186                                 $category_qpart,
2187                                 $parent_qpart,
2188                                 title = '$feed_title', feed_url = '$feed_link',
2189                                 update_interval = '$upd_intl',
2190                                 purge_interval = '$purge_intl',
2191                                 auth_login = '$auth_login',
2192                                 auth_pass = '$auth_pass',
2193                                 private = '$private',
2194                                 rtl_content = '$rtl_content'
2195                                 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);                    
2196                 }
2197
2198                 if ($subop == "saveCat") {
2199                         $cat_title = db_escape_string($_GET["title"]);
2200                         $cat_id = db_escape_string($_GET["id"]);
2201
2202                         $result = db_query($link, "UPDATE ttrss_feed_categories SET
2203                                 title = '$cat_title' WHERE id = '$cat_id' AND owner_uid = ".$_SESSION["uid"]);
2204
2205                 }
2206
2207                 if ($subop == "remove") {
2208
2209                         if (!WEB_DEMO_MODE) {
2210
2211                                 $ids = split(",", db_escape_string($_GET["ids"]));
2212
2213                                 foreach ($ids as $id) {
2214                                         db_query($link, "DELETE FROM ttrss_feeds 
2215                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2216
2217                                         $icons_dir = ICONS_DIR;
2218                                         
2219                                         if (file_exists($icons_dir . "/$id.ico")) {
2220                                                 unlink($icons_dir . "/$id.ico");
2221                                         }
2222                                 }
2223                         }
2224                 }
2225
2226                 if ($subop == "add") {
2227                 
2228                         if (!WEB_DEMO_MODE) {
2229
2230                                 $feed_link = db_escape_string(trim($_GET["link"]));
2231                                 $cat_id = db_escape_string($_GET["cid"]);
2232
2233                                 if ($cat_id == "0" || !$cat_id) {
2234                                         $cat_qpart = "NULL";
2235                                 } else {
2236                                         $cat_qpart = "'$cat_id'";
2237                                 }
2238
2239                                 $result = db_query($link,
2240                                         "SELECT id FROM ttrss_feeds 
2241                                         WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2242
2243                                 if (db_num_rows($result) == 0) {
2244                                         
2245                                         $result = db_query($link,
2246                                                 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) 
2247                                                 VALUES ('".$_SESSION["uid"]."', '$feed_link', 
2248                                                 '[Unknown]', $cat_qpart)");
2249
2250                                         $result = db_query($link,
2251                                                 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' 
2252                                                 AND owner_uid = " . $_SESSION["uid"]);
2253
2254                                         $feed_id = db_fetch_result($result, 0, "id");
2255
2256                                         if ($feed_id) {
2257                                                 update_rss_feed($link, $feed_link, $feed_id, true);
2258                                         }
2259                                 } else {
2260
2261                                         print "<div class=\"warning\">
2262                                                 Feed <b>$feed_link</b> already exists in the database.
2263                                         </div>";
2264                                 }
2265                         }
2266                 }
2267
2268                 if ($subop == "addCat") {
2269
2270                         if (!WEB_DEMO_MODE) {
2271
2272                                 $feed_cat = db_escape_string(trim($_GET["cat"]));
2273
2274                                 $result = db_query($link,
2275                                         "SELECT id FROM ttrss_feed_categories
2276                                         WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
2277
2278                                 if (db_num_rows($result) == 0) {
2279                                         
2280                                         $result = db_query($link,
2281                                                 "INSERT INTO ttrss_feed_categories (owner_uid,title) 
2282                                                 VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
2283
2284                                 } else {
2285
2286                                         print "<div class=\"warning\">
2287                                                 Category <b>$feed_cat</b> already exists in the database.
2288                                         </div>";
2289                                 }
2290
2291
2292                         }
2293                 }
2294
2295                 if ($subop == "removeCats") {
2296
2297                         if (!WEB_DEMO_MODE) {
2298
2299                                 $ids = split(",", db_escape_string($_GET["ids"]));
2300
2301                                 foreach ($ids as $id) {
2302
2303                                         db_query($link, "BEGIN");
2304
2305                                         $result = db_query($link, 
2306                                                 "SELECT count(id) as num_feeds FROM ttrss_feeds 
2307                                                         WHERE cat_id = '$id'");
2308
2309                                         $num_feeds = db_fetch_result($result, 0, "num_feeds");
2310
2311                                         if ($num_feeds == 0) {
2312                                                 db_query($link, "DELETE FROM ttrss_feed_categories
2313                                                         WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2314                                         } else {
2315
2316                                                 print "<div class=\"warning\">
2317                                                         Unable to delete non empty feed categories.</div>";
2318                                                         
2319                                         }
2320
2321                                         db_query($link, "COMMIT");
2322                                 }
2323                         }
2324                 }
2325
2326                 if ($subop == "categorize") {
2327
2328                         if (!WEB_DEMO_MODE) {
2329
2330                                 $ids = split(",", db_escape_string($_GET["ids"]));
2331
2332                                 $cat_id = db_escape_string($_GET["cat_id"]);
2333
2334                                 if ($cat_id == 0) {
2335                                         $cat_id_qpart = 'NULL';
2336                                 } else {
2337                                         $cat_id_qpart = "'$cat_id'";
2338                                 }
2339
2340                                 db_query($link, "BEGIN");
2341
2342                                 foreach ($ids as $id) {
2343                                 
2344                                         db_query($link, "UPDATE ttrss_feeds SET cat_id = $cat_id_qpart
2345                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2346                                 }
2347
2348                                 db_query($link, "COMMIT");
2349                         }
2350
2351                 }
2352
2353                 if ($quiet) return;
2354
2355 //              print "<h3>Edit Feeds</h3>";
2356
2357                 $result = db_query($link, "SELECT id,title,feed_url,last_error 
2358                         FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
2359
2360                 if (db_num_rows($result) > 0) {
2361                 
2362                         print "<div class=\"warning\">";
2363                         
2364 //                      print"<img class=\"closeButton\" 
2365 //                              onclick=\"javascript:hideParentElement(this);\" src=\"images/close.png\">";
2366         
2367                         print "<a href=\"javascript:showBlockElement('feedUpdateErrors')\">
2368                                 <b>Feeds with update errors</b> (click to expand)</a>";
2369
2370                         print "<ul id=\"feedUpdateErrors\" class=\"nomarks\">";
2371                                                 
2372                         while ($line = db_fetch_assoc($result)) {
2373                                 print "<li>" . $line["title"] . " (" . $line["feed_url"] . "): " . 
2374                                         $line["last_error"];
2375                         }
2376
2377                         print "</ul>";
2378                         print "</div>";
2379
2380                 }
2381
2382                 $feed_search = db_escape_string($_GET["search"]);
2383
2384                 if (array_key_exists("search", $_GET)) {
2385                         $_SESSION["prefs_feed_search"] = $feed_search;
2386                 } else {
2387                         $feed_search = $_SESSION["prefs_feed_search"];
2388                 }
2389
2390                 print "<table width='100%' class=\"prefGenericAddBox\" 
2391                         cellspacing='0' cellpadding='0'><tr>
2392                         <td>
2393                                 <input id=\"fadd_link\" 
2394                                 onchange=\"javascript:addFeed()\"
2395                                 size=\"40\">
2396                                 <input type=\"submit\" class=\"button\"
2397                                 onclick=\"javascript:addFeed()\" value=\"Add feed\">";
2398
2399                 if (ENABLE_FEED_BROWSER) {
2400                         print "&nbsp;(<a href='javascript:browseFeeds()'>Top 50</a>)";
2401                 }
2402                 
2403                 print "</td><td align='right'>
2404                                 <input id=\"feed_search\" size=\"20\"  
2405                                 onchange=\"javascript:updateFeedList()\"
2406                                 value=\"$feed_search\">
2407                                 <input type=\"submit\" class=\"button\" 
2408                                 onclick=\"javascript:updateFeedList()\" value=\"Search\">
2409                         </td>                   
2410                         </tr></table>";
2411
2412                 $feeds_sort = db_escape_string($_GET["sort"]);
2413
2414                 if (!$feeds_sort || $feeds_sort == "undefined") {
2415                         $feeds_sort = $_SESSION["pref_sort_feeds"];                     
2416                         if (!$feeds_sort) $feeds_sort = "title";
2417                 }
2418
2419                 $_SESSION["pref_sort_feeds"] = $feeds_sort;
2420
2421                 if ($feed_search) {
2422                         $search_qpart = "(UPPER(F1.title) LIKE UPPER('%$feed_search%') OR
2423                                 UPPER(F1.feed_url) LIKE UPPER('%$feed_search%')) AND";
2424                 } else {
2425                         $search_qpart = "";
2426                 }
2427
2428                 $result = db_query($link, "SELECT 
2429                                 F1.id,
2430                                 F1.title,
2431                                 F1.feed_url,
2432                                 substring(F1.last_updated,1,16) AS last_updated,
2433                                 F1.parent_feed,
2434                                 F1.update_interval,
2435                                 F1.purge_interval,
2436                                 F1.cat_id,
2437                                 F2.title AS parent_title,
2438                                 C1.title AS category                            
2439                         FROM 
2440                                 ttrss_feeds AS F1 
2441                                 LEFT JOIN ttrss_feeds AS F2
2442                                         ON (F1.parent_feed = F2.id)
2443                                 LEFT JOIN ttrss_feed_categories AS C1
2444                                         ON (F1.cat_id = C1.id)
2445                         WHERE 
2446                                 $search_qpart F1.owner_uid = '".$_SESSION["uid"]."'                     
2447                         ORDER by category,$feeds_sort,title");
2448
2449                 if (db_num_rows($result) != 0) {
2450
2451                         print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
2452
2453                         print "<p><table width=\"100%\" cellspacing=\"0\" 
2454                                 class=\"prefFeedList\" id=\"prefFeedList\">";
2455                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
2456                                 Select: 
2457                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedList', 
2458                                                 'FEEDR-', 'FRCHK-', true)\">All</a>,
2459                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedList', 
2460                                                 'FEEDR-', 'FRCHK-', false)\">None</a>
2461                                 </td</tr>";
2462
2463                         if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2464                                 print "<tr class=\"title\">
2465                                         <td width='5%' align='center'>&nbsp;</td>
2466                                         <td width='30%'><a href=\"javascript:updateFeedList('title')\">Title</a></td>
2467                                         <td width='30%'><a href=\"javascript:updateFeedList('feed_url')\">Feed</a></td>
2468                                         <td width='15%'><a href=\"javascript:updateFeedList('update_interval')\">Update Interval</a></td>
2469                                         <td width='15%'><a href=\"javascript:updateFeedList('purge_interval')\">Purge Interval</a></td></tr>";
2470                         }
2471                         
2472                         $lnum = 0;
2473
2474                         $cur_cat_id = -1;
2475                         
2476                         while ($line = db_fetch_assoc($result)) {
2477         
2478                                 $feed_id = $line["id"];
2479                                 $cat_id = $line["cat_id"];
2480
2481                                 $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
2482                                 $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
2483                                 $edit_cat = htmlspecialchars(db_unescape_string($line["category"]));
2484         
2485 //                              if ($line["update_interval"] == "0") $line["update_interval"] = "Default";
2486 //                              if ($line["update_interval"] == "-1") $line["update_interval"] = "Disabled";
2487 //                              if ($line["purge_interval"] == "0") $line["purge_interval"] = "Default";
2488 //                              if ($line["purge_interval"] < 0)        $line["purge_interval"] = "Disabled";
2489
2490                                 if (!$edit_cat) $edit_cat = "Uncategorized";
2491
2492                                 if (get_pref($link, 'ENABLE_FEED_CATS') && $cur_cat_id != $cat_id) {
2493                                         $lnum = 0;
2494                                 
2495                                         print "<tr><td colspan=\"6\" class=\"feedEditCat\">$edit_cat</td></tr>";
2496
2497                                         print "<tr class=\"title\">
2498                                                 <td width='5%' align='center'>&nbsp;</td>
2499                                                 <td width='30%'><a href=\"javascript:updateFeedList('title')\">Title</a></td>
2500                                                 <td width='30%'><a href=\"javascript:updateFeedList('feed_url')\">Feed</a></td>
2501                                                 <td width='15%'><a href=\"javascript:updateFeedList('update_interval')\">Update Interval</a></td>
2502                                                 <td width='15%'><a href=\"javascript:updateFeedList('purge_interval')\">Purge Interval</a></td></tr>";
2503
2504                                         $cur_cat_id = $cat_id;
2505                                 }
2506
2507                                 $class = ($lnum % 2) ? "even" : "odd";
2508                                 $this_row_id = "id=\"FEEDR-$feed_id\"";
2509
2510                                 print "<tr class=\"$class\" $this_row_id>";
2511         
2512                                 $icon_file = ICONS_DIR . "/$feed_id.ico";
2513         
2514                                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
2515                                                 $feed_icon = "<img class=\"tinyFeedIcon\"       src=\"" . ICONS_URL . "/$feed_id.ico\">";
2516                                 } else {
2517                                         $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
2518                                 }
2519 //                              print "<td class='feedIcon'>$feed_icon</td>";           
2520         
2521                                 print "<td class='feedSelect'><input onclick='toggleSelectRow(this);' 
2522                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
2523
2524                                 $edit_title = truncate_string($edit_title, 40);
2525                                 $edit_link = truncate_string($edit_link, 60);
2526
2527                                 $parent_title = $line["parent_title"];
2528                                 if ($parent_title) {
2529                                         $parent_title = "<span class='groupPrompt'>(linked to 
2530                                                 $parent_title)</span>";
2531                                 }
2532
2533                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
2534                                         "$feed_icon $edit_title $parent_title" . "</a></td>";           
2535                                         
2536                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
2537                                         $edit_link . "</a></td>";               
2538
2539 /*                              if (get_pref($link, 'ENABLE_FEED_CATS')) {
2540                                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
2541                                                 $edit_cat . "</a></td>";                
2542                                 } */
2543
2544                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
2545                                         $update_intervals[$line["update_interval"]] . "</a></td>";
2546
2547                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
2548                                         $purge_intervals[$line["purge_interval"]] . "</a></td>";
2549         
2550                                 print "</tr>";
2551         
2552                                 ++$lnum;
2553                         }
2554         
2555                         print "</table>";
2556
2557                         print "<p>";
2558         
2559                         if ($subop == "edit") {
2560                                 print "Edit feed:&nbsp;
2561                                         <input type=\"submit\" class=\"button\" 
2562                                                 onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
2563                                         <input type=\"submit\" class=\"button\" 
2564                                                 onclick=\"javascript:feedEditSave()\" value=\"Save\">";
2565                         } else {
2566         
2567                                 print "
2568                                         Selection:&nbsp;
2569                                 <input type=\"submit\" class=\"button\" 
2570                                         onclick=\"javascript:selectedFeedDetails()\" value=\"Details\">
2571                                 <input type=\"submit\" class=\"button\" 
2572                                         onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
2573                                 <input type=\"submit\" class=\"button\" 
2574                                         onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
2575
2576                                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2577
2578                                         print "&nbsp;&nbsp;";                           
2579
2580                                         $result = db_query($link, "SELECT title,id FROM ttrss_feed_categories
2581                                                 WHERE owner_uid = ".$_SESSION["uid"]."
2582                                                 ORDER BY title");
2583
2584                                         print "<select id=\"sfeed_set_fcat\">";
2585                                         print "<option id=\"0\">Uncategorized</option>";
2586
2587                                         if (db_num_rows($result) != 0) {
2588                 
2589                                                 print "<option disabled>--------</option>";
2590
2591                                                 while ($line = db_fetch_assoc($result)) {
2592                                                         printf("<option id='%d'>%s</option>", 
2593                                                                 $line["id"], $line["title"]);
2594                                                 }               
2595                                         }
2596
2597                                         print "</select>";
2598
2599                                         print " <input type=\"submit\" class=\"button\" 
2600                                         onclick=\"javascript:categorizeSelectedFeeds()\" value=\"Set category\">";
2601
2602                                 }
2603
2604                                 if (get_pref($link, 'ENABLE_PREFS_CATCHUP_UNCATCHUP')) {
2605                                         print "
2606                                         <input type=\"submit\" class=\"button\" 
2607                                                 onclick=\"javascript:readSelectedFeeds(true)\" value=\"Mark as read\">
2608                                         <input type=\"submit\" class=\"button\" 
2609                                                 onclick=\"javascript:readSelectedFeeds(false)\" 
2610                                                 value=\"Mark as unread\">&nbsp;";
2611                                 }
2612                                 
2613                                 print "
2614                                         &nbsp;All feeds: <input type=\"submit\" 
2615                                                         class=\"button\" onclick=\"gotoExportOpml()\" 
2616                                                         value=\"Export OPML\">";                        
2617                                 }
2618                 } else {
2619
2620                         print "<p>No feeds defined.</p>";
2621
2622                 }
2623
2624                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2625
2626                         print "<h3>Edit Categories</h3>";
2627
2628         //              print "<h3>Categories</h3>";
2629
2630                         print "<div class=\"prefGenericAddBox\">
2631                                 <input id=\"fadd_cat\" 
2632                                         onchange=\"javascript:addFeedCat()\"
2633                                         size=\"40\">&nbsp;
2634                                 <input 
2635                                         type=\"submit\" class=\"button\" 
2636                                         onclick=\"javascript:addFeedCat()\" value=\"Add category\"></div>";
2637         
2638                         $result = db_query($link, "SELECT title,id FROM ttrss_feed_categories
2639                                 WHERE owner_uid = ".$_SESSION["uid"]."
2640                                 ORDER BY title");
2641
2642                         if (db_num_rows($result) != 0) {
2643         
2644                                 print "<p><table width=\"100%\" class=\"prefFeedCatList\" 
2645                                         cellspacing=\"0\" id=\"prefFeedCatList\">";
2646
2647                                 print "<tr><td class=\"selectPrompt\" colspan=\"8\">
2648                                 Select: 
2649                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedCatList', 
2650                                                 'FCATR-', 'FCCHK-', true)\">All</a>,
2651                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFeedCatList', 
2652                                                 'FCATR-', 'FCCHK-', false)\">None</a>
2653                                 </td</tr>";
2654
2655                                 print "<tr class=\"title\">
2656                                                         <td width=\"5%\"></td><td width=\"80%\">Title</td>
2657                                                 </tr>";
2658                                                 
2659                                 $lnum = 0;
2660                                 
2661                                 while ($line = db_fetch_assoc($result)) {
2662                 
2663                                         $class = ($lnum % 2) ? "even" : "odd";
2664                 
2665                                         $cat_id = $line["id"];
2666                 
2667                                         $edit_cat_id = $_GET["id"];
2668                 
2669                                         if ($subop == "editCat" && $cat_id != $edit_cat_id) {
2670                                                 $class .= "Grayed";
2671                                                 $this_row_id = "";
2672                                         } else {
2673                                                 $this_row_id = "id=\"FCATR-$cat_id\"";
2674                                         }
2675                 
2676                                         print "<tr class=\"$class\" $this_row_id>";
2677                 
2678                                         $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
2679                 
2680                                         if (!$edit_cat_id || $subop != "editCat") {
2681                 
2682                                                 print "<td align='center'><input onclick='toggleSelectRow(this);' 
2683                                                 type=\"checkbox\" id=\"FCCHK-".$line["id"]."\"></td>";
2684                 
2685                                                 print "<td><a href=\"javascript:editFeedCat($cat_id);\">" . 
2686                                                         $edit_title . "</a></td>";              
2687                 
2688                                         } else if ($cat_id != $edit_cat_id) {
2689                 
2690                                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
2691                                                         id=\"FRCHK-".$line["id"]."\"></td>";
2692                 
2693                                                 print "<td>$edit_title</td>";           
2694                 
2695                                         } else {
2696                 
2697                                                 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
2698                 
2699                                                 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
2700                                                 
2701                                         }
2702                                         
2703                                         print "</tr>";
2704                 
2705                                         ++$lnum;
2706                                 }
2707         
2708                                 print "</table>";
2709         
2710                                 print "<p>";
2711         
2712                                 if ($subop == "editCat") {
2713                                         print "Edit category:&nbsp;
2714                                                 <input type=\"submit\" class=\"button\" 
2715                                                         onclick=\"javascript:feedCatEditCancel()\" value=\"Cancel\">
2716                                                 <input type=\"submit\" class=\"button\" 
2717                                                         onclick=\"javascript:feedCatEditSave()\" value=\"Save\">";
2718                                         } else {
2719                 
2720                                         print "
2721                                                 Selection:&nbsp;
2722                                         <input type=\"submit\" class=\"button\" 
2723                                                 onclick=\"javascript:editSelectedFeedCat()\" value=\"Edit\">
2724                                         <input type=\"submit\" class=\"button\" 
2725                                                 onclick=\"javascript:removeSelectedFeedCats()\" value=\"Remove\">";
2726         
2727                                 }
2728         
2729                         } else {
2730                                 print "<p>No feed categories defined.</p>";
2731                         }
2732                 }
2733
2734                 print "<h3>Import OPML</h3>
2735                 <form   enctype=\"multipart/form-data\" method=\"POST\" action=\"opml.php\">
2736                         File: <input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
2737                         <input class=\"button\" name=\"op\" onclick=\"return validateOpmlImport();\"
2738                                 type=\"submit\" value=\"Import\">
2739                         </form>";
2740
2741         }
2742
2743         if ($op == "pref-filters") {
2744
2745                 $subop = $_GET["subop"];
2746                 $quiet = $_GET["quiet"];
2747
2748                 if ($subop == "editSave") {
2749
2750                         $regexp = db_escape_string($_GET["r"]);
2751                         $descr = db_escape_string($_GET["d"]);
2752                         $match = db_escape_string($_GET["m"]);
2753                         $filter_id = db_escape_string($_GET["id"]);
2754                         $feed_id = db_escape_string($_GET["fid"]);
2755                         $action_id = db_escape_string($_GET["aid"]); 
2756
2757                         if (!$feed_id) {
2758                                 $feed_id = 'NULL';
2759                         } else {
2760                                 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
2761                         }
2762                         
2763                         $result = db_query($link, "UPDATE ttrss_filters SET 
2764                                 reg_exp = '$regexp', 
2765                                 description = '$descr',
2766                                 feed_id = $feed_id,
2767                                 action_id = '$action_id',
2768                                 filter_type = (SELECT id FROM ttrss_filter_types WHERE
2769                                         description = '$match')
2770                                 WHERE id = '$filter_id'");
2771                 }
2772
2773                 if ($subop == "remove") {
2774
2775                         if (!WEB_DEMO_MODE) {
2776
2777                                 $ids = split(",", db_escape_string($_GET["ids"]));
2778
2779                                 foreach ($ids as $id) {
2780                                         db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
2781                                         
2782                                 }
2783                         }
2784                 }
2785
2786                 if ($subop == "add") {
2787                 
2788                         if (!WEB_DEMO_MODE) {
2789
2790                                 $regexp = db_escape_string(trim($_GET["regexp"]));
2791                                 $match = db_escape_string(trim($_GET["match"]));
2792                                 $feed_id = db_escape_string($_GET["fid"]);
2793                                 $action_id = db_escape_string($_GET["aid"]); 
2794
2795                                 if (!$feed_id) {
2796                                         $feed_id = 'NULL';
2797                                 } else {
2798                                         $feed_id = sprintf("'%s'", db_escape_string($feed_id));
2799                                 }
2800
2801                                 $result = db_query($link,
2802                                         "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
2803                                                 action_id) 
2804                                         VALUES 
2805                                                 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
2806                                                         description = '$match'),'".$_SESSION["uid"]."', 
2807                                                         $feed_id, '$action_id')");
2808                         } 
2809                 }
2810
2811                 if ($quiet) return;
2812
2813                 $result = db_query($link, "SELECT description 
2814                         FROM ttrss_filter_types ORDER BY description");
2815
2816                 $filter_types = array();
2817
2818                 while ($line = db_fetch_assoc($result)) {
2819                         array_push($filter_types, $line["description"]);
2820                 }
2821
2822                 print "<div class=\"prefGenericAddBox\">
2823                 <input id=\"fadd_regexp\" size=\"40\">&nbsp;";
2824                 
2825                 print_select("fadd_match", "Title", $filter_types);     
2826
2827                 print "&nbsp;<select id=\"fadd_feed\">";
2828
2829                 print "<option selected id=\"0\">All feeds</option>";
2830
2831                 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2832                         WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2833
2834                 if (db_num_rows($result) > 0) {
2835                         print "<option disabled>--------</option>";
2836                 }
2837
2838                 while ($line = db_fetch_assoc($result)) {
2839                         printf("<option id='%d'>%s</option>", $line["id"], $line["title"]);
2840                 }
2841
2842                 print "</select>&nbsp;";
2843
2844                 print "&nbsp;Action: ";
2845
2846                 print "<select id=\"fadd_action\">";
2847
2848                 $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions 
2849                         ORDER BY name");
2850
2851                 while ($line = db_fetch_assoc($result)) {                       
2852                         printf("<option id='%d'>%s</option>", $line["id"], $line["description"]);
2853                 }
2854
2855                 print "</select>&nbsp;";
2856
2857                 print "<input type=\"submit\" 
2858                         class=\"button\" onclick=\"javascript:addFilter()\" 
2859                         value=\"Add filter\">";
2860
2861                 print "</div>";
2862
2863                 $result = db_query($link, "SELECT 
2864                                 ttrss_filters.id AS id,reg_exp,
2865                                 ttrss_filters.description AS description,
2866                                 ttrss_filter_types.name AS filter_type_name,
2867                                 ttrss_filter_types.description AS filter_type_descr,
2868                                 feed_id,
2869                                 ttrss_filter_actions.description AS action_description,
2870                                 ttrss_feeds.title AS feed_title
2871                         FROM 
2872                                 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
2873                                         ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
2874                         WHERE
2875                                 filter_type = ttrss_filter_types.id AND
2876                                 ttrss_filter_actions.id = action_id AND
2877                                 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
2878                         ORDER by reg_exp");
2879
2880                 if (db_num_rows($result) != 0) {
2881
2882                         print "<p><table width=\"100%\" cellspacing=\"0\" class=\"prefFilterList\" 
2883                                 id=\"prefFilterList\">";
2884
2885                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
2886                                 Select: 
2887                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFilterList', 
2888                                                 'FILRR-', 'FICHK-', true)\">All</a>,
2889                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefFilterList', 
2890                                                 'FILRR-', 'FICHK-', false)\">None</a>
2891                                 </td</tr>";
2892
2893                         print "<tr class=\"title\">
2894                                                 <td align='center' width=\"5%\">&nbsp;</td>
2895                                                 <td width=\"20%\">Filter expression</td>
2896                                                 <td width=\"20%\">Feed</td>
2897                                                 <td width=\"15%\">Match</td>
2898                                                 <td width=\"15%\">Action</td>
2899                                                 <td width=\"30%\">Description</td></tr>";
2900                 
2901                         $lnum = 0;
2902                         
2903                         while ($line = db_fetch_assoc($result)) {
2904         
2905                                 $class = ($lnum % 2) ? "even" : "odd";
2906         
2907                                 $filter_id = $line["id"];
2908                                 $edit_filter_id = $_GET["id"];
2909         
2910                                 if ($subop == "edit" && $filter_id != $edit_filter_id) {
2911                                         $class .= "Grayed";
2912                                         $this_row_id = "";
2913                                 } else {
2914                                         $this_row_id = "id=\"FILRR-$filter_id\"";
2915                                 }
2916         
2917                                 print "<tr class=\"$class\" $this_row_id>";
2918         
2919                                 $line["regexp"] = htmlspecialchars($line["reg_exp"]);
2920                                 $line["description"] = htmlspecialchars($line["description"]);
2921         
2922                                 if (!$line["feed_title"]) $line["feed_title"] = "All feeds";
2923         
2924                                 if (!$edit_filter_id || $subop != "edit") {
2925         
2926                                         if (!$line["description"]) $line["description"] = "[No description]";
2927         
2928                                         print "<td align='center'><input onclick='toggleSelectRow(this);' 
2929                                         type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
2930         
2931                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2932                                                 $line["reg_exp"] . "</td>";             
2933         
2934                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2935                                                 $line["feed_title"] . "</td>";                  
2936         
2937                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2938                                                 $line["filter_type_descr"] . "</td>";           
2939                 
2940                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2941                                                 $line["action_description"] . "</td>";                  
2942
2943                                         print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
2944                                                 $line["description"] . "</td>";                 
2945         
2946                                 } else if ($filter_id != $edit_filter_id) {
2947         
2948                                         if (!$line["description"]) $line["description"] = "[No description]";
2949         
2950                                         print "<td><input disabled=\"true\" type=\"checkbox\" 
2951                                                 id=\"FICHK-".$line["id"]."\"></td>";
2952         
2953                                         print "<td>".$line["reg_exp"]."</td>";          
2954                                         print "<td>".$line["feed_title"]."</td>";
2955                                         print "<td>".$line["filter_type_descr"]."</td>";
2956                                         print "<td>".$line["action_description"]."</td>";               
2957                                         print "<td>".$line["description"]."</td>";              
2958
2959                                 } else {
2960         
2961                                         print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
2962         
2963                                         print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
2964                                                 "\"></td>";
2965         
2966                                         print "<td>";
2967                                         print "<select id=\"iedit_feed\">";
2968                                         print "<option id=\"0\">All feeds</option>";
2969         
2970                                         $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feeds
2971                                                 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2972
2973                                         if (db_num_rows($tmp_result) > 0) {
2974                                                 print "<option disabled>--------</option>";
2975                                         }
2976
2977                                         while ($tmp_line = db_fetch_assoc($tmp_result)) {
2978                                                 if ($tmp_line["id"] == $line["feed_id"]) {
2979                                                         $is_selected = "selected";
2980                                                 } else {
2981                                                         $is_selected = "";
2982                                                 }
2983                                                 printf("<option $is_selected id='%d'>%s</option>", 
2984                                                         $tmp_line["id"], $tmp_line["title"]);
2985                                         }
2986         
2987                                         print "</select></td>";
2988         
2989                                         print "<td>";
2990                                         print_select("iedit_match", $line["filter_type_descr"], $filter_types);
2991                                         print "</td>";
2992
2993                                         print "<td>";
2994                                         print "<select id=\"iedit_filter_action\">";
2995         
2996                                         $tmp_result = db_query($link, "SELECT id,description FROM ttrss_filter_actions
2997                                                 ORDER BY description");
2998
2999                                         while ($tmp_line = db_fetch_assoc($tmp_result)) {
3000                                                 if ($tmp_line["description"] == $line["action_description"]) {
3001                                                         $is_selected = "selected";
3002                                                 } else {
3003                                                         $is_selected = "";
3004                                                 }
3005                                                 printf("<option $is_selected id='%d'>%s</option>", 
3006                                                         $tmp_line["id"], $tmp_line["description"]);
3007                                         }
3008         
3009                                         print "</select></td>";
3010
3011
3012                                         print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
3013                                                 "\"></td>";
3014         
3015                                         print "</td>";
3016                                 }
3017                                 
3018                                 print "</tr>";
3019         
3020                                 ++$lnum;
3021                         }
3022         
3023                         if ($lnum == 0) {
3024                                 print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
3025                         }
3026         
3027                         print "</table>";
3028         
3029                         print "<p>";
3030         
3031                         if ($subop == "edit") {
3032                                 print "Edit filter:
3033                                         <input type=\"submit\" class=\"button\" 
3034                                                 onclick=\"javascript:filterEditSave()\" value=\"Save\">
3035                                         <input type=\"submit\" class=\"button\" 
3036                                                 onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">";
3037                                                 
3038                         } else {
3039         
3040                                 print "
3041                                         Selection:
3042                                 <input type=\"submit\" class=\"button\" 
3043                                         onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
3044                                 <input type=\"submit\" class=\"button\" 
3045                                         onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
3046                         }
3047
3048                 } else {
3049
3050                         print "<p>No filters defined.</p>";
3051
3052                 }
3053         }
3054
3055         // We need to accept raw SQL data in label queries, so not everything is escaped
3056         // here, this is by design. If you don't like the whole idea, disable labels
3057         // altogether with GLOBAL_ENABLE_LABELS = false
3058
3059         if ($op == "pref-labels") {
3060
3061                 if (!GLOBAL_ENABLE_LABELS) { 
3062                         return; 
3063                 }
3064
3065                 $subop = $_GET["subop"];
3066
3067                 if ($subop == "test") {
3068
3069                         $expr = $_GET["expr"];
3070                         $descr = $_GET["descr"];
3071
3072                         print "<div class='infoBoxContents'>";
3073                 
3074                         print "<h1>Label &laquo;$descr&raquo;</h1>";
3075
3076 //                      print "<p><b>Expression</b>: $expr</p>";
3077
3078                         $result = db_query($link, 
3079                                 "SELECT count(id) AS num_matches
3080                                         FROM ttrss_entries,ttrss_user_entries
3081                                         WHERE ($expr) AND 
3082                                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
3083                                                 owner_uid = " . $_SESSION["uid"]);
3084
3085                         $num_matches = db_fetch_result($result, 0, "num_matches");;
3086                         
3087                         if ($num_matches > 0) { 
3088
3089                                 print "<p>Query returned <b>$num_matches</b> matches, first 5 follow:</p>";
3090
3091                                 $result = db_query($link, 
3092                                         "SELECT title, 
3093                                                 (SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
3094                                         FROM ttrss_entries,ttrss_user_entries
3095                                                         WHERE ($expr) AND 
3096                                                         ttrss_user_entries.ref_id = ttrss_entries.id
3097                                                         AND owner_uid = " . $_SESSION["uid"] . " 
3098                                                         ORDER BY date_entered DESC LIMIT 5");
3099
3100                                 print "<ul class=\"nomarks\">";
3101                                 while ($line = db_fetch_assoc($result)) {
3102                                         print "<li>".$line["title"].
3103                                                 " <span class=\"insensitive\">(".$line["feed_title"].")</span></li>";
3104                                 }
3105                                 print "</ul>";
3106
3107                         } else {
3108                                 print "<p>Query didn't return any matches.</p>";
3109                         }
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                         return;
3117                 }
3118
3119                 if ($subop == "editSave") {
3120
3121                         $sql_exp = $_GET["s"];
3122                         $descr = $_GET["d"];
3123                         $label_id = db_escape_string($_GET["id"]);
3124                         
3125 //                      print "$sql_exp : $descr : $label_id";
3126                         
3127                         $result = db_query($link, "UPDATE ttrss_labels SET 
3128                                 sql_exp = '$sql_exp', 
3129                                 description = '$descr'
3130                                 WHERE id = '$label_id'");
3131                 }
3132
3133                 if ($subop == "remove") {
3134
3135                         if (!WEB_DEMO_MODE) {
3136
3137                                 $ids = split(",", db_escape_string($_GET["ids"]));
3138
3139                                 foreach ($ids as $id) {
3140                                         db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
3141                                         
3142                                 }
3143                         }
3144                 }
3145
3146                 if ($subop == "add") {
3147                 
3148                         if (!WEB_DEMO_MODE) {
3149
3150                                 // no escaping is done here on purpose
3151                                 $exp = trim($_GET["exp"]);
3152                                         
3153                                 $result = db_query($link,
3154                                         "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
3155                                                 VALUES ('$exp', '$exp', '".$_SESSION["uid"]."')");
3156                         } 
3157                 }
3158
3159                 print "<div class=\"prefGenericAddBox\">
3160                         <input size=\"40\" id=\"ladd_expr\">&nbsp;";
3161                         
3162                 print"<input type=\"submit\" class=\"button\" 
3163                         onclick=\"javascript:addLabel()\" value=\"Add label\"></div>";
3164
3165                 $result = db_query($link, "SELECT 
3166                                 id,sql_exp,description
3167                         FROM 
3168                                 ttrss_labels 
3169                         WHERE 
3170                                 owner_uid = ".$_SESSION["uid"]."
3171                         ORDER by description");
3172
3173                 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
3174
3175                 if (db_num_rows($result) != 0) {
3176
3177                         print "<p><table width=\"100%\" cellspacing=\"0\" 
3178                                 class=\"prefLabelList\" id=\"prefLabelList\">";
3179
3180                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
3181                                 Select: 
3182                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefLabelList', 
3183                                                 'LILRR-', 'LICHK-', true)\">All</a>,
3184                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefLabelList', 
3185                                                 'LILRR-', 'LICHK-', false)\">None</a>
3186                                 </td</tr>";
3187
3188                         print "<tr class=\"title\">
3189                                                 <td align='center' width=\"5%\">&nbsp;</td>
3190                                                 <td width=\"40%\">SQL expression
3191                                                 <a class=\"helpLink\" href=\"javascript:displayHelpInfobox(1)\">(?)</a>
3192                                                 </td>
3193                                                 <td width=\"40%\">Caption</td></tr>";
3194                         
3195                         $lnum = 0;
3196                         
3197                         while ($line = db_fetch_assoc($result)) {
3198         
3199                                 $class = ($lnum % 2) ? "even" : "odd";
3200         
3201                                 $label_id = $line["id"];
3202                                 $edit_label_id = $_GET["id"];
3203         
3204                                 if ($subop == "edit" && $label_id != $edit_label_id) {
3205                                         $class .= "Grayed";
3206                                         $this_row_id = "";
3207                                 } else {
3208                                         $this_row_id = "id=\"LILRR-$label_id\"";
3209                                 }
3210         
3211                                 print "<tr class=\"$class\" $this_row_id>";
3212         
3213                                 $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
3214                                 $line["description"] = htmlspecialchars($line["description"]);
3215         
3216                                 if (!$edit_label_id || $subop != "edit") {
3217         
3218                                         if (!$line["description"]) $line["description"] = "[No caption]";
3219         
3220                                         print "<td align='center'><input onclick='toggleSelectRow(this);' 
3221                                         type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
3222         
3223                                         print "<td><a href=\"javascript:editLabel($label_id);\">" . 
3224                                                 $line["sql_exp"] . "</td>";             
3225                                                 
3226                                         print "<td><a href=\"javascript:editLabel($label_id);\">" . 
3227                                                 $line["description"] . "</td>";                 
3228         
3229                                 } else if ($label_id != $edit_label_id) {
3230         
3231                                         if (!$line["description"]) $line["description"] = "[No description]";
3232         
3233                                         print "<td><input disabled=\"true\" type=\"checkbox\" 
3234                                                 id=\"LICHK-".$line["id"]."\"></td>";
3235         
3236                                         print "<td>".$line["sql_exp"]."</td>";          
3237                                         print "<td>".$line["description"]."</td>";              
3238         
3239                                 } else {
3240         
3241                                         print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
3242         
3243                                         print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
3244                                                 "\"></td>";
3245         
3246                                         print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
3247                                                 "\"></td>";
3248                                                         
3249                                 }
3250                                         
3251                                 
3252                                 print "</tr>";
3253         
3254                                 ++$lnum;
3255                         }
3256         
3257                         if ($lnum == 0) {
3258                                 print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
3259                         }
3260         
3261                         print "</table>";
3262         
3263                         print "<p>";
3264         
3265                         if ($subop == "edit") {
3266                                 print "Edit label:
3267                                         <input type=\"submit\" class=\"button\" 
3268                                                 onclick=\"javascript:labelTest()\" value=\"Test\">
3269                                         <input type=\"submit\" class=\"button\" 
3270                                                 onclick=\"javascript:labelEditSave()\" value=\"Save\">
3271                                         <input type=\"submit\" class=\"button\" 
3272                                                 onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">";
3273                                                 
3274                         } else {
3275         
3276                                 print "
3277                                         Selection:
3278                                 <input type=\"submit\" class=\"button\" 
3279                                         onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
3280                                 <input type=\"submit\" class=\"button\" 
3281                                         onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
3282                         }
3283                 } else {
3284                         print "<p>No labels defined.</p>";
3285                 }
3286         }
3287
3288         if ($op == "error") {
3289                 print "<div width=\"100%\" align='center'>";
3290                 $msg = $_GET["msg"];
3291                 print $msg;
3292                 print "</div>";
3293         }
3294
3295         if ($op == "help") {
3296                 if (!$_GET["noheaders"]) {
3297                         print "<html><head>
3298                                 <title>Tiny Tiny RSS : Help</title>
3299                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
3300                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
3301                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
3302                                 </head><body>";
3303                 }
3304
3305                 $tid = sprintf("%d", $_GET["tid"]);
3306
3307                 print "<div class='infoBoxContents'>";
3308
3309                 if (file_exists("help/$tid.php")) {
3310                         include("help/$tid.php");
3311                 } else {
3312                         print "<p>Help topic not found.</p>";
3313                 }
3314
3315                 print "</div>";
3316
3317                 print "<div align='center'>
3318                         <input type='submit' class='button'                     
3319                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
3320
3321                 if (!$_GET["noheaders"]) { 
3322                         print "</body></html>";
3323                 }
3324
3325         }
3326
3327         if ($op == "dlg") {
3328                 $id = $_GET["id"];
3329                 $param = $_GET["param"];
3330
3331                 if ($id == "quickAddFeed") {
3332                         print "
3333                         Feed URL: <input 
3334                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
3335                         id=\"qafInput\">";
3336                 
3337                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
3338                                 $result = db_query($link, "SELECT title,id FROM ttrss_feed_categories
3339                                         WHERE owner_uid = ".$_SESSION["uid"]."
3340                                         ORDER BY title");
3341
3342                                 print " <select id=\"qafCat\">";
3343                                 print "<option id=\"0\">Uncategorized</option>";
3344
3345                                 if (db_num_rows($result) != 0) {
3346         
3347                                         print "<option disabled>--------</option>";
3348
3349                                         while ($line = db_fetch_assoc($result)) {
3350                                                 printf("<option id='%d'>%s</option>", 
3351                                                         $line["id"], $line["title"]);
3352                                         }               
3353                                 }
3354
3355                                 print "</select>";
3356                         }
3357                         
3358                         print "&nbsp;<input class=\"button\"
3359                                 type=\"submit\" onclick=\"javascript:qafAdd()\" value=\"Add feed\">
3360                         <input class=\"button\"
3361                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
3362                                 value=\"Cancel\">";
3363                 }
3364
3365                 if ($id == "quickDelFeed") {
3366
3367                         $param = db_escape_string($param);
3368
3369                         $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$param'");
3370
3371                         if ($result) {
3372
3373                                 $f_title = db_fetch_result($result, 0, "title");
3374                 
3375                                 print "Remove current feed (<b>$f_title</b>)?&nbsp;
3376                                 <input class=\"button\"
3377                                         type=\"submit\" onclick=\"javascript:qfdDelete($param)\" value=\"Remove\">
3378                                 <input class=\"button\"
3379                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
3380                                         value=\"Cancel\">";
3381                         } else {
3382                                 print "Error: Feed $param not found.&nbsp;
3383                                 <input class=\"button\"
3384                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
3385                                         value=\"Cancel\">";             
3386                         }
3387                 }
3388
3389                 if ($id == "search") {
3390
3391                         $active_feed_id = db_escape_string($_GET["param"]);
3392
3393                         print "<input id=\"searchbox\" class=\"extSearch\"                      
3394                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
3395                         onchange=\"javascript:search()\">
3396                         <select id=\"searchmodebox\">
3397                                 <option selected>All feeds</option>";
3398                                 
3399                         if ($active_feed_id) {                          
3400                                 print "<option>This feed</option>";
3401                         } else {
3402                                 print "<option disabled>This feed</option>";
3403                         }
3404
3405                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
3406                                 print "<option>This category</option>";
3407                         }
3408
3409                         print "</select>                
3410                         <input type=\"submit\" 
3411                                 class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
3412                         <input class=\"button\"
3413                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
3414                                 value=\"Close\">";
3415
3416                 }
3417
3418                 if ($id == "quickAddFilter") {
3419
3420                         $result = db_query($link, "SELECT description 
3421                                 FROM ttrss_filter_types ORDER BY description");
3422         
3423                         $filter_types = array();
3424         
3425                         while ($line = db_fetch_assoc($result)) {
3426                                 array_push($filter_types, $line["description"]);
3427                         }
3428
3429                         print "<table>";
3430
3431                         print "<tr><td>Match:</td><td><input id=\"fadd_regexp\" size=\"40\">&nbsp;";
3432                         
3433                         print_select("fadd_match", "Title", $filter_types);     
3434         
3435                         print "</td></tr>";
3436                         print "<tr><td>Feed:</td><td><select id=\"fadd_feed\">";
3437         
3438                         print "<option selected id=\"0\">All feeds</option>";
3439         
3440                         $result = db_query($link, "SELECT id,title FROM ttrss_feeds
3441                                 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
3442         
3443                         if (db_num_rows($result) > 0) {
3444                                 print "<option disabled>--------</option>";
3445                         }
3446         
3447                         while ($line = db_fetch_assoc($result)) {
3448                                 if ($param == $line["id"]) {
3449                                         $selected = "selected";
3450                                 } else {
3451                                         $selected = "";
3452                                 }
3453                                 printf("<option id='%d' %s>%s</option>", $line["id"], $selected, $line["title"]);
3454                         }
3455         
3456                         print "</select></td></tr>";
3457         
3458                         print "<tr><td>Action:</td>";
3459         
3460                         print "<td><select id=\"fadd_action\">";
3461         
3462                         $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions 
3463                                 ORDER BY name");
3464
3465                         while ($line = db_fetch_assoc($result)) {
3466                                 printf("<option id='%d'>%s</option>", $line["id"], $line["description"]);
3467                         }
3468         
3469                         print "</select>";
3470         
3471                         print "</td></tr><tr><td colspan=\"2\" align=\"right\">";
3472         
3473                         print "<input type=\"submit\" 
3474                                 class=\"button\" onclick=\"javascript:qaddFilter()\" 
3475                                 value=\"Add filter\"> ";
3476
3477                         print "<input class=\"button\"
3478                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
3479                                 value=\"Close\">";
3480
3481                         print "</td></tr></table>";
3482                 }
3483         }
3484
3485         // update feeds of all users, may be used anonymously
3486         if ($op == "globalUpdateFeeds") {
3487
3488                 $result = db_query($link, "SELECT id FROM ttrss_users");
3489
3490                 while ($line = db_fetch_assoc($result)) {
3491                         $user_id = $line["id"];
3492 //                      print "<!-- updating feeds of uid $user_id -->";
3493                         update_all_feeds($link, false, $user_id);
3494                 }
3495
3496                 print "<rpc-reply>
3497                         <message msg=\"All feeds updated\"/>
3498                 </rpc-reply>";
3499
3500         }
3501
3502         if ($op == "pref-prefs") {
3503
3504                 $subop = $_REQUEST["subop"];
3505
3506                 if ($subop == "Save configuration") {
3507
3508                         if (WEB_DEMO_MODE) {
3509                                 header("Location: prefs.php");
3510                                 return;
3511                         }
3512
3513                         $_SESSION["prefs_op_result"] = "save-config";
3514
3515                         $_SESSION["prefs_cache"] = false;
3516
3517                         foreach (array_keys($_POST) as $pref_name) {
3518                         
3519                                 $pref_name = db_escape_string($pref_name);
3520                                 $value = db_escape_string($_POST[$pref_name]);
3521
3522                                 $result = db_query($link, "SELECT type_name 
3523                                         FROM ttrss_prefs,ttrss_prefs_types 
3524                                         WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
3525
3526                                 if (db_num_rows($result) > 0) {
3527
3528                                         $type_name = db_fetch_result($result, 0, "type_name");
3529
3530 //                                      print "$pref_name : $type_name : $value<br>";
3531
3532                                         if ($type_name == "bool") {
3533                                                 if ($value == "1") {
3534                                                         $value = "true";
3535                                                 } else {
3536                                                         $value = "false";
3537                                                 }
3538                                         } else if ($type_name == "integer") {
3539                                                 $value = sprintf("%d", $value);
3540                                         }
3541
3542 //                                      print "$pref_name : $type_name : $value<br>";
3543
3544                                         db_query($link, "UPDATE ttrss_user_prefs SET value = '$value' 
3545                                                 WHERE pref_name = '$pref_name' AND owner_uid = ".$_SESSION["uid"]);
3546
3547                                 }
3548
3549                                 header("Location: prefs.php");
3550
3551                         }
3552
3553                 } else if ($subop == "getHelp") {
3554
3555                         $pref_name = db_escape_string($_GET["pn"]);
3556
3557                         $result = db_query($link, "SELECT help_text FROM ttrss_prefs
3558                                 WHERE pref_name = '$pref_name'");
3559
3560                         if (db_num_rows($result) > 0) {
3561                                 $help_text = db_fetch_result($result, 0, "help_text");
3562                                 print $help_text;
3563                         } else {
3564                                 print "Unknown option: $pref_name";
3565                         }
3566
3567                 } else if ($subop == "Change e-mail") {
3568
3569                         if (WEB_DEMO_MODE) {
3570                                 header("Location: prefs.php");
3571                                 return;
3572                         }
3573
3574                         $email = db_escape_string($_GET["email"]);
3575                         $active_uid = $_SESSION["uid"];
3576
3577                         if ($email) {
3578                                 db_query($link, "UPDATE ttrss_users SET email = '$email' 
3579                                                 WHERE id = '$active_uid'");                             
3580                         }
3581
3582                         header("Location: prefs.php");
3583
3584                 } else if ($subop == "Change password") {
3585
3586                         if (WEB_DEMO_MODE) {
3587                                 header("Location: prefs.php");
3588                                 return;
3589                         }
3590
3591                         $old_pw = $_POST["OLD_PASSWORD"];
3592                         $new_pw = $_POST["OLD_PASSWORD"];
3593
3594                         $old_pw_hash = 'SHA1:' . sha1($_POST["OLD_PASSWORD"]);
3595                         $new_pw_hash = 'SHA1:' . sha1($_POST["NEW_PASSWORD"]);
3596
3597                         $active_uid = $_SESSION["uid"];
3598
3599                         if ($old_pw && $new_pw) {
3600
3601                                 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
3602
3603                                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
3604                                         id = '$active_uid' AND (pwd_hash = '$old_pw' OR 
3605                                                 pwd_hash = '$old_pw_hash')");
3606
3607                                 if (db_num_rows($result) == 1) {
3608                                         db_query($link, "UPDATE ttrss_users SET pwd_hash = '$new_pw_hash' 
3609                                                 WHERE id = '$active_uid'");                             
3610
3611                                         $_SESSION["pwd_change_result"] = "ok";
3612                                 } else {
3613                                         $_SESSION["pwd_change_result"] = "failed";                                      
3614                                 }
3615                         }
3616
3617                         header("Location: prefs.php");
3618
3619                 } else if ($subop == "Reset to defaults") {
3620
3621                         if (WEB_DEMO_MODE) {
3622                                 header("Location: prefs.php");
3623                                 return;
3624                         }
3625
3626                         $_SESSION["prefs_op_result"] = "reset-to-defaults";
3627
3628                         if (DB_TYPE == "pgsql") {
3629                                 db_query($link,"UPDATE ttrss_user_prefs 
3630                                         SET value = ttrss_prefs.def_value 
3631                                         WHERE owner_uid = '".$_SESSION["uid"]."' AND
3632                                         ttrss_prefs.pref_name = ttrss_user_prefs.pref_name");
3633                         } else {
3634                                 db_query($link, "DELETE FROM ttrss_user_prefs 
3635                                         WHERE owner_uid = ".$_SESSION["uid"]);
3636                                 initialize_user_prefs($link, $_SESSION["uid"]);
3637                         }
3638
3639                         header("Location: prefs.php");
3640
3641                 } else if ($subop == "Change theme") {
3642
3643                         $theme = db_escape_string($_POST["theme"]);
3644
3645                         if ($theme == "Default") {
3646                                 $theme_qpart = 'NULL';
3647                         } else {
3648                                 $theme_qpart = "'$theme'";
3649                         }
3650
3651                         $result = db_query($link, "SELECT id,theme_path FROM ttrss_themes
3652                                 WHERE theme_name = '$theme'");
3653
3654                         if (db_num_rows($result) == 1) {
3655                                 $theme_id = db_fetch_result($result, 0, "id");
3656                                 $theme_path = db_fetch_result($result, 0, "theme_path");
3657                         } else {
3658                                 $theme_id = "NULL";
3659                                 $theme_path = "";
3660                         }
3661
3662                         db_query($link, "UPDATE ttrss_users SET
3663                                 theme_id = $theme_id WHERE id = " . $_SESSION["uid"]);
3664
3665                         $_SESSION["theme"] = $theme_path;
3666
3667                         header("Location: prefs.php");
3668
3669                 } else {
3670
3671                         if (!SINGLE_USER_MODE) {
3672
3673                                 $result = db_query($link, "SELECT id,email FROM ttrss_users
3674                                         WHERE id = ".$_SESSION["uid"]." AND (pwd_hash = 'password' OR
3675                                                 pwd_hash = 'SHA1:".sha1("password")."')");
3676
3677                                 if (db_num_rows($result) != 0) {
3678                                         print "<div class=\"warning\"> 
3679                                                 Your password is at default value, please change it.
3680                                         </div>";
3681                                 }
3682
3683                                 if ($_SESSION["pwd_change_result"] == "failed") {
3684                                         print "<div class=\"warning\"> 
3685                                                         There was an error while changing your password.
3686                                                 </div>";
3687                                 }
3688
3689                                 if ($_SESSION["pwd_change_result"] == "ok") {
3690                                         print "<div class=\"notice\"> 
3691                                                         Password changed successfully.
3692                                                 </div>";
3693                                 }
3694
3695                                 $_SESSION["pwd_change_result"] = "";
3696
3697                                 if ($_SESSION["prefs_op_result"] == "reset-to-defaults") {
3698                                         print "<div class=\"notice\"> 
3699                                                         Your configuration was reset to defaults.
3700                                                 </div>";
3701                                 }
3702
3703                                 if ($_SESSION["prefs_op_result"] == "save-config") {
3704                                         print "<div class=\"notice\"> 
3705                                                         Your configuration was saved successfully.
3706                                                 </div>";
3707                                 }
3708
3709                                 $_SESSION["prefs_op_result"] = "";
3710
3711                                 print "<form action=\"backend.php\" method=\"GET\">";
3712         
3713                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
3714                                 print "<tr><td colspan='3'><h3>Personal data</h3></tr></td>";
3715
3716                                 $result = db_query($link, "SELECT email FROM ttrss_users
3717                                         WHERE id = ".$_SESSION["uid"]);
3718                                         
3719                                 $email = db_fetch_result($result, 0, "email");
3720         
3721                                 print "<tr><td width=\"40%\">E-mail</td>";
3722                                 print "<td><input class=\"editbox\" name=\"email\" 
3723                                         value=\"$email\"></td></tr>";
3724         
3725                                 print "</table>";
3726         
3727                                 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
3728         
3729                                 print "<p><input class=\"button\" type=\"submit\" 
3730                                         value=\"Change e-mail\" name=\"subop\">";
3731
3732                                 print "</form>";
3733
3734                                 print "<form action=\"backend.php\" method=\"POST\">";
3735         
3736                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
3737                                 print "<tr><td colspan='3'><h3>Authentication</h3></tr></td>";
3738         
3739                                 print "<tr><td width=\"40%\">Old password</td>";
3740                                 print "<td><input class=\"editbox\" type=\"password\"
3741                                         name=\"OLD_PASSWORD\"></td></tr>";
3742         
3743                                 print "<tr><td width=\"40%\">New password</td>";
3744                                 
3745                                 print "<td><input class=\"editbox\" type=\"password\"
3746                                         name=\"NEW_PASSWORD\"></td></tr>";
3747         
3748                                 print "</table>";
3749         
3750                                 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
3751         
3752                                 print "<p><input class=\"button\" type=\"submit\" 
3753                                         value=\"Change password\" name=\"subop\">";
3754         
3755                                 print "</form>";
3756
3757                         }
3758
3759                         $result = db_query($link, "SELECT
3760                                 theme_id FROM ttrss_users WHERE id = " . $_SESSION["uid"]);
3761
3762                         $user_theme_id = db_fetch_result($result, 0, "theme_id");
3763
3764                         $result = db_query($link, "SELECT
3765                                 id,theme_name FROM ttrss_themes ORDER BY theme_name");
3766
3767                         if (db_num_rows($result) > 0) {
3768
3769                                 print "<form action=\"backend.php\" method=\"POST\">";
3770                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
3771                                 print "<tr><td colspan='3'><h3>Themes</h3></tr></td>";
3772                                 print "<tr><td width=\"40%\">Select theme</td>";
3773                                 print "<td><select name=\"theme\">";
3774                                 print "<option>Default</option>";
3775                                 print "<option disabled>--------</option>";                             
3776                                 
3777                                 while ($line = db_fetch_assoc($result)) {       
3778                                         if ($line["id"] == $user_theme_id) {
3779                                                 $selected = "selected";
3780                                         } else {
3781                                                 $selected = "";
3782                                         }
3783                                         print "<option $selected>" . $line["theme_name"] . "</option>";
3784                                 }
3785                                 print "</select></td></tr>";
3786                                 print "</table>";
3787                                 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
3788                                 print "<p><input class=\"button\" type=\"submit\" 
3789                                         value=\"Change theme\" name=\"subop\">";
3790                                 print "</form>";
3791                         }
3792
3793                         $result = db_query($link, "SELECT 
3794                                 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
3795                                 section_name,def_value
3796                                 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
3797                                 WHERE type_id = ttrss_prefs_types.id AND 
3798                                         section_id = ttrss_prefs_sections.id AND
3799                                         ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
3800                                         owner_uid = ".$_SESSION["uid"]."
3801                                 ORDER BY section_id,short_desc");
3802
3803                         print "<form action=\"backend.php\" method=\"POST\">";
3804
3805                         $lnum = 0;
3806
3807                         $active_section = "";
3808         
3809                         while ($line = db_fetch_assoc($result)) {
3810
3811                                 if ($active_section != $line["section_name"]) {
3812
3813                                         if ($active_section != "") {
3814                                                 print "</table>";
3815                                         }
3816
3817                                         print "<p><table width=\"100%\" class=\"prefPrefsList\">";
3818                                 
3819                                         $active_section = $line["section_name"];                                
3820                                         
3821                                         print "<tr><td colspan=\"3\"><h3>$active_section</h3></td></tr>";
3822 //                                      print "<tr class=\"title\">
3823 //                                              <td width=\"25%\">Option</td><td>Value</td></tr>";
3824
3825                                         $lnum = 0;
3826                                 }
3827
3828 //                              $class = ($lnum % 2) ? "even" : "odd";
3829
3830                                 print "<tr>";
3831
3832                                 $type_name = $line["type_name"];
3833                                 $pref_name = $line["pref_name"];
3834                                 $value = $line["value"];
3835                                 $def_value = $line["def_value"];
3836                                 $help_text = $line["help_text"];
3837
3838                                 print "<td width=\"40%\" id=\"$pref_name\">" . $line["short_desc"];
3839
3840                                 if ($help_text) print "<div class=\"prefHelp\">$help_text</div>";
3841                                 
3842                                 print "</td>";
3843
3844                                 print "<td>";
3845
3846                                 if ($type_name == "bool") {
3847 //                                      print_select($pref_name, $value, array("true", "false"));
3848
3849                                         if ($value == "true") {
3850                                                 $value = "Yes";
3851                                         } else {
3852                                                 $value = "No";
3853                                         }
3854
3855                                         print_radio($pref_name, $value, array("Yes", "No"));
3856                         
3857                                 } else {
3858                                         print "<input class=\"editbox\" name=\"$pref_name\" value=\"$value\">";
3859                                 }
3860
3861                                 print "</td>";
3862
3863                                 print "</tr>";
3864
3865                                 $lnum++;
3866                         }
3867
3868                         print "</table>";
3869
3870                         print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
3871
3872                         print "<p><input class=\"button\" type=\"submit\" 
3873                                 name=\"subop\" value=\"Save configuration\">";
3874                                 
3875                         print "&nbsp;<input class=\"button\" type=\"submit\" 
3876                                 name=\"subop\" onclick=\"return validatePrefsReset()\" 
3877                                 value=\"Reset to defaults\"></p>";
3878
3879                         print "</form>";
3880
3881                 }
3882
3883         }
3884
3885         if ($op == "pref-users") {
3886
3887                 $subop = $_GET["subop"];
3888
3889                 if ($subop == "editSave") {
3890         
3891                         if (!WEB_DEMO_MODE) {
3892
3893                                 $login = db_escape_string($_GET["l"]);
3894                                 $uid = db_escape_string($_GET["id"]);
3895                                 $access_level = sprintf("%d", $_GET["al"]);
3896                                 $email = db_escape_string($_GET["e"]);
3897
3898                                 db_query($link, "UPDATE ttrss_users SET login = '$login', 
3899                                         access_level = '$access_level', email = '$email' WHERE id = '$uid'");
3900
3901                         }
3902                 } else if ($subop == "remove") {
3903
3904                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
3905
3906                                 $ids = split(",", db_escape_string($_GET["ids"]));
3907
3908                                 foreach ($ids as $id) {
3909                                         db_query($link, "DELETE FROM ttrss_users WHERE id = '$id' AND id != " . $_SESSION["uid"]);
3910                                         
3911                                 }
3912                         }
3913                 } else if ($subop == "add") {
3914                 
3915                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
3916
3917                                 $login = db_escape_string(trim($_GET["login"]));
3918                                 $tmp_user_pwd = make_password(8);
3919                                 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
3920
3921                                 db_query($link, "INSERT INTO ttrss_users 
3922                                         (login,pwd_hash,access_level,last_login)
3923                                         VALUES ('$login', '$pwd_hash', 0, NOW())");
3924
3925
3926                                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
3927                                         login = '$login' AND pwd_hash = '$pwd_hash'");
3928
3929                                 if (db_num_rows($result) == 1) {
3930
3931                                         $new_uid = db_fetch_result($result, 0, "id");
3932
3933                                         print "<div class=\"notice\">Added user <b>".$_GET["login"].
3934                                                 "</b> with password <b>$tmp_user_pwd</b>.</div>";
3935
3936                                         initialize_user($link, $new_uid);
3937
3938                                 } else {
3939                                 
3940                                         print "<div class=\"warning\">Error while adding user <b>".
3941                                         $_GET["login"].".</b></div>";
3942
3943                                 }
3944                         } 
3945                 } else if ($subop == "resetPass") {
3946
3947                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
3948
3949                                 $uid = db_escape_string($_GET["id"]);
3950
3951                                 $result = db_query($link, "SELECT login,email 
3952                                         FROM ttrss_users WHERE id = '$uid'");
3953
3954                                 $login = db_fetch_result($result, 0, "login");
3955                                 $email = db_fetch_result($result, 0, "email");
3956                                 $tmp_user_pwd = make_password(8);
3957                                 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
3958
3959                                 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$pwd_hash'
3960                                         WHERE id = '$uid'");
3961
3962                                 print "<div class=\"notice\">Changed password of 
3963                                         user <b>$login</b> to <b>$tmp_user_pwd</b>.";
3964
3965                                 if (MAIL_RESET_PASS && $email) {
3966                                         print " Notifying <b>$email</b>.";
3967
3968                                         mail("$login <$email>", "Password reset notification",
3969                                                 "Hi, $login.\n".
3970                                                 "\n".
3971                                                 "Your password for this TT-RSS installation was reset by".
3972                                                         " an administrator.\n".
3973                                                 "\n".
3974                                                 "Your new password is $tmp_user_pwd, please remember".
3975                                                         " it for later reference.\n".
3976                                                 "\n".
3977                                                 "Sincerely, TT-RSS Mail Daemon.", "From: " . MAIL_FROM);
3978                                 }
3979                                         
3980                                 print "</div>";                         
3981
3982                         }
3983                 }
3984
3985                 print "<div class=\"prefGenericAddBox\">
3986                         <input id=\"uadd_box\" onchange=\"javascript:addUser()\" size=\"40\">&nbsp;";
3987                         
3988                 print"<input type=\"submit\" class=\"button\" 
3989                         onclick=\"javascript:addUser()\" value=\"Add user\"></div>";
3990
3991                 $result = db_query($link, "SELECT 
3992                                 id,login,access_level,email,
3993                                 SUBSTRING(last_login,1,16) as last_login
3994                         FROM 
3995                                 ttrss_users
3996                         ORDER by login");
3997
3998                 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
3999
4000                 print "<p><table width=\"100%\" cellspacing=\"0\" 
4001                         class=\"prefUserList\" id=\"prefUserList\">";
4002
4003                 print "<tr><td class=\"selectPrompt\" colspan=\"8\">
4004                                 Select: 
4005                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefUserList', 
4006                                                 'UMRR-', 'UMCHK-', true)\">All</a>,
4007                                         <a href=\"javascript:selectTableRowsByIdPrefix('prefUserList', 
4008                                                 'UMRR-', 'UMCHK-', false)\">None</a>
4009                                 </td</tr>";
4010
4011                 print "<tr class=\"title\">
4012                                         <td align='center' width=\"5%\">&nbsp;</td>
4013                                         <td width='20%'>Username</td>
4014                                         <td width='20%'>E-mail</td>
4015                                         <td width='20%'>Access Level</td>
4016                                         <td width='20%'>Last login</td></tr>";
4017                 
4018                 $lnum = 0;
4019                 
4020                 while ($line = db_fetch_assoc($result)) {
4021
4022                         $class = ($lnum % 2) ? "even" : "odd";
4023
4024                         $uid = $line["id"];
4025                         $edit_uid = $_GET["id"];
4026
4027                         if ($subop == "edit" && $uid != $edit_uid) {
4028                                 $class .= "Grayed";
4029                                 $this_row_id = "";
4030                         } else {
4031                                 $this_row_id = "id=\"UMRR-$uid\"";
4032                         }               
4033                         
4034                         print "<tr class=\"$class\" $this_row_id>";
4035
4036                         $line["login"] = htmlspecialchars($line["login"]);
4037
4038                         $line["last_login"] = date(get_pref($link, 'SHORT_DATE_FORMAT'),
4039                                 strtotime($line["last_login"]));
4040
4041                         $access_level_names = array(0 => "User", 10 => "Administrator");
4042
4043 /*                      if ($uid == $_SESSION["uid"]) {
4044
4045                                 print "<td align='center'><input disabled=\"true\" type=\"checkbox\" 
4046                                         id=\"UMCHK-".$line["id"]."\"></td>";
4047
4048                                 print "<td>".$line["login"]."</td>";
4049                                 print "<td>".$line["email"]."</td>";
4050                                 print "<td>".$line["access_level"]."</td>";
4051
4052                         } else */ if (!$edit_uid || $subop != "edit") {
4053
4054                                 print "<td align='center'><input onclick='toggleSelectRow(this);' 
4055                                 type=\"checkbox\" id=\"UMCHK-$uid\"></td>";
4056
4057                                 print "<td><a href=\"javascript:editUser($uid);\">" . 
4058                                         $line["login"] . "</td>";               
4059
4060                                 print "<td><a href=\"javascript:editUser($uid);\">" . 
4061                                         $line["email"] . "</td>";                       
4062
4063                                 print "<td><a href=\"javascript:editUser($uid);\">" . 
4064                                         $access_level_names[$line["access_level"]] . "</td>";                   
4065
4066                         } else if ($uid != $edit_uid) {
4067
4068                                 print "<td align='center'><input disabled=\"true\" type=\"checkbox\" 
4069                                         id=\"UMCHK-".$line["id"]."\"></td>";
4070
4071                                 print "<td>".$line["login"]."</td>";            
4072                                 print "<td>".$line["email"]."</td>";            
4073                                 print "<td>".$access_level_names[$line["access_level"]]."</td>";
4074
4075                         } else {
4076
4077                                 print "<td align='center'>
4078                                         <input disabled=\"true\" type=\"checkbox\" checked></td>";
4079
4080                                 print "<td><input id=\"iedit_ulogin\" value=\"".$line["login"].
4081                                         "\"></td>";
4082
4083                                 print "<td><input id=\"iedit_email\" value=\"".$line["email"].
4084                                         "\"></td>";
4085
4086 //                              print "<td><input id=\"iedit_ulevel\" value=\"".$line["access_level"].
4087 //                                      "\"></td>";
4088
4089                                 print "<td>";
4090                                 print "<select id=\"iedit_ulevel\">";
4091                                 foreach (array_keys($access_level_names) as $al) {
4092                                         if ($al == $line["access_level"]) {
4093                                                 $selected = "selected";
4094                                         } else {
4095                                                 $selected = "";
4096                                         }                                       
4097                                         print "<option $selected id=\"$al\">" . 
4098                                                 $access_level_names[$al] . "</option>";
4099                                 }
4100                                 print "</select>";
4101                                 print "</td>";
4102
4103                         }
4104                                 
4105                         print "<td>".$line["last_login"]."</td>";               
4106                 
4107                         print "</tr>";
4108
4109                         ++$lnum;
4110                 }
4111
4112                 print "</table>";
4113
4114                 print "<p>";
4115
4116                 if ($subop == "edit") {
4117                         print "Edit user:
4118                                 <input type=\"submit\" class=\"button\" 
4119                                         onclick=\"javascript:userEditSave()\" value=\"Save\">
4120                                 <input type=\"submit\" class=\"button\" 
4121                                         onclick=\"javascript:userEditCancel()\" value=\"Cancel\">";
4122                                         
4123                 } else {
4124
4125                         print "
4126                                 Selection:
4127                         <input type=\"submit\" class=\"button\" 
4128                                 onclick=\"javascript:selectedUserDetails()\" value=\"User details\">
4129                         <input type=\"submit\" class=\"button\" 
4130                                 onclick=\"javascript:editSelectedUser()\" value=\"Edit\">
4131                         <input type=\"submit\" class=\"button\" 
4132                                 onclick=\"javascript:removeSelectedUsers()\" value=\"Remove\">
4133                         <input type=\"submit\" class=\"button\" 
4134                                 onclick=\"javascript:resetSelectedUserPass()\" value=\"Reset password\">";
4135
4136                 }
4137         }
4138
4139         if ($op == "user-details") {
4140
4141                 if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
4142                         return;
4143                 }
4144                           
4145 /*              print "<html><head>
4146                         <title>Tiny Tiny RSS : User Details</title>
4147                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
4148                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
4149                         </head><body>"; */
4150
4151                 $uid = sprintf("%d", $_GET["id"]);
4152
4153                 print "<div class='infoBoxContents'>";
4154
4155                 $result = db_query($link, "SELECT login,
4156                         SUBSTRING(last_login,1,16) AS last_login,
4157                         access_level,
4158                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
4159                                 WHERE owner_uid = id) AS stored_articles
4160                         FROM ttrss_users 
4161                         WHERE id = '$uid'");
4162                         
4163                 if (db_num_rows($result) == 0) {
4164                         print "<h1>User not found</h1>";
4165                         return;
4166                 }
4167                 
4168                 print "<h1>User Details</h1>";
4169
4170                 print "<table width='100%'>";
4171
4172                 $login = db_fetch_result($result, 0, "login");
4173                 $last_login = date(get_pref($link, 'LONG_DATE_FORMAT'),
4174                         strtotime(db_fetch_result($result, 0, "last_login")));
4175                 $access_level = db_fetch_result($result, 0, "access_level");
4176                 $stored_articles = db_fetch_result($result, 0, "stored_articles");
4177
4178                 print "<tr><td>Username</td><td>$login</td></tr>";
4179                 print "<tr><td>Access level</td><td>$access_level</td></tr>";
4180                 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
4181                 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
4182
4183                 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
4184                         WHERE owner_uid = '$uid'");
4185
4186                 $num_feeds = db_fetch_result($result, 0, "num_feeds");
4187
4188                 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
4189
4190 /*              $result = db_query($link, "SELECT 
4191                         SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size 
4192                         FROM ttrss_user_entries,ttrss_entries 
4193                                 WHERE owner_uid = '$uid' AND ref_id = id");
4194
4195                 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
4196
4197                 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>";  */
4198
4199                 print "</table>";
4200
4201                 print "<h1>Subscribed feeds</h1>";
4202
4203                 $result = db_query($link, "SELECT id,title,site_url FROM ttrss_feeds
4204                         WHERE owner_uid = '$uid' ORDER BY title");
4205
4206                 print "<ul class=\"userFeedList\">";
4207
4208                 while ($line = db_fetch_assoc($result)) {
4209
4210                         $icon_file = ICONS_URL."/".$line["id"].".ico";
4211
4212                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
4213                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
4214                         } else {
4215                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
4216                         }
4217
4218                         print "<li>$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
4219                 }
4220
4221                 if (db_num_rows($result) < $num_feeds) {
4222                          // FIXME - add link to show ALL subscribed feeds here somewhere
4223                         print "<li><img 
4224                                 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
4225                 }
4226                 
4227                 print "</ul>";
4228
4229                 print "</div>";
4230
4231                 print "<div align='center'>
4232                         <input type='submit' class='button'                     
4233                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
4234
4235 //              print "</body></html>"; 
4236
4237         }
4238
4239         if ($op == "feed-details") {
4240
4241 //              $feed_id = $_GET["id"];
4242
4243                 $feed_ids = split(",", db_escape_string($_GET["id"]));
4244
4245                 print "<div class=\"infoBoxContents\">";
4246
4247                 foreach ($feed_ids as $feed_id) {
4248
4249                         $result = db_query($link, 
4250                                 "SELECT 
4251                                         title,feed_url,
4252                                         SUBSTRING(last_updated,1,16) as last_updated,
4253                                         icon_url,site_url,
4254                                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
4255                                                 WHERE feed_id = id) AS total,
4256                                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
4257                                                 WHERE feed_id = id AND unread = true) AS unread,
4258                                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
4259                                                 WHERE feed_id = id AND marked = true) AS marked
4260                                 FROM ttrss_feeds
4261                                 WHERE id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
4262         
4263                         if (db_num_rows($result) == 0) return;
4264         
4265                         $title = db_unescape_string(db_fetch_result($result, 0, "title"));
4266                         $last_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4267                                 strtotime(db_fetch_result($result, 0, "last_updated")));
4268                         $feed_url = db_fetch_result($result, 0, "feed_url");
4269                         $icon_url = db_fetch_result($result, 0, "icon_url");
4270                         $total = db_fetch_result($result, 0, "total");
4271                         $unread = db_fetch_result($result, 0, "unread");
4272                         $marked = db_fetch_result($result, 0, "marked");
4273                         $site_url = db_fetch_result($result, 0, "site_url");
4274         
4275                         $result = db_query($link, "SELECT COUNT(id) AS subscribed
4276                                                 FROM ttrss_feeds WHERE feed_url = '$feed_url' AND private = false");
4277         
4278                         $subscribed = db_fetch_result($result, 0, "subscribed");
4279         
4280                         $icon_file = ICONS_DIR . "/$feed_id.ico";
4281         
4282                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
4283                                         $feed_icon = "<img width=\"16\" height=\"16\"
4284                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
4285                         } else {
4286                                 $feed_icon = "";
4287                         }
4288         
4289                         print "<h1>$feed_icon $title</h1>";
4290         
4291                         print "<table width='100%'>";
4292         
4293                         if ($site_url) {
4294                                 print "<tr><td width='30%'>Link</td>
4295                                         <td><a href=\"$site_url\">$site_url</a>
4296                                         <a href=\"$feed_url\">(feed)</a></td>
4297                                         </td></tr>";
4298                         } else {
4299                                 print "<tr><td width='30%'>Feed URL</td>
4300                                         <td><a href=\"$feed_url\">$feed_url</a></td></tr>";
4301                         }
4302                         print "<tr><td>Last updated</td><td>$last_updated</td></tr>";
4303                         print "<tr><td>Total articles</td><td>$total</td></tr>";
4304                         print "<tr><td>Unread articles</td><td>$unread</td></tr>";
4305                         print "<tr><td>Starred articles</td><td>$marked</td></tr>";
4306                         print "<tr><td>Subscribed users</td><td>$subscribed</td></tr>";
4307         
4308                         print "</table>";
4309         
4310 /*                      $result = db_query($link, "SELECT title,
4311                                 SUBSTRING(updated,1,16) AS updated,unread
4312                                 FROM ttrss_entries,ttrss_user_entries
4313                                 WHERE ref_id = id AND feed_id = '$feed_id' 
4314                                 ORDER BY date_entered DESC LIMIT 5");
4315         
4316                         if (db_num_rows($result) > 0) {
4317         
4318                                 print "<h1>Latest headlines</h1>";
4319         
4320                                 print "<ul class=\"nomarks\">";
4321                 
4322                                 while ($line = db_fetch_assoc($result)) {
4323                                         if ($line["unread"] == "t" || $line["unread"] == "1") {
4324                                                 $line["title"] = "<b>" . $line["title"] . "</b>";
4325                                         }                               
4326                                         print "<li>" . $line["title"].
4327                                         "&nbsp;<span class=\"insensitive\">(" .
4328                                                 date(get_pref($link, 'SHORT_DATE_FORMAT'), 
4329                                                         strtotime($line["updated"])).
4330                                         ")</span></li>";
4331                                 }
4332                 
4333                                 print "</ul>";
4334                 
4335                         } */
4336                 }
4337
4338                 print "</div>";
4339         
4340                 print "<div align='center'>
4341                         <input type='submit' class='button'                     
4342                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
4343         }       
4344
4345         if ($op == "pref-feed-browser") {
4346
4347                 if (!ENABLE_FEED_BROWSER) {
4348                         print "Feed browser is administratively disabled.";
4349                         return;
4350                 }
4351
4352                 $subop = $_REQUEST["subop"];
4353
4354                 if ($subop == "details") {
4355                         $id = db_escape_string($_GET["id"]);
4356
4357                         print "<div class=\"browserFeedInfo\">";
4358                         print "<b>Feed information:</b>";
4359                         print "<div class=\"detailsPart\">";
4360
4361                         $result = db_query($link, "SELECT 
4362                                         feed_url,site_url,
4363                                         SUBSTRING(last_updated,1,19) AS last_updated
4364                                 FROM ttrss_feeds WHERE id = '$id'");
4365
4366                         $feed_url = db_fetch_result($result, 0, "feed_url");
4367                         $site_url = db_fetch_result($result, 0, "site_url");
4368                         $last_updated = db_fetch_result($result, 0, "last_updated");
4369
4370                         if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4371                                 $last_updated = smart_date_time(strtotime($last_updated));
4372                         } else {
4373                                 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4374                                 $last_updated = date($short_date, strtotime($last_updated));
4375                         }
4376
4377                         print "Site: <a href='$site_url'>$site_url</a> ".
4378                                 "(<a href='$feed_url'>feed</a>), ".
4379                                 "Last updated: $last_updated";
4380
4381                         print "</div>";
4382
4383                         $result = db_query($link, "SELECT 
4384                                         ttrss_entries.title,
4385                                         content,
4386                                         substring(date_entered,1,19) as date_entered,
4387                                         substring(updated,1,19) as updated
4388                                 FROM ttrss_entries,ttrss_user_entries
4389                                 WHERE   ttrss_entries.id = ref_id AND feed_id = '$id' 
4390                                 ORDER BY updated DESC LIMIT 5");
4391
4392                         if (db_num_rows($result) > 0) {
4393                                 
4394                                 print "<b>Last headlines:</b><br>";
4395                                 
4396                                 print "<div class=\"detailsPart\">";
4397                                 print "<ul class=\"compact\">";
4398                                 while ($line = db_fetch_assoc($result)) {
4399
4400                                         if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4401                                                 $entry_dt = smart_date_time(strtotime($line["updated"]));
4402                                         } else {
4403                                                 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4404                                                 $entry_dt = date($short_date, strtotime($line["updated"]));
4405                                         }                               
4406                 
4407                                         print "<li>" . $line["title"] . 
4408                                                 "&nbsp;<span class=\"insensitive\">($entry_dt)</span></li>";    
4409                                 }               
4410                                 print "</ul></div>";
4411                         }
4412
4413                         print "</div>";
4414                                 
4415                         return;
4416                 }
4417
4418                 $result = db_query($link, "SELECT feed_url,count(id) AS subscribers 
4419                         FROM ttrss_feeds 
4420                         WHERE auth_login = '' AND auth_pass = '' AND private = false
4421                         GROUP BY feed_url ORDER BY subscribers DESC LIMIT 100");
4422                 
4423                 print "<ul class='nomarks' id='browseBigFeedList'>";
4424
4425                 $feedctr = 0;
4426                 
4427                 while ($line = db_fetch_assoc($result)) {
4428                         $feed_url = $line["feed_url"];
4429                         $subscribers = $line["subscribers"];
4430
4431                         $sub_result = db_query($link, "SELECT id
4432                                 FROM ttrss_feeds WHERE feed_url = '$feed_url' AND owner_uid =" . 
4433                                 $_SESSION["uid"]);
4434
4435                         if (db_num_rows($sub_result) > 0) {
4436                                 continue; // already subscribed
4437                         }
4438                 
4439                         $det_result = db_query($link, "SELECT site_url,title,id 
4440                                 FROM ttrss_feeds WHERE feed_url = '$feed_url' LIMIT 1");
4441
4442                         $details = db_fetch_assoc($det_result);
4443                 
4444                         $icon_file = ICONS_DIR . "/" . $details["id"] . ".ico";
4445
4446                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
4447                                         $feed_icon = "<img class=\"tinyFeedIcon\"       src=\"" . ICONS_URL . 
4448                                                 "/".$details["id"].".ico\">";
4449                         } else {
4450                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
4451                         }
4452
4453                         $check_box = "<input onclick='toggleSelectListRow(this)' class='feedBrowseCB' 
4454                                 type=\"checkbox\" id=\"FBCHK-" . $details["id"] . "\">";
4455
4456                         $class = ($feedctr % 2) ? "even" : "odd";
4457
4458                         print "<li class='$class' id=\"FBROW-".$details["id"]."\">$check_box".
4459                                 "$feed_icon ";
4460                                 
4461                         print "<a href=\"javascript:browserToggleExpand('".$details["id"]."')\">" . 
4462                                 $details["title"] ."</a>&nbsp;" .
4463                                 "<span class='subscribers'>($subscribers)</span>";
4464                         
4465                         print "<div class=\"browserDetails\" id=\"BRDET-" . $details["id"] . "\">";
4466                         print "</div>";
4467                                 
4468                         print "</li>";
4469
4470                                 ++$feedctr;
4471                 }
4472
4473                 if ($feedctr == 0) {
4474                         print "<li>No feeds found to subscribe.</li>";
4475                 }
4476
4477                 print "</ul>";
4478
4479                 print "<p>Selection: 
4480                         <input type='submit' class='button' onclick=\"feedBrowserSubscribe()\" 
4481                         value=\"Subscribe\"></p>";
4482
4483                 print "</div>";
4484
4485         }
4486
4487         db_close($link);
4488 ?>
4489
4490 <!-- <?= sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
4491