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