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