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