]>
git.wh0rd.org - tt-rss.git/blob - backend.php
2 set_include_path(get_include_path() . PATH_SEPARATOR
. "include");
4 /* remove ill effects of magic quotes */
6 if (get_magic_quotes_gpc()) {
7 function stripslashes_deep($value) {
8 $value = is_array($value) ?
9 array_map('stripslashes_deep', $value) : stripslashes($value);
13 $_POST = array_map('stripslashes_deep', $_POST);
14 $_GET = array_map('stripslashes_deep', $_GET);
15 $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
16 $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
19 $op = $_REQUEST["op"];
21 require_once "functions.php";
22 if ($op != "share") require_once "sessions.php";
23 require_once "sanity_check.php";
24 require_once "config.php";
25 require_once "db.php";
26 require_once "db-prefs.php";
28 no_cache_incantation();
32 $script_started = getmicrotime();
34 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
37 if (DB_TYPE
== "mysql") {
40 // PG seems to display its own errors just fine by default.
44 init_connection($link);
46 $method = $_REQUEST["method"];
47 $mode = $_REQUEST["mode"];
49 if ((!$op ||
$op == "rss" ||
$op == "dlg") && !$_REQUEST["noxml"]) {
50 header("Content-Type: application/xml; charset=utf-8");
52 header("Content-Type: text/plain; charset=utf-8");
55 if (ENABLE_GZIP_OUTPUT
) {
56 ob_start("ob_gzhandler");
59 if (SINGLE_USER_MODE
) {
60 authenticate_user($link, "admin", null);
63 $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
64 "fbexport", "logout", "pubsub");
66 if (array_search($op, $public_calls) !== false) {
68 handle_public_request($link, $op);
71 } else if (!($_SESSION["uid"] && validate_session($link))) {
72 if ($op == 'pref-feeds' && $_REQUEST['method'] == 'add') {
73 header("Content-Type: text/html");
74 login_sequence($link);
75 render_login_form($link);
77 header("Content-Type: text/plain");
78 print json_encode(array("error" => array("code" => 6)));
83 $purge_intervals = array(
84 0 => __("Use default"),
85 -1 => __("Never purge"),
86 5 => __("1 week old"),
87 14 => __("2 weeks old"),
88 31 => __("1 month old"),
89 60 => __("2 months old"),
90 90 => __("3 months old"));
92 $update_intervals = array(
93 0 => __("Default interval"),
94 -1 => __("Disable updates"),
95 15 => __("Each 15 minutes"),
96 30 => __("Each 30 minutes"),
98 240 => __("Each 4 hours"),
99 720 => __("Each 12 hours"),
101 10080 => __("Weekly"));
103 $update_intervals_nodefault = array(
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_methods = array(
116 2 => __("SimplePie"),
117 3 => __("Twitter OAuth"));
119 if (DEFAULT_UPDATE_METHOD
== "1") {
120 $update_methods[0] .= ' (SimplePie)';
122 $update_methods[0] .= ' (Magpie)';
125 $access_level_names = array(
127 5 => __("Power User"),
128 10 => __("Administrator"));
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 require_once "modules/backend-rpc.php";
142 handle_rpc_request($link);
146 $method = $_REQUEST["method"];
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 $method = db_escape_string($_REQUEST["method"]);
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"]) == "true";
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, $method,
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 $reply['headlines-info'] = array("count" => (int) $headlines_count,
374 "vgroup_last_feed" => $vgroup_last_feed,
375 "disable_cache" => (bool) $disable_cache);
377 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
379 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
382 foreach ($topmost_article_ids as $id) {
383 array_push($articles, format_article($link, $id, false));
386 $reply['articles'] = $articles;
390 // $reply['counters'] = getAllCounters($link, $omode, $feed);
393 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
395 $reply['runtime-info'] = make_runtime_info($link);
397 print json_encode($reply);
401 require_once "modules/pref-feeds.php";
402 module_pref_feeds($link);
406 require_once "modules/pref-filters.php";
407 module_pref_filters($link);
408 break; // pref-filters
411 require_once "modules/pref-labels.php";
412 module_pref_labels($link);
413 break; // pref-labels
416 require_once "modules/pref-prefs.php";
417 module_pref_prefs($link);
421 require_once "modules/pref-users.php";
422 module_pref_users($link);
423 break; // prefs-users
426 require_once "modules/help.php";
431 require_once "modules/popup-dialog.php";
432 module_popup_dialog($link);
435 case "pref-instances":
436 require_once "modules/pref-instances.php";
437 module_pref_instances($link);
438 break; // pref-instances
441 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
445 send_headlines_digests($link);
449 header("Content-type: text/html");
450 print __("Loading, please wait...") . " " .
451 "<img src='images/indicator_tiny.gif'>";
455 header("Content-Type: text/plain");
456 print json_encode(array("error" => array("code" => 7)));
458 } // Select action according to $op value.
460 // We close the connection to database.