]>
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 == "view" && $mode != "zoom") ||
53 $op == "digestSend" ||
$op == "dlg" ||
54 $op == "viewfeed" ||
$op == "publish" ||
55 $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
56 header("Content-Type: application/xml; charset=utf-8");
58 if (ENABLE_GZIP_OUTPUT
) {
59 ob_start("ob_gzhandler");
63 if (!$_REQUEST["noxml"]) {
64 header("Content-Type: text/html; charset=utf-8");
66 header("Content-Type: text/plain; charset=utf-8");
71 header("Content-Type: application/xml");
72 print_error_xml(7); exit;
75 if (SINGLE_USER_MODE
) {
76 authenticate_user($link, "admin", null);
79 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds"
80 && $op != "rss" && $op != "getUnread" && $op != "publish" && $op != "getProfiles") {
82 if ($op == "rpc" ||
$op == "viewfeed" ||
$op == "view") {
83 print_error_xml(6); die;
87 <p>Error: Not logged in.</p>
88 <script type=\"text/javascript\">
89 if (parent.window != 'undefined') {
90 parent.window.location = \"tt-rss.php\";
92 window.location = \"tt-rss.php\";
101 $purge_intervals = array(
102 0 => __("Use default"),
103 -1 => __("Never purge"),
104 5 => __("1 week old"),
105 14 => __("2 weeks old"),
106 31 => __("1 month old"),
107 60 => __("2 months old"),
108 90 => __("3 months old"));
110 $update_intervals = array(
111 0 => __("Default interval"),
112 -1 => __("Disable updates"),
113 15 => __("Each 15 minutes"),
114 30 => __("Each 30 minutes"),
116 240 => __("Each 4 hours"),
117 720 => __("Each 12 hours"),
119 10080 => __("Weekly"));
121 $update_intervals_nodefault = array(
122 -1 => __("Disable updates"),
123 15 => __("Each 15 minutes"),
124 30 => __("Each 30 minutes"),
126 240 => __("Each 4 hours"),
127 720 => __("Each 12 hours"),
129 10080 => __("Weekly"));
131 $update_methods = array(
134 2 => __("SimplePie"),
135 3 => __("Twitter OAuth"));
137 if (DEFAULT_UPDATE_METHOD
== "1") {
138 $update_methods[0] .= ' (SimplePie)';
140 $update_methods[0] .= ' (Magpie)';
143 $access_level_names = array(
145 5 => __("Power User"),
146 10 => __("Administrator"));
148 require_once "modules/pref-prefs.php";
149 require_once "modules/popup-dialog.php";
150 require_once "modules/help.php";
151 require_once "modules/pref-feeds.php";
152 require_once "modules/pref-filters.php";
153 require_once "modules/pref-labels.php";
154 require_once "modules/pref-users.php";
156 if (!sanity_check($link)) { return; }
158 switch($op) { // Select action according to $op value.
160 // Handle remote procedure calls.
161 handle_rpc_request($link);
165 $subop = $_REQUEST["subop"];
166 $root = (bool)$_REQUEST["root"];
170 db_query($link, "UPDATE ttrss_user_entries SET
171 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
172 ccache_zero_all($link, $_SESSION["uid"]);
177 $cat_id = db_escape_string($_REQUEST["cid"]);
178 $mode = (int) db_escape_string($_REQUEST['mode']);
179 toggle_collapse_cat($link, $cat_id, $mode);
185 print json_encode(outputFeedList($link));
188 $feeds = outputFeedList($link, false);
191 $root['id'] = 'root';
192 $root['name'] = __('Feeds');
193 $root['items'] = $feeds['items'];
196 $fl['identifier'] = 'id';
197 $fl['label'] = 'name';
198 $fl['items'] = array($root);
200 print json_encode($fl);
206 $id = db_escape_string($_REQUEST['id']);
208 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
209 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
211 if (db_num_rows($result) == 1) {
212 $article_url = db_fetch_result($result, 0, 'link');
213 $article_url = str_replace("\n", "", $article_url);
215 header("Location: $article_url");
219 print_error(__("Article not found."));
225 $id = db_escape_string($_REQUEST["id"]);
226 $cids = split(",", db_escape_string($_REQUEST["cids"]));
227 $mode = db_escape_string($_REQUEST["mode"]);
228 $omode = db_escape_string($_REQUEST["omode"]);
230 if ($mode != "zoom") print "<reply>";
232 // in prefetch mode we only output requested cids, main article
233 // just gets marked as read (it already exists in client cache)
236 outputArticleXML($link, $id, false);
237 } else if ($mode == "zoom") {
238 outputArticleXML($link, $id, false, true, true);
240 catchupArticleById($link, $id, 0);
243 if (!$_SESSION["bw_limit"]) {
244 foreach ($cids as $cid) {
246 outputArticleXML($link, $cid, false, false);
251 /* if ($mode == "prefetch") {
252 print "<counters><![CDATA[";
253 print json_encode(getAllCounters($link, $omode));
254 print "]]></counters>";
257 if ($mode != "zoom") print "</reply>";
262 $print_exec_time = true;
263 $timing_info = getmicrotime();
267 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
269 $omode = db_escape_string($_REQUEST["omode"]);
271 $feed = db_escape_string($_REQUEST["feed"]);
272 $subop = db_escape_string($_REQUEST["subop"]);
273 $view_mode = db_escape_string($_REQUEST["view_mode"]);
274 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
275 @$cat_view = db_escape_string($_REQUEST["cat"]);
276 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
277 @$offset = db_escape_string($_REQUEST["skip"]);
278 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
279 $order_by = db_escape_string($_REQUEST["order_by"]);
281 /* Feed -5 is a special case: it is used to display auxiliary information
282 * when there's nothing to load - e.g. no stuff in fresh feed */
285 generate_dashboard_feed($link);
290 /* Updating a label ccache means recalculating all of the caches
291 * so for performance reasons we don't do that here */
294 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
297 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
298 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
299 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
301 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
302 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
303 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
306 if (!$next_unread_feed) {
307 print "<headlines id=\"$feed\" is_cat=\"$cat_view\">";
309 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\">";
312 $override_order = false;
314 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
315 $date_sort_field = "updated";
317 $date_sort_field = "date_entered";
322 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
323 $override_order = "$date_sort_field";
325 $override_order = "$date_sort_field DESC";
330 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
331 $override_order = "title DESC, $date_sort_field";
333 $override_order = "title, $date_sort_field DESC";
338 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
339 $override_order = "score, $date_sort_field";
341 $override_order = "score DESC, $date_sort_field DESC";
346 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
348 $ret = outputHeadlinesList($link, $feed, $subop,
349 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
350 $vgroup_last_feed, $override_order);
352 $topmost_article_ids = $ret[0];
353 $headlines_count = $ret[1];
354 $returned_feed = $ret[2];
355 $disable_cache = $ret[3];
356 $vgroup_last_feed = $ret[4];
358 print "</headlines>";
360 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
362 //print "<headlines-count value=\"$headlines_count\"/>";
363 //print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
365 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
368 if ($headlines_unread == -1) {
369 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
373 //print "<headlines-unread value=\"$headlines_unread\"/>";
374 //printf("<disable-cache value=\"%d\"/>", $disable_cache);
376 print "<headlines-info><![CDATA[";
378 $info = array("count" => (int) $headlines_count,
379 "vgroup_last_feed" => $vgroup_last_feed,
380 "unread" => (int) $headlines_unread,
381 "disable_cache" => (bool) $disable_cache);
383 print json_encode($info);
385 print "]]></headlines-info>";
387 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("10", $timing_info);
389 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
391 foreach ($topmost_article_ids as $id) {
392 outputArticleXML($link, $id, $feed, false);
397 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
399 //if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop) {
401 print "<counters><![CDATA[";
402 print json_encode(getAllCounters($link, $omode, $feed));
403 print "]]></counters>";
406 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
408 print_runtime_info($link);
414 module_pref_feeds($link);
418 module_pref_filters($link);
419 break; // pref-filters
422 module_pref_labels($link);
423 break; // pref-labels
426 module_pref_prefs($link);
430 module_pref_users($link);
431 break; // prefs-users
438 module_popup_dialog($link);
441 case "pref-pub-items":
442 module_pref_pub_items($link);
443 break; // pref-pub-items
445 case "globalUpdateFeeds":
446 // update feeds of all users, may be used anonymously
449 // Update all feeds needing a update.
450 update_daemon_common($link, 0, true, true);
454 <message msg=\"All feeds updated\"/>
456 break; // globalUpdateFeeds
458 case "pref-feed-browser":
459 module_pref_feed_browser($link);
460 break; // pref-feed-browser
463 $feed = db_escape_string($_REQUEST["id"]);
464 $key = db_escape_string($_REQUEST["key"]);
465 $is_cat = $_REQUEST["is_cat"] != false;
466 $limit = (int)db_escape_string($_REQUEST["limit"]);
468 $search = db_escape_string($_REQUEST["q"]);
469 $match_on = db_escape_string($_REQUEST["m"]);
470 $search_mode = db_escape_string($_REQUEST["smode"]);
471 $view_mode = db_escape_string($_REQUEST["view-mode"]);
473 if (SINGLE_USER_MODE
) {
474 authenticate_user($link, "admin", null);
478 $result = db_query($link, "SELECT owner_uid FROM
479 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
481 if (db_num_rows($result) == 1)
482 $_SESSION["uid"] = db_fetch_result($result, 0, "owner_uid");
486 if ($_SESSION["uid"]) {
487 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
488 $search, $search_mode, $match_on, $view_mode);
493 $login = db_escape_string($_REQUEST["login"]);
494 $fresh = $_REQUEST["fresh"] == "1";
496 header("Content-Type: text/plain; charset=utf-8");
498 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
500 if (db_num_rows($result) == 1) {
501 $uid = db_fetch_result($result, 0, "id");
503 print getGlobalUnread($link, $uid);
507 print getFeedArticles($link, -3, false, true, $uid);
511 print "-1;User not found";
514 $print_exec_time = false;
518 header("Content-Type: text/plain");
519 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
520 $print_exec_time = false;
524 header("Content-Type: text/plain");
525 send_headlines_digests($link);
526 $print_exec_time = false;
530 print __("Loading, please wait...") . " " .
531 "<img src='images/indicator_tiny.gif'>";
534 $login = db_escape_string($_REQUEST["login"]);
535 $password = db_escape_string($_REQUEST["password"]);
537 if (authenticate_user($link, $login, $password)) {
538 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
539 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
541 print "<select style='width: 100%' name='profile'>";
543 print "<option value='0'>" . __("Default profile") . "</option>";
545 while ($line = db_fetch_assoc($result)) {
547 $title = $line["title"];
549 print "<option value='$id'>$title</option>";
558 } // Select action according to $op value.
561 // We close the connection to database.
565 <?php
if ($print_exec_time) { ?
>
566 <!-- <?php
echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?
> -->