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