]>
git.wh0rd.org - tt-rss.git/blob - backend.php
6b93d1b3c5444c55079fa68e8bdc05b1ca3cc40c
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 header("Content-Type: text/plain");
66 print json_encode(array("error" => array("code" => 6)));
70 $purge_intervals = array(
71 0 => __("Use default"),
72 -1 => __("Never purge"),
73 5 => __("1 week old"),
74 14 => __("2 weeks old"),
75 31 => __("1 month old"),
76 60 => __("2 months old"),
77 90 => __("3 months old"));
79 $update_intervals = array(
80 0 => __("Default interval"),
81 -1 => __("Disable updates"),
82 15 => __("Each 15 minutes"),
83 30 => __("Each 30 minutes"),
85 240 => __("Each 4 hours"),
86 720 => __("Each 12 hours"),
88 10080 => __("Weekly"));
90 $update_intervals_nodefault = array(
91 -1 => __("Disable updates"),
92 15 => __("Each 15 minutes"),
93 30 => __("Each 30 minutes"),
95 240 => __("Each 4 hours"),
96 720 => __("Each 12 hours"),
98 10080 => __("Weekly"));
100 $update_methods = array(
103 2 => __("SimplePie"),
104 3 => __("Twitter OAuth"));
106 if (DEFAULT_UPDATE_METHOD
== "1") {
107 $update_methods[0] .= ' (SimplePie)';
109 $update_methods[0] .= ' (Magpie)';
112 $access_level_names = array(
114 5 => __("Power User"),
115 10 => __("Administrator"));
117 require_once "modules/pref-prefs.php";
118 require_once "modules/popup-dialog.php";
119 require_once "modules/help.php";
120 require_once "modules/pref-feeds.php";
121 require_once "modules/pref-filters.php";
122 require_once "modules/pref-labels.php";
123 require_once "modules/pref-users.php";
124 require_once "modules/pref-instances.php";
126 $error = sanity_check($link);
128 if ($error['code'] != 0 && $op != "logout") {
129 print json_encode(array("error" => $error));
133 switch($op) { // Select action according to $op value.
135 // Handle remote procedure calls.
136 handle_rpc_request($link);
140 $subop = $_REQUEST["subop"];
141 $root = (bool)$_REQUEST["root"];
145 db_query($link, "UPDATE ttrss_user_entries SET
146 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
147 ccache_zero_all($link, $_SESSION["uid"]);
152 $cat_id = db_escape_string($_REQUEST["cid"]);
153 $mode = (int) db_escape_string($_REQUEST['mode']);
154 toggle_collapse_cat($link, $cat_id, $mode);
160 print json_encode(outputFeedList($link));
163 $feeds = outputFeedList($link, false);
166 $root['id'] = 'root';
167 $root['name'] = __('Feeds');
168 $root['items'] = $feeds['items'];
171 $fl['identifier'] = 'id';
172 $fl['label'] = 'name';
173 $fl['items'] = array($root);
175 print json_encode($fl);
181 $id = db_escape_string($_REQUEST['id']);
183 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
184 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
187 if (db_num_rows($result) == 1) {
188 $article_url = db_fetch_result($result, 0, 'link');
189 $article_url = str_replace("\n", "", $article_url);
191 header("Location: $article_url");
195 print_error(__("Article not found."));
201 $id = db_escape_string($_REQUEST["id"]);
202 $cids = split(",", db_escape_string($_REQUEST["cids"]));
203 $mode = db_escape_string($_REQUEST["mode"]);
204 $omode = db_escape_string($_REQUEST["omode"]);
206 // in prefetch mode we only output requested cids, main article
207 // just gets marked as read (it already exists in client cache)
212 array_push($articles, format_article($link, $id, false));
213 } else if ($mode == "zoom") {
214 array_push($articles, format_article($link, $id, false, true, true));
215 } else if ($mode == "raw") {
216 if ($_REQUEST['html']) {
217 header("Content-Type: text/html");
218 print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
221 $article = format_article($link, $id, false);
222 print $article['content'];
225 catchupArticleById($link, $id, 0);
228 if (!$_SESSION["bw_limit"]) {
229 foreach ($cids as $cid) {
231 array_push($articles, format_article($link, $cid, false, false));
236 print json_encode($articles);
242 $timing_info = getmicrotime();
246 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
248 $omode = db_escape_string($_REQUEST["omode"]);
250 $feed = db_escape_string($_REQUEST["feed"]);
251 $subop = db_escape_string($_REQUEST["subop"]);
252 $view_mode = db_escape_string($_REQUEST["view_mode"]);
253 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
254 @$cat_view = db_escape_string($_REQUEST["cat"]);
255 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
256 @$offset = db_escape_string($_REQUEST["skip"]);
257 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
258 $order_by = db_escape_string($_REQUEST["order_by"]);
260 /* Feed -5 is a special case: it is used to display auxiliary information
261 * when there's nothing to load - e.g. no stuff in fresh feed */
264 print json_encode(generate_dashboard_feed($link));
271 $label_feed = -11-$feed;
272 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
273 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
274 } else if (!$cat_view && $feed > 0) {
275 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
276 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
277 } else if ($cat_view && $feed > 0) {
278 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
279 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
282 if ($result && db_num_rows($result) == 0) {
283 print json_encode(generate_error_feed($link, __("Feed not found.")));
287 /* Updating a label ccache means recalculating all of the caches
288 * so for performance reasons we don't do that here */
291 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
294 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
295 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
296 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
298 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
299 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
300 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
303 $reply['headlines'] = array();
305 if (!$next_unread_feed)
306 $reply['headlines']['id'] = $feed;
308 $reply['headlines']['id'] = $next_unread_feed;
310 $reply['headlines']['is_cat'] = (bool) $cat_view;
312 $override_order = false;
314 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
315 $date_sort_field = "updated";
317 $date_sort_field = "date_entered";
322 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
323 $override_order = "$date_sort_field";
325 $override_order = "$date_sort_field DESC";
330 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
331 $override_order = "title DESC, $date_sort_field";
333 $override_order = "title, $date_sort_field DESC";
338 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
339 $override_order = "score, $date_sort_field";
341 $override_order = "score DESC, $date_sort_field DESC";
346 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
348 $ret = format_headlines_list($link, $feed, $subop,
349 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
350 $vgroup_last_feed, $override_order);
352 $topmost_article_ids = $ret[0];
353 $headlines_count = $ret[1];
354 $returned_feed = $ret[2];
355 $disable_cache = $ret[3];
356 $vgroup_last_feed = $ret[4];
358 $reply['headlines']['content'] = $ret[5];
359 $reply['headlines']['toolbar'] = $ret[6];
361 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
363 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
366 if ($headlines_unread == -1) {
367 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
370 $reply['headlines-info'] = array("count" => (int) $headlines_count,
371 "vgroup_last_feed" => $vgroup_last_feed,
372 "unread" => (int) $headlines_unread,
373 "disable_cache" => (bool) $disable_cache);
375 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
377 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
380 foreach ($topmost_article_ids as $id) {
381 array_push($articles, format_article($link, $id, $feed, false));
384 $reply['articles'] = $articles;
388 $reply['counters'] = getAllCounters($link, $omode, $feed);
391 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
393 $reply['runtime-info'] = make_runtime_info($link);
395 print json_encode($reply);
400 module_pref_feeds($link);
404 module_pref_filters($link);
405 break; // pref-filters
408 module_pref_labels($link);
409 break; // pref-labels
412 module_pref_prefs($link);
416 module_pref_users($link);
417 break; // prefs-users
424 module_popup_dialog($link);
427 case "pref-pub-items":
428 module_pref_pub_items($link);
429 break; // pref-pub-items
431 case "globalUpdateFeeds":
432 // Update all feeds needing a update.
433 update_daemon_common($link, 0, true, true);
434 break; // globalUpdateFeeds
436 case "pref-feed-browser":
437 module_pref_feed_browser($link);
438 break; // pref-feed-browser
440 case "pref-instances":
441 module_pref_instances($link);
442 break; // pref-instances
445 $feed = db_escape_string($_REQUEST["id"]);
446 $key = db_escape_string($_REQUEST["key"]);
447 $is_cat = $_REQUEST["is_cat"] != false;
448 $limit = (int)db_escape_string($_REQUEST["limit"]);
450 $search = db_escape_string($_REQUEST["q"]);
451 $match_on = db_escape_string($_REQUEST["m"]);
452 $search_mode = db_escape_string($_REQUEST["smode"]);
453 $view_mode = db_escape_string($_REQUEST["view-mode"]);
455 if (SINGLE_USER_MODE
) {
456 authenticate_user($link, "admin", null);
462 $result = db_query($link, "SELECT owner_uid FROM
463 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
465 if (db_num_rows($result) == 1)
466 $owner_id = db_fetch_result($result, 0, "owner_uid");
470 $_SESSION['uid'] = $owner_id;
472 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
473 $search, $search_mode, $match_on, $view_mode);
475 header('HTTP/1.1 403 Forbidden');
480 $login = db_escape_string($_REQUEST["login"]);
481 $fresh = $_REQUEST["fresh"] == "1";
483 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
485 if (db_num_rows($result) == 1) {
486 $uid = db_fetch_result($result, 0, "id");
488 print getGlobalUnread($link, $uid);
492 print getFeedArticles($link, -3, false, true, $uid);
496 print "-1;User not found";
502 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
506 send_headlines_digests($link);
510 header("Content-type: text/html");
511 print __("Loading, please wait...") . " " .
512 "<img src='images/indicator_tiny.gif'>";
516 $login = db_escape_string($_REQUEST["login"]);
517 $password = db_escape_string($_REQUEST["password"]);
519 if (authenticate_user($link, $login, $password)) {
520 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
521 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
523 print "<select style='width: 100%' name='profile'>";
525 print "<option value='0'>" . __("Default profile") . "</option>";
527 while ($line = db_fetch_assoc($result)) {
529 $title = $line["title"];
531 print "<option value='$id'>$title</option>";
538 break; // getprofiles
541 $mode = db_escape_string($_REQUEST['hub_mode']);
542 $feed_id = db_escape_string($_REQUEST['id']);
543 $feed_url = db_escape_string($_REQUEST['hub_topic']);
545 // TODO: implement hub_verifytoken checking
547 $result = db_query($link, "SELECT feed_url FROM ttrss_feeds
548 WHERE id = '$feed_id'");
550 $check_feed_url = db_fetch_result($result, 0, "feed_url");
552 if ($check_feed_url && ($check_feed_url == $feed_url ||
!$feed_url)) {
553 if ($mode == "subscribe") {
555 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 2
556 WHERE id = '$feed_id'");
558 print $_REQUEST['hub_challenge'];
561 } else if ($mode == "unsubscribe") {
563 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 0
564 WHERE id = '$feed_id'");
566 print $_REQUEST['hub_challenge'];
571 // Received update ping, schedule feed update.
573 update_rss_feed($link, $feed_id, true, true);
577 header('HTTP/1.0 404 Not Found');
584 header("Location: tt-rss.php");
589 $access_key = db_escape_string($_POST["key"]);
591 // TODO: rate limit checking using last_connected
592 $result = db_query($link, "SELECT id FROM ttrss_linked_instances
593 WHERE access_key = '$access_key'");
595 if (db_num_rows($result) == 1) {
597 $instance_id = db_fetch_result($result, 0, "id");
599 $result = db_query($link, "SELECT feed_url, site_url, title, subscribers
600 FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
604 while ($line = db_fetch_assoc($result)) {
605 array_push($feeds, $line);
608 db_query($link, "UPDATE ttrss_linked_instances SET
609 last_status_in = 1 WHERE id = '$instance_id'");
611 print json_encode(array("feeds" => $feeds));
613 print json_encode(array("error" => array("code" => 6)));
618 header("Content-Type: text/plain");
619 print json_encode(array("error" => array("code" => 7)));
621 } // Select action according to $op value.
623 // We close the connection to database.