]> git.wh0rd.org - tt-rss.git/blob - backend.php
reduce the number of always included libraries
[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 "sanity_check.php";
22 require_once "config.php";
23 require_once "db.php";
24 require_once "db-prefs.php";
25
26 no_cache_incantation();
27
28 startup_gettext();
29
30 $script_started = getmicrotime();
31
32 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
33
34 if (!$link) {
35 if (DB_TYPE == "mysql") {
36 print mysql_error();
37 }
38 // PG seems to display its own errors just fine by default.
39 return;
40 }
41
42 init_connection($link);
43
44 $subop = $_REQUEST["subop"];
45 $mode = $_REQUEST["mode"];
46
47 if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
48 header("Content-Type: application/xml; charset=utf-8");
49 } else {
50 header("Content-Type: text/plain; charset=utf-8");
51 }
52
53 if (ENABLE_GZIP_OUTPUT) {
54 ob_start("ob_gzhandler");
55 }
56
57 if (SINGLE_USER_MODE) {
58 authenticate_user($link, "admin", null);
59 }
60
61 $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
62 "fbexport", "logout", "pubsub");
63
64 if (array_search($op, $public_calls) !== false) {
65
66 handle_public_request($link, $op);
67 return;
68
69 } else if (!($_SESSION["uid"] && validate_session($link))) {
70 if ($op == 'pref-feeds' && $_REQUEST['subop'] == 'add') {
71 header("Content-Type: text/html");
72 login_sequence($link);
73 render_login_form($link);
74 } else {
75 header("Content-Type: text/plain");
76 print json_encode(array("error" => array("code" => 6)));
77 }
78 return;
79 }
80
81 $purge_intervals = array(
82 0 => __("Use default"),
83 -1 => __("Never purge"),
84 5 => __("1 week old"),
85 14 => __("2 weeks old"),
86 31 => __("1 month old"),
87 60 => __("2 months old"),
88 90 => __("3 months old"));
89
90 $update_intervals = array(
91 0 => __("Default interval"),
92 -1 => __("Disable updates"),
93 15 => __("Each 15 minutes"),
94 30 => __("Each 30 minutes"),
95 60 => __("Hourly"),
96 240 => __("Each 4 hours"),
97 720 => __("Each 12 hours"),
98 1440 => __("Daily"),
99 10080 => __("Weekly"));
100
101 $update_intervals_nodefault = array(
102 -1 => __("Disable updates"),
103 15 => __("Each 15 minutes"),
104 30 => __("Each 30 minutes"),
105 60 => __("Hourly"),
106 240 => __("Each 4 hours"),
107 720 => __("Each 12 hours"),
108 1440 => __("Daily"),
109 10080 => __("Weekly"));
110
111 $update_methods = array(
112 0 => __("Default"),
113 1 => __("Magpie"),
114 2 => __("SimplePie"),
115 3 => __("Twitter OAuth"));
116
117 if (DEFAULT_UPDATE_METHOD == "1") {
118 $update_methods[0] .= ' (SimplePie)';
119 } else {
120 $update_methods[0] .= ' (Magpie)';
121 }
122
123 $access_level_names = array(
124 0 => __("User"),
125 5 => __("Power User"),
126 10 => __("Administrator"));
127
128
129
130 $error = sanity_check($link);
131
132 if ($error['code'] != 0 && $op != "logout") {
133 print json_encode(array("error" => $error));
134 return;
135 }
136
137 switch($op) { // Select action according to $op value.
138 case "rpc":
139 require_once "modules/backend-rpc.php";
140 handle_rpc_request($link);
141 break; // rpc
142
143 case "feeds":
144 $subop = $_REQUEST["subop"];
145 $root = (bool)$_REQUEST["root"];
146
147 switch($subop) {
148 case "catchupAll":
149 db_query($link, "UPDATE ttrss_user_entries SET
150 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
151 ccache_zero_all($link, $_SESSION["uid"]);
152
153 break;
154
155 case "collapse":
156 $cat_id = db_escape_string($_REQUEST["cid"]);
157 $mode = (int) db_escape_string($_REQUEST['mode']);
158 toggle_collapse_cat($link, $cat_id, $mode);
159 return;
160 break;
161 }
162
163 if (!$root) {
164 print json_encode(outputFeedList($link));
165 } else {
166
167 $feeds = outputFeedList($link, false);
168
169 $root = array();
170 $root['id'] = 'root';
171 $root['name'] = __('Feeds');
172 $root['items'] = $feeds['items'];
173
174 $fl = array();
175 $fl['identifier'] = 'id';
176 $fl['label'] = 'name';
177 $fl['items'] = array($root);
178
179 print json_encode($fl);
180 }
181
182 break; // feeds
183
184 case "la":
185 $id = db_escape_string($_REQUEST['id']);
186
187 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
188 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
189 LIMIT 1");
190
191 if (db_num_rows($result) == 1) {
192 $article_url = db_fetch_result($result, 0, 'link');
193 $article_url = str_replace("\n", "", $article_url);
194
195 header("Location: $article_url");
196 return;
197
198 } else {
199 print_error(__("Article not found."));
200 }
201 break;
202
203 case "view":
204
205 $id = db_escape_string($_REQUEST["id"]);
206 $cids = explode(",", db_escape_string($_REQUEST["cids"]));
207 $mode = db_escape_string($_REQUEST["mode"]);
208 $omode = db_escape_string($_REQUEST["omode"]);
209
210 // in prefetch mode we only output requested cids, main article
211 // just gets marked as read (it already exists in client cache)
212
213 $articles = array();
214
215 if ($mode == "") {
216 array_push($articles, format_article($link, $id, false));
217 } else if ($mode == "zoom") {
218 array_push($articles, format_article($link, $id, true, true));
219 } else if ($mode == "raw") {
220 if ($_REQUEST['html']) {
221 header("Content-Type: text/html");
222 print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
223 }
224
225 $article = format_article($link, $id, false);
226 print $article['content'];
227 return;
228 }
229
230 catchupArticleById($link, $id, 0);
231
232 if (!$_SESSION["bw_limit"]) {
233 foreach ($cids as $cid) {
234 if ($cid) {
235 array_push($articles, format_article($link, $cid, false, false));
236 }
237 }
238 }
239
240 print json_encode($articles);
241
242 break; // view
243
244 case "viewfeed":
245
246 $timing_info = getmicrotime();
247
248 $reply = array();
249
250 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
251
252 $omode = db_escape_string($_REQUEST["omode"]);
253
254 $feed = db_escape_string($_REQUEST["feed"]);
255 $subop = db_escape_string($_REQUEST["subop"]);
256 $view_mode = db_escape_string($_REQUEST["view_mode"]);
257 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
258 @$cat_view = db_escape_string($_REQUEST["cat"]) == "true";
259 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
260 @$offset = db_escape_string($_REQUEST["skip"]);
261 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
262 $order_by = db_escape_string($_REQUEST["order_by"]);
263
264 if (is_numeric($feed)) $feed = (int) $feed;
265
266 /* Feed -5 is a special case: it is used to display auxiliary information
267 * when there's nothing to load - e.g. no stuff in fresh feed */
268
269 if ($feed == -5) {
270 print json_encode(generate_dashboard_feed($link));
271 return;
272 }
273
274 $result = false;
275
276 if ($feed < -10) {
277 $label_feed = -11-$feed;
278 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
279 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
280 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
281 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
282 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
283 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
284 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
285 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
286 }
287
288 if ($result && db_num_rows($result) == 0) {
289 print json_encode(generate_error_feed($link, __("Feed not found.")));
290 return;
291 }
292
293 /* Updating a label ccache means recalculating all of the caches
294 * so for performance reasons we don't do that here */
295
296 if ($feed >= 0) {
297 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
298 }
299
300 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
301 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
302 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
303
304 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
305 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
306 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
307 }
308
309 $reply['headlines'] = array();
310
311 if (!$next_unread_feed)
312 $reply['headlines']['id'] = $feed;
313 else
314 $reply['headlines']['id'] = $next_unread_feed;
315
316 $reply['headlines']['is_cat'] = (bool) $cat_view;
317
318 $override_order = false;
319
320 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
321 $date_sort_field = "updated";
322 } else {
323 $date_sort_field = "date_entered";
324 }
325
326 switch ($order_by) {
327 case "date":
328 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
329 $override_order = "$date_sort_field";
330 } else {
331 $override_order = "$date_sort_field DESC";
332 }
333 break;
334
335 case "title":
336 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
337 $override_order = "title DESC, $date_sort_field";
338 } else {
339 $override_order = "title, $date_sort_field DESC";
340 }
341 break;
342
343 case "score":
344 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
345 $override_order = "score, $date_sort_field";
346 } else {
347 $override_order = "score DESC, $date_sort_field DESC";
348 }
349 break;
350 }
351
352 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
353
354 $ret = format_headlines_list($link, $feed, $subop,
355 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
356 $vgroup_last_feed, $override_order);
357
358 $topmost_article_ids = $ret[0];
359 $headlines_count = $ret[1];
360 $returned_feed = $ret[2];
361 $disable_cache = $ret[3];
362 $vgroup_last_feed = $ret[4];
363
364 // if ($_REQUEST["debug"]) print_r($ret);
365
366 $reply['headlines']['content'] =& $ret[5]['content'];
367 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
368
369 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
370
371 $reply['headlines-info'] = array("count" => (int) $headlines_count,
372 "vgroup_last_feed" => $vgroup_last_feed,
373 "disable_cache" => (bool) $disable_cache);
374
375 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
376
377 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
378 $articles = array();
379
380 foreach ($topmost_article_ids as $id) {
381 array_push($articles, format_article($link, $id, false));
382 }
383
384 $reply['articles'] = $articles;
385 }
386
387 // if ($subop) {
388 // $reply['counters'] = getAllCounters($link, $omode, $feed);
389 // }
390
391 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
392
393 $reply['runtime-info'] = make_runtime_info($link);
394
395 print json_encode($reply);
396 break; // viewfeed
397
398 case "pref-feeds":
399 require_once "modules/pref-feeds.php";
400 module_pref_feeds($link);
401 break; // pref-feeds
402
403 case "pref-filters":
404 require_once "modules/pref-filters.php";
405 module_pref_filters($link);
406 break; // pref-filters
407
408 case "pref-labels":
409 require_once "modules/pref-labels.php";
410 module_pref_labels($link);
411 break; // pref-labels
412
413 case "pref-prefs":
414 require_once "modules/pref-prefs.php";
415 module_pref_prefs($link);
416 break; // pref-prefs
417
418 case "pref-users":
419 require_once "modules/pref-users.php";
420 module_pref_users($link);
421 break; // prefs-users
422
423 case "help":
424 require_once "modules/help.php";
425 module_help($link);
426 break; // help
427
428 case "dlg":
429 require_once "modules/popup-dialog.php";
430 module_popup_dialog($link);
431 break; // dlg
432
433 case "pref-instances":
434 require_once "modules/pref-instances.php";
435 module_pref_instances($link);
436 break; // pref-instances
437
438 case "digestTest":
439 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
440 break; // digestTest
441
442 case "digestSend":
443 send_headlines_digests($link);
444 break; // digestSend
445
446 case "loading":
447 header("Content-type: text/html");
448 print __("Loading, please wait...") . " " .
449 "<img src='images/indicator_tiny.gif'>";
450 break; // loading
451
452 default:
453 header("Content-Type: text/plain");
454 print json_encode(array("error" => array("code" => 7)));
455 break; // fallback
456 } // Select action according to $op value.
457
458 // We close the connection to database.
459 db_close($link);
460 ?>