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