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