]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
016e1898687ac95168ca479651d7f50311e3bcd1
[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         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";
24
25         no_cache_incantation();
26
27         if (ENABLE_TRANSLATIONS == true) {
28                 startup_gettext();
29         }
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         $op = $_REQUEST["op"];
46         $subop = $_REQUEST["subop"];
47         $mode = $_REQUEST["mode"];
48
49         $print_exec_time = false;
50
51         if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
52                         header("Content-Type: application/xml; charset=utf-8");
53         } else {
54                         header("Content-Type: text/plain; charset=utf-8");
55         }
56
57         if (ENABLE_GZIP_OUTPUT) {
58                 ob_start("ob_gzhandler");
59         }
60
61         if (SINGLE_USER_MODE) {
62                 authenticate_user($link, "admin", null);
63         }
64
65         if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
66                                 $op != "rss" && $op != "getUnread" && $op != "getProfiles") {
67
68                 header("Content-Type: text/plain");
69                 print json_encode(array("error" => array("code" => 6)));
70                 return;
71         }
72
73         $purge_intervals = array(
74                 0  => __("Use default"),
75                 -1 => __("Never purge"),
76                 5  => __("1 week old"),
77                 14 => __("2 weeks old"),
78                 31 => __("1 month old"),
79                 60 => __("2 months old"),
80                 90 => __("3 months old"));
81
82         $update_intervals = array(
83                 0   => __("Default interval"),
84                 -1  => __("Disable updates"),
85                 15  => __("Each 15 minutes"),
86                 30  => __("Each 30 minutes"),
87                 60  => __("Hourly"),
88                 240 => __("Each 4 hours"),
89                 720 => __("Each 12 hours"),
90                 1440 => __("Daily"),
91                 10080 => __("Weekly"));
92
93         $update_intervals_nodefault = array(
94                 -1  => __("Disable updates"),
95                 15  => __("Each 15 minutes"),
96                 30  => __("Each 30 minutes"),
97                 60  => __("Hourly"),
98                 240 => __("Each 4 hours"),
99                 720 => __("Each 12 hours"),
100                 1440 => __("Daily"),
101                 10080 => __("Weekly"));
102
103         $update_methods = array(
104                 0   => __("Default"),
105                 1   => __("Magpie"),
106                 2   => __("SimplePie"),
107                 3   => __("Twitter OAuth"));
108
109         if (DEFAULT_UPDATE_METHOD == "1") {
110                 $update_methods[0] .= ' (SimplePie)';
111         } else {
112                 $update_methods[0] .= ' (Magpie)';
113         }
114
115         $access_level_names = array(
116                 0 => __("User"),
117                 5 => __("Power User"),
118                 10 => __("Administrator"));
119
120         require_once "modules/pref-prefs.php";
121         require_once "modules/popup-dialog.php";
122         require_once "modules/help.php";
123         require_once "modules/pref-feeds.php";
124         require_once "modules/pref-filters.php";
125         require_once "modules/pref-labels.php";
126         require_once "modules/pref-users.php";
127
128         $error = sanity_check($link);
129
130         if ($error['code'] != 0) {
131                 print json_encode(array("error" => $error));
132                 return;
133         }
134
135         switch($op) { // Select action according to $op value.
136                 case "rpc":
137                         // Handle remote procedure calls.
138                         handle_rpc_request($link);
139                 break; // rpc
140
141                 case "feeds":
142                         $subop = $_REQUEST["subop"];
143                         $root = (bool)$_REQUEST["root"];
144
145                         switch($subop) {
146                                 case "catchupAll":
147                                         db_query($link, "UPDATE ttrss_user_entries SET
148                                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
149                                         ccache_zero_all($link, $_SESSION["uid"]);
150
151                                 break;
152
153                                 case "collapse":
154                                         $cat_id = db_escape_string($_REQUEST["cid"]);
155                                         $mode = (int) db_escape_string($_REQUEST['mode']);
156                                         toggle_collapse_cat($link, $cat_id, $mode);
157                                         return;
158                                 break;
159                         }
160
161                         if (!$root) {
162                                 print json_encode(outputFeedList($link));
163                         } else {
164
165                                 $feeds = outputFeedList($link, false);
166
167                                 $root = array();
168                                 $root['id'] = 'root';
169                                 $root['name'] = __('Feeds');
170                                 $root['items'] = $feeds['items'];
171
172                                 $fl = array();
173                                 $fl['identifier'] = 'id';
174                                 $fl['label'] = 'name';
175                                 $fl['items'] = array($root);
176
177                                 print json_encode($fl);
178                         }
179
180                 break; // feeds
181
182                 case "la":
183                         $id = db_escape_string($_REQUEST['id']);
184
185                         $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
186                                 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
187
188                         if (db_num_rows($result) == 1) {
189                                 $article_url = db_fetch_result($result, 0, 'link');
190                                 $article_url = str_replace("\n", "", $article_url);
191
192                                 header("Location: $article_url");
193                                 return;
194
195                         } else {
196                                 print_error(__("Article not found."));
197                         }
198                 break;
199
200                 case "view":
201
202                         $id = db_escape_string($_REQUEST["id"]);
203                         $cids = split(",", db_escape_string($_REQUEST["cids"]));
204                         $mode = db_escape_string($_REQUEST["mode"]);
205                         $omode = db_escape_string($_REQUEST["omode"]);
206
207                         // in prefetch mode we only output requested cids, main article
208                         // just gets marked as read (it already exists in client cache)
209
210                         $articles = array();
211
212                         if ($mode == "") {
213                                 array_push($articles, format_article($link, $id, false));
214                         } else if ($mode == "zoom") {
215                                 array_push($articles, format_article($link, $id, false, true, true));
216                         } else {
217                                 catchupArticleById($link, $id, 0);
218                         }
219
220                         if (!$_SESSION["bw_limit"]) {
221                                 foreach ($cids as $cid) {
222                                         if ($cid) {
223                                                 array_push($articles, format_article($link, $cid, false, false));
224                                         }
225                                 }
226                         }
227
228                         print json_encode($articles);
229
230                 break; // view
231
232                 case "viewfeed":
233
234                         $timing_info = getmicrotime();
235
236                         $reply = array();
237
238                         if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
239
240                         $omode = db_escape_string($_REQUEST["omode"]);
241
242                         $feed = db_escape_string($_REQUEST["feed"]);
243                         $subop = db_escape_string($_REQUEST["subop"]);
244                         $view_mode = db_escape_string($_REQUEST["view_mode"]);
245                         $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
246                         @$cat_view = db_escape_string($_REQUEST["cat"]);
247                         @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
248                         @$offset = db_escape_string($_REQUEST["skip"]);
249                         @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
250                         $order_by = db_escape_string($_REQUEST["order_by"]);
251
252                         /* Feed -5 is a special case: it is used to display auxiliary information
253                          * when there's nothing to load - e.g. no stuff in fresh feed */
254
255                         if ($feed == -5) {
256                                 print json_encode(generate_dashboard_feed($link));
257                                 return;
258                         }
259
260                         $result = false;
261
262                         if ($feed < -10) {
263                                 $label_feed = -11-$feed;
264                                 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
265                                         id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
266                         } else if (!$cat_view && $feed > 0) {
267                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
268                                         id = '$feed' AND owner_uid = " . $_SESSION['uid']);
269                         } else if ($cat_view && $feed > 0) {
270                                 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
271                                         id = '$feed' AND owner_uid = " . $_SESSION['uid']);
272                         }
273
274                         if ($result && db_num_rows($result) == 0) {
275                                 print json_encode(generate_error_feed($link, __("Feed not found.")));
276                                 return;
277                         }
278
279                         /* Updating a label ccache means recalculating all of the caches
280                          * so for performance reasons we don't do that here */
281
282                         if ($feed >= 0) {
283                                 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
284                         }
285
286                         set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
287                         set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
288                         set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
289
290                         if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
291                                 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
292                                         WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
293                         }
294
295                         $reply['headlines'] = array();
296
297                         if (!$next_unread_feed)
298                                 $reply['headlines']['id'] = $feed;
299                         else
300                                 $reply['headlines']['id'] = $next_unread_feed;
301
302                         $reply['headlines']['is_cat'] = (bool) $cat_view;
303
304                         $override_order = false;
305
306                         if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
307                                 $date_sort_field = "updated";
308                         } else {
309                                 $date_sort_field = "date_entered";
310                         }
311
312                         switch ($order_by) {
313                                 case "date":
314                                         if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
315                                                 $override_order = "$date_sort_field";
316                                         } else {
317                                                 $override_order = "$date_sort_field DESC";
318                                         }
319                                         break;
320
321                                 case "title":
322                                         if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
323                                                 $override_order = "title DESC, $date_sort_field";
324                                         } else {
325                                                 $override_order = "title, $date_sort_field DESC";
326                                         }
327                                         break;
328
329                                 case "score":
330                                         if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
331                                                 $override_order = "score, $date_sort_field";
332                                         } else {
333                                                 $override_order = "score DESC, $date_sort_field DESC";
334                                         }
335                                         break;
336                         }
337
338                         if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
339
340                         $ret = format_headlines_list($link, $feed, $subop,
341                                 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
342                                 $vgroup_last_feed, $override_order);
343
344                         $topmost_article_ids = $ret[0];
345                         $headlines_count = $ret[1];
346                         $returned_feed = $ret[2];
347                         $disable_cache = $ret[3];
348                         $vgroup_last_feed = $ret[4];
349
350                         $reply['headlines']['content'] = $ret[5];
351                         $reply['headlines']['toolbar'] = $ret[6];
352
353                         if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
354
355                         $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
356                                         $cat_view, true);
357
358                         if ($headlines_unread == -1) {
359                                 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
360                         }
361
362                         $reply['headlines-info'] = array("count" => (int) $headlines_count,
363                                 "vgroup_last_feed" => $vgroup_last_feed,
364                                 "unread" => (int) $headlines_unread,
365                                 "disable_cache" => (bool) $disable_cache);
366
367                         if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
368
369                         if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
370                                 $articles = array();
371
372                                 foreach ($topmost_article_ids as $id) {
373                                         array_push($articles, format_article($link, $id, $feed, false));
374                                 }
375
376                                 $reply['articles'] = $articles;
377                         }
378
379                         if ($subop) {
380                                 $reply['counters'] = getAllCounters($link, $omode, $feed);
381                         }
382
383                         if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
384
385                         $reply['runtime-info'] = make_runtime_info($link);
386
387                         print json_encode($reply);
388
389                 break; // viewfeed
390
391                 case "pref-feeds":
392                         module_pref_feeds($link);
393                 break; // pref-feeds
394
395                 case "pref-filters":
396                         module_pref_filters($link);
397                 break; // pref-filters
398
399                 case "pref-labels":
400                         module_pref_labels($link);
401                 break; // pref-labels
402
403                 case "pref-prefs":
404                         module_pref_prefs($link);
405                 break; // pref-prefs
406
407                 case "pref-users":
408                         module_pref_users($link);
409                 break; // prefs-users
410
411                 case "help":
412                         module_help($link);
413                 break; // help
414
415                 case "dlg":
416                         module_popup_dialog($link);
417                 break; // dlg
418
419                 case "pref-pub-items":
420                         module_pref_pub_items($link);
421                 break; // pref-pub-items
422
423                 case "globalUpdateFeeds":
424                         // Update all feeds needing a update.
425                         update_daemon_common($link, 0, true, true);
426                 break; // globalUpdateFeeds
427
428                 case "pref-feed-browser":
429                         module_pref_feed_browser($link);
430                 break; // pref-feed-browser
431
432                 case "rss":
433                         $feed = db_escape_string($_REQUEST["id"]);
434                         $key = db_escape_string($_REQUEST["key"]);
435                         $is_cat = $_REQUEST["is_cat"] != false;
436                         $limit = (int)db_escape_string($_REQUEST["limit"]);
437
438                         $search = db_escape_string($_REQUEST["q"]);
439                         $match_on = db_escape_string($_REQUEST["m"]);
440                         $search_mode = db_escape_string($_REQUEST["smode"]);
441                         $view_mode = db_escape_string($_REQUEST["view-mode"]);
442
443                         if (SINGLE_USER_MODE) {
444                                 authenticate_user($link, "admin", null);
445                         }
446
447                         $owner_id = false;
448
449                         if ($key) {
450                                 $result = db_query($link, "SELECT owner_uid FROM
451                                         ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
452
453                                 if (db_num_rows($result) == 1)
454                                         $owner_id = db_fetch_result($result, 0, "owner_uid");
455                         }
456
457                         if ($owner_id) {
458                                 $_SESSION['uid'] = $owner_id;
459
460                                 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
461                                         $search, $search_mode, $match_on, $view_mode);
462                         } else {
463                                 header('HTTP/1.1 403 Forbidden');
464                         }
465                 break; // rss
466
467                 case "getUnread":
468                         $login = db_escape_string($_REQUEST["login"]);
469                         $fresh = $_REQUEST["fresh"] == "1";
470
471                         $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
472
473                         if (db_num_rows($result) == 1) {
474                                 $uid = db_fetch_result($result, 0, "id");
475
476                                 print getGlobalUnread($link, $uid);
477
478                                 if ($fresh) {
479                                         print ";";
480                                         print getFeedArticles($link, -3, false, true, $uid);
481                                 }
482
483                         } else {
484                                 print "-1;User not found";
485                         }
486
487                         $print_exec_time = false;
488                 break; // getUnread
489
490                 case "digestTest":
491                         print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
492                         $print_exec_time = false;
493                 break; // digestTest
494
495                 case "digestSend":
496                         send_headlines_digests($link);
497                         $print_exec_time = false;
498                 break; // digestSend
499
500                 case "loading":
501                         print __("Loading, please wait...") . " " .
502                                 "<img src='images/indicator_tiny.gif'>";
503
504                 case "getProfiles":
505                         $login = db_escape_string($_REQUEST["login"]);
506                         $password = db_escape_string($_REQUEST["password"]);
507
508                         if (authenticate_user($link, $login, $password)) {
509                                 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
510                                         WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
511
512                                 print "<select style='width: 100%' name='profile'>";
513
514                                 print "<option value='0'>" . __("Default profile") . "</option>";
515
516                                 while ($line = db_fetch_assoc($result)) {
517                                         $id = $line["id"];
518                                         $title = $line["title"];
519
520                                         print "<option value='$id'>$title</option>";
521                                 }
522
523                                 print "</select>";
524
525                                 $_SESSION = array();
526                         }
527                 break;
528
529         } // Select action according to $op value.
530
531
532         // We close the connection to database.
533         db_close($link);
534 ?>
535
536 <?php if ($print_exec_time) { ?>
537 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
538 <?php } ?>