]> git.wh0rd.org - tt-rss.git/blob - backend.php
Merge branch 'master' of github.com:gothfox/Tiny-Tiny-RSS
[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
130
131 $error = sanity_check($link);
132
133 if ($error['code'] != 0 && $op != "logout") {
134 print json_encode(array("error" => $error));
135 return;
136 }
137
138 switch($op) { // Select action according to $op value.
139 case "rpc":
140 // Handle remote procedure calls.
141 handle_rpc_request($link);
142 break; // rpc
143
144 case "feeds":
145 $subop = $_REQUEST["subop"];
146 $root = (bool)$_REQUEST["root"];
147
148 switch($subop) {
149 case "catchupAll":
150 db_query($link, "UPDATE ttrss_user_entries SET
151 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
152 ccache_zero_all($link, $_SESSION["uid"]);
153
154 break;
155
156 case "collapse":
157 $cat_id = db_escape_string($_REQUEST["cid"]);
158 $mode = (int) db_escape_string($_REQUEST['mode']);
159 toggle_collapse_cat($link, $cat_id, $mode);
160 return;
161 break;
162 }
163
164 if (!$root) {
165 print json_encode(outputFeedList($link));
166 } else {
167
168 $feeds = outputFeedList($link, false);
169
170 $root = array();
171 $root['id'] = 'root';
172 $root['name'] = __('Feeds');
173 $root['items'] = $feeds['items'];
174
175 $fl = array();
176 $fl['identifier'] = 'id';
177 $fl['label'] = 'name';
178 $fl['items'] = array($root);
179
180 print json_encode($fl);
181 }
182
183 break; // feeds
184
185 case "la":
186 $id = db_escape_string($_REQUEST['id']);
187
188 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
189 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
190 LIMIT 1");
191
192 if (db_num_rows($result) == 1) {
193 $article_url = db_fetch_result($result, 0, 'link');
194 $article_url = str_replace("\n", "", $article_url);
195
196 header("Location: $article_url");
197 return;
198
199 } else {
200 print_error(__("Article not found."));
201 }
202 break;
203
204 case "view":
205
206 $id = db_escape_string($_REQUEST["id"]);
207 $cids = explode(",", db_escape_string($_REQUEST["cids"]));
208 $mode = db_escape_string($_REQUEST["mode"]);
209 $omode = db_escape_string($_REQUEST["omode"]);
210
211 // in prefetch mode we only output requested cids, main article
212 // just gets marked as read (it already exists in client cache)
213
214 $articles = array();
215
216 if ($mode == "") {
217 array_push($articles, format_article($link, $id, false));
218 } else if ($mode == "zoom") {
219 array_push($articles, format_article($link, $id, true, true));
220 } else if ($mode == "raw") {
221 if ($_REQUEST['html']) {
222 header("Content-Type: text/html");
223 print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
224 }
225
226 $article = format_article($link, $id, false);
227 print $article['content'];
228 return;
229 }
230
231 catchupArticleById($link, $id, 0);
232
233 if (!$_SESSION["bw_limit"]) {
234 foreach ($cids as $cid) {
235 if ($cid) {
236 array_push($articles, format_article($link, $cid, false, false));
237 }
238 }
239 }
240
241 print json_encode($articles);
242
243 break; // view
244
245 case "viewfeed":
246
247 $timing_info = getmicrotime();
248
249 $reply = array();
250
251 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
252
253 $omode = db_escape_string($_REQUEST["omode"]);
254
255 $feed = db_escape_string($_REQUEST["feed"]);
256 $subop = db_escape_string($_REQUEST["subop"]);
257 $view_mode = db_escape_string($_REQUEST["view_mode"]);
258 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
259 @$cat_view = db_escape_string($_REQUEST["cat"]) == "true";
260 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
261 @$offset = db_escape_string($_REQUEST["skip"]);
262 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
263 $order_by = db_escape_string($_REQUEST["order_by"]);
264
265 if (is_numeric($feed)) $feed = (int) $feed;
266
267 /* Feed -5 is a special case: it is used to display auxiliary information
268 * when there's nothing to load - e.g. no stuff in fresh feed */
269
270 if ($feed == -5) {
271 print json_encode(generate_dashboard_feed($link));
272 return;
273 }
274
275 $result = false;
276
277 if ($feed < -10) {
278 $label_feed = -11-$feed;
279 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
280 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
281 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
282 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
283 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
284 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
285 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
286 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
287 }
288
289 if ($result && db_num_rows($result) == 0) {
290 print json_encode(generate_error_feed($link, __("Feed not found.")));
291 return;
292 }
293
294 /* Updating a label ccache means recalculating all of the caches
295 * so for performance reasons we don't do that here */
296
297 if ($feed >= 0) {
298 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
299 }
300
301 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
302 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
303 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
304
305 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
306 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
307 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
308 }
309
310 $reply['headlines'] = array();
311
312 if (!$next_unread_feed)
313 $reply['headlines']['id'] = $feed;
314 else
315 $reply['headlines']['id'] = $next_unread_feed;
316
317 $reply['headlines']['is_cat'] = (bool) $cat_view;
318
319 $override_order = false;
320
321 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
322 $date_sort_field = "updated";
323 } else {
324 $date_sort_field = "date_entered";
325 }
326
327 switch ($order_by) {
328 case "date":
329 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
330 $override_order = "$date_sort_field";
331 } else {
332 $override_order = "$date_sort_field DESC";
333 }
334 break;
335
336 case "title":
337 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
338 $override_order = "title DESC, $date_sort_field";
339 } else {
340 $override_order = "title, $date_sort_field DESC";
341 }
342 break;
343
344 case "score":
345 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
346 $override_order = "score, $date_sort_field";
347 } else {
348 $override_order = "score DESC, $date_sort_field DESC";
349 }
350 break;
351 }
352
353 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
354
355 $ret = format_headlines_list($link, $feed, $subop,
356 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
357 $vgroup_last_feed, $override_order);
358
359 $topmost_article_ids = $ret[0];
360 $headlines_count = $ret[1];
361 $returned_feed = $ret[2];
362 $disable_cache = $ret[3];
363 $vgroup_last_feed = $ret[4];
364
365 // if ($_REQUEST["debug"]) print_r($ret);
366
367 $reply['headlines']['content'] =& $ret[5]['content'];
368 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
369
370 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
371
372 $reply['headlines-info'] = array("count" => (int) $headlines_count,
373 "vgroup_last_feed" => $vgroup_last_feed,
374 "disable_cache" => (bool) $disable_cache);
375
376 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
377
378 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
379 $articles = array();
380
381 foreach ($topmost_article_ids as $id) {
382 array_push($articles, format_article($link, $id, false));
383 }
384
385 $reply['articles'] = $articles;
386 }
387
388 // if ($subop) {
389 // $reply['counters'] = getAllCounters($link, $omode, $feed);
390 // }
391
392 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
393
394 $reply['runtime-info'] = make_runtime_info($link);
395
396 print json_encode($reply);
397
398 break; // viewfeed
399
400 case "pref-feeds":
401 require_once "modules/pref-feeds.php";
402 module_pref_feeds($link);
403 break; // pref-feeds
404
405 case "pref-filters":
406 require_once "modules/pref-filters.php";
407 module_pref_filters($link);
408 break; // pref-filters
409
410 case "pref-labels":
411 require_once "modules/pref-labels.php";
412 module_pref_labels($link);
413 break; // pref-labels
414
415 case "pref-prefs":
416 require_once "modules/pref-prefs.php";
417 module_pref_prefs($link);
418 break; // pref-prefs
419
420 case "pref-users":
421 require_once "modules/pref-users.php";
422 module_pref_users($link);
423 break; // prefs-users
424
425 case "help":
426 require_once "modules/help.php";
427 module_help($link);
428 break; // help
429
430 case "dlg":
431 require_once "modules/popup-dialog.php";
432 module_popup_dialog($link);
433 break; // dlg
434
435 case "pref-instances":
436 require_once "modules/pref-instances.php";
437 module_pref_instances($link);
438 break; // pref-instances
439
440 case "digestTest":
441 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
442 break; // digestTest
443
444 case "digestSend":
445 send_headlines_digests($link);
446 break; // digestSend
447
448 case "loading":
449 header("Content-type: text/html");
450 print __("Loading, please wait...") . " " .
451 "<img src='images/indicator_tiny.gif'>";
452 break; // loading
453
454 default:
455 header("Content-Type: text/plain");
456 print json_encode(array("error" => array("code" => 7)));
457 break; // fallback
458 } // Select action according to $op value.
459
460 // We close the connection to database.
461 db_close($link);
462 ?>