]>
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();
27 if (ENABLE_TRANSLATIONS
== true) {
31 $script_started = getmicrotime();
33 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
36 if (DB_TYPE
== "mysql") {
39 // PG seems to display its own errors just fine by default.
43 init_connection($link);
45 $op = $_REQUEST["op"];
46 $subop = $_REQUEST["subop"];
47 $mode = $_REQUEST["mode"];
49 $print_exec_time = false;
51 if ((!$op ||
$op == "rpc" ||
$op == "rss" ||
52 $op == "digestSend" ||
$op == "dlg" ||
53 $op == "viewfeed" ||
$op == "publish" ||
54 $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
55 header("Content-Type: application/xml; charset=utf-8");
57 if (ENABLE_GZIP_OUTPUT
) {
58 ob_start("ob_gzhandler");
62 if (!$_REQUEST["noxml"]) {
63 header("Content-Type: text/html; charset=utf-8");
65 header("Content-Type: text/plain; charset=utf-8");
69 if (SINGLE_USER_MODE
) {
70 authenticate_user($link, "admin", null);
73 /* if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds"
74 && $op != "rss" && $op != "getUnread" && $op != "publish" && $op != "getProfiles") {
76 if ($op == "rpc" || $op == "viewfeed" || $op == "view") {
77 print_error_xml(6); exit;
79 header("Location: tt-rss.php?return=" .
80 urlencode($_SERVER['REQUEST_URI']));
85 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
86 $op != "rss" && $op != "getUnread" && $op != "getProfiles") {
88 header("Content-Type: text/plain");
89 print json_encode(array("error" => array("code" => 6)));
93 $purge_intervals = array(
94 0 => __("Use default"),
95 -1 => __("Never purge"),
96 5 => __("1 week old"),
97 14 => __("2 weeks old"),
98 31 => __("1 month old"),
99 60 => __("2 months old"),
100 90 => __("3 months old"));
102 $update_intervals = array(
103 0 => __("Default interval"),
104 -1 => __("Disable updates"),
105 15 => __("Each 15 minutes"),
106 30 => __("Each 30 minutes"),
108 240 => __("Each 4 hours"),
109 720 => __("Each 12 hours"),
111 10080 => __("Weekly"));
113 $update_intervals_nodefault = array(
114 -1 => __("Disable updates"),
115 15 => __("Each 15 minutes"),
116 30 => __("Each 30 minutes"),
118 240 => __("Each 4 hours"),
119 720 => __("Each 12 hours"),
121 10080 => __("Weekly"));
123 $update_methods = array(
126 2 => __("SimplePie"),
127 3 => __("Twitter OAuth"));
129 if (DEFAULT_UPDATE_METHOD
== "1") {
130 $update_methods[0] .= ' (SimplePie)';
132 $update_methods[0] .= ' (Magpie)';
135 $access_level_names = array(
137 5 => __("Power User"),
138 10 => __("Administrator"));
140 require_once "modules/pref-prefs.php";
141 require_once "modules/popup-dialog.php";
142 require_once "modules/help.php";
143 require_once "modules/pref-feeds.php";
144 require_once "modules/pref-filters.php";
145 require_once "modules/pref-labels.php";
146 require_once "modules/pref-users.php";
148 if (!sanity_check($link)) { return; }
150 switch($op) { // Select action according to $op value.
152 // Handle remote procedure calls.
153 handle_rpc_request($link);
157 $subop = $_REQUEST["subop"];
158 $root = (bool)$_REQUEST["root"];
162 db_query($link, "UPDATE ttrss_user_entries SET
163 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
164 ccache_zero_all($link, $_SESSION["uid"]);
169 $cat_id = db_escape_string($_REQUEST["cid"]);
170 $mode = (int) db_escape_string($_REQUEST['mode']);
171 toggle_collapse_cat($link, $cat_id, $mode);
177 print json_encode(outputFeedList($link));
180 $feeds = outputFeedList($link, false);
183 $root['id'] = 'root';
184 $root['name'] = __('Feeds');
185 $root['items'] = $feeds['items'];
188 $fl['identifier'] = 'id';
189 $fl['label'] = 'name';
190 $fl['items'] = array($root);
192 print json_encode($fl);
198 $id = db_escape_string($_REQUEST['id']);
200 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
201 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
203 if (db_num_rows($result) == 1) {
204 $article_url = db_fetch_result($result, 0, 'link');
205 $article_url = str_replace("\n", "", $article_url);
207 header("Location: $article_url");
211 print_error(__("Article not found."));
217 $id = db_escape_string($_REQUEST["id"]);
218 $cids = split(",", db_escape_string($_REQUEST["cids"]));
219 $mode = db_escape_string($_REQUEST["mode"]);
220 $omode = db_escape_string($_REQUEST["omode"]);
222 // in prefetch mode we only output requested cids, main article
223 // just gets marked as read (it already exists in client cache)
228 array_push($articles, format_article($link, $id, false));
229 } else if ($mode == "zoom") {
230 array_push($articles, format_article($link, $id, false, true, true));
232 catchupArticleById($link, $id, 0);
235 if (!$_SESSION["bw_limit"]) {
236 foreach ($cids as $cid) {
238 array_push($articles, format_article($link, $cid, false, false));
243 print json_encode($articles);
249 $print_exec_time = true;
250 $timing_info = getmicrotime();
254 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
256 $omode = db_escape_string($_REQUEST["omode"]);
258 $feed = db_escape_string($_REQUEST["feed"]);
259 $subop = db_escape_string($_REQUEST["subop"]);
260 $view_mode = db_escape_string($_REQUEST["view_mode"]);
261 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
262 @$cat_view = db_escape_string($_REQUEST["cat"]);
263 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
264 @$offset = db_escape_string($_REQUEST["skip"]);
265 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
266 $order_by = db_escape_string($_REQUEST["order_by"]);
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 generate_dashboard_feed($link);
277 /* Updating a label ccache means recalculating all of the caches
278 * so for performance reasons we don't do that here */
281 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
284 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
285 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
286 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
288 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
289 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
290 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
293 if (!$next_unread_feed) {
294 print "<headlines id=\"$feed\" is_cat=\"$cat_view\">";
296 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\">";
299 $override_order = false;
301 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
302 $date_sort_field = "updated";
304 $date_sort_field = "date_entered";
309 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
310 $override_order = "$date_sort_field";
312 $override_order = "$date_sort_field DESC";
317 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
318 $override_order = "title DESC, $date_sort_field";
320 $override_order = "title, $date_sort_field DESC";
325 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
326 $override_order = "score, $date_sort_field";
328 $override_order = "score DESC, $date_sort_field DESC";
333 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
335 $ret = outputHeadlinesList($link, $feed, $subop,
336 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
337 $vgroup_last_feed, $override_order);
339 $topmost_article_ids = $ret[0];
340 $headlines_count = $ret[1];
341 $returned_feed = $ret[2];
342 $disable_cache = $ret[3];
343 $vgroup_last_feed = $ret[4];
345 print "</headlines>";
347 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
349 //print "<headlines-count value=\"$headlines_count\"/>";
350 //print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
352 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
355 if ($headlines_unread == -1) {
356 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
360 //print "<headlines-unread value=\"$headlines_unread\"/>";
361 //printf("<disable-cache value=\"%d\"/>", $disable_cache);
363 print "<headlines-info><![CDATA[";
365 $info = array("count" => (int) $headlines_count,
366 "vgroup_last_feed" => $vgroup_last_feed,
367 "unread" => (int) $headlines_unread,
368 "disable_cache" => (bool) $disable_cache);
370 print json_encode($info);
372 print "]]></headlines-info>";
374 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("10", $timing_info);
376 /* 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 print "<articles><![CDATA[";
385 print json_encode($articles);
386 print "]]></articles>";
389 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
391 //if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop) {
393 print "<counters><![CDATA[";
394 print json_encode(getAllCounters($link, $omode, $feed));
395 print "]]></counters>";
398 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
400 print_runtime_info($link);
406 module_pref_feeds($link);
410 module_pref_filters($link);
411 break; // pref-filters
414 module_pref_labels($link);
415 break; // pref-labels
418 module_pref_prefs($link);
422 module_pref_users($link);
423 break; // prefs-users
430 module_popup_dialog($link);
433 case "pref-pub-items":
434 module_pref_pub_items($link);
435 break; // pref-pub-items
437 case "globalUpdateFeeds":
438 // update feeds of all users, may be used anonymously
441 // Update all feeds needing a update.
442 update_daemon_common($link, 0, true, true);
446 <message msg=\"All feeds updated\"/>
448 break; // globalUpdateFeeds
450 case "pref-feed-browser":
451 module_pref_feed_browser($link);
452 break; // pref-feed-browser
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 header("Content-Type: text/plain; charset=utf-8");
495 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
497 if (db_num_rows($result) == 1) {
498 $uid = db_fetch_result($result, 0, "id");
500 print getGlobalUnread($link, $uid);
504 print getFeedArticles($link, -3, false, true, $uid);
508 print "-1;User not found";
511 $print_exec_time = false;
515 header("Content-Type: text/plain");
516 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
517 $print_exec_time = false;
521 header("Content-Type: text/plain");
522 send_headlines_digests($link);
523 $print_exec_time = false;
527 print __("Loading, please wait...") . " " .
528 "<img src='images/indicator_tiny.gif'>";
531 $login = db_escape_string($_REQUEST["login"]);
532 $password = db_escape_string($_REQUEST["password"]);
534 if (authenticate_user($link, $login, $password)) {
535 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
536 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
538 print "<select style='width: 100%' name='profile'>";
540 print "<option value='0'>" . __("Default profile") . "</option>";
542 while ($line = db_fetch_assoc($result)) {
544 $title = $line["title"];
546 print "<option value='$id'>$title</option>";
555 } // Select action according to $op value.
558 // We close the connection to database.
562 <?php
if ($print_exec_time) { ?
>
563 <!-- <?php
echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?
> -->