]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
move phpmailer to lib/
[tt-rss.git] / backend.php
1 <?php
2         error_reporting(E_ERROR | E_WARNING | E_PARSE);
3
4         /* remove ill effects of magic quotes */
5
6         if (get_magic_quotes_gpc()) {
7                 $_GET = array_map('stripslashes', $_GET);
8                 $_POST = array_map('stripslashes', $_POST);
9                 $_REQUEST = array_map('stripslashes', $_REQUEST);
10                 $_COOKIE = array_map('stripslashes', $_COOKIE);
11         }
12
13         require_once "sessions.php";
14         require_once "modules/backend-rpc.php";
15
16 /*      if ($_GET["debug"]) {
17                 define('DEFAULT_ERROR_LEVEL', E_ALL);
18         } else {
19                 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
20         }
21         
22         error_reporting(DEFAULT_ERROR_LEVEL); */
23
24         require_once "sanity_check.php";
25         require_once "config.php";
26         
27         require_once "db.php";
28         require_once "db-prefs.php";
29         require_once "functions.php";
30
31         no_cache_incantation();
32
33         if (ENABLE_TRANSLATIONS == true) { 
34                 startup_gettext();
35         }
36
37         $script_started = getmicrotime();
38
39         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
40
41         if (!$link) {
42                 if (DB_TYPE == "mysql") {
43                         print mysql_error();
44                 }
45                 // PG seems to display its own errors just fine by default.             
46                 return;
47         }
48
49         init_connection($link);
50
51         $op = $_REQUEST["op"];
52         $mode = $_REQUEST["mode"];
53
54         $print_exec_time = false;
55
56         if ((!$op || $op == "rpc" || $op == "rss" || 
57                         ($op == "view" && $mode != "zoom") || 
58                         $op == "digestSend" || $op == "viewfeed" || $op == "publish" ||
59                         $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
60                                 header("Content-Type: application/xml; charset=utf-8");
61
62                                 if (ENABLE_GZIP_OUTPUT) {
63                                         ob_start("ob_gzhandler");
64                                 }
65                                 
66                 } else {
67                 if (!$_REQUEST["noxml"]) {
68                         header("Content-Type: text/html; charset=utf-8");
69                 } else {
70                         header("Content-Type: text/plain; charset=utf-8");
71                 }
72         }
73
74         if (!$op) {
75                 header("Content-Type: application/xml");
76                 print_error_xml(7); exit;
77         }
78         
79         if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" 
80                         && $op != "rss" && $op != "getUnread" && $op != "publish") {
81
82                 if ($op == "rpc" || $op == "viewfeed" || $op == "view") {
83                         print_error_xml(6); die;
84                 } else {
85                         print "
86                         <html><body>
87                                 <p>Error: Not logged in.</p>
88                                 <script type=\"text/javascript\">
89                                         if (parent.window != 'undefined') {
90                                                 parent.window.location = \"tt-rss.php\";                
91                                         } else {
92                                                 window.location = \"tt-rss.php\";
93                                         }
94                                 </script>
95                         </body></html>
96                         ";
97                 }
98                 exit;
99         }
100
101         $purge_intervals = array(
102                 0  => __("Use default"),
103                 -1 => __("Never purge"),
104                 5  => __("1 week old"),
105                 14 => __("2 weeks old"),
106                 31 => __("1 month old"),
107                 60 => __("2 months old"),
108                 90 => __("3 months old"));
109
110         $update_intervals = array(
111                 0   => __("Default interval"),
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
126         if (ENABLE_SIMPLEPIE) {
127                 $update_methods[0] .= ' (SimplePie)';
128         } else {
129                 $update_methods[0] .= ' (Magpie)';
130         }
131
132         $access_level_names = array(
133                 0 => __("User"), 
134                 5 => __("Power User"),
135                 10 => __("Administrator"));
136
137         require_once "modules/pref-prefs.php";
138         require_once "modules/popup-dialog.php";
139         require_once "modules/help.php";
140         require_once "modules/pref-feeds.php";
141         require_once "modules/pref-filters.php";
142         require_once "modules/pref-labels.php";
143         require_once "modules/pref-users.php";
144         require_once "modules/pref-feed-browser.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                         if (ENABLE_GZIP_OUTPUT) {
156                                 ob_start("ob_gzhandler");
157                         }
158
159                         $tags = $_GET["tags"];
160
161                         $subop = $_GET["subop"];
162
163                         switch($subop) {
164                                 case "catchupAll":
165                                         db_query($link, "UPDATE ttrss_user_entries SET 
166                                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
167                                         ccache_zero_all($link, $_SESSION["uid"]);
168
169                                 break;
170
171                                 case "collapse":
172                                         $cat_id = db_escape_string($_GET["cid"]);
173
174                                         db_query($link, "UPDATE ttrss_feed_categories SET
175                                                 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " . 
176                                                 $_SESSION["uid"]);
177                                         return;
178                                 break;
179
180                                 case "catsortreset":
181                                         db_query($link, "UPDATE ttrss_feed_categories 
182                                                         SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
183                                         return;
184                                 break;
185
186                                 case "catsort":
187                                         $corder = db_escape_string($_GET["corder"]);
188
189                                         $cats = split(",", $corder);
190
191                                         for ($i = 0; $i < count($cats); $i++) {
192                                                 $cat_id = $cats[$i];
193
194                                                 if ($cat_id > 0) {
195                                                         db_query($link, "UPDATE ttrss_feed_categories 
196                                                                 SET order_id = '$i' WHERE id = '$cat_id' AND
197                                                                 owner_uid = " . $_SESSION["uid"]);
198                                                 }
199                                         }
200
201                                         return;
202                                 break;
203
204                         }
205
206                         outputFeedList($link, $tags);
207                 break; // feeds
208
209                 case "view":
210
211                         $id = db_escape_string($_GET["id"]);
212                         $feed_id = db_escape_string($_GET["feed"]);
213                         $cids = split(",", db_escape_string($_GET["cids"]));
214                         $mode = db_escape_string($_GET["mode"]);
215                         $omode = db_escape_string($_GET["omode"]);
216
217                         $csync = $_GET["csync"];
218
219                         print "<reply>";
220
221                         // in prefetch mode we only output requested cids, main article 
222                         // just gets marked as read (it already exists in client cache)
223
224                         if ($mode == "") {
225                                 outputArticleXML($link, $id, $feed_id);
226                         } else if ($mode == "zoom") {
227                                 outputArticleXML($link, $id, $feed_id, true, true);
228                         } else {
229                                 catchupArticleById($link, $id, 0);
230                                 ccache_update($link, $feed_id, $_SESSION["uid"]);
231                         }
232
233                         if (!$_SESSION["bw_limit"]) {
234                                 foreach ($cids as $cid) {
235                                         if ($cid) {
236                                                 outputArticleXML($link, $cid, $feed_id, false);
237                                         }
238                                 }
239                         }
240
241
242 //                      if (get_pref($link, "SYNC_COUNTERS") || ($mode == "prefetch" && $csync)) {
243                                 print "<counters>";
244                                 getAllCounters($link, $omode);
245                                 print "</counters>";
246 //                      }
247
248                         print "</reply>";
249                 break; // view
250
251                 case "viewfeed":
252
253                         $print_exec_time = true;
254                         $timing_info = getmicrotime();
255
256                         print "<reply>";
257
258                         if ($_GET["debug"]) $timing_info = print_checkpoint("0", $timing_info);
259
260                         $omode = db_escape_string($_GET["omode"]);
261
262                         $feed = db_escape_string($_GET["feed"]);
263                         $subop = db_escape_string($_GET["subop"]);
264                         $view_mode = db_escape_string($_GET["view_mode"]);
265                         $limit = db_escape_string($_GET["limit"]);
266                         $cat_view = db_escape_string($_GET["cat"]);
267                         $next_unread_feed = db_escape_string($_GET["nuf"]);
268                         $offset = db_escape_string($_GET["skip"]);
269                         $vgroup_last_feed = db_escape_string($_GET["vgrlf"]);
270                         $csync = $_GET["csync"];
271                         $order_by = db_escape_string($_GET["order_by"]);
272
273                         /* Updating a label ccache means recalculating all of the caches
274                          * so for performance reasons we don't do that here */
275
276                         if (time() - $_SESSION["viewfeed:ccache_update_stamp"] > 120) {
277                                 if ($feed >= 0) {
278                                         ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
279                                 }
280                                 $_SESSION["viewfeed:ccache_update_stamp"] = time();
281                         }
282
283                         set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
284                         set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
285                         set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
286
287                         if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
288                                 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
289                                         WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
290                         }
291
292                         if (!$next_unread_feed) {
293                                 print "<headlines id=\"$feed\" is_cat=\"$cat_view\"><![CDATA[";
294                         } else {
295                                 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\"><![CDATA[";
296                         }
297                 
298                         $override_order = false;
299
300                         switch ($order_by) {
301                                 case "date":
302                                         if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
303                                                 $override_order = "updated";
304                                         } else {        
305                                                 $override_order = "updated DESC";
306                                         }
307                                         break;
308
309                                 case "title":
310                                         $override_order = "updated DESC";
311                                         break;
312
313                                 case "score":
314                                         $override_order = "score DESC";
315                                         break;
316                         }
317
318                         $ret = outputHeadlinesList($link, $feed, $subop, 
319                                 $view_mode, $limit, $cat_view, $next_unread_feed, $offset, 
320                                 $vgroup_last_feed, $override_order);
321
322                         $topmost_article_ids = $ret[0];
323                         $headlines_count = $ret[1];
324                         $returned_feed = $ret[2];
325                         $disable_cache = $ret[3];
326                         $vgroup_last_feed = $ret[4];
327
328                         print "]]></headlines>";
329
330                         print "<headlines-count value=\"$headlines_count\"/>";
331                         print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
332
333                         $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
334                                         $cat_view, true);
335
336                         if ($headlines_unread == -1) {
337                                 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
338
339                         }
340
341                         print "<headlines-unread value=\"$headlines_unread\"/>";
342                         printf("<disable-cache value=\"%d\"/>", $disable_cache);
343
344                         if ($_GET["debug"]) $timing_info = print_checkpoint("10", $timing_info);
345
346                         if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
347                                 print "<articles>";
348                                 foreach ($topmost_article_ids as $id) {
349                                         outputArticleXML($link, $id, $feed, false);
350                                 }
351                                 print "</articles>";
352                         }
353
354                         if ($_GET["debug"]) $timing_info = print_checkpoint("20", $timing_info);
355
356
357 //                      if (get_pref($link, "SYNC_COUNTERS") ||                         
358 //                                      time() - $_SESSION["get_all_counters_stamp"] > $viewfeed_ctr_interval) {
359 //                              print "<counters>";
360 //                              getAllCounters($link, $omode, $feed);
361 //                              print "</counters>";
362 //                      }
363
364                         if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop || 
365                                 time() - $_SESSION["viewfeed:counters_stamp"] > 60) {
366                                 if (!$offset) {
367                                         print "<counters>";
368                                         getAllCounters($link, $omode, $feed);
369                                         print "</counters>";
370                                         $_SESSION["viewfeed:counters_stamp"] = time();
371                                 }
372                         }
373
374                         if ($_GET["debug"]) $timing_info = print_checkpoint("30", $timing_info);
375
376                         print_runtime_info($link);
377
378                         print "</reply>";
379                 break; // viewfeed
380
381                 case "pref-feeds":
382                         module_pref_feeds($link);
383                 break; // pref-feeds
384
385                 case "pref-filters":
386                         module_pref_filters($link);
387                 break; // pref-filters
388
389                 case "pref-labels":
390                         module_pref_labels($link);
391                 break; // pref-labels
392
393                 case "pref-prefs":
394                         module_pref_prefs($link);
395                 break; // pref-prefs
396
397                 case "pref-users":
398                         module_pref_users($link);
399                 break; // prefs-users
400
401                 case "help":
402                         module_help($link);
403                 break; // help
404
405                 case "dlg":
406                         module_popup_dialog($link);
407                 break; // dlg
408
409                 case "pref-pub-items":
410                         module_pref_pub_items($link);
411                 break; // pref-pub-items
412
413                 case "globalUpdateFeeds":
414                         // update feeds of all users, may be used anonymously
415
416                         print "<!--";
417                         // Update all feeds needing a update.
418                         update_daemon_common($link, 0, true, true);
419                         print " -->";
420
421                         print "<rpc-reply>
422                                 <message msg=\"All feeds updated\"/>
423                         </rpc-reply>";
424                 break; // globalUpdateFeeds
425
426                 case "pref-feed-browser":
427                         module_pref_feed_browser($link);
428                 break; // pref-feed-browser
429
430                 case "publish":
431                         $key = db_escape_string($_GET["key"]);
432                         $limit = (int)db_escape_string($_GET["limit"]);
433
434                         $result = db_query($link, "SELECT login, owner_uid 
435                                 FROM ttrss_user_prefs, ttrss_users WHERE
436                                 pref_name = '_PREFS_PUBLISH_KEY' AND 
437                                 value = '$key' AND 
438                                 ttrss_users.id = owner_uid");
439
440                         if (db_num_rows($result) == 1) {
441                                 $owner = db_fetch_result($result, 0, "owner_uid");
442                                 $login = db_fetch_result($result, 0, "login");
443
444                                 generate_syndicated_feed($link, $owner, -2, false, $limit);
445
446                         } else {
447                                 print "<error>User not found</error>";
448                         }
449                 break; // publish
450
451                 case "rss":
452                         $feed = db_escape_string($_GET["id"]);
453                         $user = db_escape_string($_GET["user"]);
454                         $pass = db_escape_string($_GET["pass"]);
455                         $is_cat = $_GET["is_cat"] != false;
456                         $limit = (int)db_escape_string($_GET["limit"]);
457
458                         $search = db_escape_string($_GET["q"]);
459                         $match_on = db_escape_string($_GET["m"]);
460                         $search_mode = db_escape_string($_GET["smode"]);
461
462                         if (!$_SESSION["uid"] && $user && $pass) {
463                                 authenticate_user($link, $user, $pass);
464                         }
465
466                         if ($_SESSION["uid"] ||
467                                 http_authenticate_user($link)) {
468
469                                         generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
470                                                 $search, $search_mode, $match_on);
471                         }
472                 break; // rss
473
474                 case "getUnread":
475                         $login = db_escape_string($_GET["login"]);
476
477                         header("Content-Type: text/plain; charset=utf-8");
478
479                         $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
480
481                         if (db_num_rows($result) == 1) {
482                                 $uid = db_fetch_result($result, 0, "id");
483                                 print getGlobalUnread($link, $uid);
484                         } else {
485                                 print "-1;User not found";
486                         }
487
488                         $print_exec_time = false;
489                 break; // getUnread
490
491                 case "digestTest":
492                         header("Content-Type: text/plain");
493                         print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
494                         $print_exec_time = false;
495                 break; // digestTest
496
497                 case "digestSend":
498                         header("Content-Type: text/plain");
499                         send_headlines_digests($link);
500                         $print_exec_time = false;
501                 break; // digestSend
502
503         } // Select action according to $op value.
504
505         // We close the connection to database.
506         db_close($link);
507 ?>
508
509 <?php if ($print_exec_time) { ?>
510 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
511 <?php } ?>