]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
viewfeed: consider bw_limit situation the same as csync when sending counters with...
[tt-rss.git] / backend.php
1 <?php
2         error_reporting(E_ERROR | E_WARNING | E_PARSE);
3
4         /* remove ill effects of magic quotes */
5
6         if (get_magic_quotes_gpc()) {
7                 $_GET = array_map('stripslashes', $_GET);
8                 $_POST = array_map('stripslashes', $_POST);
9                 $_REQUEST = array_map('stripslashes', $_REQUEST);
10                 $_COOKIE = array_map('stripslashes', $_COOKIE);
11         }
12
13         require_once "sessions.php";
14         require_once "modules/backend-rpc.php";
15
16 /*      if ($_GET["debug"]) {
17                 define('DEFAULT_ERROR_LEVEL', E_ALL);
18         } else {
19                 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
20         }
21         
22         error_reporting(DEFAULT_ERROR_LEVEL); */
23
24         require_once "sanity_check.php";
25         require_once "config.php";
26         
27         require_once "db.php";
28         require_once "db-prefs.php";
29         require_once "functions.php";
30
31         no_cache_incantation();
32
33         if (ENABLE_TRANSLATIONS == true) { 
34                 startup_gettext();
35         }
36
37         $script_started = getmicrotime();
38
39         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
40
41         if (!$link) {
42                 if (DB_TYPE == "mysql") {
43                         print mysql_error();
44                 }
45                 // PG seems to display its own errors just fine by default.             
46                 return;
47         }
48
49         if (DB_TYPE == "pgsql") {
50                 pg_query("set client_encoding = 'UTF-8'");
51                 pg_set_client_encoding("UNICODE");
52         } else {
53                 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
54                         db_query($link, "SET NAMES " . MYSQL_CHARSET);
55 //                      db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
56                 }
57         }
58
59         $op = $_REQUEST["op"];
60
61         $print_exec_time = false;
62
63         if ((!$op || $op == "rpc" || $op == "rss" || $op == "view" || 
64                         $op == "digestSend" || $op == "viewfeed" || $op == "publish" ||
65                         $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
66                                 header("Content-Type: application/xml; charset=utf-8");
67
68                                 if (ENABLE_GZIP_OUTPUT) {
69                                         ob_start("ob_gzhandler");
70                                 }
71                                 
72                 } else {
73                 if (!$_REQUEST["noxml"]) {
74                         header("Content-Type: text/html; charset=utf-8");
75                 } else {
76                         header("Content-Type: text/plain; charset=utf-8");
77                 }
78         }
79
80         if (!$op) {
81                 header("Content-Type: application/xml");
82                 print_error_xml(7); exit;
83         }
84         
85         if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" 
86                         && $op != "rss" && $op != "getUnread" && $op != "publish") {
87
88                 if ($op == "rpc" || $op == "viewfeed" || $op == "view") {
89                         print_error_xml(6); die;
90                 } else {
91                         print "
92                         <html><body>
93                                 <p>Error: Not logged in.</p>
94                                 <script type=\"text/javascript\">
95                                         if (parent.window != 'undefined') {
96                                                 parent.window.location = \"tt-rss.php\";                
97                                         } else {
98                                                 window.location = \"tt-rss.php\";
99                                         }
100                                 </script>
101                         </body></html>
102                         ";
103                 }
104                 exit;
105         }
106
107         $purge_intervals = array(
108                 0  => __("Use default"),
109                 -1 => __("Never purge"),
110                 5  => __("1 week old"),
111                 14 => __("2 weeks old"),
112                 31 => __("1 month old"),
113                 60 => __("2 months old"),
114                 90 => __("3 months old"));
115
116         $update_intervals = array(
117                 0   => __("Use default"),
118                 -1  => __("Disable updates"),
119                 15  => __("Each 15 minutes"),
120                 30  => __("Each 30 minutes"),
121                 60  => __("Hourly"),
122                 240 => __("Each 4 hours"),
123                 720 => __("Each 12 hours"),
124                 1440 => __("Daily"),
125                 10080 => __("Weekly"));
126
127         $update_methods = array(
128                 0   => __("Use default"),
129                 1   => __("Magpie"),
130                 2   => __("SimplePie"));
131
132         if (ENABLE_SIMPLEPIE) {
133                 $update_methods[0] .= ' (SimplePie)';
134         } else {
135                 $update_methods[0] .= ' (Magpie)';
136         }
137
138         $access_level_names = array(
139                 0 => __("User"), 
140                 5 => __("Power User"),
141                 10 => __("Administrator"));
142
143         require_once "modules/pref-prefs.php";
144         require_once "modules/popup-dialog.php";
145         require_once "modules/help.php";
146         require_once "modules/pref-feeds.php";
147         require_once "modules/pref-filters.php";
148         require_once "modules/pref-labels.php";
149         require_once "modules/pref-users.php";
150         require_once "modules/pref-feed-browser.php"; 
151
152         if (!sanity_check($link)) { return; }
153
154         switch($op) { // Select action according to $op value.
155                 case "rpc":
156                         // Handle remote procedure calls.
157                         handle_rpc_request($link);
158                 break; // rpc
159
160                 case "feeds":
161                         if (ENABLE_GZIP_OUTPUT) {
162                                 ob_start("ob_gzhandler");
163                         }
164
165                         $tags = $_GET["tags"];
166
167                         $subop = $_GET["subop"];
168
169                         switch($subop) {
170                                 case "catchupAll":
171                                         db_query($link, "UPDATE ttrss_user_entries SET 
172                                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
173                                 break;
174
175                                 case "collapse":
176                                         $cat_id = db_escape_string($_GET["cid"]);
177
178                                         db_query($link, "UPDATE ttrss_feed_categories SET
179                                                 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " . 
180                                                 $_SESSION["uid"]);
181                                         return;
182                                 break;
183                         }
184
185                         outputFeedList($link, $tags);
186                 break; // feeds
187
188                 case "view":
189
190                         $id = db_escape_string($_GET["id"]);
191                         $feed_id = db_escape_string($_GET["feed"]);
192                         $cids = split(",", db_escape_string($_GET["cids"]));
193                         $mode = db_escape_string($_GET["mode"]);
194                         $omode = db_escape_string($_GET["omode"]);
195
196                         $csync = $_GET["csync"];
197
198                         print "<reply>";
199
200                         // in prefetch mode we only output requested cids, main article 
201                         // just gets marked as read (it already exists in client cache)
202
203                         if ($mode == "") {
204                                 outputArticleXML($link, $id, $feed_id);
205                         } else {
206                                 catchupArticleById($link, $id, 0);
207                         }
208
209                         if (!$_SESSION["bw_limit"]) {
210                                 foreach ($cids as $cid) {
211                                         if ($cid) {
212                                                 outputArticleXML($link, $cid, $feed_id, false);
213                                         }
214                                 }
215                         }
216
217                         if ($mode == "prefetch" && $csync) {
218                                 print "<counters>";
219                                 getAllCounters($link, $omode);
220                                 print "</counters>";
221                         }
222
223                         print "</reply>";
224                 break; // view
225
226                 case "viewfeed":
227
228                         $print_exec_time = true;
229                         $timing_info = getmicrotime();
230
231                         print "<reply>";
232
233                         if ($_GET["debug"]) $timing_info = print_checkpoint("0", $timing_info);
234
235                         $omode = db_escape_string($_GET["omode"]);
236
237                         $feed = db_escape_string($_GET["feed"]);
238                         $subop = db_escape_string($_GET["subop"]);
239                         $view_mode = db_escape_string($_GET["view_mode"]);
240                         $limit = db_escape_string($_GET["limit"]);
241                         $cat_view = db_escape_string($_GET["cat"]);
242                         $next_unread_feed = db_escape_string($_GET["nuf"]);
243                         $offset = db_escape_string($_GET["skip"]);
244                         $vgroup_last_feed = db_escape_string($_GET["vgrlf"]);
245                         $csync = $_GET["csync"];
246
247                         set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
248                         set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
249
250                         if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
251                                 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
252                                         WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
253                         }
254
255                         print "<headlines id=\"$feed\"><![CDATA[";
256
257                         if ($feed == -4) {
258
259                                 generate_dashboard_feed($link);
260
261                         } else {
262
263                                 $ret = outputHeadlinesList($link, $feed, $subop, 
264                                         $view_mode, $limit, $cat_view, $next_unread_feed, $offset, 
265                                         $vgroup_last_feed);
266         
267                                 $topmost_article_ids = $ret[0];
268                                 $headlines_count = $ret[1];
269                                 $returned_feed = $ret[2];
270                                 $disable_cache = $ret[3];
271                                 $vgroup_last_feed = $ret[4];
272         
273                                 print "]]></headlines>";
274         
275                                 print "<headlines-count value=\"$headlines_count\"/>";
276                                 print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
277
278                                 $headlines_unread = getFeedUnread($link, $returned_feed);
279         
280                                 print "<headlines-unread value=\"$headlines_unread\"/>";
281                                 printf("<disable-cache value=\"%d\"/>", $disable_cache);
282         
283                                 if ($_GET["debug"]) $timing_info = print_checkpoint("10", $timing_info);
284         
285                                 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
286                                         print "<articles>";
287                                         foreach ($topmost_article_ids as $id) {
288                                                 outputArticleXML($link, $id, $feed, false);
289                                         }
290                                         print "</articles>";
291                                 }
292                         }
293
294                         if ($_GET["debug"]) $timing_info = print_checkpoint("20", $timing_info);
295
296                         $viewfeed_ctr_interval = 300;
297
298                         if ($csync || $_SESSION["bw_limit"]) {
299                                 $viewfeed_ctr_interval = 60;
300                         }
301
302                         if (time() - $_SESSION["get_all_counters_stamp"] > $viewfeed_ctr_interval) {
303                                 print "<counters>";
304                                 getAllCounters($link, $omode, $feed);
305                                 print "</counters>";
306                         }
307
308                         if ($_GET["debug"]) $timing_info = print_checkpoint("30", $timing_info);
309
310                         print_runtime_info($link);
311
312                         print "</reply>";
313                 break; // viewfeed
314
315                 case "pref-feeds":
316                         module_pref_feeds($link);
317                 break; // pref-feeds
318
319                 case "pref-filters":
320                         module_pref_filters($link);
321                 break; // pref-filters
322
323                 case "pref-labels":
324                         module_pref_labels($link);
325                 break; // pref-labels
326
327                 case "pref-prefs":
328                         module_pref_prefs($link);
329                 break; // pref-prefs
330
331                 case "pref-users":
332                         module_pref_users($link);
333                 break; // prefs-users
334
335                 case "help":
336                         module_help($link);
337                 break; // help
338
339                 case "dlg":
340                         module_popup_dialog($link);
341                 break; // dlg
342
343                 case "pref-pub-items":
344                         module_pref_pub_items($link);
345                 break; // pref-pub-items
346
347                 case "globalUpdateFeeds":
348                         // update feeds of all users, may be used anonymously
349
350                         print "<!--";
351                         // Update all feeds needing a update.
352                         update_daemon_common($link, 0, true, true);
353                         print " -->";
354
355                         // FIXME : old feed update way. To be removed.
356                         //$result = db_query($link, "SELECT id FROM ttrss_users");
357
358                         //while ($line = db_fetch_assoc($result)) {
359                         //      $user_id = $line["id"];
360                         //      print "<!-- updating feeds of uid $user_id -->";
361                         //      update_all_feeds($link, false, $user_id);
362                         //}
363
364                         print "<rpc-reply>
365                                 <message msg=\"All feeds updated\"/>
366                         </rpc-reply>";
367                 break; // globalUpdateFeeds
368
369                 case "user-details":
370
371                         if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
372                                 return;
373                         }
374
375                         /*
376                         print "<html><head>
377                                 <title>Tiny Tiny RSS : User Details</title>
378                                 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
379                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
380                                 </head><body>";
381                         */
382
383                         $uid = sprintf("%d", $_GET["id"]);
384
385                         print "<div id=\"infoBoxTitle\">User details</div>";
386
387                         print "<div class='infoBoxContents'>";
388
389                         $result = db_query($link, "SELECT login,
390                                 ".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login,
391                                 access_level,
392                                 (SELECT COUNT(int_id) FROM ttrss_user_entries 
393                                         WHERE owner_uid = id) AS stored_articles,
394                                 ".SUBSTRING_FOR_DATE."(created,1,16) AS created
395                                 FROM ttrss_users 
396                                 WHERE id = '$uid'");
397                                 
398                         if (db_num_rows($result) == 0) {
399                                 print "<h1>User not found</h1>";
400                                 return;
401                         }
402                         
403                         // print "<h1>User Details</h1>";
404
405                         $login = db_fetch_result($result, 0, "login");
406
407                         // print "<h1>$login</h1>";
408
409                         print "<table width='100%'>";
410
411                         $last_login = date(get_pref($link, 'LONG_DATE_FORMAT'),
412                                 strtotime(db_fetch_result($result, 0, "last_login")));
413
414                         $created = date(get_pref($link, 'LONG_DATE_FORMAT'),
415                                 strtotime(db_fetch_result($result, 0, "created")));
416
417                         $access_level = db_fetch_result($result, 0, "access_level");
418                         $stored_articles = db_fetch_result($result, 0, "stored_articles");
419
420                         // print "<tr><td>Username</td><td>$login</td></tr>";
421                         // print "<tr><td>Access level</td><td>$access_level</td></tr>";
422                         print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
423                         print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
424                         print "<tr><td>".__('Stored articles')."</td><td>$stored_articles</td></tr>";
425
426                         $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
427                                 WHERE owner_uid = '$uid'");
428
429                         $num_feeds = db_fetch_result($result, 0, "num_feeds");
430
431                         print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>";
432
433                         /*
434                         $result = db_query($link, "SELECT 
435                                 SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size 
436                                 FROM ttrss_user_entries,ttrss_entries 
437                                         WHERE owner_uid = '$uid' AND ref_id = id");
438
439                         $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
440
441                         print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>";
442                         */
443
444                         print "</table>";
445
446                         print "<h1>".__('Subscribed feeds')."</h1>";
447
448                         $result = db_query($link, "SELECT id,title,site_url FROM ttrss_feeds
449                                 WHERE owner_uid = '$uid' ORDER BY title");
450
451                         print "<ul class=\"userFeedList\">";
452
453                         $row_class = "odd";
454
455                         while ($line = db_fetch_assoc($result)) {
456
457                                 $icon_file = ICONS_URL."/".$line["id"].".ico";
458
459                                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
460                                         $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
461                                 } else {
462                                         $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
463                                 }
464
465                                 print "<li class=\"$row_class\">$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
466
467                                 $row_class = toggleEvenOdd($row_class);
468
469                         }
470
471                         if (db_num_rows($result) < $num_feeds) {
472                                 // FIXME - add link to show ALL subscribed feeds here somewhere
473                                 print "<li><img 
474                                         class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
475                         }
476                         
477                         print "</ul>";
478
479                         print "</div>";
480
481                         print "<div align='center'>
482                                 <input type='submit' class='button'                     
483                                 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
484
485                         // print "</body></html>"; 
486
487                 break; // user-details
488
489                 case "pref-feed-browser":
490                         module_pref_feed_browser($link);
491                 break; // pref-feed-browser
492
493                 case "publish":
494                         $key = db_escape_string($_GET["key"]);
495
496                         $result = db_query($link, "SELECT login, owner_uid 
497                                 FROM ttrss_user_prefs, ttrss_users WHERE
498                                 pref_name = '_PREFS_PUBLISH_KEY' AND 
499                                 value = '$key' AND 
500                                 ttrss_users.id = owner_uid");
501
502                         if (db_num_rows($result) == 1) {
503                                 $owner = db_fetch_result($result, 0, "owner_uid");
504                                 $login = db_fetch_result($result, 0, "login");
505
506                                 generate_syndicated_feed($link, $owner, -2, false);
507
508                         } else {
509                                 print "<error>User not found</error>";
510                         }
511                 break; // publish
512
513                 case "rss":
514                         $feed = db_escape_string($_GET["id"]);
515                         $user = db_escape_string($_GET["user"]);
516                         $pass = db_escape_string($_GET["pass"]);
517                         $is_cat = $_GET["is_cat"] != false;
518
519                         $search = db_escape_string($_GET["q"]);
520                         $match_on = db_escape_string($_GET["m"]);
521                         $search_mode = db_escape_string($_GET["smode"]);
522
523                         if (!$_SESSION["uid"] && $user && $pass) {
524                                 authenticate_user($link, $user, $pass);
525                         }
526
527                         if ($_SESSION["uid"] ||
528                                 http_authenticate_user($link)) {
529
530                                         generate_syndicated_feed($link, 0, $feed, $is_cat, 
531                                                 $search, $search_mode, $match_on);
532                         }
533                 break; // rss
534
535                 case "labelFromSearch":
536                         $search = db_escape_string($_GET["search"]);
537                         $search_mode = db_escape_string($_GET["smode"]);
538                         $match_on = db_escape_string($_GET["match"]);
539                         $is_cat = db_escape_string($_GET["is_cat"]);
540                         $title = db_escape_string($_GET["title"]);
541                         $feed = sprintf("%d", $_GET["feed"]);
542
543                         $label_qparts = array();
544
545                         $search_expr = getSearchSql($search, $match_on);
546
547                         if ($is_cat) {
548                                 if ($feed != 0) {
549                                         $search_expr .= " AND ttrss_feeds.cat_id = $feed ";
550                                 } else {
551                                         $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
552                                 }
553                         } else {
554                                 if ($search_mode == "all_feeds") {
555                                         // NOOP
556                                 } else if ($search_mode == "this_cat") {
557
558                                         $tmp_result = db_query($link, "SELECT cat_id
559                                                 FROM ttrss_feeds WHERE id = '$feed'");
560
561                                         $cat_id = db_fetch_result($tmp_result, 0, "cat_id");
562
563                                         if ($cat_id > 0) {
564                                                 $search_expr .= " AND ttrss_feeds.cat_id = $cat_id ";
565                                         } else {
566                                                 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
567                                         }
568                                 } else {
569                                         $search_expr .= " AND ttrss_feeds.id = $feed ";
570                                 }
571
572                         }
573
574                         $search_expr = db_escape_string($search_expr);
575
576                         print $search_expr;
577
578                         if ($title) {
579                                 $result = db_query($link,
580                                         "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
581                                         VALUES ('$search_expr', '$title', '".$_SESSION["uid"]."')");
582                         }
583                 break; // labelFromSearch
584
585                 case "getUnread":
586                         $login = db_escape_string($_GET["login"]);
587
588                         header("Content-Type: text/plain; charset=utf-8");
589
590                         $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
591
592                         if (db_num_rows($result) == 1) {
593                                 $uid = db_fetch_result($result, 0, "id");
594                                 print getGlobalUnread($link, $uid);
595                         } else {
596                                 print "-1;User not found";
597                         }
598
599                         $print_exec_time = false;
600                 break; // getUnread
601
602                 case "digestTest":
603                         header("Content-Type: text/plain");
604                         print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
605                         $print_exec_time = false;
606                 break; // digestTest
607
608                 case "digestSend":
609                         header("Content-Type: text/plain");
610                         send_headlines_digests($link);
611                         $print_exec_time = false;
612                 break; // digestSend
613
614         } // Select action according to $op value.
615
616         // We close the connection to database.
617         db_close($link);
618 ?>
619
620 <?php if ($print_exec_time) { ?>
621 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
622 <?php } ?>