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