]> git.wh0rd.org - tt-rss.git/blob - backend.php
implement tiny-OOP routing
[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 function __autoload($class) {
20 $file = "classes/".strtolower(basename($class)).".php";
21 if (file_exists($file)) {
22 require $file;
23 }
24 }
25
26 $op = $_REQUEST["op"];
27
28 require_once "functions.php";
29 if ($op != "share") require_once "sessions.php";
30 require_once "sanity_check.php";
31 require_once "config.php";
32 require_once "db.php";
33 require_once "db-prefs.php";
34
35 no_cache_incantation();
36
37 startup_gettext();
38
39 $script_started = getmicrotime();
40
41 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
42
43 if (!$link) {
44 if (DB_TYPE == "mysql") {
45 print mysql_error();
46 }
47 // PG seems to display its own errors just fine by default.
48 return;
49 }
50
51 init_connection($link);
52
53 $method = strtolower($_REQUEST["method"]);
54 $mode = $_REQUEST["mode"];
55
56 /* if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
57 header("Content-Type: application/xml; charset=utf-8");
58 } else {
59 header("Content-Type: text/plain; charset=utf-8");
60 } */
61
62 header("Content-Type: text/plain; charset=utf-8");
63
64 if (ENABLE_GZIP_OUTPUT) {
65 ob_start("ob_gzhandler");
66 }
67
68 if (SINGLE_USER_MODE) {
69 authenticate_user($link, "admin", null);
70 }
71
72 $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
73 "fbexport", "logout", "pubsub");
74
75 if (array_search($op, $public_calls) !== false) {
76
77 handle_public_request($link, $op);
78 return;
79
80 } else if (!($_SESSION["uid"] && validate_session($link))) {
81 if ($op == 'pref-feeds' && $_REQUEST['method'] == 'add') {
82 header("Content-Type: text/html");
83 login_sequence($link);
84 render_login_form($link);
85 } else {
86 header("Content-Type: text/plain");
87 print json_encode(array("error" => array("code" => 6)));
88 }
89 return;
90 }
91
92 $purge_intervals = array(
93 0 => __("Use default"),
94 -1 => __("Never purge"),
95 5 => __("1 week old"),
96 14 => __("2 weeks old"),
97 31 => __("1 month old"),
98 60 => __("2 months old"),
99 90 => __("3 months old"));
100
101 $update_intervals = array(
102 0 => __("Default interval"),
103 -1 => __("Disable updates"),
104 15 => __("Each 15 minutes"),
105 30 => __("Each 30 minutes"),
106 60 => __("Hourly"),
107 240 => __("Each 4 hours"),
108 720 => __("Each 12 hours"),
109 1440 => __("Daily"),
110 10080 => __("Weekly"));
111
112 $update_intervals_nodefault = array(
113 -1 => __("Disable updates"),
114 15 => __("Each 15 minutes"),
115 30 => __("Each 30 minutes"),
116 60 => __("Hourly"),
117 240 => __("Each 4 hours"),
118 720 => __("Each 12 hours"),
119 1440 => __("Daily"),
120 10080 => __("Weekly"));
121
122 $update_methods = array(
123 0 => __("Default"),
124 1 => __("Magpie"),
125 2 => __("SimplePie"),
126 3 => __("Twitter OAuth"));
127
128 if (DEFAULT_UPDATE_METHOD == "1") {
129 $update_methods[0] .= ' (SimplePie)';
130 } else {
131 $update_methods[0] .= ' (Magpie)';
132 }
133
134 $access_level_names = array(
135 0 => __("User"),
136 5 => __("Power User"),
137 10 => __("Administrator"));
138
139 $error = sanity_check($link);
140
141 if ($error['code'] != 0 && $op != "logout") {
142 print json_encode(array("error" => $error));
143 return;
144 }
145
146 if (class_exists($op)) {
147 $handler = new $op($link, $_REQUEST);
148
149 if ($handler) {
150 if ($handler->before()) {
151 if ($method && method_exists($handler, $method)) {
152 $handler->$method();
153 } else if (method_exists($handler, 'index')) {
154 $handler->index();
155 }
156 $handler->after();
157 return;
158 }
159 }
160 }
161
162 switch($op) { // Select action according to $op value.
163
164 case "pref-feeds":
165 require_once "modules/pref-feeds.php";
166 module_pref_feeds($link);
167 break; // pref-feeds
168
169 case "pref-filters":
170 require_once "modules/pref-filters.php";
171 module_pref_filters($link);
172 break; // pref-filters
173
174 case "pref-labels":
175 require_once "modules/pref-labels.php";
176 module_pref_labels($link);
177 break; // pref-labels
178
179 case "pref-prefs":
180 require_once "modules/pref-prefs.php";
181 module_pref_prefs($link);
182 break; // pref-prefs
183
184 case "pref-users":
185 require_once "modules/pref-users.php";
186 module_pref_users($link);
187 break; // prefs-users
188
189 case "help":
190 require_once "modules/help.php";
191 module_help($link);
192 break; // help
193
194 case "pref-instances":
195 require_once "modules/pref-instances.php";
196 module_pref_instances($link);
197 break; // pref-instances
198
199 case "digestTest":
200 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
201 break; // digestTest
202
203 case "digestSend":
204 send_headlines_digests($link);
205 break; // digestSend
206
207 case "loading":
208 header("Content-type: text/html");
209 print __("Loading, please wait...") . " " .
210 "<img src='images/indicator_tiny.gif'>";
211 break; // loading
212
213 default:
214 header("Content-Type: text/plain");
215 print json_encode(array("error" => array("code" => 7)));
216 break; // fallback
217 } // Select action according to $op value.
218
219 // We close the connection to database.
220 db_close($link);
221 ?>