]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
109c6e586d1d479b23a2f42c849b78f195cab6bf
[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                                 break;
168
169                                 case "collapse":
170                                         $cat_id = db_escape_string($_GET["cid"]);
171
172                                         db_query($link, "UPDATE ttrss_feed_categories SET
173                                                 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " . 
174                                                 $_SESSION["uid"]);
175                                         return;
176                                 break;
177
178                                 case "catsortreset":
179                                         db_query($link, "UPDATE ttrss_feed_categories 
180                                                         SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
181                                         return;
182                                 break;
183
184                                 case "catsort":
185                                         $corder = db_escape_string($_GET["corder"]);
186
187                                         $cats = split(",", $corder);
188
189                                         for ($i = 0; $i < count($cats); $i++) {
190                                                 $cat_id = $cats[$i];
191
192                                                 if ($cat_id > 0) {
193                                                         db_query($link, "UPDATE ttrss_feed_categories 
194                                                                 SET order_id = '$i' WHERE id = '$cat_id' AND
195                                                                 owner_uid = " . $_SESSION["uid"]);
196                                                 }
197                                         }
198
199                                         return;
200                                 break;
201
202                         }
203
204                         outputFeedList($link, $tags);
205                 break; // feeds
206
207                 case "view":
208
209                         $id = db_escape_string($_GET["id"]);
210                         $feed_id = db_escape_string($_GET["feed"]);
211                         $cids = split(",", db_escape_string($_GET["cids"]));
212                         $mode = db_escape_string($_GET["mode"]);
213                         $omode = db_escape_string($_GET["omode"]);
214
215                         $csync = $_GET["csync"];
216
217                         print "<reply>";
218
219                         // in prefetch mode we only output requested cids, main article 
220                         // just gets marked as read (it already exists in client cache)
221
222                         if ($mode == "") {
223                                 outputArticleXML($link, $id, $feed_id);
224                         } else if ($mode == "zoom") {
225                                 outputArticleXML($link, $id, $feed_id, true, true);
226                         } else {
227                                 catchupArticleById($link, $id, 0);
228                                 ccache_update($link, $feed_id, $_SESSION["uid"]);
229                         }
230
231                         if (!$_SESSION["bw_limit"]) {
232                                 foreach ($cids as $cid) {
233                                         if ($cid) {
234                                                 outputArticleXML($link, $cid, $feed_id, false);
235                                         }
236                                 }
237                         }
238
239
240                         if (get_pref($link, "SYNC_COUNTERS") || ($mode == "prefetch" && $csync)) {
241                                 print "<counters>";
242                                 getAllCounters($link, $omode);
243                                 print "</counters>";
244                         }
245
246                         print "</reply>";
247                 break; // view
248
249                 case "viewfeed":
250
251                         $print_exec_time = true;
252                         $timing_info = getmicrotime();
253
254                         print "<reply>";
255
256                         if ($_GET["debug"]) $timing_info = print_checkpoint("0", $timing_info);
257
258                         $omode = db_escape_string($_GET["omode"]);
259
260                         $feed = db_escape_string($_GET["feed"]);
261                         $subop = db_escape_string($_GET["subop"]);
262                         $view_mode = db_escape_string($_GET["view_mode"]);
263                         $limit = db_escape_string($_GET["limit"]);
264                         $cat_view = db_escape_string($_GET["cat"]);
265                         $next_unread_feed = db_escape_string($_GET["nuf"]);
266                         $offset = db_escape_string($_GET["skip"]);
267                         $vgroup_last_feed = db_escape_string($_GET["vgrlf"]);
268                         $csync = $_GET["csync"];
269                         $order_by = db_escape_string($_GET["order_by"]);
270
271                         set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
272                         set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
273                         set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
274
275                         if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
276                                 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
277                                         WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
278                         }
279
280                         print "<headlines id=\"$feed\"><![CDATA[";
281
282                         if ($feed == -4) {
283
284                                 generate_dashboard_feed($link);
285
286                         } else {
287                                 
288                                 $override_order = false;
289
290                                 switch ($order_by) {
291                                         case "date":
292                                                 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
293                                                         $override_order = "updated";
294                                                 } else {        
295                                                         $override_order = "updated DESC";
296                                                 }
297                                                 break;
298
299                                         case "title":
300                                                 $override_order = "updated DESC";
301                                                 break;
302
303                                         case "score":
304                                                 $override_order = "score DESC";
305                                                 break;
306                                 }
307
308                                 $ret = outputHeadlinesList($link, $feed, $subop, 
309                                         $view_mode, $limit, $cat_view, $next_unread_feed, $offset, 
310                                         $vgroup_last_feed, $override_order);
311         
312                                 $topmost_article_ids = $ret[0];
313                                 $headlines_count = $ret[1];
314                                 $returned_feed = $ret[2];
315                                 $disable_cache = $ret[3];
316                                 $vgroup_last_feed = $ret[4];
317         
318                                 print "]]></headlines>";
319         
320                                 print "<headlines-count value=\"$headlines_count\"/>";
321                                 print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
322
323                                 $headlines_unread = getFeedUnread($link, $returned_feed);
324         
325                                 print "<headlines-unread value=\"$headlines_unread\"/>";
326                                 printf("<disable-cache value=\"%d\"/>", $disable_cache);
327         
328                                 if ($_GET["debug"]) $timing_info = print_checkpoint("10", $timing_info);
329         
330                                 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
331                                         print "<articles>";
332                                         foreach ($topmost_article_ids as $id) {
333                                                 outputArticleXML($link, $id, $feed, false);
334                                         }
335                                         print "</articles>";
336                                 }
337                         }
338
339                         if ($_GET["debug"]) $timing_info = print_checkpoint("20", $timing_info);
340
341                         $viewfeed_ctr_interval = 300;
342
343                         if ($csync || $_SESSION["bw_limit"]) {
344                                 $viewfeed_ctr_interval = 60;
345                         }
346
347                         if (get_pref($link, "SYNC_COUNTERS") ||                         
348                                         time() - $_SESSION["get_all_counters_stamp"] > $viewfeed_ctr_interval) {
349                                 print "<counters>";
350                                 getAllCounters($link, $omode, $feed);
351                                 print "</counters>";
352                         }
353
354                         if ($_GET["debug"]) $timing_info = print_checkpoint("30", $timing_info);
355
356                         print_runtime_info($link);
357
358                         print "</reply>";
359                 break; // viewfeed
360
361                 case "pref-feeds":
362                         module_pref_feeds($link);
363                 break; // pref-feeds
364
365                 case "pref-filters":
366                         module_pref_filters($link);
367                 break; // pref-filters
368
369                 case "pref-labels":
370                         module_pref_labels($link);
371                 break; // pref-labels
372
373                 case "pref-prefs":
374                         module_pref_prefs($link);
375                 break; // pref-prefs
376
377                 case "pref-users":
378                         module_pref_users($link);
379                 break; // prefs-users
380
381                 case "help":
382                         module_help($link);
383                 break; // help
384
385                 case "dlg":
386                         module_popup_dialog($link);
387                 break; // dlg
388
389                 case "pref-pub-items":
390                         module_pref_pub_items($link);
391                 break; // pref-pub-items
392
393                 case "globalUpdateFeeds":
394                         // update feeds of all users, may be used anonymously
395
396                         print "<!--";
397                         // Update all feeds needing a update.
398                         update_daemon_common($link, 0, true, true);
399                         print " -->";
400
401                         // FIXME : old feed update way. To be removed.
402                         //$result = db_query($link, "SELECT id FROM ttrss_users");
403
404                         //while ($line = db_fetch_assoc($result)) {
405                         //      $user_id = $line["id"];
406                         //      print "<!-- updating feeds of uid $user_id -->";
407                         //      update_all_feeds($link, false, $user_id);
408                         //}
409
410                         print "<rpc-reply>
411                                 <message msg=\"All feeds updated\"/>
412                         </rpc-reply>";
413                 break; // globalUpdateFeeds
414
415                 case "pref-feed-browser":
416                         module_pref_feed_browser($link);
417                 break; // pref-feed-browser
418
419                 case "publish":
420                         $key = db_escape_string($_GET["key"]);
421                         $limit = (int)db_escape_string($_GET["limit"]);
422
423                         $result = db_query($link, "SELECT login, owner_uid 
424                                 FROM ttrss_user_prefs, ttrss_users WHERE
425                                 pref_name = '_PREFS_PUBLISH_KEY' AND 
426                                 value = '$key' AND 
427                                 ttrss_users.id = owner_uid");
428
429                         if (db_num_rows($result) == 1) {
430                                 $owner = db_fetch_result($result, 0, "owner_uid");
431                                 $login = db_fetch_result($result, 0, "login");
432
433                                 generate_syndicated_feed($link, $owner, -2, false, $limit);
434
435                         } else {
436                                 print "<error>User not found</error>";
437                         }
438                 break; // publish
439
440                 case "rss":
441                         $feed = db_escape_string($_GET["id"]);
442                         $user = db_escape_string($_GET["user"]);
443                         $pass = db_escape_string($_GET["pass"]);
444                         $is_cat = $_GET["is_cat"] != false;
445                         $limit = (int)db_escape_string($_GET["limit"]);
446
447                         $search = db_escape_string($_GET["q"]);
448                         $match_on = db_escape_string($_GET["m"]);
449                         $search_mode = db_escape_string($_GET["smode"]);
450
451                         if (!$_SESSION["uid"] && $user && $pass) {
452                                 authenticate_user($link, $user, $pass);
453                         }
454
455                         if ($_SESSION["uid"] ||
456                                 http_authenticate_user($link)) {
457
458                                         generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
459                                                 $search, $search_mode, $match_on);
460                         }
461                 break; // rss
462
463                 case "labelFromSearch":
464                         $search = db_escape_string($_GET["search"]);
465                         $search_mode = db_escape_string($_GET["smode"]);
466                         $match_on = db_escape_string($_GET["match"]);
467                         $is_cat = db_escape_string($_GET["is_cat"]);
468                         $title = db_escape_string($_GET["title"]);
469                         $feed = sprintf("%d", $_GET["feed"]);
470
471                         $label_qparts = array();
472
473                         $search_expr = getSearchSql($search, $match_on);
474
475                         if ($is_cat) {
476                                 if ($feed != 0) {
477                                         $search_expr .= " AND ttrss_feeds.cat_id = $feed ";
478                                 } else {
479                                         $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
480                                 }
481                         } else {
482                                 if ($search_mode == "all_feeds") {
483                                         // NOOP
484                                 } else if ($search_mode == "this_cat") {
485
486                                         $tmp_result = db_query($link, "SELECT cat_id
487                                                 FROM ttrss_feeds WHERE id = '$feed'");
488
489                                         $cat_id = db_fetch_result($tmp_result, 0, "cat_id");
490
491                                         if ($cat_id > 0) {
492                                                 $search_expr .= " AND ttrss_feeds.cat_id = $cat_id ";
493                                         } else {
494                                                 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
495                                         }
496                                 } else {
497                                         $search_expr .= " AND ttrss_feeds.id = $feed ";
498                                 }
499
500                         }
501
502                         $search_expr = db_escape_string($search_expr);
503
504                         print $search_expr;
505
506                         if ($title) {
507                                 $result = db_query($link,
508                                         "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
509                                         VALUES ('$search_expr', '$title', '".$_SESSION["uid"]."')");
510                         }
511                 break; // labelFromSearch
512
513                 case "getUnread":
514                         $login = db_escape_string($_GET["login"]);
515
516                         header("Content-Type: text/plain; charset=utf-8");
517
518                         $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
519
520                         if (db_num_rows($result) == 1) {
521                                 $uid = db_fetch_result($result, 0, "id");
522                                 print getGlobalUnread($link, $uid);
523                         } else {
524                                 print "-1;User not found";
525                         }
526
527                         $print_exec_time = false;
528                 break; // getUnread
529
530                 case "digestTest":
531                         header("Content-Type: text/plain");
532                         print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
533                         $print_exec_time = false;
534                 break; // digestTest
535
536                 case "digestSend":
537                         header("Content-Type: text/plain");
538                         send_headlines_digests($link);
539                         $print_exec_time = false;
540                 break; // digestSend
541
542         } // Select action according to $op value.
543
544         // We close the connection to database.
545         db_close($link);
546 ?>
547
548 <?php if ($print_exec_time) { ?>
549 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
550 <?php } ?>