]>
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 $op = $_REQUEST["op"];
19 require_once "functions.php";
20 if ($op != "share") require_once "sessions.php";
21 require_once "modules/backend-rpc.php";
22 require_once "sanity_check.php";
23 require_once "config.php";
24 require_once "db.php";
25 require_once "db-prefs.php";
27 no_cache_incantation();
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 $subop = $_REQUEST["subop"];
46 $mode = $_REQUEST["mode"];
48 if ((!$op ||
$op == "rss" ||
$op == "dlg") && !$_REQUEST["noxml"]) {
49 header("Content-Type: application/xml; charset=utf-8");
51 header("Content-Type: text/plain; charset=utf-8");
54 if (ENABLE_GZIP_OUTPUT
) {
55 ob_start("ob_gzhandler");
58 if (SINGLE_USER_MODE
) {
59 authenticate_user($link, "admin", null);
62 $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
63 "fbexport", "logout", "pubsub");
65 if (array_search($op, $public_calls) !== false) {
67 handle_public_request($link, $op);
70 } else if (!($_SESSION["uid"] && validate_session($link))) {
71 if ($op == 'pref-feeds' && $_REQUEST['subop'] == 'add') {
72 header("Content-Type: text/html");
73 login_sequence($link);
74 render_login_form($link);
76 header("Content-Type: text/plain");
77 print json_encode(array("error" => array("code" => 6)));
82 $purge_intervals = array(
83 0 => __("Use default"),
84 -1 => __("Never purge"),
85 5 => __("1 week old"),
86 14 => __("2 weeks old"),
87 31 => __("1 month old"),
88 60 => __("2 months old"),
89 90 => __("3 months old"));
91 $update_intervals = array(
92 0 => __("Default interval"),
93 -1 => __("Disable updates"),
94 15 => __("Each 15 minutes"),
95 30 => __("Each 30 minutes"),
97 240 => __("Each 4 hours"),
98 720 => __("Each 12 hours"),
100 10080 => __("Weekly"));
102 $update_intervals_nodefault = array(
103 -1 => __("Disable updates"),
104 15 => __("Each 15 minutes"),
105 30 => __("Each 30 minutes"),
107 240 => __("Each 4 hours"),
108 720 => __("Each 12 hours"),
110 10080 => __("Weekly"));
112 $update_methods = array(
115 2 => __("SimplePie"),
116 3 => __("Twitter OAuth"));
118 if (DEFAULT_UPDATE_METHOD
== "1") {
119 $update_methods[0] .= ' (SimplePie)';
121 $update_methods[0] .= ' (Magpie)';
124 $access_level_names = array(
126 5 => __("Power User"),
127 10 => __("Administrator"));
129 require_once "modules/pref-prefs.php";
130 require_once "modules/popup-dialog.php";
131 require_once "modules/help.php";
132 require_once "modules/pref-feeds.php";
133 require_once "modules/pref-filters.php";
134 require_once "modules/pref-labels.php";
135 require_once "modules/pref-users.php";
136 require_once "modules/pref-instances.php";
138 $error = sanity_check($link);
140 if ($error['code'] != 0 && $op != "logout") {
141 print json_encode(array("error" => $error));
145 switch($op) { // Select action according to $op value.
147 // Handle remote procedure calls.
148 handle_rpc_request($link);
152 $subop = $_REQUEST["subop"];
153 $root = (bool)$_REQUEST["root"];
157 db_query($link, "UPDATE ttrss_user_entries SET
158 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
159 ccache_zero_all($link, $_SESSION["uid"]);
164 $cat_id = db_escape_string($_REQUEST["cid"]);
165 $mode = (int) db_escape_string($_REQUEST['mode']);
166 toggle_collapse_cat($link, $cat_id, $mode);
172 print json_encode(outputFeedList($link));
175 $feeds = outputFeedList($link, false);
178 $root['id'] = 'root';
179 $root['name'] = __('Feeds');
180 $root['items'] = $feeds['items'];
183 $fl['identifier'] = 'id';
184 $fl['label'] = 'name';
185 $fl['items'] = array($root);
187 print json_encode($fl);
193 $id = db_escape_string($_REQUEST['id']);
195 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
196 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
199 if (db_num_rows($result) == 1) {
200 $article_url = db_fetch_result($result, 0, 'link');
201 $article_url = str_replace("\n", "", $article_url);
203 header("Location: $article_url");
207 print_error(__("Article not found."));
213 $id = db_escape_string($_REQUEST["id"]);
214 $cids = explode(",", db_escape_string($_REQUEST["cids"]));
215 $mode = db_escape_string($_REQUEST["mode"]);
216 $omode = db_escape_string($_REQUEST["omode"]);
218 // in prefetch mode we only output requested cids, main article
219 // just gets marked as read (it already exists in client cache)
224 array_push($articles, format_article($link, $id, false));
225 } else if ($mode == "zoom") {
226 array_push($articles, format_article($link, $id, true, true));
227 } else if ($mode == "raw") {
228 if ($_REQUEST['html']) {
229 header("Content-Type: text/html");
230 print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
233 $article = format_article($link, $id, false);
234 print $article['content'];
238 catchupArticleById($link, $id, 0);
240 if (!$_SESSION["bw_limit"]) {
241 foreach ($cids as $cid) {
243 array_push($articles, format_article($link, $cid, false, false));
248 print json_encode($articles);
254 $timing_info = getmicrotime();
258 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
260 $omode = db_escape_string($_REQUEST["omode"]);
262 $feed = db_escape_string($_REQUEST["feed"]);
263 $subop = db_escape_string($_REQUEST["subop"]);
264 $view_mode = db_escape_string($_REQUEST["view_mode"]);
265 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
266 @$cat_view = db_escape_string($_REQUEST["cat"]) == "true";
267 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
268 @$offset = db_escape_string($_REQUEST["skip"]);
269 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
270 $order_by = db_escape_string($_REQUEST["order_by"]);
272 if (is_numeric($feed)) $feed = (int) $feed;
274 /* Feed -5 is a special case: it is used to display auxiliary information
275 * when there's nothing to load - e.g. no stuff in fresh feed */
278 print json_encode(generate_dashboard_feed($link));
285 $label_feed = -11-$feed;
286 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
287 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
288 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
289 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
290 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
291 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
292 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
293 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
296 if ($result && db_num_rows($result) == 0) {
297 print json_encode(generate_error_feed($link, __("Feed not found.")));
301 /* Updating a label ccache means recalculating all of the caches
302 * so for performance reasons we don't do that here */
305 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
308 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
309 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
310 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
312 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
313 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
314 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
317 $reply['headlines'] = array();
319 if (!$next_unread_feed)
320 $reply['headlines']['id'] = $feed;
322 $reply['headlines']['id'] = $next_unread_feed;
324 $reply['headlines']['is_cat'] = (bool) $cat_view;
326 $override_order = false;
328 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
329 $date_sort_field = "updated";
331 $date_sort_field = "date_entered";
336 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
337 $override_order = "$date_sort_field";
339 $override_order = "$date_sort_field DESC";
344 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
345 $override_order = "title DESC, $date_sort_field";
347 $override_order = "title, $date_sort_field DESC";
352 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
353 $override_order = "score, $date_sort_field";
355 $override_order = "score DESC, $date_sort_field DESC";
360 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
362 $ret = format_headlines_list($link, $feed, $subop,
363 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
364 $vgroup_last_feed, $override_order);
366 $topmost_article_ids = $ret[0];
367 $headlines_count = $ret[1];
368 $returned_feed = $ret[2];
369 $disable_cache = $ret[3];
370 $vgroup_last_feed = $ret[4];
372 // if ($_REQUEST["debug"]) print_r($ret);
374 $reply['headlines']['content'] =& $ret[5]['content'];
375 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
377 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
379 $reply['headlines-info'] = array("count" => (int) $headlines_count,
380 "vgroup_last_feed" => $vgroup_last_feed,
381 "disable_cache" => (bool) $disable_cache);
383 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
385 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
388 foreach ($topmost_article_ids as $id) {
389 array_push($articles, format_article($link, $id, false));
392 $reply['articles'] = $articles;
396 // $reply['counters'] = getAllCounters($link, $omode, $feed);
399 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
401 $reply['runtime-info'] = make_runtime_info($link);
403 print json_encode($reply);
408 module_pref_feeds($link);
412 module_pref_filters($link);
413 break; // pref-filters
416 module_pref_labels($link);
417 break; // pref-labels
420 module_pref_prefs($link);
424 module_pref_users($link);
425 break; // prefs-users
432 module_popup_dialog($link);
435 case "pref-pub-items":
436 module_pref_pub_items($link);
437 break; // pref-pub-items
439 case "pref-feed-browser":
440 module_pref_feed_browser($link);
441 break; // pref-feed-browser
443 case "pref-instances":
444 module_pref_instances($link);
445 break; // pref-instances
448 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
452 send_headlines_digests($link);
456 header("Content-type: text/html");
457 print __("Loading, please wait...") . " " .
458 "<img src='images/indicator_tiny.gif'>";
462 header("Content-Type: text/plain");
463 print json_encode(array("error" => array("code" => 7)));
465 } // Select action according to $op value.
467 // We close the connection to database.