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