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