]> git.wh0rd.org Git - tt-rss.git/blob - api/index.php
more work on singleton-based DB
[tt-rss.git] / api / index.php
1 <?php
2         error_reporting(E_ERROR | E_PARSE);
3
4         require_once "../config.php";
5
6         set_include_path(dirname(__FILE__) . PATH_SEPARATOR .
7                 dirname(dirname(__FILE__)) . PATH_SEPARATOR .
8                 dirname(dirname(__FILE__)) . "/include" . PATH_SEPARATOR .
9                 get_include_path());
10
11         chdir("..");
12
13         define('TTRSS_SESSION_NAME', 'ttrss_api_sid');
14         define('NO_SESSION_AUTOSTART', true);
15
16         require_once "autoload.php";
17         require_once "db.php";
18         require_once "db-prefs.php";
19         require_once "functions.php";
20         require_once "sessions.php";
21
22         ini_set("session.gc_maxlifetime", 86400);
23
24         define('AUTH_DISABLE_OTP', true);
25
26         if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT &&
27                         function_exists("ob_gzhandler")) {
28
29                 ob_start("ob_gzhandler");
30         } else {
31                 ob_start();
32         }
33
34         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
35
36         $input = file_get_contents("php://input");
37
38         if (defined('_API_DEBUG_HTTP_ENABLED') && _API_DEBUG_HTTP_ENABLED) {
39                 // Override $_REQUEST with JSON-encoded data if available
40                 // fallback on HTTP parameters
41                 if ($input) {
42                         $input = json_decode($input, true);
43                         if ($input) $_REQUEST = $input;
44                 }
45         } else {
46                 // Accept JSON only
47                 $input = json_decode($input, true);
48                 $_REQUEST = $input;
49         }
50
51         if ($_REQUEST["sid"]) {
52                 session_id($_REQUEST["sid"]);
53                 @session_start();
54         } else if (defined('_API_DEBUG_HTTP_ENABLED')) {
55                 @session_start();
56         }
57
58         if (!init_plugins($link)) return;
59
60         $method = strtolower($_REQUEST["op"]);
61
62         $handler = new API($link, $_REQUEST);
63
64         if ($handler->before($method)) {
65                 if ($method && method_exists($handler, $method)) {
66                         $handler->$method();
67                 } else if (method_exists($handler, 'index')) {
68                         $handler->index($method);
69                 }
70                 $handler->after();
71         }
72
73         db_close($link);
74
75         header("Api-Content-Length: " . ob_get_length());
76
77         ob_end_flush();
78 ?>