greader.php 37 KB

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