greader.php 47 KB

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