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