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