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