]>
git.wh0rd.org - tt-rss.git/blob - backend.php
2 /* remove ill effects of magic quotes */
4 if (get_magic_quotes_gpc()) {
5 function stripslashes_deep($value) {
6 $value = is_array($value) ?
7 array_map('stripslashes_deep', $value) : stripslashes($value);
11 $_POST = array_map('stripslashes_deep', $_POST);
12 $_GET = array_map('stripslashes_deep', $_GET);
13 $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
14 $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
17 require_once "functions.php";
18 require_once "sessions.php";
19 require_once "modules/backend-rpc.php";
20 require_once "sanity_check.php";
21 require_once "config.php";
22 require_once "db.php";
23 require_once "db-prefs.php";
25 no_cache_incantation();
29 $script_started = getmicrotime();
31 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
34 if (DB_TYPE
== "mysql") {
37 // PG seems to display its own errors just fine by default.
41 init_connection($link);
43 $op = $_REQUEST["op"];
44 $subop = $_REQUEST["subop"];
45 $mode = $_REQUEST["mode"];
47 if ((!$op ||
$op == "rss" ||
$op == "dlg") && !$_REQUEST["noxml"]) {
48 header("Content-Type: application/xml; charset=utf-8");
50 header("Content-Type: text/plain; charset=utf-8");
53 if (ENABLE_GZIP_OUTPUT
) {
54 ob_start("ob_gzhandler");
57 if (SINGLE_USER_MODE
) {
58 authenticate_user($link, "admin", null);
61 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
62 $op != "rss" && $op != "getUnread" && $op != "getProfiles" &&
63 $op != "fbexport" && $op != "logout" && $op != "pubsub") {
65 if ($op == 'pref-feeds' && $_REQUEST['subop'] == 'add') {
66 header("Content-Type: text/html");
67 login_sequence($link);
68 render_login_form($link);
70 header("Content-Type: text/plain");
71 print json_encode(array("error" => array("code" => 6)));
76 $purge_intervals = array(
77 0 => __("Use default"),
78 -1 => __("Never purge"),
79 5 => __("1 week old"),
80 14 => __("2 weeks old"),
81 31 => __("1 month old"),
82 60 => __("2 months old"),
83 90 => __("3 months old"));
85 $update_intervals = array(
86 0 => __("Default interval"),
87 -1 => __("Disable updates"),
88 15 => __("Each 15 minutes"),
89 30 => __("Each 30 minutes"),
91 240 => __("Each 4 hours"),
92 720 => __("Each 12 hours"),
94 10080 => __("Weekly"));
96 $update_intervals_nodefault = array(
97 -1 => __("Disable updates"),
98 15 => __("Each 15 minutes"),
99 30 => __("Each 30 minutes"),
101 240 => __("Each 4 hours"),
102 720 => __("Each 12 hours"),
104 10080 => __("Weekly"));
106 $update_methods = array(
109 2 => __("SimplePie"),
110 3 => __("Twitter OAuth"));
112 if (DEFAULT_UPDATE_METHOD
== "1") {
113 $update_methods[0] .= ' (SimplePie)';
115 $update_methods[0] .= ' (Magpie)';
118 $access_level_names = array(
120 5 => __("Power User"),
121 10 => __("Administrator"));
123 require_once "modules/pref-prefs.php";
124 require_once "modules/popup-dialog.php";
125 require_once "modules/help.php";
126 require_once "modules/pref-feeds.php";
127 require_once "modules/pref-filters.php";
128 require_once "modules/pref-labels.php";
129 require_once "modules/pref-users.php";
130 require_once "modules/pref-instances.php";
132 $error = sanity_check($link);
134 if ($error['code'] != 0 && $op != "logout") {
135 print json_encode(array("error" => $error));
139 switch($op) { // Select action according to $op value.
141 // Handle remote procedure calls.
142 handle_rpc_request($link);
146 $subop = $_REQUEST["subop"];
147 $root = (bool)$_REQUEST["root"];
151 db_query($link, "UPDATE ttrss_user_entries SET
152 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
153 ccache_zero_all($link, $_SESSION["uid"]);
158 $cat_id = db_escape_string($_REQUEST["cid"]);
159 $mode = (int) db_escape_string($_REQUEST['mode']);
160 toggle_collapse_cat($link, $cat_id, $mode);
166 print json_encode(outputFeedList($link));
169 $feeds = outputFeedList($link, false);
172 $root['id'] = 'root';
173 $root['name'] = __('Feeds');
174 $root['items'] = $feeds['items'];
177 $fl['identifier'] = 'id';
178 $fl['label'] = 'name';
179 $fl['items'] = array($root);
181 print json_encode($fl);
187 $id = db_escape_string($_REQUEST['id']);
189 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
190 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
193 if (db_num_rows($result) == 1) {
194 $article_url = db_fetch_result($result, 0, 'link');
195 $article_url = str_replace("\n", "", $article_url);
197 header("Location: $article_url");
201 print_error(__("Article not found."));
207 $id = db_escape_string($_REQUEST["id"]);
208 $cids = explode(",", db_escape_string($_REQUEST["cids"]));
209 $mode = db_escape_string($_REQUEST["mode"]);
210 $omode = db_escape_string($_REQUEST["omode"]);
212 // in prefetch mode we only output requested cids, main article
213 // just gets marked as read (it already exists in client cache)
218 array_push($articles, format_article($link, $id, false));
219 } else if ($mode == "zoom") {
220 array_push($articles, format_article($link, $id, true, true));
221 } else if ($mode == "raw") {
222 if ($_REQUEST['html']) {
223 header("Content-Type: text/html");
224 print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
227 $article = format_article($link, $id, false);
228 print $article['content'];
232 catchupArticleById($link, $id, 0);
234 if (!$_SESSION["bw_limit"]) {
235 foreach ($cids as $cid) {
237 array_push($articles, format_article($link, $cid, false, false));
242 print json_encode($articles);
248 $timing_info = getmicrotime();
252 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
254 $omode = db_escape_string($_REQUEST["omode"]);
256 $feed = db_escape_string($_REQUEST["feed"]);
257 $subop = db_escape_string($_REQUEST["subop"]);
258 $view_mode = db_escape_string($_REQUEST["view_mode"]);
259 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
260 @$cat_view = db_escape_string($_REQUEST["cat"]);
261 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
262 @$offset = db_escape_string($_REQUEST["skip"]);
263 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
264 $order_by = db_escape_string($_REQUEST["order_by"]);
266 if (is_numeric($feed)) $feed = (int) $feed;
268 /* Feed -5 is a special case: it is used to display auxiliary information
269 * when there's nothing to load - e.g. no stuff in fresh feed */
272 print json_encode(generate_dashboard_feed($link));
279 $label_feed = -11-$feed;
280 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
281 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
282 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
283 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
284 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
285 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
286 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
287 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
290 if ($result && db_num_rows($result) == 0) {
291 print json_encode(generate_error_feed($link, __("Feed not found.")));
295 /* Updating a label ccache means recalculating all of the caches
296 * so for performance reasons we don't do that here */
299 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
302 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
303 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
304 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
306 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
307 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
308 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
311 $reply['headlines'] = array();
313 if (!$next_unread_feed)
314 $reply['headlines']['id'] = $feed;
316 $reply['headlines']['id'] = $next_unread_feed;
318 $reply['headlines']['is_cat'] = (bool) $cat_view;
320 $override_order = false;
322 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
323 $date_sort_field = "updated";
325 $date_sort_field = "date_entered";
330 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
331 $override_order = "$date_sort_field";
333 $override_order = "$date_sort_field DESC";
338 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
339 $override_order = "title DESC, $date_sort_field";
341 $override_order = "title, $date_sort_field DESC";
346 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
347 $override_order = "score, $date_sort_field";
349 $override_order = "score DESC, $date_sort_field DESC";
354 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
356 $ret = format_headlines_list($link, $feed, $subop,
357 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
358 $vgroup_last_feed, $override_order);
360 $topmost_article_ids = $ret[0];
361 $headlines_count = $ret[1];
362 $returned_feed = $ret[2];
363 $disable_cache = $ret[3];
364 $vgroup_last_feed = $ret[4];
366 // if ($_REQUEST["debug"]) print_r($ret);
368 $reply['headlines']['content'] =& $ret[5]['content'];
369 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
371 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
373 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
376 if ($headlines_unread == -1) {
377 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
380 $reply['headlines-info'] = array("count" => (int) $headlines_count,
381 "vgroup_last_feed" => $vgroup_last_feed,
382 "unread" => (int) $headlines_unread,
383 "disable_cache" => (bool) $disable_cache);
385 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
387 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
390 foreach ($topmost_article_ids as $id) {
391 array_push($articles, format_article($link, $id, false));
394 $reply['articles'] = $articles;
398 $reply['counters'] = getAllCounters($link, $omode, $feed);
401 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
403 $reply['runtime-info'] = make_runtime_info($link);
405 print json_encode($reply);
410 module_pref_feeds($link);
414 module_pref_filters($link);
415 break; // pref-filters
418 module_pref_labels($link);
419 break; // pref-labels
422 module_pref_prefs($link);
426 module_pref_users($link);
427 break; // prefs-users
434 module_popup_dialog($link);
437 case "pref-pub-items":
438 module_pref_pub_items($link);
439 break; // pref-pub-items
441 case "globalUpdateFeeds":
442 // Update all feeds needing a update.
443 update_daemon_common($link, 0, true, true);
444 break; // globalUpdateFeeds
446 case "pref-feed-browser":
447 module_pref_feed_browser($link);
448 break; // pref-feed-browser
450 case "pref-instances":
451 module_pref_instances($link);
452 break; // pref-instances
455 $feed = db_escape_string($_REQUEST["id"]);
456 $key = db_escape_string($_REQUEST["key"]);
457 $is_cat = $_REQUEST["is_cat"] != false;
458 $limit = (int)db_escape_string($_REQUEST["limit"]);
460 $search = db_escape_string($_REQUEST["q"]);
461 $match_on = db_escape_string($_REQUEST["m"]);
462 $search_mode = db_escape_string($_REQUEST["smode"]);
463 $view_mode = db_escape_string($_REQUEST["view-mode"]);
465 if (SINGLE_USER_MODE
) {
466 authenticate_user($link, "admin", null);
472 $result = db_query($link, "SELECT owner_uid FROM
473 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
475 if (db_num_rows($result) == 1)
476 $owner_id = db_fetch_result($result, 0, "owner_uid");
480 $_SESSION['uid'] = $owner_id;
482 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
483 $search, $search_mode, $match_on, $view_mode);
485 header('HTTP/1.1 403 Forbidden');
490 $login = db_escape_string($_REQUEST["login"]);
491 $fresh = $_REQUEST["fresh"] == "1";
493 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
495 if (db_num_rows($result) == 1) {
496 $uid = db_fetch_result($result, 0, "id");
498 print getGlobalUnread($link, $uid);
502 print getFeedArticles($link, -3, false, true, $uid);
506 print "-1;User not found";
512 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
516 send_headlines_digests($link);
520 header("Content-type: text/html");
521 print __("Loading, please wait...") . " " .
522 "<img src='images/indicator_tiny.gif'>";
526 $login = db_escape_string($_REQUEST["login"]);
527 $password = db_escape_string($_REQUEST["password"]);
529 if (authenticate_user($link, $login, $password)) {
530 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
531 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
533 print "<select style='width: 100%' name='profile'>";
535 print "<option value='0'>" . __("Default profile") . "</option>";
537 while ($line = db_fetch_assoc($result)) {
539 $title = $line["title"];
541 print "<option value='$id'>$title</option>";
548 break; // getprofiles
551 $mode = db_escape_string($_REQUEST['hub_mode']);
552 $feed_id = (int) db_escape_string($_REQUEST['id']);
553 $feed_url = db_escape_string($_REQUEST['hub_topic']);
555 if (!PUBSUBHUBBUB_ENABLED
) {
556 header('HTTP/1.0 404 Not Found');
557 echo "404 Not found";
561 // TODO: implement hub_verifytoken checking
563 $result = db_query($link, "SELECT feed_url FROM ttrss_feeds
564 WHERE id = '$feed_id'");
566 if (db_num_rows($result) != 0) {
568 $check_feed_url = db_fetch_result($result, 0, "feed_url");
570 if ($check_feed_url && ($check_feed_url == $feed_url ||
!$feed_url)) {
571 if ($mode == "subscribe") {
573 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 2
574 WHERE id = '$feed_id'");
576 print $_REQUEST['hub_challenge'];
579 } else if ($mode == "unsubscribe") {
581 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 0
582 WHERE id = '$feed_id'");
584 print $_REQUEST['hub_challenge'];
589 // Received update ping, schedule feed update.
590 //update_rss_feed($link, $feed_id, true, true);
592 db_query($link, "UPDATE ttrss_feeds SET
593 last_update_started = '1970-01-01',
594 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
595 owner_uid = ".$_SESSION["uid"]);
599 header('HTTP/1.0 404 Not Found');
600 echo "404 Not found";
603 header('HTTP/1.0 404 Not Found');
604 echo "404 Not found";
611 header("Location: tt-rss.php");
616 $access_key = db_escape_string($_POST["key"]);
618 // TODO: rate limit checking using last_connected
619 $result = db_query($link, "SELECT id FROM ttrss_linked_instances
620 WHERE access_key = '$access_key'");
622 if (db_num_rows($result) == 1) {
624 $instance_id = db_fetch_result($result, 0, "id");
626 $result = db_query($link, "SELECT feed_url, site_url, title, subscribers
627 FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
631 while ($line = db_fetch_assoc($result)) {
632 array_push($feeds, $line);
635 db_query($link, "UPDATE ttrss_linked_instances SET
636 last_status_in = 1 WHERE id = '$instance_id'");
638 print json_encode(array("feeds" => $feeds));
640 print json_encode(array("error" => array("code" => 6)));
645 header("Content-Type: text/plain");
646 print json_encode(array("error" => array("code" => 7)));
648 } // Select action according to $op value.
650 // We close the connection to database.