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