]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
3b1208f316997965ba901716ca519eabec7c50e3
[tt-rss.git] / backend.php
1 <?php
2         set_include_path(get_include_path() . PATH_SEPARATOR .
3                 dirname(__FILE__) . "/include");
4
5         /* remove ill effects of magic quotes */
6
7         if (get_magic_quotes_gpc()) {
8                 function stripslashes_deep($value) {
9                         $value = is_array($value) ?
10                                 array_map('stripslashes_deep', $value) : stripslashes($value);
11                                 return $value;
12                 }
13
14                 $_POST = array_map('stripslashes_deep', $_POST);
15                 $_GET = array_map('stripslashes_deep', $_GET);
16                 $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
17                 $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
18         }
19
20         $op = $_REQUEST["op"];
21         @$method = $_REQUEST['subop'] ? $_REQUEST['subop'] : $_REQUEST["method"];
22
23         if (!$method)
24                 $method = 'index';
25         else
26                 $method = strtolower($method);
27
28         /* Public calls compatibility shim */
29
30         $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
31                 "fbexport", "logout", "pubsub");
32
33         if (array_search($op, $public_calls) !== false) {
34                 header("Location: public.php?" . $_SERVER['QUERY_STRING']);
35                 return;
36         }
37
38         $csrf_token = $_REQUEST['csrf_token'];
39
40         require_once "functions.php";
41         require_once "sessions.php";
42         require_once "sanity_check.php";
43         require_once "config.php";
44         require_once "db.php";
45         require_once "db-prefs.php";
46
47         no_cache_incantation();
48
49         startup_gettext();
50
51         $script_started = getmicrotime();
52
53         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
54
55         if (!init_connection($link)) return;
56
57         header("Content-Type: text/plain; charset=utf-8");
58
59         if (ENABLE_GZIP_OUTPUT) {
60                 ob_start("ob_gzhandler");
61         }
62
63         if (SINGLE_USER_MODE) {
64                 authenticate_user($link, "admin", null);
65         }
66
67         // TODO remove and handle within Handlers
68
69         if (!($_SESSION["uid"] && validate_session($link))) {
70                 if ($op == 'pref-feeds' && $method == 'add') {
71                         header("Content-Type: text/html");
72                         login_sequence($link);
73                         render_login_form($link);
74                 } else {
75                         header("Content-Type: text/plain");
76                         print json_encode(array("error" => array("code" => 6)));
77                 }
78                 return;
79         }
80
81         $purge_intervals = array(
82                 0  => __("Use default"),
83                 -1 => __("Never purge"),
84                 5  => __("1 week old"),
85                 14 => __("2 weeks old"),
86                 31 => __("1 month old"),
87                 60 => __("2 months old"),
88                 90 => __("3 months old"));
89
90         $update_intervals = array(
91                 0   => __("Default interval"),
92                 -1  => __("Disable updates"),
93                 15  => __("Each 15 minutes"),
94                 30  => __("Each 30 minutes"),
95                 60  => __("Hourly"),
96                 240 => __("Each 4 hours"),
97                 720 => __("Each 12 hours"),
98                 1440 => __("Daily"),
99                 10080 => __("Weekly"));
100
101         $update_intervals_nodefault = array(
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_methods = array(
112                 0   => __("Default"),
113                 1   => __("Magpie"),
114                 2   => __("SimplePie"),
115                 3   => __("Twitter OAuth"));
116
117         if (DEFAULT_UPDATE_METHOD == "1") {
118                 $update_methods[0] .= ' (SimplePie)';
119         } else {
120                 $update_methods[0] .= ' (Magpie)';
121         }
122
123         $access_level_names = array(
124                 0 => __("User"),
125                 5 => __("Power User"),
126                 10 => __("Administrator"));
127
128         $error = sanity_check($link);
129
130         if ($error['code'] != 0 && $op != "logout") {
131                 print json_encode(array("error" => $error));
132                 return;
133         }
134
135         function __autoload($class) {
136                 $file = "classes/".strtolower(basename($class)).".php";
137                 if (file_exists($file)) {
138                         require $file;
139                 }
140         }
141
142         $op = str_replace("-", "_", $op);
143
144         if (class_exists($op)) {
145                 $handler = new $op($link, $_REQUEST);
146
147                 if ($handler) {
148                         if (validate_csrf($csrf_token) || $handler->csrf_ignore($method)) {
149                                 if ($handler->before($method)) {
150                                         if ($method && method_exists($handler, $method)) {
151                                                 $handler->$method();
152                                         }
153                                         $handler->after();
154                                         return;
155                                 }
156                         } else {
157                                 header("Content-Type: text/plain");
158                                 print json_encode(array("error" => array("code" => 6)));
159                                 return;
160                         }
161                 }
162         }
163
164         header("Content-Type: text/plain");
165         print json_encode(array("error" => array("code" => 7)));
166
167         // We close the connection to database.
168         db_close($link);
169 ?>