]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
1dcfd4deb485b38053390e595db966a57d631c9b
[tt-rss.git] / backend.php
1 <?
2         session_start();
3
4         $op = $_REQUEST["op"];
5
6         if ((!$op || $op == "rpc" || $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
7                 header("Content-Type: application/xml");
8         }
9
10         if (!$_SESSION["uid"] && $op != "globalUpdateFeeds") {
11
12                 if ($op == "rpc") {
13                         print "<error error-code=\"6\"/>";
14                 }
15                 exit;
16         }
17
18         if (!$op) {
19                 print "<error error-code=\"7\"/>";
20                 exit;
21         }
22
23         define(SCHEMA_VERSION, 2);
24
25         require_once "config.php";
26         require_once "db.php";
27         require_once "db-prefs.php";
28         require_once "functions.php";
29         require_once "magpierss/rss_fetch.inc";
30
31         $script_started = getmicrotime();
32
33         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
34
35         if (!$link) {
36                 if (DB_TYPE == "mysql") {
37                         print mysql_error();
38                 }
39                 // PG seems to display its own errors just fine by default.             
40                 return;
41         }
42
43         if (DB_TYPE == "pgsql") {
44                 pg_query("set client_encoding = 'utf-8'");
45         }
46
47         $fetch = $_GET["fetch"];
48
49         /* FIXME this needs reworking */
50
51         function getGlobalCounters($link) {
52                 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
53                         WHERE unread = true AND 
54                         ttrss_user_entries.ref_id = ttrss_entries.id AND 
55                         owner_uid = " . $_SESSION["uid"]);
56                 $c_id = db_fetch_result($result, 0, "c_id");
57                 print "<counter id='global-unread' counter='$c_id'/>";
58         }
59
60         function getTagCounters($link) {
61         
62                 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
63                         FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
64                         ttrss_user_entries.ref_id = ttrss_entries.id AND 
65                         ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
66                         post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name 
67                 UNION
68                         select tag_name,0 as count FROM ttrss_tags
69                         WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]);
70
71                 $tags = array();
72
73                 while ($line = db_fetch_assoc($result)) {
74                         $tags[$line["tag_name"]] += $line["count"];
75                 }
76
77                 foreach (array_keys($tags) as $tag) {
78                         $unread = $tags[$tag];                  
79
80                         $tag = htmlspecialchars($tag);
81                         print "<tag id=\"$tag\" counter=\"$unread\"/>";
82                 } 
83         }
84
85         function getLabelCounters($link) {
86
87                 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
88                         WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND 
89                         unread = true AND owner_uid = ".$_SESSION["uid"]);
90
91                 $count = db_fetch_result($result, 0, "count");
92
93                 print "<label id=\"-1\" counter=\"$count\"/>";
94
95                 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
96                         ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
97         
98                 while ($line = db_fetch_assoc($result)) {
99
100                         $id = -$line["id"] - 11;
101
102                         error_reporting (0);
103
104                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
105                                 WHERE (" . $line["sql_exp"] . ") AND unread = true AND 
106                                 ttrss_user_entries.ref_id = ttrss_entries.id AND 
107                                 owner_uid = ".$_SESSION["uid"]);
108
109                         $count = db_fetch_result($tmp_result, 0, "count");
110
111                         print "<label id=\"$id\" counter=\"$count\"/>";
112
113                         error_reporting (E_ERROR | E_WARNING | E_PARSE);
114         
115                 }
116         }
117
118         function getFeedCounter($link, $id) {
119         
120                 $result = db_query($link, "SELECT 
121                                 count(id) as count FROM ttrss_entries,ttrss_user_entries
122                         WHERE feed_id = '$id' AND unread = true
123                         AND ttrss_user_entries.ref_id = ttrss_entries.id");
124         
125                         $count = db_fetch_result($result, 0, "count");
126                         
127                         print "<feed id=\"$id\" counter=\"$count\"/>";          
128         }
129
130         function getFeedCounters($link) {
131         
132                 $result = db_query($link, "SELECT id,
133                         (SELECT count(id) 
134                                 FROM ttrss_entries,ttrss_user_entries 
135                                 WHERE feed_id = ttrss_feeds.id AND ttrss_user_entries.ref_id = ttrss_entries.id
136                                 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
137                         FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
138         
139                 while ($line = db_fetch_assoc($result)) {
140                 
141                         $id = $line["id"];
142                         $count = $line["count"];
143
144                         print "<feed id=\"$id\" counter=\"$count\"/>";
145                 }
146         }
147
148         function outputFeedList($link, $tags = false) {
149
150                 print "<html><head>
151                         <title>Tiny Tiny RSS : Feedlist</title>
152                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
153
154                 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
155                         print "<link rel=\"stylesheet\" type=\"text/css\" 
156                                 href=\"tt-rss_compact.css\"/>";
157                 } else {
158                         print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
159                                         type=\"text/css\" href=\"tt-rss_compact.css\"/>";
160                 }
161
162                 print "<script type=\"text/javascript\" src=\"functions.js\"></script>
163                         <script type=\"text/javascript\" src=\"feedlist.js\"></script>
164                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
165                         </head><body onload=\"init()\">";
166
167                 print "<ul class=\"feedList\" id=\"feedList\">";
168
169                 $owner_uid = $_SESSION["uid"];
170
171                 if (!$tags) {
172
173                         /* virtual feeds */
174
175                         $result = db_query($link, "SELECT count(id) as num_starred 
176                                 FROM ttrss_entries,ttrss_user_entries 
177                                 WHERE marked = true AND 
178                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
179                                 unread = true AND owner_uid = '$owner_uid'");
180                         $num_starred = db_fetch_result($result, 0, "num_starred");
181
182                         $class = "virt";
183
184                         if ($num_starred > 0) $class .= "Unread";
185
186                         printFeedEntry(-1, $class, "Starred articles", $num_starred, 
187                                 "images/mark_set.png", $link);
188
189                         if (get_pref($link, 'ENABLE_LABELS')) {
190         
191                                 $result = db_query($link, "SELECT id,sql_exp,description FROM
192                                         ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
193                 
194                                 if (db_num_rows($result) > 0) {
195                                         print "<li><hr></li>";
196                                 }
197                 
198                                 while ($line = db_fetch_assoc($result)) {
199         
200                                         error_reporting (0);
201                 
202                                         $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
203                                                 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
204                                                 ttrss_user_entries.ref_id = ttrss_entries.id
205                                                 AND owner_uid = '$owner_uid'");
206         
207                                         $count = db_fetch_result($tmp_result, 0, "count");
208         
209                                         $class = "label";
210         
211                                         if ($count > 0) {
212                                                 $class .= "Unread";
213                                         }
214                                         
215                                         error_reporting (E_ERROR | E_WARNING | E_PARSE);
216         
217                                         printFeedEntry(-$line["id"]-11, 
218                                                 $class, $line["description"], $count, "images/label.png", $link);
219                 
220                                 }
221                         }
222         
223                         print "<li><hr></li>";
224
225                         $result = db_query($link, "SELECT *,
226                                 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
227                                         WHERE feed_id = ttrss_feeds.id AND 
228                                         ttrss_user_entries.ref_id = ttrss_entries.id AND
229                                         owner_uid = '$owner_uid') AS total,
230                                 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
231                                         WHERE feed_id = ttrss_feeds.id AND unread = true
232                                                 AND ttrss_user_entries.ref_id = ttrss_entries.id
233                                                 AND owner_uid = '$owner_uid') as unread
234                                 FROM ttrss_feeds WHERE owner_uid = '$owner_uid' ORDER BY title");                       
235         
236                         $actid = $_GET["actid"];
237         
238                         /* real feeds */
239         
240                         $lnum = 0;
241         
242                         $total_unread = 0;
243         
244                         while ($line = db_fetch_assoc($result)) {
245                         
246                                 $feed = $line["title"];
247                                 $feed_id = $line["id"];   
248         
249                                 $subop = $_GET["subop"];
250                                 
251                                 $total = $line["total"];
252                                 $unread = $line["unread"];
253                                 
254         //                      $class = ($lnum % 2) ? "even" : "odd";
255         
256                                 $class = "feed";
257         
258                                 if ($unread > 0) $class .= "Unread";
259         
260                                 if ($actid == $feed_id) {
261                                         $class .= "Selected";
262                                 }
263         
264                                 $total_unread += $unread;
265         
266                                 printFeedEntry($feed_id, $class, $feed, $unread, "icons/$feed_id.ico", $link);
267         
268                                 ++$lnum;
269                         }
270                 } else {
271
272                         // tags
273
274                         $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
275                                 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
276                                 post_int_id = ttrss_user_entries.int_id AND 
277                                 unread = true AND ref_id = ttrss_entries.id
278                                 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name       
279                         UNION
280                                 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
281                         ORDER BY tag_name");
282         
283                         $tags = array();
284         
285                         while ($line = db_fetch_assoc($result)) {
286                                 $tags[$line["tag_name"]] += $line["count"];
287                         }
288         
289                         foreach (array_keys($tags) as $tag) {
290         
291                                 $unread = $tags[$tag];
292         
293                                 $class = "odd";
294         
295                                 if ($unread > 0) {
296                                         $class .= "Unread";
297                                 }
298         
299                                 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
300         
301                         } 
302
303                 }
304
305                 if (db_num_rows($result) == 0) {
306                         print "<li>No tags/feeds to display.</li>";
307                 }
308
309                 print "</ul>";
310
311                 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
312
313         }
314
315
316         if ($op == "rpc") {
317
318                 $subop = $_GET["subop"];
319
320                 if ($subop == "getLabelCounters") {
321                         $aid = $_GET["aid"];            
322                         print "<rpc-reply>";
323                         getLabelCounters($link);
324                         if ($aid) {
325                                 getFeedCounter($link, $aid);
326                         }
327                         print "</rpc-reply>";
328                 }
329
330                 if ($subop == "getFeedCounters") {
331                         print "<rpc-reply>";
332                         getFeedCounters($link);
333                         print "</rpc-reply>";
334                 }
335
336                 if ($subop == "getAllCounters") {
337                         print "<rpc-reply>";
338                         getLabelCounters($link);
339                         getFeedCounters($link);
340                         getTagCounters($link);
341                         getGlobalCounters($link);
342                         print "</rpc-reply>";
343                 }
344
345                 if ($subop == "mark") {
346                         $mark = $_GET["mark"];
347                         $id = db_escape_string($_GET["id"]);
348
349                         if ($mark == "1") {
350                                 $mark = "true";
351                         } else {
352                                 $mark = "false";
353                         }
354
355                         // FIXME this needs collision testing
356
357                         $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
358                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
359                 }
360
361                 if ($subop == "updateFeed") {
362                         $feed_id = db_escape_string($_GET["feed"]);
363
364                         $result = db_query($link, 
365                                 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
366                                         AND owner_uid = " . $_SESSION["uid"]);
367
368                         if (db_num_rows($result) > 0) {                 
369                                 $feed_url = db_fetch_result($result, 0, "feed_url");
370                                 update_rss_feed($link, $feed_url, $feed_id);
371                         }
372
373                         print "<rpc-reply>";
374                         getFeedCounter($link, $feed_id);
375                         print "</rpc-reply>";
376                         
377                         return;
378                 }
379
380                 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
381                 
382                         update_all_feeds($link, $subop == "forceUpdateAllFeeds");                       
383
384                         $omode = $_GET["omode"];
385
386                         if (!$omode) $omode = "tfl";
387
388                         print "<rpc-reply>";
389                         if (strchr($omode, "l")) getLabelCounters($link);
390                         if (strchr($omode, "f")) getFeedCounters($link);
391                         if (strchr($omode, "t")) getTagCounters($link);
392                         getGlobalCounters($link);
393                         print "</rpc-reply>";
394                 }
395                 
396                 if ($subop == "catchupSelected") {
397
398                         $ids = split(",", $_GET["ids"]);
399
400                         foreach ($ids as $id) {
401
402                                 db_query($link, "UPDATE ttrss_user_entries SET unread=false,last_read = NOW()
403                                         WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
404
405                         }
406
407                         print "Marked active page as read.";
408                 }
409
410                 if ($subop == "sanityCheck") {
411
412                         $error_code = 0;
413
414                         $result = db_query($link, "SELECT schema_version FROM ttrss_version");
415
416                         $schema_version = db_fetch_result($result, 0, "schema_version");
417
418                         if ($schema_version != SCHEMA_VERSION) {
419                                 $error_code = 5;
420                         }
421
422                         print "<error error-code='$error_code'/>";
423                 }
424
425                 if ($subop == "globalPurge") {
426
427                         print "<rpc-reply>";
428                         global_purge_old_posts($link, true);
429                         print "</rpc-reply>";
430
431                 }
432
433         }
434         
435         if ($op == "feeds") {
436
437                 $tags = $_GET["tags"];
438
439                 $subop = $_GET["subop"];
440
441                 if ($subop == "catchupAll") {
442                         db_query($link, "UPDATE ttrss_user_entries SET 
443                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
444                 }
445
446                 outputFeedList($link, $tags);
447
448         }
449
450         if ($op == "view") {
451
452                 $id = $_GET["id"];
453                 $feed_id = $_GET["feed"];
454
455                 $result = db_query($link, "UPDATE ttrss_user_entries 
456                         SET unread = false,last_read = NOW() 
457                         WHERE ref_id = '$id' AND feed_id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
458
459                 $addheader = $_GET["addheader"];
460
461                 $result = db_query($link, "SELECT title,link,content,feed_id,comments,
462                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url 
463                         FROM ttrss_entries,ttrss_user_entries
464                         WHERE   id = '$id' AND ref_id = id");
465
466                 if ($addheader) {
467                         print "<html><head>
468                                 <title>Tiny Tiny RSS : Article $id</title>
469                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
470                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
471                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
472                                 </head><body>";
473                 }
474
475                 if ($result) {
476
477                         $line = db_fetch_assoc($result);
478
479                         if ($line["icon_url"]) {
480                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
481                         } else {
482                                 $feed_icon = "&nbsp;";
483                         }
484
485                         if ($line["comments"] && $line["link"] != $line["comments"]) {
486                                 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
487                         } else {
488                                 $entry_comments = "";
489                         }
490
491                         print "<div class=\"postReply\">";
492
493                         print "<div class=\"postHeader\"><table>";
494
495                         print "<tr><td><b>Title:</b></td>
496                                 <td width='100%'>" . $line["title"] . "</td></tr>";
497                                 
498                         print "<tr><td><b>Link:</b></td>
499                                 <td width='100%'>
500                                 <a href=\"" . $line["link"] . "\">".$line["link"]."</a>
501                                 $entry_comments</td></tr>";
502                                         
503                         print "</table></div>";
504
505                         print "<div class=\"postIcon\">" . $feed_icon . "</div>";
506                         print "<div class=\"postContent\">" . $line["content"] . "</div>";
507                         
508                         print "</div>";
509
510                         print "<script type=\"text/javascript\">
511                                 update_label_counters('$feed_id');
512                         </script>";
513                 }
514
515                 if ($addheader) {
516                         print "</body></html>";
517                 }
518         }
519
520         if ($op == "viewfeed") {
521
522                 $feed = $_GET["feed"];
523                 $skip = $_GET["skip"];
524                 $subop = $_GET["subop"];
525                 $view_mode = $_GET["view"];
526                 $addheader = $_GET["addheader"];
527                 $limit = $_GET["limit"];
528
529                 if (!$feed) {
530                         print "Error: no feed to display.";
531                         return;
532                 }
533
534                 if (!$skip) $skip = 0;
535
536                 if ($subop == "undefined") $subop = "";
537
538                 if ($addheader) {
539                         print "<html><head>
540                                 <title>Tiny Tiny RSS : Feed $feed</title>
541                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
542
543                         if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
544                                 print "<link rel=\"stylesheet\" 
545                                                 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
546
547                         } else {
548                                 print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
549                                                 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
550                         }
551                         print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">  
552                                 <script type=\"text/javascript\" src=\"functions.js\"></script>
553                                 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
554                                 </head><body onload='init()'>";
555                 }
556
557                 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
558
559                         $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
560                                 WHERE id = '$feed'");
561
562                         $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
563
564                         update_rss_feed($link, $feed_url, $feed);
565
566                 }
567
568                 if ($subop == "MarkAllRead")  {
569
570                         if (sprintf("%d", $feed) != 0) {
571                         
572                                 if ($feed > 0) {
573                                         db_query($link, "UPDATE ttrss_user_entries 
574                                                 SET unread = false,last_read = NOW() 
575                                                 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
576                                                 
577                                 } else if ($feed < 0 && $feed > -10) { // special, like starred
578
579                                         if ($feed == -1) {
580                                                 db_query($link, "UPDATE ttrss_user_entries 
581                                                         SET unread = false,last_read = NOW()
582                                                         WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
583                                         }
584                         
585                                 } else if ($feed < -10) { // label
586
587                                         // TODO make this more efficient
588
589                                         $label_id = -$feed - 11;
590
591                                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
592                                                 WHERE id = '$label_id'");                                       
593
594                                         if ($tmp_result) {
595                                                 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
596
597                                                 db_query($link, "BEGIN");
598
599                                                 $tmp2_result = db_query($link,
600                                                         "SELECT 
601                                                                 int_id 
602                                                         FROM 
603                                                                 ttrss_user_entries,ttrss_entries 
604                                                         WHERE
605                                                                 ref_id = id AND 
606                                                                 $sql_exp AND
607                                                                 owner_uid = " . $_SESSION["uid"]);
608
609                                                 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
610                                                         db_query($link, "UPDATE 
611                                                                 ttrss_user_entries 
612                                                         SET 
613                                                                 unread = false, last_read = NOW()
614                                                         WHERE
615                                                                 int_id = " . $tmp_line["int_id"]);
616                                                 }
617                                                                 
618                                                 db_query($link, "COMMIT");
619
620 /*                                              db_query($link, "UPDATE ttrss_user_entries,ttrss_entries 
621                                                         SET unread = false,last_read = NOW()
622                                                         WHERE $sql_exp
623                                                         AND ref_id = id
624                                                         AND owner_uid = ".$_SESSION["uid"]); */
625                                         }
626                                 }
627                         } else { // tag
628                                 // FIXME, implement catchup for tags
629                         }
630
631                 }
632
633                 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
634
635                 $search = $_GET["search"];
636
637                 $search_mode = $_GET["smode"];
638
639                 if ($search) {
640                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
641                                 OR content LIKE '%$search%') AND";
642                 } else {
643                         $search_query_part = "";
644                 }
645
646                 $view_query_part = "";
647
648                 if ($view_mode == "Starred") {
649                         $view_query_part = " marked = true AND ";
650                 }
651
652                 if ($view_mode == "Unread") {
653                         $view_query_part = " unread = true AND ";
654                 }
655
656                 if ($view_mode == "Unread or Starred") {
657                         $view_query_part = " (unread = true OR marked = true) AND ";
658                 }
659
660                 if ($view_mode == "Unread or Updated") {
661                         $view_query_part = " (unread = true OR last_read is NULL) AND ";
662                 }
663
664 /*              $result = db_query($link, "SELECT count(id) AS total_entries 
665                         FROM ttrss_entries WHERE 
666                         $search_query_part
667                         feed_id = '$feed'");
668
669                 $total_entries = db_fetch_result($result, 0, "total_entries"); */
670
671 /*              $result = db_query("SELECT count(id) AS unread_entries 
672                         FROM ttrss_entries WHERE 
673                         $search_query_part
674                         unread = true AND
675                         feed_id = '$feed'");
676
677                 $unread_entries = db_fetch_result($result, 0, "unread_entries"); */
678
679                 if ($limit && $limit != "All") {
680                         $limit_query_part = "LIMIT " . $limit;
681                 } 
682
683                 $vfeed_query_part = "";
684
685                 // override query strategy and enable feed display when searching globally
686                 if ($search && $search_mode == "All feeds") {
687                         $query_strategy_part = "id > 0";
688                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
689                                 id = feed_id) as feed_title,";
690                 } else if (sprintf("%d", $feed) == 0) {
691                         $query_strategy_part = "ttrss_entries.id > 0";
692                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
693                                 id = feed_id) as feed_title,";
694                 } else if ($feed >= 0) {
695                         $query_strategy_part = "feed_id = '$feed'";
696                 } else if ($feed == -1) { // starred virtual feed
697                         $query_strategy_part = "marked = true";
698                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
699                                 id = feed_id) as feed_title,";
700                 } else if ($feed <= -10) { // labels
701                         $label_id = -$feed - 11;
702
703                         $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
704                                 WHERE id = '$label_id'");
705                 
706                         $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
707         
708                         $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
709                                 id = feed_id) as feed_title,";
710                 } else {
711                         $query_strategy_part = "id > 0"; // dumb
712                 }
713
714
715                 $order_by = "updated DESC";
716
717 //              if ($feed < -10) {
718 //                      $order_by = "feed_id,updated DESC";
719 //              }
720
721                 if ($feed < -10) error_reporting (0);
722
723                 if (sprintf("%d", $feed) != 0) {
724
725                         $result = db_query($link, "SELECT 
726                                         id,title,updated,unread,feed_id,marked,link,last_read,
727                                         SUBSTRING(last_read,1,19) as last_read_noms,
728                                         $vfeed_query_part
729                                         SUBSTRING(updated,1,19) as updated_noms
730                                 FROM
731                                         ttrss_entries,ttrss_user_entries
732                                 WHERE
733                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
734                                 owner_uid = '".$_SESSION["uid"]."' AND
735                                 $search_query_part
736                                 $view_query_part
737                                 $query_strategy_part ORDER BY $order_by
738                                 $limit_query_part");
739
740                 } else {
741                         // browsing by tag
742
743                         $result = db_query($link, "SELECT
744                                 ttrss_entries.id as id,title,updated,unread,feed_id,
745                                 marked,link,last_read,
746                                 SUBSTRING(last_read,1,19) as last_read_noms,
747                                 $vfeed_query_part
748                                 SUBSTRING(updated,1,19) as updated_noms
749                                 FROM
750                                         ttrss_entries,ttrss_user_entries,ttrss_tags
751                                 WHERE
752                                         ref_id = ttrss_entries.id AND
753                                         ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
754                                         post_int_id = int_id AND tag_name = '$feed' AND
755                                         $view_query_part
756                                         $search_query_part
757                                         $query_strategy_part ORDER BY $order_by
758                                 $limit_query_part");    
759                 }
760
761                 if (!$result) {
762                         print "<tr><td colspan='4' align='center'>
763                                 Could not display feed (query failed). Please check match syntax or local configuration.</td></tr>";
764                         return;
765                 }
766
767                 $lnum = 0;
768
769                 error_reporting (E_ERROR | E_WARNING | E_PARSE);
770
771                 $num_unread = 0;
772
773                 while ($line = db_fetch_assoc($result)) {
774
775                         $class = ($lnum % 2) ? "even" : "odd";
776
777                         $id = $line["id"];
778                         $feed_id = $line["feed_id"];
779
780 //                      printf("L %d (%s) &gt; U %d (%s) = %d<br>", 
781 //                              strtotime($line["last_read_noms"]), $line["last_read_noms"],
782 //                              strtotime($line["updated"]), $line["updated"],
783 //                              strtotime($line["last_read"]) >= strtotime($line["updated"]));
784
785 /*                      if ($line["last_read"] != "" && $line["updated"] != "" &&
786                                 strtotime($line["last_read_noms"]) < strtotime($line["updated_noms"])) {
787
788                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
789                                         alt=\"Updated\">";
790
791                         } else {
792
793                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
794                                         alt=\"Updated\">";
795
796                         } */
797
798                         if ($line["last_read"] == "" && 
799                                         ($line["unread"] != "t" && $line["unread"] != "1")) {
800
801                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
802                                         alt=\"Updated\">";
803                         } else {
804                                 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
805                                         alt=\"Updated\">";
806                         }
807
808                         if ($line["unread"] == "t" || $line["unread"] == "1") {
809                                 $class .= "Unread";
810                                 ++$num_unread;
811                         }
812
813                         if ($line["marked"] == "t" || $line["marked"] == "1") {
814                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\" 
815                                         alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
816                         } else {
817                                 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\" 
818                                         alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
819                         }
820
821                         $content_link = "<a id=\"FTITLE-$id\" href=\"javascript:view($id,$feed_id);\">" .
822                                 $line["title"] . "</a>";
823                                 
824                         print "<tr class='$class' id='RROW-$id'>";
825                         // onclick=\"javascript:view($id,$feed_id)\">
826
827                         print "<td valign='center' align='center'>$update_pic</td>";
828                         print "<td valign='center' align='center'>$marked_pic</td>";
829
830                         print "<td width='25%'>
831                                 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
832
833                         if ($line["feed_title"]) {                      
834                                 print "<td width='50%'>$content_link</td>";
835                                 print "<td width='20%'>
836                                         <a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a></td>";
837                         } else {
838                                 print "<td width='70%'>$content_link</td>";
839                         }
840
841                         print "</tr>";
842
843                         ++$lnum;
844                 }
845
846                 if ($lnum == 0) {
847                         print "<tr><td align='center'>No articles found.</td></tr>";
848                 }
849                 
850                 print "</table>";
851                 
852                 print "<script type=\"text/javascript\">
853                         document.onkeydown = hotkey_handler;
854                         update_label_counters('$feed');
855                 </script>";
856
857                 if ($addheader) {
858                         print "</body></html>";
859                 }
860
861         }
862
863         if ($op == "pref-rpc") {
864
865                 $subop = $_GET["subop"];
866
867                 if ($subop == "unread") {
868                         $ids = split(",", $_GET["ids"]);
869                         foreach ($ids as $id) {
870                                 db_query($link, "UPDATE ttrss_user_entries SET unread = true 
871                                         WHERE feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
872                         }
873
874                         print "Marked selected feeds as unread.";
875                 }
876
877                 if ($subop == "read") {
878                         $ids = split(",", $_GET["ids"]);
879                         foreach ($ids as $id) {
880                                 db_query($link, "UPDATE ttrss_user_entries 
881                                         SET unread = false,last_read = NOW() WHERE 
882                                                 feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
883                         }
884
885                         print "Marked selected feeds as read.";
886
887                 }
888
889         }
890
891         if ($op == "pref-feeds") {
892         
893                 $subop = $_GET["subop"];
894
895                 if ($subop == "editSave") {
896                         $feed_title = db_escape_string($_GET["t"]);
897                         $feed_link = db_escape_string($_GET["l"]);
898                         $upd_intl = db_escape_string($_GET["ui"]);
899                         $purge_intl = db_escape_string($_GET["pi"]);
900                         $feed_id = $_GET["id"];
901
902                         if (strtoupper($upd_intl) == "DEFAULT")
903                                 $upd_intl = 0;
904
905                         if (strtoupper($purge_intl) == "DEFAULT")
906                                 $purge_intl = 0;
907
908                         if (strtoupper($purge_intl) == "DISABLED")
909                                 $purge_intl = -1;
910
911                         $result = db_query($link, "UPDATE ttrss_feeds SET 
912                                 title = '$feed_title', feed_url = '$feed_link',
913                                 update_interval = '$upd_intl',
914                                 purge_interval = '$purge_intl' 
915                                 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);                    
916
917                 }
918
919                 if ($subop == "remove") {
920
921                         if (!WEB_DEMO_MODE) {
922
923                                 $ids = split(",", $_GET["ids"]);
924
925                                 foreach ($ids as $id) {
926                                         db_query($link, "DELETE FROM ttrss_feeds 
927                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
928
929                                         $icons_dir = ICONS_DIR;
930                                         
931                                         if (file_exists($icons_dir . "/$id.ico")) {
932                                                 unlink($icons_dir . "/$id.ico");
933                                         }
934                                 }
935                         }
936                 }
937
938                 if ($subop == "add") {
939                 
940                         if (!WEB_DEMO_MODE) {
941
942                                 $feed_link = db_escape_string(trim($_GET["link"]));
943
944                                 $result = db_query($link,
945                                         "SELECT id FROM ttrss_feeds 
946                                         WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
947
948                                 if (db_num_rows($result) == 0) {
949                                         
950                                         $result = db_query($link,
951                                                 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title) 
952                                                 VALUES ('".$_SESSION["uid"]."', '$feed_link', '')");
953
954                                         $result = db_query($link,
955                                         "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' 
956                                                 AND owner_uid = " . $_SESSION["uid"]);
957
958                                         $feed_id = db_fetch_result($result, 0, "id");
959
960                                         if ($feed_id) {
961                                                 update_rss_feed($link, $feed_link, $feed_id);
962                                         }
963                                 } else {
964
965                                         print "<div class=\"warning\">
966                                                 Feed <b>$feed_link</b> already exists in the database.
967                                         </div>";
968                                 }
969                         }
970                 }
971
972                 $result = db_query($link, "SELECT id,title,feed_url,last_error 
973                         FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
974
975                 if (db_num_rows($result) > 0) {
976                 
977                         print "<div class=\"warning\">";
978                 
979                         print "<b>Feeds with update errors:</b>";
980
981                         print "<ul class=\"nomarks\">";
982                                                 
983                         while ($line = db_fetch_assoc($result)) {
984                                 print "<li>" . $line["title"] . " (" . $line["feed_url"] . "): " . 
985                                         $line["last_error"];
986                         }
987
988                         print "</ul>";
989                         print "</div>";
990
991                 }
992
993                 print "<div class=\"prefGenericAddBox\">
994                         <input id=\"fadd_link\" size=\"40\">&nbsp;<input 
995                                 type=\"submit\" class=\"button\" 
996                                 onclick=\"javascript:addFeed()\" value=\"Add feed\"></div>";
997
998                 $result = db_query($link, "SELECT 
999                                 id,title,feed_url,substring(last_updated,1,16) as last_updated,
1000                                 update_interval,purge_interval
1001                         FROM 
1002                                 ttrss_feeds WHERE owner_uid = '".$_SESSION["uid"]."' ORDER by title");
1003
1004                 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
1005
1006                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
1007                 print "<tr class=\"title\">
1008                                         <td>&nbsp;</td><td>Select</td><td width=\"30%\">Title</td>
1009                                         <td width=\"30%\">Link</td>
1010                                         <td width=\"10%\">Update Interval</td>
1011                                         <td width=\"10%\">Purge Days</td>
1012                                         <td>Last updated</td></tr>";
1013                 
1014                 $lnum = 0;
1015                 
1016                 while ($line = db_fetch_assoc($result)) {
1017
1018                         $class = ($lnum % 2) ? "even" : "odd";
1019
1020                         $feed_id = $line["id"];
1021
1022                         $edit_feed_id = $_GET["id"];
1023
1024                         if ($subop == "edit" && $feed_id != $edit_feed_id) {
1025                                 $class .= "Grayed";
1026                         }
1027
1028                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
1029
1030                         $icon_file = ICONS_DIR . "/$feed_id.ico";
1031
1032                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
1033                                         $feed_icon = "<img width=\"16\" height=\"16\"
1034                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
1035                         } else {
1036                                 $feed_icon = "&nbsp;";
1037                         }
1038                         print "<td align='center'>$feed_icon</td>";             
1039
1040                         $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
1041                         $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
1042
1043                         if (!$edit_feed_id || $subop != "edit") {
1044
1045                                 print "<td><input onclick='toggleSelectRow(this);' 
1046                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
1047
1048                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1049                                         $edit_title . "</a></td>";              
1050                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1051                                         $edit_link . "</a></td>";               
1052
1053                                 if ($line["update_interval"] == "0")
1054                                         $line["update_interval"] = "Default";
1055
1056                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1057                                         $line["update_interval"] . "</a></td>";
1058
1059                                 if ($line["purge_interval"] == "0")
1060                                         $line["purge_interval"] = "Default";
1061
1062                                 if ($line["purge_interval"] < 0)
1063                                         $line["purge_interval"] = "Disabled";
1064
1065                                 print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
1066                                         $line["purge_interval"] . "</a></td>";
1067
1068                         } else if ($feed_id != $edit_feed_id) {
1069
1070                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
1071                                         id=\"FRCHK-".$line["id"]."\"></td>";
1072
1073                                 print "<td>$edit_title</td>";           
1074                                 print "<td>$edit_link</td>";            
1075
1076                                 if ($line["update_interval"] == "0")
1077                                         $line["update_interval"] = "Default";
1078
1079                                 print "<td>" . $line["update_interval"] . "</td>";
1080
1081                                 if ($line["purge_interval"] == "0")
1082                                         $line["purge_interval"] = "Default";
1083
1084                                 if ($line["purge_interval"] < 0)
1085                                         $line["purge_interval"] = "Disabled";
1086
1087                                 print "<td>" . $line["purge_interval"] . "</td>";
1088
1089                         } else {
1090
1091                                 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1092
1093                                 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
1094                                 print "<td><input id=\"iedit_link\" value=\"$edit_link\"></td>";
1095                                 print "<td><input id=\"iedit_updintl\" value=\"".$line["update_interval"]."\"></td>";
1096                                 print "<td><input id=\"iedit_purgintl\" value=\"".$line["purge_interval"]."\"></td>";
1097                                         
1098                         }
1099
1100                         if (!$line["last_updated"]) $line["last_updated"] = "Never";
1101
1102                         print "<td>" . $line["last_updated"] . "</td>";
1103                         
1104                         print "</tr>";
1105
1106                         ++$lnum;
1107                 }
1108
1109                 if ($lnum == 0) {
1110                         print "<tr><td colspan=\"5\" align=\"center\">No feeds defined.</td></tr>";
1111                 }
1112
1113                 print "</table>";
1114
1115                 print "<p>";
1116
1117                 if ($subop == "edit") {
1118                         print "Edit feed:&nbsp;
1119                                 <input type=\"submit\" class=\"button\" 
1120                                         onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
1121                                 <input type=\"submit\" class=\"button\" 
1122                                         onclick=\"javascript:feedEditSave()\" value=\"Save\">";
1123                         } else {
1124
1125                         print "
1126                                 Selection:&nbsp;
1127                         <input type=\"submit\" class=\"button\" 
1128                                 onclick=\"javascript:selectedFeedDetails()\" value=\"Details\">
1129                         <input type=\"submit\" class=\"button\" 
1130                                 onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
1131                         <input type=\"submit\" class=\"button\" 
1132                                 onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
1133                                 
1134                         if (get_pref($link, 'ENABLE_PREFS_CATCHUP_UNCATCHUP')) {
1135                                 print "
1136                                 <input type=\"submit\" class=\"button\" 
1137                                         onclick=\"javascript:readSelectedFeeds()\" value=\"Mark as read\">
1138                                 <input type=\"submit\" class=\"button\" 
1139                                         onclick=\"javascript:unreadSelectedFeeds()\" value=\"Mark as unread\">&nbsp;";
1140                         }
1141                         print "
1142                         All feeds: 
1143                                 <input type=\"submit\" 
1144                                         class=\"button\" onclick=\"gotoExportOpml()\" value=\"Export OPML\">";
1145                 
1146                         }
1147
1148                 print "<h3>OPML Import</h3>
1149                 <form   enctype=\"multipart/form-data\" method=\"POST\" action=\"opml.php\">
1150                         File: <input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
1151                         <input class=\"button\" name=\"op\" onclick=\"return validateOpmlImport();\"
1152                                 type=\"submit\" value=\"Import\">
1153                         </form>";
1154
1155         }
1156
1157         if ($op == "pref-filters") {
1158
1159                 $subop = $_GET["subop"];
1160
1161                 if ($subop == "editSave") {
1162
1163                         $regexp = db_escape_string($_GET["r"]);
1164                         $descr = db_escape_string($_GET["d"]);
1165                         $match = db_escape_string($_GET["m"]);
1166                         $filter_id = db_escape_string($_GET["id"]);
1167                         $feed_id = db_escape_string($_GET["fid"]);
1168
1169                         if (!$feed_id) {
1170                                 $feed_id = 'NULL';
1171                         } else {
1172                                 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
1173                         }
1174                         
1175                         $result = db_query($link, "UPDATE ttrss_filters SET 
1176                                 reg_exp = '$regexp', 
1177                                 description = '$descr',
1178                                 feed_id = $feed_id,
1179                                 filter_type = (SELECT id FROM ttrss_filter_types WHERE
1180                                         description = '$match')
1181                                 WHERE id = '$filter_id'");
1182                 }
1183
1184                 if ($subop == "remove") {
1185
1186                         if (!WEB_DEMO_MODE) {
1187
1188                                 $ids = split(",", $_GET["ids"]);
1189
1190                                 foreach ($ids as $id) {
1191                                         db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
1192                                         
1193                                 }
1194                         }
1195                 }
1196
1197                 if ($subop == "add") {
1198                 
1199                         if (!WEB_DEMO_MODE) {
1200
1201                                 $regexp = db_escape_string(trim($_GET["regexp"]));
1202                                 $match = db_escape_string(trim($_GET["match"]));
1203                                 $feed_id = db_escape_string($_GET["fid"]);
1204
1205                                 if (!$feed_id) {
1206                                         $feed_id = 'NULL';
1207                                 } else {
1208                                         $feed_id = sprintf("'%s'", db_escape_string($feed_id));
1209                                 }
1210
1211                                 $result = db_query($link,
1212                                         "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id) VALUES 
1213                                                 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
1214                                                         description = '$match'),'".$_SESSION["uid"]."', $feed_id)");
1215                         } 
1216                 }
1217
1218                 $result = db_query($link, "SELECT description 
1219                         FROM ttrss_filter_types ORDER BY description");
1220
1221                 $filter_types = array();
1222
1223                 while ($line = db_fetch_assoc($result)) {
1224                         array_push($filter_types, $line["description"]);
1225                 }
1226
1227                 print "<div class=\"prefGenericAddBox\">
1228                 <input id=\"fadd_regexp\" size=\"40\">&nbsp;";
1229                 
1230                 print_select("fadd_match", "Title", $filter_types);     
1231
1232                 print "&nbsp;<select id=\"fadd_feed\">";
1233
1234                 print "<option selected id=\"0\">All feeds</option>";
1235
1236                 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1237                         WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1238
1239                 if (db_num_rows($result) > 0) {
1240                         print "<option disabled>--------</option>";
1241                 }
1242
1243                 while ($line = db_fetch_assoc($result)) {
1244                         printf("<option id='%d'>%s</option>", $line["id"], $line["title"]);
1245                 }
1246
1247                 print "</select>&nbsp;";
1248         
1249                 print "<input type=\"submit\" 
1250                         class=\"button\" onclick=\"javascript:addFilter()\" 
1251                         value=\"Add filter\">";
1252
1253                 $result = db_query($link, "SELECT 
1254                                 ttrss_filters.id AS id,reg_exp,
1255                                 ttrss_filters.description AS description,
1256                                 ttrss_filter_types.name AS filter_type_name,
1257                                 ttrss_filter_types.description AS filter_type_descr,
1258                                 feed_id,
1259                                 (SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
1260                         FROM 
1261                                 ttrss_filters,ttrss_filter_types
1262                         WHERE
1263                                 filter_type = ttrss_filter_types.id AND
1264                                 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
1265                         ORDER by reg_exp");
1266
1267                 print "<p><table width=\"100%\" class=\"prefFilterList\" id=\"prefFilterList\">";
1268
1269                 print "<tr class=\"title\">
1270                                         <td width=\"5%\">Select</td><td width=\"30%\">Filter expression</td>
1271                                         <td width=\"30%\">Feed</td><td width=\"10%\">Match</td>
1272                                         <td width=\"30%\">Description</td></tr>";
1273                 
1274                 $lnum = 0;
1275                 
1276                 while ($line = db_fetch_assoc($result)) {
1277
1278                         $class = ($lnum % 2) ? "even" : "odd";
1279
1280                         $filter_id = $line["id"];
1281                         $edit_filter_id = $_GET["id"];
1282
1283                         if ($subop == "edit" && $filter_id != $edit_filter_id) {
1284                                 $class .= "Grayed";
1285                         }
1286
1287                         print "<tr class=\"$class\" id=\"FILRR-$filter_id\">";
1288
1289                         $line["regexp"] = htmlspecialchars($line["reg_exp"]);
1290                         $line["description"] = htmlspecialchars($line["description"]);
1291
1292                         if (!$line["feed_title"]) $line["feed_title"] = "All feeds";
1293
1294                         if (!$edit_filter_id || $subop != "edit") {
1295
1296                                 if (!$line["description"]) $line["description"] = "[No description]";
1297
1298                                 print "<td><input onclick='toggleSelectRow(this);' 
1299                                 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
1300
1301                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
1302                                         $line["reg_exp"] . "</td>";             
1303
1304                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
1305                                         $line["feed_title"] . "</td>";                  
1306
1307                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
1308                                         $line["filter_type_descr"] . "</td>";           
1309                                         
1310                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
1311                                         $line["description"] . "</td>";                 
1312
1313                         } else if ($filter_id != $edit_filter_id) {
1314
1315                                 if (!$line["description"]) $line["description"] = "[No description]";
1316
1317                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
1318                                         id=\"FICHK-".$line["id"]."\"></td>";
1319
1320                                 print "<td>".$line["reg_exp"]."</td>";          
1321                                 print "<td>".$line["feed_title"]."</td>";
1322                                 print "<td>".$line["filter_type_descr"]."</td>";
1323                                 print "<td>".$line["description"]."</td>";              
1324
1325                         } else {
1326
1327                                 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1328
1329                                 print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
1330                                         "\"></td>";
1331
1332                                 print "<td>";
1333
1334                                 print "<select id=\"iedit_feed\">";
1335
1336                                 print "<option id=\"0\">All feeds</option>";
1337
1338                                 if (db_num_rows($result) > 0) {
1339                                         print "<option disabled>--------</option>";
1340                                 }
1341
1342                                 $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feeds
1343                                         WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1344
1345                                 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1346                                         if ($tmp_line["id"] == $line["feed_id"]) {
1347                                                 $is_selected = "selected";
1348                                         } else {
1349                                                 $is_selected = "";
1350                                         }
1351                                         printf("<option $is_selected id='%d'>%s</option>", 
1352                                                 $tmp_line["id"], $tmp_line["title"]);
1353                                 }
1354
1355                                 print "</select></td>";
1356
1357                                 print "<td>";
1358                                 print_select("iedit_match", $line["filter_type_descr"], $filter_types);
1359                                 print "</td>";
1360
1361                                 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1362                                         "\"></td>";
1363
1364                                 print "</td>";
1365                         }
1366                         
1367                         print "</tr>";
1368
1369                         ++$lnum;
1370                 }
1371
1372                 if ($lnum == 0) {
1373                         print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
1374                 }
1375
1376                 print "</table>";
1377
1378                 print "<p>";
1379
1380                 if ($subop == "edit") {
1381                         print "Edit feed:
1382                                 <input type=\"submit\" class=\"button\" 
1383                                         onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">
1384                                 <input type=\"submit\" class=\"button\" 
1385                                         onclick=\"javascript:filterEditSave()\" value=\"Save\">";
1386                                         
1387                 } else {
1388
1389                         print "
1390                                 Selection:
1391                         <input type=\"submit\" class=\"button\" 
1392                                 onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
1393                         <input type=\"submit\" class=\"button\" 
1394                                 onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
1395                 }
1396         }
1397
1398         if ($op == "pref-labels") {
1399
1400                 $subop = $_GET["subop"];
1401
1402                 if ($subop == "editSave") {
1403
1404                         $sql_exp = $_GET["s"];
1405                         $descr = $_GET["d"];
1406                         $label_id = db_escape_string($_GET["id"]);
1407                         
1408 //                      print "$sql_exp : $descr : $label_id";
1409                         
1410                         $result = db_query($link, "UPDATE ttrss_labels SET 
1411                                 sql_exp = '$sql_exp', 
1412                                 description = '$descr'
1413                                 WHERE id = '$label_id'");
1414                 }
1415
1416                 if ($subop == "remove") {
1417
1418                         if (!WEB_DEMO_MODE) {
1419
1420                                 $ids = split(",", $_GET["ids"]);
1421
1422                                 foreach ($ids as $id) {
1423                                         db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
1424                                         
1425                                 }
1426                         }
1427                 }
1428
1429                 if ($subop == "add") {
1430                 
1431                         if (!WEB_DEMO_MODE) {
1432
1433                                 // no escaping is done here on purpose
1434                                 $exp = trim($_GET["exp"]);
1435                                         
1436                                 $result = db_query($link,
1437                                         "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
1438                                                 VALUES ('$exp', '$exp', '".$_SESSION["uid"]."')");
1439                         } 
1440                 }
1441
1442                 print "<div class=\"prefGenericAddBox\">
1443                         <input size=\"40\" id=\"ladd_expr\">&nbsp;";
1444                         
1445                 print"<input type=\"submit\" class=\"button\" 
1446                         onclick=\"javascript:addLabel()\" value=\"Add label\"></div>";
1447
1448                 $result = db_query($link, "SELECT 
1449                                 id,sql_exp,description
1450                         FROM 
1451                                 ttrss_labels 
1452                         WHERE 
1453                                 owner_uid = ".$_SESSION["uid"]."
1454                         ORDER by description");
1455
1456                 print "<p><table width=\"100%\" class=\"prefLabelList\" id=\"prefLabelList\">";
1457
1458                 print "<tr class=\"title\">
1459                                         <td width=\"5%\">Select</td><td width=\"40%\">SQL expression
1460                                         <a class=\"helpLink\" href=\"javascript:popupHelp(1)\">(?)</a>
1461                                         </td>
1462                                         <td width=\"40%\">Caption</td></tr>";
1463                 
1464                 $lnum = 0;
1465                 
1466                 while ($line = db_fetch_assoc($result)) {
1467
1468                         $class = ($lnum % 2) ? "even" : "odd";
1469
1470                         $label_id = $line["id"];
1471                         $edit_label_id = $_GET["id"];
1472
1473                         if ($subop == "edit" && $label_id != $edit_label_id) {
1474                                 $class .= "Grayed";
1475                         }
1476
1477                         print "<tr class=\"$class\" id=\"LILRR-$label_id\">";
1478
1479                         $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
1480                         $line["description"] = htmlspecialchars($line["description"]);
1481
1482                         if (!$edit_label_id || $subop != "edit") {
1483
1484                                 if (!$line["description"]) $line["description"] = "[No caption]";
1485
1486                                 print "<td><input onclick='toggleSelectRow(this);' 
1487                                 type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
1488
1489                                 print "<td><a href=\"javascript:editLabel($label_id);\">" . 
1490                                         $line["sql_exp"] . "</td>";             
1491                                         
1492                                 print "<td><a href=\"javascript:editLabel($label_id);\">" . 
1493                                         $line["description"] . "</td>";                 
1494
1495                         } else if ($label_id != $edit_label_id) {
1496
1497                                 if (!$line["description"]) $line["description"] = "[No description]";
1498
1499                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
1500                                         id=\"LICHK-".$line["id"]."\"></td>";
1501
1502                                 print "<td>".$line["sql_exp"]."</td>";          
1503                                 print "<td>".$line["description"]."</td>";              
1504
1505                         } else {
1506
1507                                 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1508
1509                                 print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
1510                                         "\"></td>";
1511
1512                                 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1513                                         "\"></td>";
1514                                                 
1515                         }
1516                                 
1517                         
1518                         print "</tr>";
1519
1520                         ++$lnum;
1521                 }
1522
1523                 if ($lnum == 0) {
1524                         print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
1525                 }
1526
1527                 print "</table>";
1528
1529                 print "<p>";
1530
1531                 if ($subop == "edit") {
1532                         print "Edit label:
1533                                 <input type=\"submit\" class=\"button\" 
1534                                         onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">
1535                                 <input type=\"submit\" class=\"button\" 
1536                                         onclick=\"javascript:labelEditSave()\" value=\"Save\">";
1537                                         
1538                 } else {
1539
1540                         print "
1541                                 Selection:
1542                         <input type=\"submit\" class=\"button\" 
1543                                 onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
1544                         <input type=\"submit\" class=\"button\" 
1545                                 onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
1546                 }
1547         }
1548
1549         if ($op == "error") {
1550                 print "<div width=\"100%\" align='center'>";
1551                 $msg = $_GET["msg"];
1552                 print $msg;
1553                 print "</div>";
1554         }
1555
1556         if ($op == "help") {
1557                 print "<html><head>
1558                         <title>Tiny Tiny RSS : Help</title>
1559                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
1560                         <script type=\"text/javascript\" src=\"functions.js\"></script>
1561                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
1562                         </head><body>";
1563
1564                 $tid = sprintf("%d", $_GET["tid"]);
1565
1566                 /* FIXME this badly needs real implementation */
1567
1568                 print "<div class='helpResponse'>";
1569
1570                 ?>
1571
1572                 <h1>Help for SQL expressions</h1>
1573
1574                 <h2>Description</h2>
1575
1576                 <p>The &laquo;SQL expression&raquo; is added to WHERE clause of
1577                         view feed query. You can match on ttrss_entries table fields
1578                         and even use subselect to query additional information. This 
1579                         functionality is considered to be advanced and requires basic
1580                         understanding of SQL.</p>
1581                         
1582                 <h2>Examples</h2>
1583
1584                 <pre>unread = true</pre>
1585
1586                 Matches all unread articles
1587
1588                 <pre>title like '%Linux%'</pre>
1589
1590                 Matches all articles which mention Linux in the title. You get the idea.
1591
1592                 <p>See the database schema included in the distribution package for gruesome
1593                 details.</p>
1594
1595                 <?
1596
1597                 print "<div align='center'>
1598                         <a class=\"helpLink\"
1599                         href=\"javascript:window.close()\">(Close this window)</a></div>";
1600
1601                 print "</div>";
1602
1603                 print "</body></html>";
1604
1605         }
1606
1607         if ($op == "dlg") {
1608                 $id = $_GET["id"];
1609                 $param = $_GET["param"];
1610
1611                 if ($id == "quickAddFeed") {
1612                         print "Feed URL: <input 
1613                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1614                         id=\"qafInput\">
1615                         <input class=\"button\"
1616                                 type=\"submit\" onclick=\"javascript:qafAdd()\" value=\"Add feed\">
1617                         <input class=\"button\"
1618                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
1619                                 value=\"Cancel\">";
1620                 }
1621
1622                 if ($id == "quickDelFeed") {
1623
1624                         $param = db_escape_string($param);
1625
1626                         $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$param'");
1627
1628                         if ($result) {
1629
1630                                 $f_title = db_fetch_result($result, 0, "title");
1631                 
1632                                 print "Remove current feed ($f_title)?&nbsp;
1633                                 <input class=\"button\"
1634                                         type=\"submit\" onclick=\"javascript:qfdDelete($param)\" value=\"Remove\">
1635                                 <input class=\"button\"
1636                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
1637                                         value=\"Cancel\">";
1638                         } else {
1639                                 print "Error: Feed $param not found.&nbsp;
1640                                 <input class=\"button\"
1641                                         type=\"submit\" onclick=\"javascript:closeDlg()\" 
1642                                         value=\"Cancel\">";             
1643                         }
1644                 }
1645
1646                 if ($id == "search") {
1647
1648                         print "<input id=\"searchbox\" class=\"extSearch\"                      
1649                         onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1650                         onchange=\"javascript:search()\">
1651                         <select id=\"searchmodebox\">
1652                                 <option selected>All feeds</option>
1653                                 <option>This feed</option>
1654                         </select>               
1655                         <input type=\"submit\" 
1656                                 class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
1657                         <input class=\"button\"
1658                                 type=\"submit\" onclick=\"javascript:closeDlg()\" 
1659                                 value=\"Close\">";
1660
1661                 }
1662
1663         }
1664
1665         // update feeds of all users, may be used anonymously
1666         if ($op == "globalUpdateFeeds") {
1667
1668                 $result = db_query($link, "SELECT id FROM ttrss_users");
1669
1670                 while ($line = db_fetch_assoc($result)) {
1671                         $user_id = $line["id"];
1672 //                      print "<!-- updating feeds of uid $user_id -->";
1673                         update_all_feeds($link, false, $user_id);
1674                 }
1675
1676                 print "<rpc-reply>
1677                         <message msg=\"All feeds updated\"/>
1678                 </rpc-reply>";
1679
1680         }
1681
1682         if ($op == "pref-prefs") {
1683
1684                 $subop = $_REQUEST["subop"];
1685
1686                 if ($subop == "Save configuration") {
1687
1688                         if (WEB_DEMO_MODE) {
1689                                 header("Location: prefs.php");
1690                                 return;
1691                         }
1692
1693                         $_SESSION["prefs_op_result"] = "save-config";
1694
1695                         foreach (array_keys($_POST) as $pref_name) {
1696                         
1697                                 $pref_name = db_escape_string($pref_name);
1698                                 $value = db_escape_string($_POST[$pref_name]);
1699
1700                                 $result = db_query($link, "SELECT type_name 
1701                                         FROM ttrss_prefs,ttrss_prefs_types 
1702                                         WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
1703
1704                                 if (db_num_rows($result) > 0) {
1705
1706                                         $type_name = db_fetch_result($result, 0, "type_name");
1707
1708 //                                      print "$pref_name : $type_name : $value<br>";
1709
1710                                         if ($type_name == "bool") {
1711                                                 if ($value == "1") {
1712                                                         $value = "true";
1713                                                 } else {
1714                                                         $value = "false";
1715                                                 }
1716                                         } else if ($type_name == "integer") {
1717                                                 $value = sprintf("%d", $value);
1718                                         }
1719
1720 //                                      print "$pref_name : $type_name : $value<br>";
1721
1722                                         db_query($link, "UPDATE ttrss_user_prefs SET value = '$value' 
1723                                                 WHERE pref_name = '$pref_name' AND owner_uid = ".$_SESSION["uid"]);
1724
1725                                 }
1726
1727                                 header("Location: prefs.php");
1728
1729                         }
1730
1731                 } else if ($subop == "getHelp") {
1732
1733                         $pref_name = db_escape_string($_GET["pn"]);
1734
1735                         $result = db_query($link, "SELECT help_text FROM ttrss_prefs
1736                                 WHERE pref_name = '$pref_name'");
1737
1738                         if (db_num_rows($result) > 0) {
1739                                 $help_text = db_fetch_result($result, 0, "help_text");
1740                                 print $help_text;
1741                         } else {
1742                                 print "Unknown option: $pref_name";
1743                         }
1744
1745                 } else if ($subop == "Change password") {
1746
1747                         if (WEB_DEMO_MODE) {
1748                                 header("Location: prefs.php");
1749                                 return;
1750                         }
1751
1752                         $old_pw = $_POST["OLD_PASSWORD"];
1753                         $new_pw = $_POST["OLD_PASSWORD"];
1754
1755                         $old_pw_hash = 'SHA1:' . sha1($_POST["OLD_PASSWORD"]);
1756                         $new_pw_hash = 'SHA1:' . sha1($_POST["NEW_PASSWORD"]);
1757
1758                         $active_uid = $_SESSION["uid"];
1759
1760                         if ($old_pw && $new_pw) {
1761
1762                                 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
1763
1764                                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
1765                                         id = '$active_uid' AND (pwd_hash = '$old_pw' OR 
1766                                                 pwd_hash = '$old_pw_hash')");
1767
1768                                 if (db_num_rows($result) == 1) {
1769                                         db_query($link, "UPDATE ttrss_users SET pwd_hash = '$new_pw_hash' 
1770                                                 WHERE id = '$active_uid'");                             
1771
1772                                         $_SESSION["pwd_change_result"] = "ok";
1773                                 } else {
1774                                         $_SESSION["pwd_change_result"] = "failed";                                      
1775                                 }
1776                         }
1777
1778                         header("Location: prefs.php");
1779
1780                 } else if ($subop == "Reset to defaults") {
1781
1782                         if (WEB_DEMO_MODE) {
1783                                 header("Location: prefs.php");
1784                                 return;
1785                         }
1786
1787                         $_SESSION["prefs_op_result"] = "reset-to-defaults";
1788
1789                         if (DB_TYPE == "pgsql") {
1790                                 db_query($link,"UPDATE ttrss_user_prefs 
1791                                         SET value = ttrss_prefs.def_value 
1792                                         WHERE owner_uid = '".$_SESSION["uid"]."' AND
1793                                         ttrss_prefs.pref_name = ttrss_user_prefs.pref_name");
1794                         } else {
1795                                 db_query($link, "DELETE FROM ttrss_user_prefs 
1796                                         WHERE owner_uid = ".$_SESSION["uid"]);
1797                                 initialize_user_prefs($link, $_SESSION["uid"]);
1798                         }
1799
1800                         header("Location: prefs.php");
1801
1802                 } else {
1803
1804                         if (!SINGLE_USER_MODE) {
1805
1806                                 $result = db_query($link, "SELECT id FROM ttrss_users
1807                                         WHERE id = ".$_SESSION["uid"]." AND (pwd_hash = 'password' OR
1808                                                 pwd_hash = 'SHA1:".sha1("password")."')");
1809
1810                                 if (db_num_rows($result) != 0) {
1811                                         print "<div class=\"warning\"> 
1812                                                 Your password is at default value, please change it.
1813                                         </div>";
1814                                 }
1815
1816                                 if ($_SESSION["pwd_change_result"] == "failed") {
1817                                         print "<div class=\"warning\"> 
1818                                                         There was an error while changing your password.
1819                                                 </div>";
1820                                 }
1821
1822                                 if ($_SESSION["pwd_change_result"] == "ok") {
1823                                         print "<div class=\"notice\"> 
1824                                                         Password changed successfully.
1825                                                 </div>";
1826                                 }
1827
1828                                 $_SESSION["pwd_change_result"] = "";
1829
1830                                 if ($_SESSION["prefs_op_result"] == "reset-to-defaults") {
1831                                         print "<div class=\"notice\"> 
1832                                                         Your configuration was reset to defaults.
1833                                                 </div>";
1834                                 }
1835
1836                                 if ($_SESSION["prefs_op_result"] == "save-config") {
1837                                         print "<div class=\"notice\"> 
1838                                                         Your configuration was saved successfully.
1839                                                 </div>";
1840                                 }
1841
1842                                 $_SESSION["prefs_op_result"] = "";
1843
1844                                 print "<form action=\"backend.php\" method=\"POST\">";
1845         
1846                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
1847                                 print "<tr><td colspan='3'><h3>Authentication</h3></tr></td>";
1848         
1849                                 print "<tr><td width=\"40%\">Old password</td>";
1850                                 print "<td><input class=\"editbox\" type=\"password\"
1851                                         name=\"OLD_PASSWORD\"></td></tr>";
1852         
1853                                 print "<tr><td width=\"40%\">New password</td>";
1854                                 
1855                                 print "<td><input class=\"editbox\" type=\"password\"
1856                                         name=\"NEW_PASSWORD\"></td></tr>";
1857         
1858                                 print "</table>";
1859         
1860                                 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
1861         
1862                                 print "<p><input class=\"button\" type=\"submit\" 
1863                                         value=\"Change password\" name=\"subop\">";
1864         
1865                                 print "</form>";
1866
1867                         }
1868
1869                         $result = db_query($link, "SELECT 
1870                                 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
1871                                 section_name,def_value
1872                                 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
1873                                 WHERE type_id = ttrss_prefs_types.id AND 
1874                                         section_id = ttrss_prefs_sections.id AND
1875                                         ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
1876                                         owner_uid = ".$_SESSION["uid"]."
1877                                 ORDER BY section_id,short_desc");
1878
1879                         print "<form action=\"backend.php\" method=\"POST\">";
1880
1881                         $lnum = 0;
1882
1883                         $active_section = "";
1884         
1885                         while ($line = db_fetch_assoc($result)) {
1886
1887                                 if ($active_section != $line["section_name"]) {
1888
1889                                         if ($active_section != "") {
1890                                                 print "</table>";
1891                                         }
1892
1893                                         print "<p><table width=\"100%\" class=\"prefPrefsList\">";
1894                                 
1895                                         $active_section = $line["section_name"];                                
1896                                         
1897                                         print "<tr><td colspan=\"3\"><h3>$active_section</h3></td></tr>";
1898 //                                      print "<tr class=\"title\">
1899 //                                              <td width=\"25%\">Option</td><td>Value</td></tr>";
1900
1901                                         $lnum = 0;
1902                                 }
1903
1904 //                              $class = ($lnum % 2) ? "even" : "odd";
1905
1906                                 print "<tr>";
1907
1908                                 $type_name = $line["type_name"];
1909                                 $pref_name = $line["pref_name"];
1910                                 $value = $line["value"];
1911                                 $def_value = $line["def_value"];
1912                                 $help_text = $line["help_text"];
1913
1914                                 print "<td width=\"40%\" id=\"$pref_name\">" . $line["short_desc"];
1915
1916                                 if ($help_text) print "<div class=\"prefHelp\">$help_text</div>";
1917                                 
1918                                 print "</td>";
1919
1920                                 print "<td>";
1921
1922                                 if ($type_name == "bool") {
1923 //                                      print_select($pref_name, $value, array("true", "false"));
1924
1925                                         if ($value == "true") {
1926                                                 $value = "Yes";
1927                                         } else {
1928                                                 $value = "No";
1929                                         }
1930
1931                                         print_radio($pref_name, $value, array("Yes", "No"));
1932                         
1933                                 } else {
1934                                         print "<input class=\"editbox\" name=\"$pref_name\" value=\"$value\">";
1935                                 }
1936
1937                                 print "</td>";
1938
1939                                 print "</tr>";
1940
1941                                 $lnum++;
1942                         }
1943
1944                         print "</table>";
1945
1946                         print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
1947
1948                         print "<p><input class=\"button\" type=\"submit\" 
1949                                 name=\"subop\" value=\"Save configuration\">";
1950                                 
1951                         print "&nbsp;<input class=\"button\" type=\"submit\" 
1952                                 name=\"subop\" value=\"Reset to defaults\"></p>";
1953
1954                         print "</form>";
1955
1956                 }
1957
1958         }
1959
1960         if ($op == "pref-users") {
1961
1962                 $subop = $_GET["subop"];
1963
1964                 if ($subop == "editSave") {
1965         
1966                         if (!WEB_DEMO_MODE) {
1967
1968                                 $login = db_escape_string($_GET["l"]);
1969                                 $uid = db_escape_string($_GET["id"]);
1970                                 $access_level = sprintf("%d", $_GET["al"]);
1971
1972                                 db_query($link, "UPDATE ttrss_users SET login = '$login', access_level = '$access_level' WHERE id = '$uid'");
1973
1974                         }
1975                 } else if ($subop == "remove") {
1976
1977                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
1978
1979                                 $ids = split(",", $_GET["ids"]);
1980
1981                                 foreach ($ids as $id) {
1982                                         db_query($link, "DELETE FROM ttrss_users WHERE id = '$id' AND id != " . $_SESSION["uid"]);
1983                                         
1984                                 }
1985                         }
1986                 } else if ($subop == "add") {
1987                 
1988                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
1989
1990                                 $login = db_escape_string(trim($_GET["login"]));
1991                                 $tmp_user_pwd = make_password(8);
1992                                 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
1993
1994                                 db_query($link, "INSERT INTO ttrss_users (login,pwd_hash,access_level)
1995                                         VALUES ('$login', '$pwd_hash', 0)");
1996
1997
1998                                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
1999                                         login = '$login' AND pwd_hash = '$pwd_hash'");
2000
2001                                 if (db_num_rows($result) == 1) {
2002
2003                                         $new_uid = db_fetch_result($result, 0, "id");
2004
2005                                         print "<div class=\"notice\">Added user <b>".$_GET["login"].
2006                                                 "</b> with password <b>$tmp_user_pwd</b>.</div>";
2007
2008                                         initialize_user($link, $new_uid);
2009
2010                                 } else {
2011                                 
2012                                         print "<div class=\"warning\">Error while adding user <b>".
2013                                         $_GET["login"].".</b></div>";
2014
2015                                 }
2016                         } 
2017                 } else if ($subop == "resetPass") {
2018
2019                         if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
2020
2021                                 $uid = db_escape_string($_GET["id"]);
2022
2023                                 $result = db_query($link, "SELECT login FROM ttrss_users WHERE id = '$uid'");
2024
2025                                 $login = db_fetch_result($result, 0, "login");
2026                                 $tmp_user_pwd = make_password(8);
2027                                 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
2028
2029                                 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$pwd_hash'
2030                                         WHERE id = '$uid'");
2031
2032                                 print "<div class=\"notice\">Changed password of 
2033                                         user <b>$login</b> to <b>$tmp_user_pwd</b>.</div>";                             
2034
2035                         }
2036                 }
2037
2038                 print "<div class=\"prefGenericAddBox\">
2039                         <input id=\"uadd_box\" size=\"40\">&nbsp;";
2040                         
2041                 print"<input type=\"submit\" class=\"button\" 
2042                         onclick=\"javascript:addUser()\" value=\"Add user\"></div>";
2043
2044                 $result = db_query($link, "SELECT 
2045                                 id,login,access_level,
2046                                 SUBSTRING(last_login,1,16) as last_login
2047                         FROM 
2048                                 ttrss_users
2049                         ORDER by login");
2050
2051                 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
2052
2053                 print "<p><table width=\"100%\" class=\"prefUserList\" id=\"prefUserList\">";
2054
2055                 print "<tr class=\"title\">
2056                                         <td width=\"5%\">Select</td>
2057                                         <td width='30%'>Username</td>
2058                                         <td width='30%'>Access Level</td>
2059                                         <td width='30%'>Last login</td></tr>";
2060                 
2061                 $lnum = 0;
2062                 
2063                 while ($line = db_fetch_assoc($result)) {
2064
2065                         $class = ($lnum % 2) ? "even" : "odd";
2066
2067                         $uid = $line["id"];
2068                         $edit_uid = $_GET["id"];
2069
2070                         if ($uid == $_SESSION["uid"] || ($subop == "edit" && $uid != $edit_uid)) {
2071                                 $class .= "Grayed";
2072                         }
2073                 
2074                         print "<tr class=\"$class\" id=\"UMRR-$uid\">";
2075
2076                         $line["login"] = htmlspecialchars($line["login"]);
2077
2078                         if ($uid == $_SESSION["uid"]) {
2079
2080                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
2081                                         id=\"UMCHK-".$line["id"]."\"></td>";
2082
2083                                 print "<td>".$line["login"]."</td>";            
2084                                 print "<td>".$line["access_level"]."</td>";             
2085
2086                         } else if (!$edit_uid || $subop != "edit") {
2087
2088                                 print "<td><input onclick='toggleSelectRow(this);' 
2089                                 type=\"checkbox\" id=\"UMCHK-$uid\"></td>";
2090
2091                                 print "<td><a href=\"javascript:editUser($uid);\">" . 
2092                                         $line["login"] . "</td>";               
2093                                         
2094                                 print "<td><a href=\"javascript:editUser($uid);\">" . 
2095                                         $line["access_level"] . "</td>";                        
2096
2097                         } else if ($uid != $edit_uid) {
2098
2099                                 print "<td><input disabled=\"true\" type=\"checkbox\" 
2100                                         id=\"UMCHK-".$line["id"]."\"></td>";
2101
2102                                 print "<td>".$line["login"]."</td>";            
2103                                 print "<td>".$line["access_level"]."</td>";             
2104
2105                         } else {
2106
2107                                 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
2108
2109                                 print "<td><input id=\"iedit_ulogin\" value=\"".$line["login"].
2110                                         "\"></td>";
2111
2112                                 print "<td><input id=\"iedit_ulevel\" value=\"".$line["access_level"].
2113                                         "\"></td>";
2114                                                 
2115                         }
2116                                 
2117                         print "<td>".$line["last_login"]."</td>";               
2118                 
2119                         print "</tr>";
2120
2121                         ++$lnum;
2122                 }
2123
2124                 print "</table>";
2125
2126                 print "<p>";
2127
2128                 if ($subop == "edit") {
2129                         print "Edit label:
2130                                 <input type=\"submit\" class=\"button\" 
2131                                         onclick=\"javascript:userEditCancel()\" value=\"Cancel\">
2132                                 <input type=\"submit\" class=\"button\" 
2133                                         onclick=\"javascript:userEditSave()\" value=\"Save\">";
2134                                         
2135                 } else {
2136
2137                         print "
2138                                 Selection:
2139                         <input type=\"submit\" class=\"button\" 
2140                                 onclick=\"javascript:selectedUserDetails()\" value=\"User details\">
2141                         <input type=\"submit\" class=\"button\" 
2142                                 onclick=\"javascript:editSelectedUser()\" value=\"Edit\">
2143                         <input type=\"submit\" class=\"button\" 
2144                                 onclick=\"javascript:removeSelectedUsers()\" value=\"Remove\">
2145                         <input type=\"submit\" class=\"button\" 
2146                                 onclick=\"javascript:resetSelectedUserPass()\" value=\"Reset password\">";
2147
2148                 }
2149         }
2150
2151         if ($op == "user-details") {
2152
2153                 if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
2154                         return;
2155                 }
2156                           
2157 /*              print "<html><head>
2158                         <title>Tiny Tiny RSS : User Details</title>
2159                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
2160                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
2161                         </head><body>"; */
2162
2163                 $uid = sprintf("%d", $_GET["id"]);
2164
2165                 print "<div class='infoBoxContents'>";
2166
2167                 $result = db_query($link, "SELECT login,
2168                         SUBSTRING(last_login,1,16) AS last_login,
2169                         access_level,
2170                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
2171                                 WHERE owner_uid = id) AS stored_articles
2172                         FROM ttrss_users 
2173                         WHERE id = '$uid'");
2174                         
2175                 if (db_num_rows($result) == 0) {
2176                         print "<h1>User not found</h1>";
2177                         return;
2178                 }
2179                 
2180                 print "<h1>User Details</h1>";
2181
2182                 print "<table width='100%'>";
2183
2184                 $login = db_fetch_result($result, 0, "login");
2185                 $last_login = db_fetch_result($result, 0, "last_login");
2186                 $access_level = db_fetch_result($result, 0, "access_level");
2187                 $stored_articles = db_fetch_result($result, 0, "stored_articles");
2188
2189                 print "<tr><td>Username</td><td>$login</td></tr>";
2190                 print "<tr><td>Access level</td><td>$access_level</td></tr>";
2191                 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
2192                 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
2193
2194                 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
2195                         WHERE owner_uid = '$uid'");
2196
2197                 $num_feeds = db_fetch_result($result, 0, "num_feeds");
2198
2199                 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
2200
2201 /*              $result = db_query($link, "SELECT 
2202                         SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size 
2203                         FROM ttrss_user_entries,ttrss_entries 
2204                                 WHERE owner_uid = '$uid' AND ref_id = id");
2205
2206                 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
2207
2208                 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>";  */
2209
2210                 print "</table>";
2211
2212                 print "<h1>Subscribed feeds</h1>";
2213
2214                 $result = db_query($link, "SELECT id,title,feed_url FROM ttrss_feeds
2215                         WHERE owner_uid = '$uid' ORDER BY title");
2216
2217                 print "<ul class=\"nomarks\">";
2218
2219                 while ($line = db_fetch_assoc($result)) {
2220
2221                         $icon_file = ICONS_URL."/".$line["id"].".ico";
2222
2223                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
2224                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
2225                         } else {
2226                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
2227                         }
2228
2229                         print "<li>$feed_icon&nbsp;<a href=\"".$line["feed_url"]."\">".$line["title"]."</a></li>";
2230                 }
2231
2232                 print "</ul>";
2233
2234                 print "</div>";
2235
2236                 print "<div align='center'>
2237                         <input type='submit' class='button'                     
2238                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
2239
2240 //              print "</body></html>"; 
2241
2242         }
2243
2244         if ($op == "feed-details") {
2245
2246                 $feed_id = $_GET["id"];
2247
2248                 $result = db_query($link, 
2249                         "SELECT 
2250                                 title,feed_url,last_updated,icon_url,site_url,
2251                                 (SELECT COUNT(int_id) FROM ttrss_user_entries 
2252                                         WHERE feed_id = id) AS total,
2253                                 (SELECT COUNT(int_id) FROM ttrss_user_entries 
2254                                         WHERE feed_id = id AND unread = true) AS unread,
2255                                 (SELECT COUNT(int_id) FROM ttrss_user_entries 
2256                                         WHERE feed_id = id AND marked = true) AS marked
2257                         FROM ttrss_feeds
2258                         WHERE id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
2259
2260                 if (db_num_rows($result) == 0) return;
2261
2262                 $title = db_fetch_result($result, 0, "title");
2263                 $last_updated = db_fetch_result($result, 0, "last_updated");
2264                 $feed_url = db_fetch_result($result, 0, "feed_url");
2265                 $icon_url = db_fetch_result($result, 0, "icon_url");
2266                 $total = db_fetch_result($result, 0, "total");
2267                 $unread = db_fetch_result($result, 0, "unread");
2268                 $marked = db_fetch_result($result, 0, "marked");
2269                 $site_url = db_fetch_result($result, 0, "site_url");
2270
2271                 $result = db_query($link, "SELECT COUNT(id) AS subscribed
2272                                         FROM ttrss_feeds WHERE feed_url = '$feed_url'");
2273
2274                 $subscribed = db_fetch_result($result, 0, "subscribed");
2275
2276                 print "<div class=\"infoBoxContents\">";
2277
2278                 $icon_file = ICONS_DIR . "/$feed_id.ico";
2279
2280                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
2281                                 $feed_icon = "<img width=\"16\" height=\"16\"
2282                                         src=\"" . ICONS_URL . "/$feed_id.ico\">";
2283                 } else {
2284                         $feed_icon = "";
2285                 }
2286
2287                 print "<h1>$feed_icon $title</h1>";
2288
2289                 print "<table width='100%'>";
2290
2291                 if ($site_url) {
2292                         print "<tr><td width='30%'>Link</td>
2293                                 <td><a href=\"$site_url\">$site_url</a>
2294                                 <a href=\"$feed_url\">(feed)</a></td>
2295                                 </td></tr>";
2296                 } else {
2297                         print "<tr><td width='30%'>Feed URL</td>
2298                                 <td><a href=\"$feed_url\">$feed_url</a></td></tr>";
2299                 }
2300                 print "<tr><td>Last updated</td><td>$last_updated</td></tr>";
2301                 print "<tr><td>Total articles</td><td>$total</td></tr>";
2302                 print "<tr><td>Unread articles</td><td>$unread</td></tr>";
2303                 print "<tr><td>Starred articles</td><td>$marked</td></tr>";
2304                 print "<tr><td>Subscribed users</td><td>$subscribed</td></tr>";
2305
2306                 print "</table>";
2307
2308                 $result = db_query($link, "SELECT title,
2309                         SUBSTRING(updated,1,16) AS updated,unread
2310                         FROM ttrss_entries,ttrss_user_entries
2311                         WHERE ref_id = id AND feed_id = '$feed_id' 
2312                         ORDER BY date_entered DESC LIMIT 5");
2313
2314                 if (db_num_rows($result) > 0) {
2315
2316                         print "<h1>Latest headlines</h1>";
2317
2318                         print "<ul class=\"nomarks\">";
2319         
2320                         while ($line = db_fetch_assoc($result)) {
2321                                 if ($line["unread"] == "t" || $line["unread"] == "1") {
2322                                         $line["title"] = "<b>" . $line["title"] . "</b>";
2323                                 }                               
2324                                 print "<li>" . $line["title"].
2325                                 "&nbsp;<span class=\"insensitive\">(" .$line["updated"].")</span></li>";
2326                         }
2327         
2328                         print "</ul>";
2329         
2330                         print "</div>";
2331         
2332                         print "<div align='center'>
2333                                 <input type='submit' class='button'                     
2334                                 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
2335                 }
2336         }
2337
2338         db_close($link);
2339 ?>
2340
2341 <!-- <?= sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
2342