]> git.wh0rd.org - tt-rss.git/blob - backend.php
experimental split of public calls into public.php (refs #389)
[tt-rss.git] / backend.php
1 <?php
2 /* remove ill effects of magic quotes */
3
4 if (get_magic_quotes_gpc()) {
5 function stripslashes_deep($value) {
6 $value = is_array($value) ?
7 array_map('stripslashes_deep', $value) : stripslashes($value);
8 return $value;
9 }
10
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);
15 }
16
17 $op = $_REQUEST["op"];
18
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";
26
27 no_cache_incantation();
28
29 startup_gettext();
30
31 $script_started = getmicrotime();
32
33 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
34
35 if (!$link) {
36 if (DB_TYPE == "mysql") {
37 print mysql_error();
38 }
39 // PG seems to display its own errors just fine by default.
40 return;
41 }
42
43 init_connection($link);
44
45 $subop = $_REQUEST["subop"];
46 $mode = $_REQUEST["mode"];
47
48 if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
49 header("Content-Type: application/xml; charset=utf-8");
50 } else {
51 header("Content-Type: text/plain; charset=utf-8");
52 }
53
54 if (ENABLE_GZIP_OUTPUT) {
55 ob_start("ob_gzhandler");
56 }
57
58 if (SINGLE_USER_MODE) {
59 authenticate_user($link, "admin", null);
60 }
61
62 $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
63 "fbexport", "logout", "pubsub");
64
65 if (array_search($op, $public_calls) !== false) {
66
67 handle_public_request($link, $op);
68 return;
69
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);
75 } else {
76 header("Content-Type: text/plain");
77 print json_encode(array("error" => array("code" => 6)));
78 }
79 return;
80 }
81
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"));
90
91 $update_intervals = array(
92 0 => __("Default interval"),
93 -1 => __("Disable updates"),
94 15 => __("Each 15 minutes"),
95 30 => __("Each 30 minutes"),
96 60 => __("Hourly"),
97 240 => __("Each 4 hours"),
98 720 => __("Each 12 hours"),
99 1440 => __("Daily"),
100 10080 => __("Weekly"));
101
102 $update_intervals_nodefault = array(
103 -1 => __("Disable updates"),
104 15 => __("Each 15 minutes"),
105 30 => __("Each 30 minutes"),
106 60 => __("Hourly"),
107 240 => __("Each 4 hours"),
108 720 => __("Each 12 hours"),
109 1440 => __("Daily"),
110 10080 => __("Weekly"));
111
112 $update_methods = array(
113 0 => __("Default"),
114 1 => __("Magpie"),
115 2 => __("SimplePie"),
116 3 => __("Twitter OAuth"));
117
118 if (DEFAULT_UPDATE_METHOD == "1") {
119 $update_methods[0] .= ' (SimplePie)';
120 } else {
121 $update_methods[0] .= ' (Magpie)';
122 }
123
124 $access_level_names = array(
125 0 => __("User"),
126 5 => __("Power User"),
127 10 => __("Administrator"));
128
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";
137
138 $error = sanity_check($link);
139
140 if ($error['code'] != 0 && $op != "logout") {
141 print json_encode(array("error" => $error));
142 return;
143 }
144
145 switch($op) { // Select action according to $op value.
146 case "rpc":
147 // Handle remote procedure calls.
148 handle_rpc_request($link);
149 break; // rpc
150
151 case "feeds":
152 $subop = $_REQUEST["subop"];
153 $root = (bool)$_REQUEST["root"];
154
155 switch($subop) {
156 case "catchupAll":
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"]);
160
161 break;
162
163 case "collapse":
164 $cat_id = db_escape_string($_REQUEST["cid"]);
165 $mode = (int) db_escape_string($_REQUEST['mode']);
166 toggle_collapse_cat($link, $cat_id, $mode);
167 return;
168 break;
169 }
170
171 if (!$root) {
172 print json_encode(outputFeedList($link));
173 } else {
174
175 $feeds = outputFeedList($link, false);
176
177 $root = array();
178 $root['id'] = 'root';
179 $root['name'] = __('Feeds');
180 $root['items'] = $feeds['items'];
181
182 $fl = array();
183 $fl['identifier'] = 'id';
184 $fl['label'] = 'name';
185 $fl['items'] = array($root);
186
187 print json_encode($fl);
188 }
189
190 break; // feeds
191
192 case "la":
193 $id = db_escape_string($_REQUEST['id']);
194
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']."'
197 LIMIT 1");
198
199 if (db_num_rows($result) == 1) {
200 $article_url = db_fetch_result($result, 0, 'link');
201 $article_url = str_replace("\n", "", $article_url);
202
203 header("Location: $article_url");
204 return;
205
206 } else {
207 print_error(__("Article not found."));
208 }
209 break;
210
211 case "view":
212
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"]);
217
218 // in prefetch mode we only output requested cids, main article
219 // just gets marked as read (it already exists in client cache)
220
221 $articles = array();
222
223 if ($mode == "") {
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"/>';
231 }
232
233 $article = format_article($link, $id, false);
234 print $article['content'];
235 return;
236 }
237
238 catchupArticleById($link, $id, 0);
239
240 if (!$_SESSION["bw_limit"]) {
241 foreach ($cids as $cid) {
242 if ($cid) {
243 array_push($articles, format_article($link, $cid, false, false));
244 }
245 }
246 }
247
248 print json_encode($articles);
249
250 break; // view
251
252 case "viewfeed":
253
254 $timing_info = getmicrotime();
255
256 $reply = array();
257
258 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
259
260 $omode = db_escape_string($_REQUEST["omode"]);
261
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"]);
271
272 if (is_numeric($feed)) $feed = (int) $feed;
273
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 */
276
277 if ($feed == -5) {
278 print json_encode(generate_dashboard_feed($link));
279 return;
280 }
281
282 $result = false;
283
284 if ($feed < -10) {
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']);
294 }
295
296 if ($result && db_num_rows($result) == 0) {
297 print json_encode(generate_error_feed($link, __("Feed not found.")));
298 return;
299 }
300
301 /* Updating a label ccache means recalculating all of the caches
302 * so for performance reasons we don't do that here */
303
304 if ($feed >= 0) {
305 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
306 }
307
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);
311
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"]);
315 }
316
317 $reply['headlines'] = array();
318
319 if (!$next_unread_feed)
320 $reply['headlines']['id'] = $feed;
321 else
322 $reply['headlines']['id'] = $next_unread_feed;
323
324 $reply['headlines']['is_cat'] = (bool) $cat_view;
325
326 $override_order = false;
327
328 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
329 $date_sort_field = "updated";
330 } else {
331 $date_sort_field = "date_entered";
332 }
333
334 switch ($order_by) {
335 case "date":
336 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
337 $override_order = "$date_sort_field";
338 } else {
339 $override_order = "$date_sort_field DESC";
340 }
341 break;
342
343 case "title":
344 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
345 $override_order = "title DESC, $date_sort_field";
346 } else {
347 $override_order = "title, $date_sort_field DESC";
348 }
349 break;
350
351 case "score":
352 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
353 $override_order = "score, $date_sort_field";
354 } else {
355 $override_order = "score DESC, $date_sort_field DESC";
356 }
357 break;
358 }
359
360 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
361
362 $ret = format_headlines_list($link, $feed, $subop,
363 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
364 $vgroup_last_feed, $override_order);
365
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];
371
372 // if ($_REQUEST["debug"]) print_r($ret);
373
374 $reply['headlines']['content'] =& $ret[5]['content'];
375 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
376
377 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
378
379 $reply['headlines-info'] = array("count" => (int) $headlines_count,
380 "vgroup_last_feed" => $vgroup_last_feed,
381 "disable_cache" => (bool) $disable_cache);
382
383 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
384
385 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
386 $articles = array();
387
388 foreach ($topmost_article_ids as $id) {
389 array_push($articles, format_article($link, $id, false));
390 }
391
392 $reply['articles'] = $articles;
393 }
394
395 // if ($subop) {
396 // $reply['counters'] = getAllCounters($link, $omode, $feed);
397 // }
398
399 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
400
401 $reply['runtime-info'] = make_runtime_info($link);
402
403 print json_encode($reply);
404
405 break; // viewfeed
406
407 case "pref-feeds":
408 module_pref_feeds($link);
409 break; // pref-feeds
410
411 case "pref-filters":
412 module_pref_filters($link);
413 break; // pref-filters
414
415 case "pref-labels":
416 module_pref_labels($link);
417 break; // pref-labels
418
419 case "pref-prefs":
420 module_pref_prefs($link);
421 break; // pref-prefs
422
423 case "pref-users":
424 module_pref_users($link);
425 break; // prefs-users
426
427 case "help":
428 module_help($link);
429 break; // help
430
431 case "dlg":
432 module_popup_dialog($link);
433 break; // dlg
434
435 case "pref-pub-items":
436 module_pref_pub_items($link);
437 break; // pref-pub-items
438
439 case "pref-feed-browser":
440 module_pref_feed_browser($link);
441 break; // pref-feed-browser
442
443 case "pref-instances":
444 module_pref_instances($link);
445 break; // pref-instances
446
447 case "digestTest":
448 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
449 break; // digestTest
450
451 case "digestSend":
452 send_headlines_digests($link);
453 break; // digestSend
454
455 case "loading":
456 header("Content-type: text/html");
457 print __("Loading, please wait...") . " " .
458 "<img src='images/indicator_tiny.gif'>";
459 break; // loading
460
461 default:
462 header("Content-Type: text/plain");
463 print json_encode(array("error" => array("code" => 7)));
464 break; // fallback
465 } // Select action according to $op value.
466
467 // We close the connection to database.
468 db_close($link);
469 ?>