greader.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. <?php
  2. /**
  3. == Description ==
  4. Server-side API compatible with Google Reader API layer 2
  5. for the FreshRSS project https://freshrss.org
  6. == Credits ==
  7. * 2014-03: Released by Alexandre Alapetite https://alexandre.alapetite.fr
  8. under GNU AGPL 3 license http://www.gnu.org/licenses/agpl-3.0.html
  9. == Documentation ==
  10. * http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  11. * https://web.archive.org/web/20130718025427/http://undoc.in/
  12. * http://ranchero.com/downloads/GoogleReaderAPI-2009.pdf
  13. * http://code.google.com/p/google-reader-api/w/list
  14. * https://web.archive.org/web/20210126115837/https://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/
  15. * https://github.com/noinnion/newsplus/blob/master/extensions/GoogleReaderCloneExtension/src/com/noinnion/android/newsplus/extension/google_reader/GoogleReaderClient.java
  16. * https://github.com/ericmann/gReader-Library/blob/master/greader.class.php
  17. * https://github.com/devongovett/reader
  18. * https://github.com/theoldreader/api
  19. * https://www.inoreader.com/developers/
  20. * https://feedhq.readthedocs.io/en/latest/api/index.html
  21. * https://github.com/bazqux/bazqux-api
  22. */
  23. require(__DIR__ . '/../../constants.php');
  24. require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader
  25. $ORIGINAL_INPUT = file_get_contents('php://input', false, null, 0, 1048576);
  26. if (PHP_INT_SIZE < 8) { //32-bit
  27. /**
  28. * @param string $hex
  29. * @return string
  30. */
  31. function hex2dec($hex) {
  32. if (!ctype_xdigit($hex)) return '0';
  33. return gmp_strval(gmp_init($hex, 16), 10);
  34. }
  35. } else { //64-bit
  36. /**
  37. * @param string $hex
  38. * @return string
  39. */
  40. function hex2dec($hex) {
  41. if (!ctype_xdigit($hex)) return '0';
  42. return '' . hexdec($hex);
  43. }
  44. }
  45. define('JSON_OPTIONS', JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  46. function headerVariable($headerName, $varName) {
  47. $header = '';
  48. $upName = 'HTTP_' . strtoupper($headerName);
  49. if (isset($_SERVER[$upName])) {
  50. $header = $_SERVER[$upName];
  51. } elseif (isset($_SERVER['REDIRECT_' . $upName])) {
  52. $header = $_SERVER['REDIRECT_' . $upName];
  53. } elseif (function_exists('getallheaders')) {
  54. $ALL_HEADERS = getallheaders();
  55. if (isset($ALL_HEADERS[$headerName])) {
  56. $header = $ALL_HEADERS[$headerName];
  57. }
  58. }
  59. parse_str($header, $pairs);
  60. return isset($pairs[$varName]) ? $pairs[$varName] : null;
  61. }
  62. function multiplePosts($name) {
  63. //https://bugs.php.net/bug.php?id=51633
  64. global $ORIGINAL_INPUT;
  65. $inputs = explode('&', $ORIGINAL_INPUT);
  66. $result = array();
  67. $prefix = $name . '=';
  68. $prefixLength = strlen($prefix);
  69. foreach ($inputs as $input) {
  70. if (strpos($input, $prefix) === 0) {
  71. $result[] = urldecode(substr($input, $prefixLength));
  72. }
  73. }
  74. return $result;
  75. }
  76. /**
  77. * @return string
  78. */
  79. function debugInfo() {
  80. if (function_exists('getallheaders')) {
  81. $ALL_HEADERS = getallheaders();
  82. } else { //nginx http://php.net/getallheaders#84262
  83. $ALL_HEADERS = array();
  84. foreach ($_SERVER as $name => $value) {
  85. if (substr($name, 0, 5) === 'HTTP_') {
  86. $ALL_HEADERS[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  87. }
  88. }
  89. }
  90. global $ORIGINAL_INPUT;
  91. return print_r(
  92. array(
  93. 'date' => date('c'),
  94. 'headers' => $ALL_HEADERS,
  95. '_SERVER' => $_SERVER,
  96. '_GET' => $_GET,
  97. '_POST' => $_POST,
  98. '_COOKIE' => $_COOKIE,
  99. 'INPUT' => $ORIGINAL_INPUT
  100. ), true);
  101. }
  102. function badRequest() {
  103. Minz_Log::warning('badRequest() ' . debugInfo(), API_LOG);
  104. header('HTTP/1.1 400 Bad Request');
  105. header('Content-Type: text/plain; charset=UTF-8');
  106. die('Bad Request!');
  107. }
  108. function unauthorized() {
  109. Minz_Log::warning('unauthorized() ' . debugInfo(), API_LOG);
  110. header('HTTP/1.1 401 Unauthorized');
  111. header('Content-Type: text/plain; charset=UTF-8');
  112. header('Google-Bad-Token: true');
  113. die('Unauthorized!');
  114. }
  115. function notImplemented() {
  116. Minz_Log::warning('notImplemented() ' . debugInfo(), API_LOG);
  117. header('HTTP/1.1 501 Not Implemented');
  118. header('Content-Type: text/plain; charset=UTF-8');
  119. die('Not Implemented!');
  120. }
  121. function serviceUnavailable() {
  122. Minz_Log::warning('serviceUnavailable() ' . debugInfo(), API_LOG);
  123. header('HTTP/1.1 503 Service Unavailable');
  124. header('Content-Type: text/plain; charset=UTF-8');
  125. die('Service Unavailable!');
  126. }
  127. function checkCompatibility() {
  128. Minz_Log::warning('checkCompatibility() ' . debugInfo(), API_LOG);
  129. header('Content-Type: text/plain; charset=UTF-8');
  130. if (PHP_INT_SIZE < 8 && !function_exists('gmp_init')) {
  131. die('FAIL 64-bit or GMP extension! Wrong PHP configuration.');
  132. }
  133. $headerAuth = headerVariable('Authorization', 'GoogleLogin_auth');
  134. if ($headerAuth == '') {
  135. die('FAIL get HTTP Authorization header! Wrong Web server configuration.');
  136. }
  137. echo 'PASS';
  138. exit();
  139. }
  140. function authorizationToUser() {
  141. //Input is 'GoogleLogin auth', but PHP replaces spaces by '_' http://php.net/language.variables.external
  142. $headerAuth = headerVariable('Authorization', 'GoogleLogin_auth');
  143. if ($headerAuth != '') {
  144. $headerAuthX = explode('/', $headerAuth, 2);
  145. if (count($headerAuthX) === 2) {
  146. $user = $headerAuthX[0];
  147. if (FreshRSS_user_Controller::checkUsername($user)) {
  148. FreshRSS_Context::initUser($user);
  149. if (FreshRSS_Context::$user_conf == null) {
  150. Minz_Log::warning('Invalid API user ' . $user . ': configuration cannot be found.');
  151. unauthorized();
  152. }
  153. if (!FreshRSS_Context::$user_conf->enabled) {
  154. Minz_Log::warning('Invalid API user ' . $user . ': configuration cannot be found.');
  155. unauthorized();
  156. }
  157. if ($headerAuthX[1] === sha1(FreshRSS_Context::$system_conf->salt . $user . FreshRSS_Context::$user_conf->apiPasswordHash)) {
  158. return $user;
  159. } else {
  160. Minz_Log::warning('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1], API_LOG);
  161. Minz_Log::warning('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1]);
  162. unauthorized();
  163. }
  164. } else {
  165. badRequest();
  166. }
  167. }
  168. }
  169. return '';
  170. }
  171. function clientLogin($email, $pass) {
  172. //https://web.archive.org/web/20130604091042/http://undoc.in/clientLogin.html
  173. if (FreshRSS_user_Controller::checkUsername($email)) {
  174. FreshRSS_Context::initUser($email);
  175. if (FreshRSS_Context::$user_conf == null) {
  176. Minz_Log::warning('Invalid API user ' . $email . ': configuration cannot be found.');
  177. unauthorized();
  178. }
  179. if (FreshRSS_Context::$user_conf->apiPasswordHash != '' && password_verify($pass, FreshRSS_Context::$user_conf->apiPasswordHash)) {
  180. header('Content-Type: text/plain; charset=UTF-8');
  181. $auth = $email . '/' . sha1(FreshRSS_Context::$system_conf->salt . $email . FreshRSS_Context::$user_conf->apiPasswordHash);
  182. echo 'SID=', $auth, "\n",
  183. 'LSID=null', "\n", //Vienna RSS
  184. 'Auth=', $auth, "\n";
  185. exit();
  186. } else {
  187. Minz_Log::warning('Password API mismatch for user ' . $email);
  188. unauthorized();
  189. }
  190. } else {
  191. badRequest();
  192. }
  193. die();
  194. }
  195. function token($conf) {
  196. //http://blog.martindoms.com/2009/08/15/using-the-google-reader-api-part-1/
  197. //https://github.com/ericmann/gReader-Library/blob/master/greader.class.php
  198. $user = Minz_Session::param('currentUser', '_');
  199. //Minz_Log::debug('token('. $user . ')', API_LOG); //TODO: Implement real token that expires
  200. $token = str_pad(sha1(FreshRSS_Context::$system_conf->salt . $user . $conf->apiPasswordHash), 57, 'Z'); //Must have 57 characters
  201. echo $token, "\n";
  202. exit();
  203. }
  204. function checkToken($conf, $token) {
  205. //http://code.google.com/p/google-reader-api/wiki/ActionToken
  206. $user = Minz_Session::param('currentUser', '_');
  207. if ($user !== '_' && ( //TODO: Check security consequences
  208. $token == '' || //FeedMe
  209. $token === 'x')) { //Reeder
  210. return true;
  211. }
  212. if ($token === str_pad(sha1(FreshRSS_Context::$system_conf->salt . $user . $conf->apiPasswordHash), 57, 'Z')) {
  213. return true;
  214. }
  215. Minz_Log::warning('Invalid POST token: ' . $token, API_LOG);
  216. unauthorized();
  217. }
  218. function userInfo() {
  219. //https://github.com/theoldreader/api#user-info
  220. $user = Minz_Session::param('currentUser', '_');
  221. exit(json_encode(array(
  222. 'userId' => $user,
  223. 'userName' => $user,
  224. 'userProfileId' => $user,
  225. 'userEmail' => FreshRSS_Context::$user_conf->mail_login,
  226. ), JSON_OPTIONS));
  227. }
  228. function tagList() {
  229. header('Content-Type: application/json; charset=UTF-8');
  230. $tags = array(
  231. array('id' => 'user/-/state/com.google/starred'),
  232. //array('id' => 'user/-/state/com.google/broadcast', 'sortid' => '2'),
  233. );
  234. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  235. $categories = $categoryDAO->listCategories(true, false);
  236. foreach ($categories as $cat) {
  237. $tags[] = array(
  238. 'id' => 'user/-/label/' . htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  239. //'sortid' => $cat->name(),
  240. 'type' => 'folder', //Inoreader
  241. );
  242. }
  243. $tagDAO = FreshRSS_Factory::createTagDao();
  244. $labels = $tagDAO->listTags(true);
  245. foreach ($labels as $label) {
  246. $tags[] = array(
  247. 'id' => 'user/-/label/' . htmlspecialchars_decode($label->name(), ENT_QUOTES),
  248. //'sortid' => $label->name(),
  249. 'type' => 'tag', //Inoreader
  250. 'unread_count' => $label->nbUnread(), //Inoreader
  251. );
  252. }
  253. echo json_encode(array('tags' => $tags), JSON_OPTIONS), "\n";
  254. exit();
  255. }
  256. function subscriptionExport() {
  257. $user = Minz_Session::param('currentUser', '_');
  258. $export_service = new FreshRSS_Export_Service($user);
  259. list($filename, $content) = $export_service->generateOpml();
  260. header('Content-Type: application/xml; charset=UTF-8');
  261. header('Content-disposition: attachment; filename="' . $filename . '"');
  262. echo $content;
  263. exit();
  264. }
  265. function subscriptionImport($opml) {
  266. $user = Minz_Session::param('currentUser', '_');
  267. $importService = new FreshRSS_Import_Service($user);
  268. $importService->importOpml($opml);
  269. if ($importService->lastStatus()) {
  270. list($nbUpdatedFeeds, $feed, $nbNewArticles) = FreshRSS_feed_Controller::actualizeFeed(0, '', true);
  271. invalidateHttpCache($user);
  272. exit('OK');
  273. } else {
  274. badRequest();
  275. }
  276. }
  277. function subscriptionList() {
  278. header('Content-Type: application/json; charset=UTF-8');
  279. $salt = FreshRSS_Context::$system_conf->salt;
  280. $faviconsUrl = Minz_Url::display('/f.php?', '', true);
  281. $faviconsUrl = str_replace('/api/greader.php/reader/api/0/subscription', '', $faviconsUrl); //Security if base_url is not set properly
  282. $subscriptions = array();
  283. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  284. foreach ($categoryDAO->listCategories(true, true) as $cat) {
  285. foreach ($cat->feeds() as $feed) {
  286. $subscriptions[] = [
  287. 'id' => 'feed/' . $feed->id(),
  288. 'title' => escapeToUnicodeAlternative($feed->name(), true),
  289. 'categories' => [
  290. [
  291. 'id' => 'user/-/label/' . htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  292. 'label' => htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  293. ],
  294. ],
  295. //'sortid' => $feed->name(),
  296. //'firstitemmsec' => 0,
  297. 'url' => htmlspecialchars_decode($feed->url(), ENT_QUOTES),
  298. 'htmlUrl' => htmlspecialchars_decode($feed->website(), ENT_QUOTES),
  299. 'iconUrl' => $faviconsUrl . hash('crc32b', $salt . $feed->url()),
  300. ];
  301. }
  302. }
  303. echo json_encode(array('subscriptions' => $subscriptions), JSON_OPTIONS), "\n";
  304. exit();
  305. }
  306. function subscriptionEdit($streamNames, $titles, $action, $add = '', $remove = '') {
  307. //https://github.com/mihaip/google-reader-api/blob/master/wiki/ApiSubscriptionEdit.wiki
  308. switch ($action) {
  309. case 'subscribe':
  310. case 'unsubscribe':
  311. case 'edit':
  312. break;
  313. default:
  314. badRequest();
  315. }
  316. $addCatId = 0;
  317. $categoryDAO = null;
  318. if ($add != '' || $remove != '') {
  319. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  320. }
  321. $c_name = '';
  322. if ($add != '' && strpos($add, 'user/') === 0) { //user/-/label/Example ; user/username/label/Example
  323. if (strpos($add, 'user/-/label/') === 0) {
  324. $c_name = substr($add, 13);
  325. } else {
  326. $user = Minz_Session::param('currentUser', '_');
  327. $prefix = 'user/' . $user . '/label/';
  328. if (strpos($add, $prefix) === 0) {
  329. $c_name = substr($add, strlen($prefix));
  330. } else {
  331. $c_name = '';
  332. }
  333. }
  334. $c_name = htmlspecialchars($c_name, ENT_COMPAT, 'UTF-8');
  335. $cat = $categoryDAO->searchByName($c_name);
  336. $addCatId = $cat == null ? 0 : $cat->id();
  337. } elseif ($remove != '' && strpos($remove, 'user/-/label/') === 0) {
  338. $addCatId = 1; //Default category
  339. }
  340. $feedDAO = FreshRSS_Factory::createFeedDao();
  341. if (!is_array($streamNames) || count($streamNames) < 1) {
  342. badRequest();
  343. }
  344. for ($i = count($streamNames) - 1; $i >= 0; $i--) {
  345. $streamUrl = $streamNames[$i]; //feed/http://example.net/sample.xml ; feed/338
  346. if (strpos($streamUrl, 'feed/') === 0) {
  347. $streamUrl = preg_replace('%^(feed/)+%', '', $streamUrl);
  348. $feedId = 0;
  349. if (ctype_digit($streamUrl)) {
  350. if ($action === 'subscribe') {
  351. continue;
  352. }
  353. $feedId = $streamUrl;
  354. } else {
  355. $streamUrl = htmlspecialchars($streamUrl, ENT_COMPAT, 'UTF-8');
  356. $feed = $feedDAO->searchByUrl($streamUrl);
  357. $feedId = $feed == null ? -1 : $feed->id();
  358. }
  359. $title = isset($titles[$i]) ? $titles[$i] : '';
  360. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
  361. switch ($action) {
  362. case 'subscribe':
  363. if ($feedId <= 0) {
  364. $http_auth = '';
  365. try {
  366. $feed = FreshRSS_feed_Controller::addFeed($streamUrl, $title, $addCatId, $c_name, $http_auth);
  367. continue 2;
  368. } catch (Exception $e) {
  369. Minz_Log::error('subscriptionEdit error subscribe: ' . $e->getMessage(), API_LOG);
  370. }
  371. }
  372. badRequest();
  373. break;
  374. case 'unsubscribe':
  375. if (!($feedId > 0 && FreshRSS_feed_Controller::deleteFeed($feedId))) {
  376. badRequest();
  377. }
  378. break;
  379. case 'edit':
  380. if ($feedId > 0) {
  381. if ($addCatId > 0 || $c_name != '') {
  382. FreshRSS_feed_Controller::moveFeed($feedId, $addCatId, $c_name);
  383. }
  384. if ($title != '') {
  385. FreshRSS_feed_Controller::renameFeed($feedId, $title);
  386. }
  387. } else {
  388. badRequest();
  389. }
  390. break;
  391. }
  392. }
  393. }
  394. exit('OK');
  395. }
  396. function quickadd($url) {
  397. try {
  398. $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
  399. if (substr($url, 0, 5) === 'feed/') {
  400. $url = substr($url, 5);
  401. }
  402. $feed = FreshRSS_feed_Controller::addFeed($url);
  403. exit(json_encode(array(
  404. 'numResults' => 1,
  405. 'query' => $feed->url(),
  406. 'streamId' => 'feed/' . $feed->id(),
  407. 'streamName' => $feed->name(),
  408. ), JSON_OPTIONS));
  409. } catch (Exception $e) {
  410. Minz_Log::error('quickadd error: ' . $e->getMessage(), API_LOG);
  411. die(json_encode(array(
  412. 'numResults' => 0,
  413. 'error' => $e->getMessage(),
  414. ), JSON_OPTIONS));
  415. }
  416. }
  417. function unreadCount() {
  418. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#unread-count
  419. header('Content-Type: application/json; charset=UTF-8');
  420. $totalUnreads = 0;
  421. $totalLastUpdate = 0;
  422. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  423. $feedDAO = FreshRSS_Factory::createFeedDao();
  424. $feedsNewestItemUsec = $feedDAO->listFeedsNewestItemUsec();
  425. foreach ($categoryDAO->listCategories(true, true) as $cat) {
  426. $catLastUpdate = 0;
  427. foreach ($cat->feeds() as $feed) {
  428. $lastUpdate = isset($feedsNewestItemUsec['f_' . $feed->id()]) ? $feedsNewestItemUsec['f_' . $feed->id()] : 0;
  429. $unreadcounts[] = array(
  430. 'id' => 'feed/' . $feed->id(),
  431. 'count' => $feed->nbNotRead(),
  432. 'newestItemTimestampUsec' => '' . $lastUpdate,
  433. );
  434. if ($catLastUpdate < $lastUpdate) {
  435. $catLastUpdate = $lastUpdate;
  436. }
  437. }
  438. $unreadcounts[] = array(
  439. 'id' => 'user/-/label/' . htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  440. 'count' => $cat->nbNotRead(),
  441. 'newestItemTimestampUsec' => '' . $catLastUpdate,
  442. );
  443. $totalUnreads += $cat->nbNotRead();
  444. if ($totalLastUpdate < $catLastUpdate) {
  445. $totalLastUpdate = $catLastUpdate;
  446. }
  447. }
  448. $tagDAO = FreshRSS_Factory::createTagDao();
  449. $tagsNewestItemUsec = $tagDAO->listTagsNewestItemUsec();
  450. foreach ($tagDAO->listTags(true) as $label) {
  451. $lastUpdate = isset($tagsNewestItemUsec['t_' . $label->id()]) ? $tagsNewestItemUsec['t_' . $label->id()] : 0;
  452. $unreadcounts[] = array(
  453. 'id' => 'user/-/label/' . htmlspecialchars_decode($label->name(), ENT_QUOTES),
  454. 'count' => $label->nbUnread(),
  455. 'newestItemTimestampUsec' => '' . $lastUpdate,
  456. );
  457. }
  458. $unreadcounts[] = array(
  459. 'id' => 'user/-/state/com.google/reading-list',
  460. 'count' => $totalUnreads,
  461. 'newestItemTimestampUsec' => '' . $totalLastUpdate,
  462. );
  463. echo json_encode(array(
  464. 'max' => $totalUnreads,
  465. 'unreadcounts' => $unreadcounts,
  466. ), JSON_OPTIONS), "\n";
  467. exit();
  468. }
  469. function entriesToArray($entries) {
  470. if (empty($entries)) {
  471. return array();
  472. }
  473. $catDAO = FreshRSS_Factory::createCategoryDao();
  474. $categories = $catDAO->listCategories(true);
  475. $tagDAO = FreshRSS_Factory::createTagDao();
  476. $entryIdsTagNames = $tagDAO->getEntryIdsTagNames($entries);
  477. if ($entryIdsTagNames == false) {
  478. $entryIdsTagNames = array();
  479. }
  480. $items = array();
  481. foreach ($entries as $item) {
  482. /** @var FreshRSS_Entry $entry */
  483. $entry = Minz_ExtensionManager::callHook('entry_before_display', $item);
  484. if ($entry == null) {
  485. continue;
  486. }
  487. $feed = FreshRSS_CategoryDAO::findFeed($categories, $entry->feedId());
  488. $entry->_feed($feed);
  489. if (isset($entryIdsTagNames['e_' . $entry->id()])) {
  490. $entry->_tags($entryIdsTagNames['e_' . $entry->id()]);
  491. }
  492. $items[] = $entry->toGReader('compat');
  493. }
  494. return $items;
  495. }
  496. function streamContentsFilters($type, $streamId, $filter_target, $exclude_target, $start_time, $stop_time) {
  497. switch ($type) {
  498. case 'f': //feed
  499. if ($streamId != '' && !ctype_digit($streamId)) {
  500. $feedDAO = FreshRSS_Factory::createFeedDao();
  501. $streamId = htmlspecialchars($streamId, ENT_COMPAT, 'UTF-8');
  502. $feed = $feedDAO->searchByUrl($streamId);
  503. $streamId = $feed == null ? -1 : $feed->id();
  504. }
  505. break;
  506. case 'c': //category or label
  507. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  508. $streamId = htmlspecialchars($streamId, ENT_COMPAT, 'UTF-8');
  509. $cat = $categoryDAO->searchByName($streamId);
  510. if ($cat != null) {
  511. $type = 'c';
  512. $streamId = $cat->id();
  513. } else {
  514. $tagDAO = FreshRSS_Factory::createTagDao();
  515. $tag = $tagDAO->searchByName($streamId);
  516. if ($tag != null) {
  517. $type = 't';
  518. $streamId = $tag->id();
  519. } else {
  520. $type = 'A';
  521. $streamId = -1;
  522. }
  523. }
  524. break;
  525. }
  526. switch ($filter_target) {
  527. case 'user/-/state/com.google/read':
  528. $state = FreshRSS_Entry::STATE_READ;
  529. break;
  530. case 'user/-/state/com.google/unread':
  531. $state = FreshRSS_Entry::STATE_NOT_READ;
  532. break;
  533. case 'user/-/state/com.google/starred':
  534. $state = FreshRSS_Entry::STATE_FAVORITE;
  535. break;
  536. default:
  537. $state = FreshRSS_Entry::STATE_ALL;
  538. break;
  539. }
  540. switch ($exclude_target) {
  541. case 'user/-/state/com.google/read':
  542. $state &= FreshRSS_Entry::STATE_NOT_READ;
  543. break;
  544. case 'user/-/state/com.google/unread':
  545. $state &= FreshRSS_Entry::STATE_READ;
  546. break;
  547. case 'user/-/state/com.google/starred':
  548. $state &= FreshRSS_Entry::STATE_NOT_FAVORITE;
  549. break;
  550. }
  551. $searches = new FreshRSS_BooleanSearch('');
  552. if ($start_time != '') {
  553. $search = new FreshRSS_Search('');
  554. $search->setMinDate($start_time);
  555. $searches->add($search);
  556. }
  557. if ($stop_time != '') {
  558. $search = new FreshRSS_Search('');
  559. $search->setMaxDate($stop_time);
  560. $searches->add($search);
  561. }
  562. return array($type, $streamId, $state, $searches);
  563. }
  564. function streamContents($path, $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation) {
  565. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  566. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  567. header('Content-Type: application/json; charset=UTF-8');
  568. switch ($path) {
  569. case 'reading-list':
  570. $type = 'A';
  571. break;
  572. case 'starred':
  573. $type = 's';
  574. break;
  575. case 'feed':
  576. $type = 'f';
  577. break;
  578. case 'label':
  579. $type = 'c';
  580. break;
  581. default:
  582. $type = 'A';
  583. break;
  584. }
  585. list($type, $include_target, $state, $searches) = streamContentsFilters($type, $include_target, $filter_target, $exclude_target, $start_time, $stop_time);
  586. if ($continuation != '') {
  587. $count++; //Shift by one element
  588. }
  589. $entryDAO = FreshRSS_Factory::createEntryDao();
  590. $entries = $entryDAO->listWhere($type, $include_target, $state, $order === 'o' ? 'ASC' : 'DESC', $count, $continuation, $searches);
  591. $entries = iterator_to_array($entries); //TODO: Improve
  592. $items = entriesToArray($entries);
  593. if ($continuation != '') {
  594. array_shift($items); //Discard first element that was already sent in the previous response
  595. $count--;
  596. }
  597. $response = array(
  598. 'id' => 'user/-/state/com.google/reading-list',
  599. 'updated' => time(),
  600. 'items' => $items,
  601. );
  602. if (count($entries) >= $count) {
  603. $entry = end($entries);
  604. if ($entry != false) {
  605. $response['continuation'] = '' . $entry->id();
  606. }
  607. }
  608. echo json_encode($response, JSON_OPTIONS), "\n";
  609. exit();
  610. }
  611. function streamContentsItemsIds($streamId, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation) {
  612. //http://code.google.com/p/google-reader-api/wiki/ApiStreamItemsIds
  613. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  614. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  615. $type = 'A';
  616. $id = '';
  617. if ($streamId === 'user/-/state/com.google/reading-list') {
  618. $type = 'A';
  619. } elseif ($streamId === 'user/-/state/com.google/starred') {
  620. $type = 's';
  621. } elseif (strpos($streamId, 'feed/') === 0) {
  622. $type = 'f';
  623. $streamId = substr($streamId, 5);
  624. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  625. $type = 'c';
  626. $streamId = substr($streamId, 13);
  627. }
  628. list($type, $id, $state, $searches) = streamContentsFilters($type, $streamId, $filter_target, $exclude_target, $start_time, $stop_time);
  629. if ($continuation != '') {
  630. $count++; //Shift by one element
  631. }
  632. $entryDAO = FreshRSS_Factory::createEntryDao();
  633. $ids = $entryDAO->listIdsWhere($type, $id, $state, $order === 'o' ? 'ASC' : 'DESC', $count, $continuation, $searches);
  634. if ($continuation != '') {
  635. array_shift($ids); //Discard first element that was already sent in the previous response
  636. $count--;
  637. }
  638. if (empty($ids) && isset($_GET['client']) && $_GET['client'] === 'newsplus') {
  639. $ids[] = 0; //For News+ bug https://github.com/noinnion/newsplus/issues/84#issuecomment-57834632
  640. }
  641. $itemRefs = array();
  642. foreach ($ids as $id) {
  643. $itemRefs[] = array(
  644. 'id' => '' . $id, //64-bit decimal
  645. );
  646. }
  647. $response = array(
  648. 'itemRefs' => $itemRefs,
  649. );
  650. if (count($ids) >= $count) {
  651. $id = end($ids);
  652. if ($id != false) {
  653. $response['continuation'] = '' . $id;
  654. }
  655. }
  656. echo json_encode($response, JSON_OPTIONS), "\n";
  657. exit();
  658. }
  659. function streamContentsItems($e_ids, $order) {
  660. header('Content-Type: application/json; charset=UTF-8');
  661. foreach ($e_ids as $i => $e_id) {
  662. // https://feedhq.readthedocs.io/en/latest/api/terminology.html#items
  663. if (!ctype_digit($e_id) || $e_id[0] === '0') {
  664. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  665. }
  666. }
  667. $entryDAO = FreshRSS_Factory::createEntryDao();
  668. $entries = $entryDAO->listByIds($e_ids, $order === 'o' ? 'ASC' : 'DESC');
  669. $entries = iterator_to_array($entries); //TODO: Improve
  670. $items = entriesToArray($entries);
  671. $response = array(
  672. 'id' => 'user/-/state/com.google/reading-list',
  673. 'updated' => time(),
  674. 'items' => $items,
  675. );
  676. echo json_encode($response, JSON_OPTIONS), "\n";
  677. exit();
  678. }
  679. function editTag($e_ids, $a, $r) {
  680. foreach ($e_ids as $i => $e_id) {
  681. if (!ctype_digit($e_id) || $e_id[0] === '0') {
  682. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  683. }
  684. }
  685. $entryDAO = FreshRSS_Factory::createEntryDao();
  686. $tagDAO = FreshRSS_Factory::createTagDao();
  687. switch ($a) {
  688. case 'user/-/state/com.google/read':
  689. $entryDAO->markRead($e_ids, true);
  690. break;
  691. case 'user/-/state/com.google/starred':
  692. $entryDAO->markFavorite($e_ids, true);
  693. break;
  694. /*case 'user/-/state/com.google/tracking-kept-unread':
  695. break;
  696. case 'user/-/state/com.google/like':
  697. break;
  698. case 'user/-/state/com.google/broadcast':
  699. break;*/
  700. default:
  701. $tagName = '';
  702. if (strpos($a, 'user/-/label/') === 0) {
  703. $tagName = substr($a, 13);
  704. } else {
  705. $user = Minz_Session::param('currentUser', '_');
  706. $prefix = 'user/' . $user . '/label/';
  707. if (strpos($a, $prefix) === 0) {
  708. $tagName = substr($a, strlen($prefix));
  709. }
  710. }
  711. if ($tagName != '') {
  712. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  713. $tag = $tagDAO->searchByName($tagName);
  714. if ($tag == null) {
  715. $tagDAO->addTag(array('name' => $tagName));
  716. $tag = $tagDAO->searchByName($tagName);
  717. }
  718. if ($tag != null) {
  719. foreach ($e_ids as $e_id) {
  720. $tagDAO->tagEntry($tag->id(), $e_id, true);
  721. }
  722. }
  723. }
  724. break;
  725. }
  726. switch ($r) {
  727. case 'user/-/state/com.google/read':
  728. $entryDAO->markRead($e_ids, false);
  729. break;
  730. case 'user/-/state/com.google/starred':
  731. $entryDAO->markFavorite($e_ids, false);
  732. break;
  733. default:
  734. if (strpos($r, 'user/-/label/') === 0) {
  735. $tagName = substr($r, 13);
  736. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  737. $tag = $tagDAO->searchByName($tagName);
  738. if ($tag != null) {
  739. foreach ($e_ids as $e_id) {
  740. $tagDAO->tagEntry($tag->id(), $e_id, false);
  741. }
  742. }
  743. }
  744. break;
  745. }
  746. exit('OK');
  747. }
  748. function renameTag($s, $dest) {
  749. if ($s != '' && strpos($s, 'user/-/label/') === 0 &&
  750. $dest != '' && strpos($dest, 'user/-/label/') === 0) {
  751. $s = substr($s, 13);
  752. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  753. $dest = substr($dest, 13);
  754. $dest = htmlspecialchars($dest, ENT_COMPAT, 'UTF-8');
  755. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  756. $cat = $categoryDAO->searchByName($s);
  757. if ($cat != null) {
  758. $categoryDAO->updateCategory($cat->id(), array('name' => $dest));
  759. exit('OK');
  760. } else {
  761. $tagDAO = FreshRSS_Factory::createTagDao();
  762. $tag = $tagDAO->searchByName($s);
  763. if ($tag != null) {
  764. $tagDAO->updateTag($tag->id(), array('name' => $dest));
  765. exit('OK');
  766. }
  767. }
  768. }
  769. badRequest();
  770. }
  771. function disableTag($s) {
  772. if ($s != '' && strpos($s, 'user/-/label/') === 0) {
  773. $s = substr($s, 13);
  774. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  775. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  776. $cat = $categoryDAO->searchByName($s);
  777. if ($cat != null) {
  778. $feedDAO = FreshRSS_Factory::createFeedDao();
  779. $feedDAO->changeCategory($cat->id(), 0);
  780. if ($cat->id() > 1) {
  781. $categoryDAO->deleteCategory($cat->id());
  782. }
  783. exit('OK');
  784. } else {
  785. $tagDAO = FreshRSS_Factory::createTagDao();
  786. $tag = $tagDAO->searchByName($s);
  787. if ($tag != null) {
  788. $tagDAO->deleteTag($tag->id());
  789. exit('OK');
  790. }
  791. }
  792. }
  793. badRequest();
  794. }
  795. function markAllAsRead($streamId, $olderThanId) {
  796. $entryDAO = FreshRSS_Factory::createEntryDao();
  797. if (strpos($streamId, 'feed/') === 0) {
  798. $f_id = basename($streamId);
  799. if (!ctype_digit($f_id)) {
  800. badRequest();
  801. }
  802. $f_id = intval($f_id);
  803. $entryDAO->markReadFeed($f_id, $olderThanId);
  804. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  805. $c_name = substr($streamId, 13);
  806. $c_name = htmlspecialchars($c_name, ENT_COMPAT, 'UTF-8');
  807. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  808. $cat = $categoryDAO->searchByName($c_name);
  809. if ($cat != null) {
  810. $entryDAO->markReadCat($cat->id(), $olderThanId);
  811. } else {
  812. $tagDAO = FreshRSS_Factory::createTagDao();
  813. $tag = $tagDAO->searchByName($c_name);
  814. if ($tag != null) {
  815. $entryDAO->markReadTag($tag->id(), $olderThanId);
  816. } else {
  817. badRequest();
  818. }
  819. }
  820. } elseif ($streamId === 'user/-/state/com.google/reading-list') {
  821. $entryDAO->markReadEntries($olderThanId, false, -1);
  822. } else {
  823. badRequest();
  824. }
  825. exit('OK');
  826. }
  827. $pathInfo = '';
  828. if (empty($_SERVER['PATH_INFO'])) {
  829. if (!empty($_SERVER['ORIG_PATH_INFO'])) {
  830. // Compatibility https://php.net/reserved.variables.server
  831. $pathInfo = $_SERVER['ORIG_PATH_INFO'];
  832. }
  833. } else {
  834. $pathInfo = $_SERVER['PATH_INFO'];
  835. }
  836. $pathInfo = urldecode($pathInfo);
  837. $pathInfo = preg_replace('%^(/api)?(/greader\.php)?%', '', $pathInfo); //Discard common errors
  838. if ($pathInfo == '') {
  839. exit('OK');
  840. }
  841. $pathInfos = explode('/', $pathInfo);
  842. if (count($pathInfos) < 3) {
  843. badRequest();
  844. }
  845. FreshRSS_Context::initSystem();
  846. //Minz_Log::debug('----------------------------------------------------------------', API_LOG);
  847. //Minz_Log::debug(debugInfo(), API_LOG);
  848. if (!FreshRSS_Context::$system_conf->api_enabled) {
  849. serviceUnavailable();
  850. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  851. checkCompatibility();
  852. }
  853. Minz_Session::init('FreshRSS', true);
  854. if ($pathInfos[1] !== 'accounts') {
  855. authorizationToUser();
  856. }
  857. if (FreshRSS_Context::$user_conf != null) {
  858. Minz_Translate::init(FreshRSS_Context::$user_conf->language);
  859. Minz_ExtensionManager::init();
  860. Minz_ExtensionManager::enableByList(FreshRSS_Context::$user_conf->extensions_enabled);
  861. } else {
  862. Minz_Translate::init();
  863. }
  864. if ($pathInfos[1] === 'accounts') {
  865. if (($pathInfos[2] === 'ClientLogin') && isset($_REQUEST['Email']) && isset($_REQUEST['Passwd'])) {
  866. clientLogin($_REQUEST['Email'], $_REQUEST['Passwd']);
  867. }
  868. } elseif ($pathInfos[1] === 'reader' && $pathInfos[2] === 'api' && isset($pathInfos[3]) && $pathInfos[3] === '0' && isset($pathInfos[4])) {
  869. if (Minz_Session::param('currentUser', '') == '') {
  870. unauthorized();
  871. }
  872. $timestamp = isset($_GET['ck']) ? intval($_GET['ck']) : 0; //ck=[unix timestamp] : Use the current Unix time here, helps Google with caching.
  873. switch ($pathInfos[4]) {
  874. case 'stream':
  875. /* xt=[exclude target] : Used to exclude certain items from the feed.
  876. * For example, using xt=user/-/state/com.google/read will exclude items
  877. * that the current user has marked as read, or xt=feed/[feedurl] will
  878. * exclude items from a particular feed (obviously not useful in this
  879. * request, but xt appears in other listing requests). */
  880. $exclude_target = isset($_GET['xt']) ? $_GET['xt'] : '';
  881. $filter_target = isset($_GET['it']) ? $_GET['it'] : '';
  882. //n=[integer] : The maximum number of results to return.
  883. $count = isset($_GET['n']) ? intval($_GET['n']) : 20;
  884. //r=[d|n|o] : Sort order of item results. d or n gives items in descending date order, o in ascending order.
  885. $order = isset($_GET['r']) ? $_GET['r'] : 'd';
  886. /* ot=[unix timestamp] : The time from which you want to retrieve
  887. * items. Only items that have been crawled by Google Reader after
  888. * this time will be returned. */
  889. $start_time = isset($_GET['ot']) ? intval($_GET['ot']) : 0;
  890. $stop_time = isset($_GET['nt']) ? intval($_GET['nt']) : 0;
  891. /* Continuation token. If a StreamContents response does not represent
  892. * all items in a timestamp range, it will have a continuation attribute.
  893. * The same request can be re-issued with the value of that attribute put
  894. * in this parameter to get more items */
  895. $continuation = isset($_GET['c']) ? trim($_GET['c']) : '';
  896. if (!ctype_digit($continuation)) {
  897. $continuation = '';
  898. }
  899. if (isset($pathInfos[5]) && $pathInfos[5] === 'contents') {
  900. if (!isset($pathInfos[6]) && isset($_GET['s'])) {
  901. // Compatibility BazQux API https://github.com/bazqux/bazqux-api#fetching-streams
  902. $streamIdInfos = explode('/', $_GET['s']);
  903. foreach ($streamIdInfos as $streamIdInfo) {
  904. $pathInfos[] = $streamIdInfo;
  905. }
  906. }
  907. if (isset($pathInfos[6]) && isset($pathInfos[7])) {
  908. if ($pathInfos[6] === 'feed') {
  909. $include_target = $pathInfos[7];
  910. if ($include_target != '' && !ctype_digit($include_target)) {
  911. $include_target = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
  912. if (preg_match('#/reader/api/0/stream/contents/feed/([A-Za-z0-9\'!*()%$_.~+-]+)#', $include_target, $matches) && isset($matches[1])) {
  913. $include_target = urldecode($matches[1]);
  914. } else {
  915. $include_target = '';
  916. }
  917. }
  918. streamContents($pathInfos[6], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  919. } elseif ($pathInfos[6] === 'user' && isset($pathInfos[8]) && isset($pathInfos[9])) {
  920. if ($pathInfos[8] === 'state') {
  921. if ($pathInfos[9] === 'com.google' && isset($pathInfos[10])) {
  922. if ($pathInfos[10] === 'reading-list' || $pathInfos[10] === 'starred') {
  923. $include_target = '';
  924. streamContents($pathInfos[10], $include_target, $start_time, $stop_time, $count, $order,
  925. $filter_target, $exclude_target, $continuation);
  926. }
  927. }
  928. } elseif ($pathInfos[8] === 'label') {
  929. $include_target = $pathInfos[9];
  930. streamContents($pathInfos[8], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  931. }
  932. }
  933. } else { //EasyRSS, FeedMe
  934. $include_target = '';
  935. streamContents('reading-list', $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  936. }
  937. } elseif ($pathInfos[5] === 'items') {
  938. if ($pathInfos[6] === 'ids' && isset($_GET['s'])) {
  939. /* StreamId for which to fetch the item IDs. The parameter may
  940. * be repeated to fetch the item IDs from multiple streams at once
  941. * (more efficient from a backend perspective than multiple requests). */
  942. $streamId = $_GET['s'];
  943. streamContentsItemsIds($streamId, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  944. } elseif ($pathInfos[6] === 'contents' && isset($_POST['i'])) { //FeedMe
  945. $e_ids = multiplePosts('i'); //item IDs
  946. streamContentsItems($e_ids, $order);
  947. }
  948. }
  949. break;
  950. case 'tag':
  951. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  952. $output = isset($_GET['output']) ? $_GET['output'] : '';
  953. if ($output !== 'json') notImplemented();
  954. tagList();
  955. }
  956. break;
  957. case 'subscription':
  958. if (isset($pathInfos[5])) {
  959. switch ($pathInfos[5]) {
  960. case 'export':
  961. subscriptionExport();
  962. break;
  963. case 'import':
  964. if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && $ORIGINAL_INPUT != '') {
  965. subscriptionImport($ORIGINAL_INPUT);
  966. }
  967. break;
  968. case 'list':
  969. $output = isset($_GET['output']) ? $_GET['output'] : '';
  970. if ($output !== 'json') notImplemented();
  971. subscriptionList();
  972. break;
  973. case 'edit':
  974. if (isset($_REQUEST['s']) && isset($_REQUEST['ac'])) {
  975. //StreamId to operate on. The parameter may be repeated to edit multiple subscriptions at once
  976. $streamNames = empty($_POST['s']) && isset($_GET['s']) ? array($_GET['s']) : multiplePosts('s');
  977. /* Title to use for the subscription. For the `subscribe` action,
  978. * if not specified then the feed’s current title will be used. Can
  979. * be used with the `edit` action to rename a subscription */
  980. $titles = empty($_POST['t']) && isset($_GET['t']) ? array($_GET['t']) : multiplePosts('t');
  981. $action = $_REQUEST['ac']; //Action to perform on the given StreamId. Possible values are `subscribe`, `unsubscribe` and `edit`
  982. $add = isset($_REQUEST['a']) ? $_REQUEST['a'] : ''; //StreamId to add the subscription to (generally a user label)
  983. $remove = isset($_REQUEST['r']) ? $_REQUEST['r'] : ''; //StreamId to remove the subscription from (generally a user label)
  984. subscriptionEdit($streamNames, $titles, $action, $add, $remove);
  985. }
  986. break;
  987. case 'quickadd': //https://github.com/theoldreader/api
  988. if (isset($_REQUEST['quickadd'])) {
  989. quickadd($_REQUEST['quickadd']);
  990. }
  991. break;
  992. }
  993. }
  994. break;
  995. case 'unread-count':
  996. $output = isset($_GET['output']) ? $_GET['output'] : '';
  997. if ($output !== 'json') notImplemented();
  998. unreadCount();
  999. break;
  1000. case 'edit-tag': //http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/
  1001. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1002. checkToken(FreshRSS_Context::$user_conf, $token);
  1003. $a = isset($_POST['a']) ? $_POST['a'] : ''; //Add: user/-/state/com.google/read user/-/state/com.google/starred
  1004. $r = isset($_POST['r']) ? $_POST['r'] : ''; //Remove: user/-/state/com.google/read user/-/state/com.google/starred
  1005. $e_ids = multiplePosts('i'); //item IDs
  1006. editTag($e_ids, $a, $r);
  1007. break;
  1008. case 'rename-tag': //https://github.com/theoldreader/api
  1009. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1010. checkToken(FreshRSS_Context::$user_conf, $token);
  1011. $s = isset($_POST['s']) ? $_POST['s'] : ''; //user/-/label/Folder
  1012. $dest = isset($_POST['dest']) ? $_POST['dest'] : ''; //user/-/label/NewFolder
  1013. renameTag($s, $dest);
  1014. break;
  1015. case 'disable-tag': //https://github.com/theoldreader/api
  1016. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1017. checkToken(FreshRSS_Context::$user_conf, $token);
  1018. $s_s = multiplePosts('s');
  1019. foreach ($s_s as $s) {
  1020. disableTag($s); //user/-/label/Folder
  1021. }
  1022. break;
  1023. case 'mark-all-as-read':
  1024. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1025. checkToken(FreshRSS_Context::$user_conf, $token);
  1026. $streamId = $_POST['s'] ?? '';
  1027. $ts = isset($_POST['ts']) ? $_POST['ts'] : '0'; //Older than timestamp in nanoseconds
  1028. if (!ctype_digit($ts)) {
  1029. badRequest();
  1030. }
  1031. markAllAsRead($streamId, $ts);
  1032. break;
  1033. case 'token':
  1034. token(FreshRSS_Context::$user_conf);
  1035. break;
  1036. case 'user-info':
  1037. userInfo();
  1038. break;
  1039. }
  1040. }
  1041. badRequest();