]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
implement CDM auto-catchup for articles
[tt-rss.git] / backend.php
1 <?php
2         require_once "sessions.php";
3         require_once "modules/backend-rpc.php";
4         
5         header("Cache-Control: no-cache, must-revalidate");
6         header("Cache-Control: no-cache, must-revalidate");
7
8         header("Pragma: no-cache");
9         header("Expires: -1");
10         
11 /*      if ($_GET["debug"]) {
12                 define('DEFAULT_ERROR_LEVEL', E_ALL);
13         } else {
14                 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
15         }
16         
17         error_reporting(DEFAULT_ERROR_LEVEL); */
18
19         $op = $_REQUEST["op"];
20
21         define('SCHEMA_VERSION', 12);
22
23         require_once "sanity_check.php";
24         require_once "config.php";
25         
26         require_once "db.php";
27         require_once "db-prefs.php";
28         require_once "functions.php";
29
30         $print_exec_time = false;
31
32         if ((!$op || $op == "rpc" || $op == "rss" || $op == "digestSend" ||
33                         $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
34                 header("Content-Type: application/xml; charset=utf-8");
35         } else {
36                 header("Content-Type: text/html; charset=utf-8");
37         }
38
39         if (!$op) {
40                 header("Content-Type: application/xml");
41                 print_error_xml(7); exit;
42         }
43
44         if (!$_SESSION["uid"] && $op != "globalUpdateFeeds" && $op != "rss" && $op != "getUnread") {
45
46                 if ($op == "rpc") {
47                         print_error_xml(6); die;
48                 } else {
49                         print "
50                         <html><body>
51                                 <p>Error: Not logged in.</p>
52                                 <script type=\"text/javascript\">
53                                         if (parent.window != 'undefined') {
54                                                 parent.window.location = \"login.php\";         
55                                         } else {
56                                                 window.location = \"login.php\";
57                                         }
58                                 </script>
59                         </body></html>
60                         ";
61                 }
62                 exit;
63         }
64
65         $purge_intervals = array(
66                 0  => _("Use default"),
67                 -1 => _("Never purge"),
68                 5  => _("1 week old"),
69                 14 => _("2 weeks old"),
70                 31 => _("1 month old"),
71                 60 => _("2 months old"),
72                 90 => _("3 months old"));
73
74         $update_intervals = array(
75                 0   => _("Use default"),
76                 -1  => _("Disable updates"),
77                 30  => _("Each 30 minutes"),
78                 60  => _("Hourly"),
79                 240 => _("Each 4 hours"),
80                 720 => _("Each 12 hours"),
81                 1440 => _("Daily"),
82                 10080 => _("Weekly"));
83
84
85         $access_level_names = array(
86                 0 => _("User"), 
87                 10 => _("Administrator"));
88
89         require_once "modules/pref-prefs.php";
90         require_once "modules/popup-dialog.php";
91         require_once "modules/help.php";
92         require_once "modules/pref-feeds.php";
93         require_once "modules/pref-filters.php";
94         require_once "modules/pref-labels.php";
95         require_once "modules/pref-users.php";
96         require_once "modules/pref-feed-browser.php"; 
97
98         $script_started = getmicrotime();
99
100         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
101
102         if (!$link) {
103                 if (DB_TYPE == "mysql") {
104                         print mysql_error();
105                 }
106                 // PG seems to display its own errors just fine by default.             
107                 return;
108         }
109
110         if (DB_TYPE == "pgsql") {
111                 pg_query("set client_encoding = 'utf-8'");
112         }
113
114         if (!sanity_check($link)) { return; }
115
116         if ($op == "rpc") {
117                 handle_rpc_request($link);
118         }
119         
120         if ($op == "feeds") {
121
122                 $tags = $_GET["tags"];
123
124                 $subop = $_GET["subop"];
125
126                 if ($subop == "catchupAll") {
127                         db_query($link, "UPDATE ttrss_user_entries SET 
128                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
129                 }
130
131                 if ($subop == "collapse") {
132                         $cat_id = db_escape_string($_GET["cid"]);
133
134                         db_query($link, "UPDATE ttrss_feed_categories SET
135                                 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " . 
136                                 $_SESSION["uid"]);
137                         return;
138                 }
139
140                 outputFeedList($link, $tags);
141
142         }
143
144         if ($op == "view") {
145
146                 $id = db_escape_string($_GET["id"]);
147                 $feed_id = db_escape_string($_GET["feed"]);
148
149                 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
150                         WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
151
152                 if (db_num_rows($result) == 1) {
153                         $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
154                 } else {
155                         $rtl_content = false;
156                 }
157
158                 if ($rtl_content) {
159                         $rtl_tag = "dir=\"RTL\"";
160                         $rtl_class = "RTL";
161                 } else {
162                         $rtl_tag = "";
163                         $rtl_class = "";
164                 }
165
166                 $result = db_query($link, "UPDATE ttrss_user_entries 
167                         SET unread = false,last_read = NOW() 
168                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
169
170                 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
171                         SUBSTRING(updated,1,16) as updated,
172                         (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
173                         num_comments,
174                         author
175                         FROM ttrss_entries,ttrss_user_entries
176                         WHERE   id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
177
178                 if ($result) {
179
180                         $link_target = "";
181
182                         if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
183                                 $link_target = "target=\"_new\"";
184                         }
185
186                         $line = db_fetch_assoc($result);
187
188                         if ($line["icon_url"]) {
189                                 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
190                         } else {
191                                 $feed_icon = "&nbsp;";
192                         }
193
194 /*                      if ($line["comments"] && $line["link"] != $line["comments"]) {
195                                 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
196                         } else {
197                                 $entry_comments = "";
198                         } */
199
200                         $num_comments = $line["num_comments"];
201                         $entry_comments = "";
202
203                         if ($num_comments > 0) {
204                                 if ($line["comments"]) {
205                                         $comments_url = $line["comments"];
206                                 } else {
207                                         $comments_url = $line["link"];
208                                 }
209                                 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
210                         } else {
211                                 if ($line["comments"] && $line["link"] != $line["comments"]) {
212                                         $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
213                                 }                               
214                         }
215
216                         print "<div class=\"postReply\">";
217
218                         print "<div class=\"postHeader\">";
219
220                         $entry_author = $line["author"];
221
222                         if ($entry_author) {
223                                 $entry_author = _(" - by ") . $entry_author;
224                         }
225
226                         $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), 
227                                 strtotime($line["updated"]));
228                 
229                         print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
230
231                         if ($line["link"]) {
232                                 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" . 
233                                         $line["title"] . "</a>$entry_author</div>";
234                         } else {
235                                 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
236                         }
237
238                         $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
239                                 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
240                                 ORDER BY tag_name");
241         
242                         $tags_str = "";
243                         $f_tags_str = "";
244
245                         $num_tags = 0;
246
247                         while ($tmp_line = db_fetch_assoc($tmp_result)) {
248                                 $num_tags++;
249                                 $tag = $tmp_line["tag_name"];                           
250                                 $tag_str = "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; 
251                                 
252                                 if ($num_tags == 6) {
253                                         $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
254                                 } else if ($num_tags < 6) {
255                                         $tags_str .= $tag_str;
256                                 }
257                                 $f_tags_str .= $tag_str;
258                         }
259
260                         $tags_str = preg_replace("/, $/", "", $tags_str);
261                         $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
262
263                         if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
264
265                         if (!$tags_str) $tags_str = '<span class="tagList">'._('no tags').'</span>';
266
267                         print "<div style='float : right'>$tags_str 
268                                 <a title=\"Edit tags for this article\" 
269                                         href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
270                                 <div clear='both'>$entry_comments</div>";
271
272                         print "</div>";
273
274                         print "<div class=\"postIcon\">" . $feed_icon . "</div>";
275                         print "<div class=\"postContent\">";
276                         
277                         if (db_num_rows($tmp_result) > 0) {
278                                 print "<div id=\"allEntryTags\">"._('Tags:')."$f_tags_str</div>";
279                         }
280
281                         if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
282                                 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
283                         }
284
285                         $line["content"] = sanitize_rss($line["content"]);
286
287                         print $line["content"] . "</div>";
288                         
289                         print "</div>";
290
291                 }
292         }
293
294         if ($op == "viewfeed") {
295
296                 $feed = db_escape_string($_GET["feed"]);
297                 $subop = db_escape_string($_GET["subop"]);
298                 $view_mode = db_escape_string($_GET["view_mode"]);
299                 $limit = db_escape_string($_GET["limit"]);
300                 $cat_view = db_escape_string($_GET["cat"]);
301                 $next_unread_feed = db_escape_string($_GET["nuf"]);
302                 $offset = db_escape_string($_GET["skip"]);
303
304                 if (!$offset) $offset = 0;
305
306                 if ($subop == "undefined") $subop = "";
307
308                 if ($subop == "CatchupSelected") {
309                         $ids = split(",", db_escape_string($_GET["ids"]));
310                         $cmode = sprintf("%d", $_GET["cmode"]);
311
312                         catchupArticlesById($link, $ids, $cmode);
313                 }
314
315                 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
316                         update_generic_feed($link, $feed, $cat_view);
317                 }
318
319                 if ($subop == "MarkAllRead")  {
320                         catchup_feed($link, $feed, $cat_view);
321
322                         if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
323                                 if ($next_unread_feed) {
324                                         $feed = $next_unread_feed;
325                                 }
326                         }
327                 }
328
329                 if ($feed_id > 0) {             
330                         $result = db_query($link,
331                                 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
332                 
333                         if (db_num_rows($result) == 0) {
334                                 print "<div align='center'>"._('Feed not found.')."</div>";
335                                 return;
336                         }
337                 }
338
339                 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
340         
341                         $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
342                                 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
343
344                         if (db_num_rows($result) == 1) {
345                                 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
346                         } else {
347                                 $rtl_content = false;
348                         }
349         
350                         if ($rtl_content) {
351                                 $rtl_tag = "dir=\"RTL\"";
352                         } else {
353                                 $rtl_tag = "";
354                         }
355                 } else {
356                         $rtl_tag = "";
357                         $rtl_content = false;
358                 }
359
360                 $script_dt_add = get_script_dt_add();
361
362 /*              print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">  
363                         <script type=\"text/javascript\" src=\"prototype.js\"></script>
364                         <script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
365                         <script type=\"text/javascript\" src=\"viewfeed.js?$script_dt_add\"></script>
366                         <!--[if gte IE 5.5000]>
367                         <script type=\"text/javascript\" src=\"pngfix.js\"></script>
368                         <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
369                         <![endif]-->
370                         </head><body $rtl_tag>
371                         <script type=\"text/javascript\">
372                         if (document.addEventListener) {
373                                 document.addEventListener(\"DOMContentLoaded\", init, null);
374                         }
375                         window.onload = init;
376                         </script>"; */
377
378                 /// START /////////////////////////////////////////////////////////////////////////////////
379
380                 $search = db_escape_string($_GET["query"]);
381                 $search_mode = db_escape_string($_GET["search_mode"]);
382                 $match_on = db_escape_string($_GET["match_on"]);
383
384                 if (!$match_on) {
385                         $match_on = "both";
386                 }
387
388                 $real_offset = $offset * $limit;
389
390                 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, 
391                         $search, $search_mode, $match_on, false, $real_offset);
392
393                 $result = $qfh_ret[0];
394                 $feed_title = $qfh_ret[1];
395                 $feed_site_url = $qfh_ret[2];
396                 $last_error = $qfh_ret[3];
397                 
398                 /// STOP //////////////////////////////////////////////////////////////////////////////////
399
400                 print "<div id=\"headlinesContainer\" $rtl_tag>";
401
402                 if (!$result) {
403                         print "<div align='center'>"._("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
404                         return;
405                 }
406
407                 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false, 
408                         $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode, $offset);
409
410                 print "<div id=\"headlinesInnerContainer\">";
411
412                 if (db_num_rows($result) > 0) {
413
414 #                       print "\{$offset}";
415
416                         if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
417                                 print "<table class=\"headlinesList\" id=\"headlinesList\" 
418                                         cellspacing=\"0\" width=\"100%\">";
419                         }
420
421                         $lnum = 0;
422         
423                         error_reporting (DEFAULT_ERROR_LEVEL);
424         
425                         $num_unread = 0;
426         
427                         while ($line = db_fetch_assoc($result)) {
428
429                                 $class = ($lnum % 2) ? "even" : "odd";
430         
431                                 $id = $line["id"];
432                                 $feed_id = $line["feed_id"];
433         
434                                 if ($line["last_read"] == "" && 
435                                                 ($line["unread"] != "t" && $line["unread"] != "1")) {
436         
437                                         $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" 
438                                                 alt=\"Updated\">";
439                                 } else {
440                                         $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" 
441                                                 alt=\"Updated\">";
442                                 }
443         
444                                 if ($line["unread"] == "t" || $line["unread"] == "1") {
445                                         $class .= "Unread";
446                                         ++$num_unread;
447                                         $is_unread = true;
448                                 } else {
449                                         $is_unread = false;
450                                 }
451         
452                                 if ($line["marked"] == "t" || $line["marked"] == "1") {
453                                         $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\" 
454                                                 alt=\"Reset mark\" onclick='javascript:toggleMark($id)'>";
455                                 } else {
456                                         $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\" 
457                                                 alt=\"Set mark\" onclick='javascript:toggleMark($id)'>";
458                                 }
459
460 #                               $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
461 #                                       $line["title"] . "</a>";
462
463                                 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
464                                         $line["title"] . "</a>";
465
466 #                               $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
467 #                                       $line["title"] . "</a>";
468
469                                 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
470                                         $updated_fmt = smart_date_time(strtotime($line["updated"]));
471                                 } else {
472                                         $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
473                                         $updated_fmt = date($short_date, strtotime($line["updated"]));
474                                 }                               
475
476                                 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
477                                         $content_preview = truncate_string(strip_tags($line["content_preview"]), 
478                                                 100);
479                                 }
480
481                                 $entry_author = $line["author"];
482
483                                 if ($entry_author) {
484                                         $entry_author = " - by $entry_author";
485                                 }
486
487                                 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
488                                         
489                                         print "<tr class='$class' id='RROW-$id'>";
490                 
491                                         print "<td class='hlUpdatePic'>$update_pic</td>";
492                 
493                                         print "<td class='hlSelectRow'>
494                                                 <input type=\"checkbox\" onclick=\"toggleSelectRow(this)\"
495                                                         class=\"feedCheckBox\" id=\"RCHK-$id\">
496                                                 </td>";
497                 
498                                         print "<td class='hlMarkedPic'>$marked_pic</td>";
499
500                                         if ($line["feed_title"]) {                      
501                                                 print "<td class='hlContent'>$content_link</td>";
502                                                 print "<td class='hlFeed'>
503                                                         <a href=\"javascript:viewfeed($feed_id, '', false)\">".
504                                                                 $line["feed_title"]."</a>&nbsp;</td>";
505                                         } else {                        
506                                                 print "<td class='hlContent' valign='middle'>";
507
508                                                 print "<a href=\"javascript:view($id,$feed_id);\">" .
509                                                         $line["title"];
510
511                                                 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
512                                                         if ($content_preview) {
513                                                                 print "<span class=\"contentPreview\"> - $content_preview</span>";
514                                                         }
515                                                 }
516                 
517                                                 print "</a>";
518                                                 print "</td>";
519                                         }
520                                         
521                                         print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
522                 
523                                         print "</tr>";
524
525                                 } else {
526                                         
527                                         if ($is_unread) {
528                                                 $add_class = "Unread";
529                                         } else {
530                                                 $add_class = "";
531                                         }       
532                                         
533                                         print "<div class=\"cdmArticle$add_class\" id=\"RROW-$id\">";
534
535                                         print "<div class=\"cdmHeader\">";
536
537                                         print "<div style=\"float : right\">$updated_fmt $id
538                                                 <!-- <a class=\"cdmToggleLink\"
539                                                         href=\"javascript:toggleUnread($id)\">Toggle unread</a> -->
540                                         </div>";
541                                         
542                                         print "<a class=\"title\" 
543                                                 onclick=\"javascript:toggleUnread($id, 0)\"
544                                                 target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
545
546                                         print $entry_author;
547
548                                         if ($line["feed_title"]) {      
549                                                 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
550                                         }
551
552                                         print "</div>";
553
554                                         print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
555
556                                         print "<div style=\"float : right\">$marked_pic</div>
557                                                 <div lass=\"cdmFooter\">
558                                                         <input type=\"checkbox\" onclick=\"toggleSelectRowById(this, 
559                                                         'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\"></div>";
560
561 #                                       print "<div align=\"center\"><a class=\"cdmToggleLink\"
562 #                                                       href=\"javascript:toggleUnread($id)\">
563 #                                                       Toggle unread</a></div>";
564
565                                         print "</div>"; 
566
567                                 }                               
568         
569                                 ++$lnum;
570                         }
571
572                         if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {                        
573                                 print "</table>";
574                         }
575
576 //                      print_headline_subtoolbar($link, 
577 //                              "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
578
579
580                 } else {
581                         print "<div class='whiteBox'>"._('No articles found.')."</div>";
582                 }
583
584                 print "</div>";
585
586                 print "</div>";
587         }
588
589         if ($op == "pref-feeds") {
590                 module_pref_feeds($link);
591         }
592
593         if ($op == "pref-filters") {
594                 module_pref_filters($link);
595         }
596
597         if ($op == "pref-labels") {
598                 module_pref_labels($link);
599         }
600
601         if ($op == "pref-prefs") {
602                 module_pref_prefs($link);
603         }
604
605         if ($op == "pref-users") {
606                 module_pref_users($link);
607         }
608
609         if ($op == "help") {
610                 module_help($link);
611         }
612
613         if ($op == "dlg") {
614                 module_popup_dialog($link);
615         }
616
617         // update feeds of all users, may be used anonymously
618         if ($op == "globalUpdateFeeds") {
619
620                 $result = db_query($link, "SELECT id FROM ttrss_users");
621
622                 while ($line = db_fetch_assoc($result)) {
623                         $user_id = $line["id"];
624 //                      print "<!-- updating feeds of uid $user_id -->";
625                         update_all_feeds($link, false, $user_id);
626                 }
627
628                 print "<rpc-reply>
629                         <message msg=\"All feeds updated\"/>
630                 </rpc-reply>";
631
632         }
633
634         if ($op == "user-details") {
635
636                 if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
637                         return;
638                 }
639                           
640 /*              print "<html><head>
641                         <title>Tiny Tiny RSS : User Details</title>
642                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
643                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
644                         </head><body>"; */
645
646                 $uid = sprintf("%d", $_GET["id"]);
647
648                 print "<div id=\"infoBoxTitle\">User details</div>";
649
650                 print "<div class='infoBoxContents'>";
651
652                 $result = db_query($link, "SELECT login,
653                         SUBSTRING(last_login,1,16) AS last_login,
654                         access_level,
655                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
656                                 WHERE owner_uid = id) AS stored_articles
657                         FROM ttrss_users 
658                         WHERE id = '$uid'");
659                         
660                 if (db_num_rows($result) == 0) {
661                         print "<h1>User not found</h1>";
662                         return;
663                 }
664                 
665 #               print "<h1>User Details</h1>";
666
667                 $login = db_fetch_result($result, 0, "login");
668
669 #               print "<h1>$login</h1>";
670
671                 print "<table width='100%'>";
672
673                 $last_login = date(get_pref($link, 'LONG_DATE_FORMAT'),
674                         strtotime(db_fetch_result($result, 0, "last_login")));
675                 $access_level = db_fetch_result($result, 0, "access_level");
676                 $stored_articles = db_fetch_result($result, 0, "stored_articles");
677
678 #               print "<tr><td>Username</td><td>$login</td></tr>";
679 #               print "<tr><td>Access level</td><td>$access_level</td></tr>";
680                 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
681                 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
682
683                 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
684                         WHERE owner_uid = '$uid'");
685
686                 $num_feeds = db_fetch_result($result, 0, "num_feeds");
687
688                 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
689
690 /*              $result = db_query($link, "SELECT 
691                         SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size 
692                         FROM ttrss_user_entries,ttrss_entries 
693                                 WHERE owner_uid = '$uid' AND ref_id = id");
694
695                 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
696
697                 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>";  */
698
699                 print "</table>";
700
701                 print "<h1>Subscribed feeds</h1>";
702
703                 $result = db_query($link, "SELECT id,title,site_url FROM ttrss_feeds
704                         WHERE owner_uid = '$uid' ORDER BY title");
705
706                 print "<ul class=\"userFeedList\">";
707
708                 $row_class = "odd";
709
710                 while ($line = db_fetch_assoc($result)) {
711
712                         $icon_file = ICONS_URL."/".$line["id"].".ico";
713
714                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
715                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
716                         } else {
717                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
718                         }
719
720                         print "<li class=\"$row_class\">$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
721
722                         $row_class = toggleEvenOdd($row_class);
723
724                 }
725
726                 if (db_num_rows($result) < $num_feeds) {
727                          // FIXME - add link to show ALL subscribed feeds here somewhere
728                         print "<li><img 
729                                 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
730                 }
731                 
732                 print "</ul>";
733
734                 print "</div>";
735
736                 print "<div align='center'>
737                         <input type='submit' class='button'                     
738                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
739
740 //              print "</body></html>"; 
741
742         }
743
744         if ($op == "pref-feed-browser") {
745                 module_pref_feed_browser($link);
746         }
747
748         if ($op == "rss") {
749                 $feed = db_escape_string($_GET["id"]);
750                 $user = db_escape_string($_GET["user"]);
751                 $pass = db_escape_string($_GET["pass"]);
752                 $is_cat = $_GET["is_cat"] != false;
753
754                 $search = db_escape_string($_GET["q"]);
755                 $match_on = db_escape_string($_GET["m"]);
756                 $search_mode = db_escape_string($_GET["smode"]);
757
758                 if (!$_SESSION["uid"] && $user && $pass) {
759                         authenticate_user($link, $user, $pass);
760                 }
761
762                 if ($_SESSION["uid"] ||
763                         http_authenticate_user($link)) {
764
765                                 generate_syndicated_feed($link, $feed, $is_cat, 
766                                         $search, $search_mode, $match_on);
767                 }
768         }
769
770         if ($op == "labelFromSearch") {
771                 $search = db_escape_string($_GET["search"]);
772                 $search_mode = db_escape_string($_GET["smode"]);
773                 $match_on = db_escape_string($_GET["match"]);
774                 $is_cat = db_escape_string($_GET["is_cat"]);
775                 $title = db_escape_string($_GET["title"]);
776                 $feed = sprintf("%d", $_GET["feed"]);
777
778                 $label_qparts = array();
779
780                 $search_expr = getSearchSql($search, $match_on);
781
782                 if ($is_cat) {
783                         if ($feed != 0) {
784                                 $search_expr .= " AND ttrss_feeds.cat_id = $feed ";
785                         } else {
786                                 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
787                         }
788                 } else {
789                         if ($search_mode == "all_feeds") {
790                                 // NOOP
791                         } else if ($search_mode == "this_cat") {
792
793                                 $tmp_result = db_query($link, "SELECT cat_id
794                                         FROM ttrss_feeds WHERE id = '$feed'");
795
796                                 $cat_id = db_fetch_result($tmp_result, 0, "cat_id");
797
798                                 if ($cat_id > 0) {
799                                         $search_expr .= " AND ttrss_feeds.cat_id = $cat_id ";
800                                 } else {
801                                         $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
802                                 }
803                         } else {
804                                 $search_expr .= " AND ttrss_feeds.id = $feed ";
805                         }
806
807                 }
808
809                 $search_expr = db_escape_string($search_expr);
810
811                 print $search_expr;
812
813                 if ($title) {
814                         $result = db_query($link,
815                                 "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
816                                 VALUES ('$search_expr', '$title', '".$_SESSION["uid"]."')");
817                 }
818         }
819
820         if ($op == "getUnread") {
821                 $login = db_escape_string($_GET["login"]);
822
823                 header("Content-Type: text/plain; charset=utf-8");
824
825                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
826
827                 if (db_num_rows($result) == 1) {
828                         $uid = db_fetch_result($result, 0, "id");
829                         print getGlobalUnread($link, $uid);
830                 } else {
831                         print "-1;User not found";
832                 }
833
834                 $print_exec_time = false;
835         }
836
837         if ($op == "digestTest") {
838                 header("Content-Type: text/plain");
839                 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
840                 $print_exec_time = false;
841
842         }
843
844         if ($op == "digestSend") {
845                 header("Content-Type: text/plain");
846                 send_headlines_digests($link);
847                 $print_exec_time = false;
848
849         }
850
851         db_close($link);
852 ?>
853
854 <?php if ($print_exec_time) { ?>
855 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
856 <?php } ?>