]>
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 == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
54 header("Content-Type: application/xml; charset=utf-8");
57 if (!$_REQUEST["noxml"]) {
58 header("Content-Type: text/html; charset=utf-8");
60 header("Content-Type: text/plain; charset=utf-8");
64 if (ENABLE_GZIP_OUTPUT
) {
65 ob_start("ob_gzhandler");
68 if (SINGLE_USER_MODE
) {
69 authenticate_user($link, "admin", null);
72 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
73 $op != "rss" && $op != "getUnread" && $op != "getProfiles") {
75 header("Content-Type: text/plain");
76 print json_encode(array("error" => array("code" => 6)));
80 $purge_intervals = array(
81 0 => __("Use default"),
82 -1 => __("Never purge"),
83 5 => __("1 week old"),
84 14 => __("2 weeks old"),
85 31 => __("1 month old"),
86 60 => __("2 months old"),
87 90 => __("3 months old"));
89 $update_intervals = array(
90 0 => __("Default interval"),
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_intervals_nodefault = array(
101 -1 => __("Disable updates"),
102 15 => __("Each 15 minutes"),
103 30 => __("Each 30 minutes"),
105 240 => __("Each 4 hours"),
106 720 => __("Each 12 hours"),
108 10080 => __("Weekly"));
110 $update_methods = array(
113 2 => __("SimplePie"),
114 3 => __("Twitter OAuth"));
116 if (DEFAULT_UPDATE_METHOD
== "1") {
117 $update_methods[0] .= ' (SimplePie)';
119 $update_methods[0] .= ' (Magpie)';
122 $access_level_names = array(
124 5 => __("Power User"),
125 10 => __("Administrator"));
127 require_once "modules/pref-prefs.php";
128 require_once "modules/popup-dialog.php";
129 require_once "modules/help.php";
130 require_once "modules/pref-feeds.php";
131 require_once "modules/pref-filters.php";
132 require_once "modules/pref-labels.php";
133 require_once "modules/pref-users.php";
135 $error = sanity_check($link);
137 if ($error['code'] != 0) {
138 print json_encode(array("error" => $error));
142 switch($op) { // Select action according to $op value.
144 // Handle remote procedure calls.
145 handle_rpc_request($link);
149 $subop = $_REQUEST["subop"];
150 $root = (bool)$_REQUEST["root"];
154 db_query($link, "UPDATE ttrss_user_entries SET
155 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
156 ccache_zero_all($link, $_SESSION["uid"]);
161 $cat_id = db_escape_string($_REQUEST["cid"]);
162 $mode = (int) db_escape_string($_REQUEST['mode']);
163 toggle_collapse_cat($link, $cat_id, $mode);
169 print json_encode(outputFeedList($link));
172 $feeds = outputFeedList($link, false);
175 $root['id'] = 'root';
176 $root['name'] = __('Feeds');
177 $root['items'] = $feeds['items'];
180 $fl['identifier'] = 'id';
181 $fl['label'] = 'name';
182 $fl['items'] = array($root);
184 print json_encode($fl);
190 $id = db_escape_string($_REQUEST['id']);
192 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
193 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
195 if (db_num_rows($result) == 1) {
196 $article_url = db_fetch_result($result, 0, 'link');
197 $article_url = str_replace("\n", "", $article_url);
199 header("Location: $article_url");
203 print_error(__("Article not found."));
209 $id = db_escape_string($_REQUEST["id"]);
210 $cids = split(",", db_escape_string($_REQUEST["cids"]));
211 $mode = db_escape_string($_REQUEST["mode"]);
212 $omode = db_escape_string($_REQUEST["omode"]);
214 // in prefetch mode we only output requested cids, main article
215 // just gets marked as read (it already exists in client cache)
220 array_push($articles, format_article($link, $id, false));
221 } else if ($mode == "zoom") {
222 array_push($articles, format_article($link, $id, false, true, true));
224 catchupArticleById($link, $id, 0);
227 if (!$_SESSION["bw_limit"]) {
228 foreach ($cids as $cid) {
230 array_push($articles, format_article($link, $cid, false, false));
235 print json_encode($articles);
241 $timing_info = getmicrotime();
245 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
247 $omode = db_escape_string($_REQUEST["omode"]);
249 $feed = db_escape_string($_REQUEST["feed"]);
250 $subop = db_escape_string($_REQUEST["subop"]);
251 $view_mode = db_escape_string($_REQUEST["view_mode"]);
252 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
253 @$cat_view = db_escape_string($_REQUEST["cat"]);
254 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
255 @$offset = db_escape_string($_REQUEST["skip"]);
256 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
257 $order_by = db_escape_string($_REQUEST["order_by"]);
259 /* Feed -5 is a special case: it is used to display auxiliary information
260 * when there's nothing to load - e.g. no stuff in fresh feed */
263 print json_encode(generate_dashboard_feed($link));
270 $label_feed = -11-$feed;
271 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
272 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
273 } else if (!$cat_view && $feed > 0) {
274 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
275 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
276 } else if ($cat_view && $feed > 0) {
277 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
278 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
281 if ($result && db_num_rows($result) == 0) {
282 print json_encode(generate_error_feed($link, __("Feed not found.")));
286 /* Updating a label ccache means recalculating all of the caches
287 * so for performance reasons we don't do that here */
290 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
293 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
294 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
295 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
297 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
298 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
299 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
302 $reply['headlines'] = array();
304 if (!$next_unread_feed)
305 $reply['headlines']['id'] = $feed;
307 $reply['headlines']['id'] = $next_unread_feed;
309 $reply['headlines']['is_cat'] = (bool) $cat_view;
311 $override_order = false;
313 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
314 $date_sort_field = "updated";
316 $date_sort_field = "date_entered";
321 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
322 $override_order = "$date_sort_field";
324 $override_order = "$date_sort_field DESC";
329 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
330 $override_order = "title DESC, $date_sort_field";
332 $override_order = "title, $date_sort_field DESC";
337 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
338 $override_order = "score, $date_sort_field";
340 $override_order = "score DESC, $date_sort_field DESC";
345 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
347 $ret = format_headlines_list($link, $feed, $subop,
348 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
349 $vgroup_last_feed, $override_order);
351 $topmost_article_ids = $ret[0];
352 $headlines_count = $ret[1];
353 $returned_feed = $ret[2];
354 $disable_cache = $ret[3];
355 $vgroup_last_feed = $ret[4];
357 $reply['headlines']['content'] = $ret[5];
358 $reply['headlines']['toolbar'] = $ret[6];
360 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
362 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
365 if ($headlines_unread == -1) {
366 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
369 $reply['headlines-info'] = array("count" => (int) $headlines_count,
370 "vgroup_last_feed" => $vgroup_last_feed,
371 "unread" => (int) $headlines_unread,
372 "disable_cache" => (bool) $disable_cache);
374 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
376 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
379 foreach ($topmost_article_ids as $id) {
380 array_push($articles, format_article($link, $id, $feed, false));
383 $reply['articles'] = $articles;
387 $reply['counters'] = getAllCounters($link, $omode, $feed);
390 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
392 $reply['runtime-info'] = make_runtime_info($link);
394 print json_encode($reply);
399 module_pref_feeds($link);
403 module_pref_filters($link);
404 break; // pref-filters
407 module_pref_labels($link);
408 break; // pref-labels
411 module_pref_prefs($link);
415 module_pref_users($link);
416 break; // prefs-users
423 module_popup_dialog($link);
426 case "pref-pub-items":
427 module_pref_pub_items($link);
428 break; // pref-pub-items
430 case "globalUpdateFeeds":
431 // update feeds of all users, may be used anonymously
434 // Update all feeds needing a update.
435 update_daemon_common($link, 0, true, true);
439 <message msg=\"All feeds updated\"/>
441 break; // globalUpdateFeeds
443 case "pref-feed-browser":
444 module_pref_feed_browser($link);
445 break; // pref-feed-browser
448 $feed = db_escape_string($_REQUEST["id"]);
449 $key = db_escape_string($_REQUEST["key"]);
450 $is_cat = $_REQUEST["is_cat"] != false;
451 $limit = (int)db_escape_string($_REQUEST["limit"]);
453 $search = db_escape_string($_REQUEST["q"]);
454 $match_on = db_escape_string($_REQUEST["m"]);
455 $search_mode = db_escape_string($_REQUEST["smode"]);
456 $view_mode = db_escape_string($_REQUEST["view-mode"]);
458 if (SINGLE_USER_MODE
) {
459 authenticate_user($link, "admin", null);
465 $result = db_query($link, "SELECT owner_uid FROM
466 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
468 if (db_num_rows($result) == 1)
469 $owner_id = db_fetch_result($result, 0, "owner_uid");
473 $_SESSION['uid'] = $owner_id;
475 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
476 $search, $search_mode, $match_on, $view_mode);
478 header('HTTP/1.1 403 Forbidden');
483 $login = db_escape_string($_REQUEST["login"]);
484 $fresh = $_REQUEST["fresh"] == "1";
486 header("Content-Type: text/plain; charset=utf-8");
488 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
490 if (db_num_rows($result) == 1) {
491 $uid = db_fetch_result($result, 0, "id");
493 print getGlobalUnread($link, $uid);
497 print getFeedArticles($link, -3, false, true, $uid);
501 print "-1;User not found";
504 $print_exec_time = false;
508 header("Content-Type: text/plain");
509 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
510 $print_exec_time = false;
514 header("Content-Type: text/plain");
515 send_headlines_digests($link);
516 $print_exec_time = false;
520 print __("Loading, please wait...") . " " .
521 "<img src='images/indicator_tiny.gif'>";
524 $login = db_escape_string($_REQUEST["login"]);
525 $password = db_escape_string($_REQUEST["password"]);
527 if (authenticate_user($link, $login, $password)) {
528 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
529 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
531 print "<select style='width: 100%' name='profile'>";
533 print "<option value='0'>" . __("Default profile") . "</option>";
535 while ($line = db_fetch_assoc($result)) {
537 $title = $line["title"];
539 print "<option value='$id'>$title</option>";
548 } // Select action according to $op value.
551 // We close the connection to database.
555 <?php
if ($print_exec_time) { ?
>
556 <!-- <?php
echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?
> -->