]>
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 function __autoload($class) {
20 $file = "classes/".strtolower(basename($class)).".php";
21 if (file_exists($file)) {
26 $op = $_REQUEST["op"];
28 require_once "functions.php";
29 if ($op != "share") require_once "sessions.php";
30 require_once "sanity_check.php";
31 require_once "config.php";
32 require_once "db.php";
33 require_once "db-prefs.php";
35 no_cache_incantation();
39 $script_started = getmicrotime();
41 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
44 if (DB_TYPE
== "mysql") {
47 // PG seems to display its own errors just fine by default.
51 init_connection($link);
53 $method = strtolower($_REQUEST["method"]);
54 $mode = $_REQUEST["mode"];
56 if ((!$op ||
$op == "rss" ||
$op == "dlg") && !$_REQUEST["noxml"]) {
57 header("Content-Type: application/xml; charset=utf-8");
59 header("Content-Type: text/plain; charset=utf-8");
62 if (ENABLE_GZIP_OUTPUT
) {
63 ob_start("ob_gzhandler");
66 if (SINGLE_USER_MODE
) {
67 authenticate_user($link, "admin", null);
70 $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
71 "fbexport", "logout", "pubsub");
73 if (array_search($op, $public_calls) !== false) {
75 handle_public_request($link, $op);
78 } else if (!($_SESSION["uid"] && validate_session($link))) {
79 if ($op == 'pref-feeds' && $_REQUEST['method'] == 'add') {
80 header("Content-Type: text/html");
81 login_sequence($link);
82 render_login_form($link);
84 header("Content-Type: text/plain");
85 print json_encode(array("error" => array("code" => 6)));
90 $purge_intervals = array(
91 0 => __("Use default"),
92 -1 => __("Never purge"),
93 5 => __("1 week old"),
94 14 => __("2 weeks old"),
95 31 => __("1 month old"),
96 60 => __("2 months old"),
97 90 => __("3 months old"));
99 $update_intervals = array(
100 0 => __("Default interval"),
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_intervals_nodefault = array(
111 -1 => __("Disable updates"),
112 15 => __("Each 15 minutes"),
113 30 => __("Each 30 minutes"),
115 240 => __("Each 4 hours"),
116 720 => __("Each 12 hours"),
118 10080 => __("Weekly"));
120 $update_methods = array(
123 2 => __("SimplePie"),
124 3 => __("Twitter OAuth"));
126 if (DEFAULT_UPDATE_METHOD
== "1") {
127 $update_methods[0] .= ' (SimplePie)';
129 $update_methods[0] .= ' (Magpie)';
132 $access_level_names = array(
134 5 => __("Power User"),
135 10 => __("Administrator"));
139 $error = sanity_check($link);
141 if ($error['code'] != 0 && $op != "logout") {
142 print json_encode(array("error" => $error));
146 if (class_exists($op)) {
147 $handler = new $op($link, $_REQUEST);
150 if ($handler->before()) {
151 if (method_exists($handler, $method)) {
152 return $handler->$method();
158 switch($op) { // Select action according to $op value.
160 $method = $_REQUEST["method"];
161 $root = (bool)$_REQUEST["root"];
165 db_query($link, "UPDATE ttrss_user_entries SET
166 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
167 ccache_zero_all($link, $_SESSION["uid"]);
172 $cat_id = db_escape_string($_REQUEST["cid"]);
173 $mode = (int) db_escape_string($_REQUEST['mode']);
174 toggle_collapse_cat($link, $cat_id, $mode);
180 print json_encode(outputFeedList($link));
183 $feeds = outputFeedList($link, false);
186 $root['id'] = 'root';
187 $root['name'] = __('Feeds');
188 $root['items'] = $feeds['items'];
191 $fl['identifier'] = 'id';
192 $fl['label'] = 'name';
193 $fl['items'] = array($root);
195 print json_encode($fl);
201 $id = db_escape_string($_REQUEST['id']);
203 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
204 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
207 if (db_num_rows($result) == 1) {
208 $article_url = db_fetch_result($result, 0, 'link');
209 $article_url = str_replace("\n", "", $article_url);
211 header("Location: $article_url");
215 print_error(__("Article not found."));
221 $id = db_escape_string($_REQUEST["id"]);
222 $cids = explode(",", db_escape_string($_REQUEST["cids"]));
223 $mode = db_escape_string($_REQUEST["mode"]);
224 $omode = db_escape_string($_REQUEST["omode"]);
226 // in prefetch mode we only output requested cids, main article
227 // just gets marked as read (it already exists in client cache)
232 array_push($articles, format_article($link, $id, false));
233 } else if ($mode == "zoom") {
234 array_push($articles, format_article($link, $id, true, true));
235 } else if ($mode == "raw") {
236 if ($_REQUEST['html']) {
237 header("Content-Type: text/html");
238 print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
241 $article = format_article($link, $id, false);
242 print $article['content'];
246 catchupArticleById($link, $id, 0);
248 if (!$_SESSION["bw_limit"]) {
249 foreach ($cids as $cid) {
251 array_push($articles, format_article($link, $cid, false, false));
256 print json_encode($articles);
262 $timing_info = getmicrotime();
266 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
268 $omode = db_escape_string($_REQUEST["omode"]);
270 $feed = db_escape_string($_REQUEST["feed"]);
271 $method = db_escape_string($_REQUEST["method"]);
272 $view_mode = db_escape_string($_REQUEST["view_mode"]);
273 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
274 @$cat_view = db_escape_string($_REQUEST["cat"]) == "true";
275 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
276 @$offset = db_escape_string($_REQUEST["skip"]);
277 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
278 $order_by = db_escape_string($_REQUEST["order_by"]);
280 if (is_numeric($feed)) $feed = (int) $feed;
282 /* Feed -5 is a special case: it is used to display auxiliary information
283 * when there's nothing to load - e.g. no stuff in fresh feed */
286 print json_encode(generate_dashboard_feed($link));
293 $label_feed = -11-$feed;
294 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
295 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
296 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
297 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
298 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
299 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
300 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
301 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
304 if ($result && db_num_rows($result) == 0) {
305 print json_encode(generate_error_feed($link, __("Feed not found.")));
309 /* Updating a label ccache means recalculating all of the caches
310 * so for performance reasons we don't do that here */
313 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
316 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
317 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
318 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
320 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
321 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
322 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
325 $reply['headlines'] = array();
327 if (!$next_unread_feed)
328 $reply['headlines']['id'] = $feed;
330 $reply['headlines']['id'] = $next_unread_feed;
332 $reply['headlines']['is_cat'] = (bool) $cat_view;
334 $override_order = false;
336 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
337 $date_sort_field = "updated";
339 $date_sort_field = "date_entered";
344 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
345 $override_order = "$date_sort_field";
347 $override_order = "$date_sort_field DESC";
352 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
353 $override_order = "title DESC, $date_sort_field";
355 $override_order = "title, $date_sort_field DESC";
360 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
361 $override_order = "score, $date_sort_field";
363 $override_order = "score DESC, $date_sort_field DESC";
368 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
370 $ret = format_headlines_list($link, $feed, $method,
371 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
372 $vgroup_last_feed, $override_order);
374 $topmost_article_ids = $ret[0];
375 $headlines_count = $ret[1];
376 $returned_feed = $ret[2];
377 $disable_cache = $ret[3];
378 $vgroup_last_feed = $ret[4];
380 // if ($_REQUEST["debug"]) print_r($ret);
382 $reply['headlines']['content'] =& $ret[5]['content'];
383 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
385 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
387 $reply['headlines-info'] = array("count" => (int) $headlines_count,
388 "vgroup_last_feed" => $vgroup_last_feed,
389 "disable_cache" => (bool) $disable_cache);
391 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
393 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
396 foreach ($topmost_article_ids as $id) {
397 array_push($articles, format_article($link, $id, false));
400 $reply['articles'] = $articles;
404 // $reply['counters'] = getAllCounters($link, $omode, $feed);
407 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
409 $reply['runtime-info'] = make_runtime_info($link);
411 print json_encode($reply);
415 require_once "modules/pref-feeds.php";
416 module_pref_feeds($link);
420 require_once "modules/pref-filters.php";
421 module_pref_filters($link);
422 break; // pref-filters
425 require_once "modules/pref-labels.php";
426 module_pref_labels($link);
427 break; // pref-labels
430 require_once "modules/pref-prefs.php";
431 module_pref_prefs($link);
435 require_once "modules/pref-users.php";
436 module_pref_users($link);
437 break; // prefs-users
440 require_once "modules/help.php";
445 require_once "modules/popup-dialog.php";
446 module_popup_dialog($link);
449 case "pref-instances":
450 require_once "modules/pref-instances.php";
451 module_pref_instances($link);
452 break; // pref-instances
455 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
459 send_headlines_digests($link);
463 header("Content-type: text/html");
464 print __("Loading, please wait...") . " " .
465 "<img src='images/indicator_tiny.gif'>";
469 header("Content-Type: text/plain");
470 print json_encode(array("error" => array("code" => 7)));
472 } // Select action according to $op value.
474 // We close the connection to database.