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