]>
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"));
136 if (DEFAULT_UPDATE_METHOD
== "1") {
137 $update_methods[0] .= ' (SimplePie)';
139 $update_methods[0] .= ' (Magpie)';
142 $access_level_names = array(
144 5 => __("Power User"),
145 10 => __("Administrator"));
147 require_once "modules/pref-prefs.php";
148 require_once "modules/popup-dialog.php";
149 require_once "modules/help.php";
150 require_once "modules/pref-feeds.php";
151 require_once "modules/pref-filters.php";
152 require_once "modules/pref-labels.php";
153 require_once "modules/pref-users.php";
155 if (!sanity_check($link)) { return; }
157 switch($op) { // Select action according to $op value.
159 // Handle remote procedure calls.
160 handle_rpc_request($link);
164 $subop = $_REQUEST["subop"];
165 $root = (bool)$_REQUEST["root"];
169 db_query($link, "UPDATE ttrss_user_entries SET
170 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
171 ccache_zero_all($link, $_SESSION["uid"]);
176 $cat_id = db_escape_string($_REQUEST["cid"]);
177 $mode = (int) db_escape_string($_REQUEST['mode']);
178 toggle_collapse_cat($link, $cat_id, $mode);
184 print json_encode(outputFeedList($link));
187 $feeds = outputFeedList($link, false);
190 $root['id'] = 'root';
191 $root['name'] = __('Feeds');
192 $root['items'] = $feeds['items'];
195 $fl['identifier'] = 'id';
196 $fl['label'] = 'name';
197 $fl['items'] = array($root);
199 print json_encode($fl);
205 $id = db_escape_string($_REQUEST['id']);
207 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
208 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
210 if (db_num_rows($result) == 1) {
211 $article_url = db_fetch_result($result, 0, 'link');
213 header("Location: $article_url");
217 print_error(__("Article not found."));
223 $id = db_escape_string($_REQUEST["id"]);
224 $cids = split(",", db_escape_string($_REQUEST["cids"]));
225 $mode = db_escape_string($_REQUEST["mode"]);
226 $omode = db_escape_string($_REQUEST["omode"]);
228 if ($mode != "zoom") print "<reply>";
230 // in prefetch mode we only output requested cids, main article
231 // just gets marked as read (it already exists in client cache)
234 outputArticleXML($link, $id, false);
235 } else if ($mode == "zoom") {
236 outputArticleXML($link, $id, false, true, true);
238 catchupArticleById($link, $id, 0);
241 if (!$_SESSION["bw_limit"]) {
242 foreach ($cids as $cid) {
244 outputArticleXML($link, $cid, false, false);
249 /* if ($mode == "prefetch") {
250 print "<counters><![CDATA[";
251 print json_encode(getAllCounters($link, $omode));
252 print "]]></counters>";
255 if ($mode != "zoom") print "</reply>";
260 $print_exec_time = true;
261 $timing_info = getmicrotime();
265 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
267 $omode = db_escape_string($_REQUEST["omode"]);
269 $feed = db_escape_string($_REQUEST["feed"]);
270 $subop = db_escape_string($_REQUEST["subop"]);
271 $view_mode = db_escape_string($_REQUEST["view_mode"]);
272 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
273 @$cat_view = db_escape_string($_REQUEST["cat"]);
274 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
275 @$offset = db_escape_string($_REQUEST["skip"]);
276 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
277 $order_by = db_escape_string($_REQUEST["order_by"]);
279 /* Feed -5 is a special case: it is used to display auxiliary information
280 * when there's nothing to load - e.g. no stuff in fresh feed */
283 generate_dashboard_feed($link);
288 /* Updating a label ccache means recalculating all of the caches
289 * so for performance reasons we don't do that here */
292 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
295 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
296 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
297 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
299 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
300 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
301 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
304 if (!$next_unread_feed) {
305 print "<headlines id=\"$feed\" is_cat=\"$cat_view\">";
307 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\">";
310 $override_order = false;
312 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
313 $date_sort_field = "updated";
315 $date_sort_field = "date_entered";
320 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
321 $override_order = "$date_sort_field";
323 $override_order = "$date_sort_field DESC";
328 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
329 $override_order = "title DESC, $date_sort_field";
331 $override_order = "title, $date_sort_field DESC";
336 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
337 $override_order = "score, $date_sort_field";
339 $override_order = "score DESC, $date_sort_field DESC";
344 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
346 $ret = outputHeadlinesList($link, $feed, $subop,
347 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
348 $vgroup_last_feed, $override_order);
350 $topmost_article_ids = $ret[0];
351 $headlines_count = $ret[1];
352 $returned_feed = $ret[2];
353 $disable_cache = $ret[3];
354 $vgroup_last_feed = $ret[4];
356 print "</headlines>";
358 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
360 //print "<headlines-count value=\"$headlines_count\"/>";
361 //print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
363 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
366 if ($headlines_unread == -1) {
367 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
371 //print "<headlines-unread value=\"$headlines_unread\"/>";
372 //printf("<disable-cache value=\"%d\"/>", $disable_cache);
374 print "<headlines-info><![CDATA[";
376 $info = array("count" => (int) $headlines_count,
377 "vgroup_last_feed" => $vgroup_last_feed,
378 "unread" => (int) $headlines_unread,
379 "disable_cache" => (bool) $disable_cache);
381 print json_encode($info);
383 print "]]></headlines-info>";
385 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("10", $timing_info);
387 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
389 foreach ($topmost_article_ids as $id) {
390 outputArticleXML($link, $id, $feed, false);
395 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
397 //if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop) {
399 print "<counters><![CDATA[";
400 print json_encode(getAllCounters($link, $omode, $feed));
401 print "]]></counters>";
404 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
406 print_runtime_info($link);
412 module_pref_feeds($link);
416 module_pref_filters($link);
417 break; // pref-filters
420 module_pref_labels($link);
421 break; // pref-labels
424 module_pref_prefs($link);
428 module_pref_users($link);
429 break; // prefs-users
436 module_popup_dialog($link);
439 case "pref-pub-items":
440 module_pref_pub_items($link);
441 break; // pref-pub-items
443 case "globalUpdateFeeds":
444 // update feeds of all users, may be used anonymously
447 // Update all feeds needing a update.
448 update_daemon_common($link, 0, true, true);
452 <message msg=\"All feeds updated\"/>
454 break; // globalUpdateFeeds
456 case "pref-feed-browser":
457 module_pref_feed_browser($link);
458 break; // pref-feed-browser
461 $feed = db_escape_string($_REQUEST["id"]);
462 $key = db_escape_string($_REQUEST["key"]);
463 $is_cat = $_REQUEST["is_cat"] != false;
464 $limit = (int)db_escape_string($_REQUEST["limit"]);
466 $search = db_escape_string($_REQUEST["q"]);
467 $match_on = db_escape_string($_REQUEST["m"]);
468 $search_mode = db_escape_string($_REQUEST["smode"]);
469 $view_mode = db_escape_string($_REQUEST["view-mode"]);
471 if (SINGLE_USER_MODE
) {
472 authenticate_user($link, "admin", null);
475 if ($key && !$_SESSION["uid"]) {
476 $result = db_query($link, "SELECT owner_uid FROM
477 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
479 if (db_num_rows($result) == 1)
480 $_SESSION["uid"] = db_fetch_result($result, 0, "owner_uid");
484 if ($_SESSION["uid"]) {
485 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
486 $search, $search_mode, $match_on, $view_mode);
491 $login = db_escape_string($_REQUEST["login"]);
492 $fresh = $_REQUEST["fresh"] == "1";
494 header("Content-Type: text/plain; charset=utf-8");
496 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
498 if (db_num_rows($result) == 1) {
499 $uid = db_fetch_result($result, 0, "id");
501 print getGlobalUnread($link, $uid);
505 print getFeedArticles($link, -3, false, true, $uid);
509 print "-1;User not found";
512 $print_exec_time = false;
516 header("Content-Type: text/plain");
517 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
518 $print_exec_time = false;
522 header("Content-Type: text/plain");
523 send_headlines_digests($link);
524 $print_exec_time = false;
528 $login = db_escape_string($_REQUEST["login"]);
529 $password = db_escape_string($_REQUEST["password"]);
531 if (authenticate_user($link, $login, $password)) {
532 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
533 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
535 print "<select style='width: 100%' name='profile'>";
537 print "<option value='0'>" . __("Default profile") . "</option>";
539 while ($line = db_fetch_assoc($result)) {
541 $title = $line["title"];
543 print "<option value='$id'>$title</option>";
552 } // Select action according to $op value.
554 // We close the connection to database.
558 <?php
if ($print_exec_time) { ?
>
559 <!-- <?php
echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?
> -->