greader.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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. }
  612. if ($stop_time !== 0) {
  613. $search = new FreshRSS_Search('');
  614. $search->setMaxDate($stop_time);
  615. $searches->add($search);
  616. }
  617. return [$type, $streamId, $state, $searches];
  618. }
  619. /**
  620. * @param numeric-string $continuation
  621. */
  622. private static function streamContents(string $path, string $include_target, int $start_time, int $stop_time, int $count,
  623. string $order, string $filter_target, string $exclude_target, string $continuation): never {
  624. // https://code.google.com/archive/p/pyrfeed/wikis/GoogleReaderAPI.wiki
  625. // https://web.archive.org/web/20210126115837/https://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2#feed
  626. header('Content-Type: application/json; charset=UTF-8');
  627. $type = match ($path) {
  628. 'starred' => 's',
  629. 'feed' => 'f',
  630. 'label' => 'c',
  631. 'reading-list' => 'A', // All except PRIORITY_HIDDEN
  632. 'main' => 'a',
  633. 'important' => 'i',
  634. default => 'A',
  635. };
  636. [$type, $include_target, $state, $searches] =
  637. self::streamContentsFilters($type, $include_target, $filter_target, $exclude_target, $start_time, $stop_time);
  638. if ($continuation !== '0') {
  639. $count++; //Shift by one element
  640. }
  641. $entryDAO = FreshRSS_Factory::createEntryDao();
  642. $entries = $entryDAO->listWhere($type, $include_target, $state, $searches,
  643. order: $order === 'o' ? 'ASC' : 'DESC',
  644. continuation_id: $continuation,
  645. limit: $count);
  646. $items = self::entriesToArray($entries);
  647. if ($continuation !== '0') {
  648. //Discard first element that was already sent in the previous response
  649. $items = new LimitIterator($items, offset: 1);
  650. $count--;
  651. }
  652. $time = time();
  653. $nbItems = 0;
  654. $lastEntryId = 0;
  655. // Note: This section must be streamed to avoid memory issues with large responses
  656. echo <<<TXT
  657. {
  658. "id": "user/-/state/com.google/reading-list",
  659. "updated": $time,
  660. "items": [
  661. TXT;
  662. foreach ($items as $item) {
  663. if (!is_array($item) || empty($item)) {
  664. continue;
  665. }
  666. if ($nbItems > 0) {
  667. echo ",\n";
  668. }
  669. $lastEntryId = is_numeric($item['frss:id'] ?? null) ? (int)$item['frss:id'] : 0;
  670. unset($item['frss:id']);
  671. echo json_encode($item, JSON_OPTIONS);
  672. $nbItems++;
  673. }
  674. echo <<<'TXT'
  675. ]
  676. TXT;
  677. if ($nbItems >= $count && $lastEntryId > 0) {
  678. echo <<<TXT
  679. ,
  680. "continuation": "$lastEntryId"
  681. TXT;
  682. }
  683. echo <<<'TXT'
  684. }
  685. TXT;
  686. exit();
  687. }
  688. /**
  689. * @param numeric-string $continuation
  690. */
  691. private static function streamContentsItemsIds(string $streamId, int $start_time, int $stop_time, int $count,
  692. string $order, string $filter_target, string $exclude_target, string $continuation): never {
  693. // https://github.com/mihaip/google-reader-api/blob/master/wiki/ApiStreamItemsIds.wiki
  694. // https://code.google.com/archive/p/pyrfeed/wikis/GoogleReaderAPI.wiki
  695. // https://web.archive.org/web/20210126115837/https://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2#feed
  696. $type = 'A';
  697. if ($streamId === 'user/-/state/com.google/reading-list') {
  698. $type = 'A';
  699. $streamId = '';
  700. } elseif ($streamId === 'user/-/state/com.google/starred') {
  701. $type = 's';
  702. $streamId = '';
  703. } elseif ($streamId === 'user/-/state/org.freshrss/main') {
  704. $type = 'a';
  705. $streamId = '';
  706. } elseif ($streamId === 'user/-/state/org.freshrss/important') {
  707. $type = 'i';
  708. $streamId = '';
  709. } elseif ($streamId === 'user/-/state/com.google/read') {
  710. $filter_target = $streamId;
  711. $type = 'A';
  712. $streamId = '';
  713. } elseif ($streamId === 'user/-/state/com.google/unread') {
  714. $filter_target = $streamId;
  715. $type = 'A';
  716. $streamId = '';
  717. } elseif (str_starts_with($streamId, 'feed/')) {
  718. $type = 'f';
  719. $streamId = substr($streamId, 5);
  720. } elseif (str_starts_with($streamId, 'user/-/label/')) {
  721. $type = 'c';
  722. $streamId = substr($streamId, 13);
  723. }
  724. [$type, $id, $state, $searches] = self::streamContentsFilters($type, $streamId, $filter_target, $exclude_target, $start_time, $stop_time);
  725. if ($continuation !== '0') {
  726. $count++; //Shift by one element
  727. }
  728. $entryDAO = FreshRSS_Factory::createEntryDao();
  729. $ids = $entryDAO->listIdsWhere($type, $id, $state, $searches,
  730. order: $order === 'o' ? 'ASC' : 'DESC',
  731. continuation_id: $continuation,
  732. limit: $count);
  733. if ($ids === null) {
  734. self::internalServerError();
  735. }
  736. if ($continuation !== '0') {
  737. array_shift($ids); //Discard first element that was already sent in the previous response
  738. $count--;
  739. }
  740. if (empty($ids) && isset($_GET['client']) && $_GET['client'] === 'newsplus') {
  741. $ids = [ 0 ]; //For News+ bug https://github.com/noinnion/newsplus/issues/84#issuecomment-57834632
  742. }
  743. $itemRefs = [];
  744. foreach ($ids as $entryId) {
  745. $itemRefs[] = [
  746. 'id' => '' . $entryId, //64-bit decimal
  747. ];
  748. }
  749. $response = [
  750. 'itemRefs' => $itemRefs,
  751. ];
  752. if (count($ids) >= $count) {
  753. $entryId = end($ids);
  754. if ($entryId != false) {
  755. $response['continuation'] = '' . $entryId;
  756. }
  757. }
  758. echo json_encode($response, JSON_OPTIONS), "\n";
  759. exit();
  760. }
  761. /**
  762. * @param list<string> $e_ids
  763. */
  764. private static function streamContentsItems(array $e_ids, string $order): never {
  765. header('Content-Type: application/json; charset=UTF-8');
  766. foreach ($e_ids as $i => $e_id) {
  767. // https://feedhq.readthedocs.io/en/latest/api/terminology.html#items
  768. if (!ctype_digit($e_id) || $e_id[0] === '0') {
  769. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  770. }
  771. }
  772. /** @var list<numeric-string> $e_ids */
  773. $entryDAO = FreshRSS_Factory::createEntryDao();
  774. $entries = $entryDAO->listByIds($e_ids, order: $order === 'o' ? 'ASC' : 'DESC');
  775. $items = self::entriesToArray($entries, $e_ids);
  776. $time = time();
  777. $nbItems = 0;
  778. // Note: This section must be streamed to avoid memory issues with large responses
  779. echo <<<TXT
  780. {
  781. "id": "user/-/state/com.google/reading-list",
  782. "updated": $time,
  783. "items": [
  784. TXT;
  785. foreach ($items as $item) {
  786. if (!is_array($item) || empty($item)) {
  787. continue;
  788. }
  789. if ($nbItems > 0) {
  790. echo ",\n";
  791. }
  792. unset($item['frss:id']);
  793. echo json_encode($item, JSON_OPTIONS);
  794. $nbItems++;
  795. }
  796. echo <<<'TXT'
  797. ]
  798. }
  799. TXT;
  800. exit();
  801. }
  802. /**
  803. * @param list<string> $e_ids IDs of the items to edit
  804. * @param list<string> $as tags to add to all the listed items
  805. * @param list<string> $rs tags to remove from all the listed items
  806. */
  807. private static function editTag(array $e_ids, array $as, array $rs): never {
  808. foreach ($e_ids as $i => $e_id) {
  809. if (!ctype_digit($e_id) || $e_id[0] === '0') {
  810. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  811. }
  812. }
  813. /** @var list<numeric-string> $e_ids */
  814. $entryDAO = FreshRSS_Factory::createEntryDao();
  815. $tagDAO = FreshRSS_Factory::createTagDao();
  816. foreach ($as as $a) {
  817. switch ($a) {
  818. case 'user/-/state/com.google/read':
  819. $entryDAO->markRead($e_ids, true);
  820. break;
  821. case 'user/-/state/com.google/starred':
  822. $entryDAO->markFavorite($e_ids, true);
  823. break;
  824. case 'user/-/state/com.google/broadcast':
  825. case 'user/-/state/com.google/like':
  826. case 'user/-/state/com.google/tracking-kept-unread':
  827. // Not supported
  828. break;
  829. default:
  830. $tagName = '';
  831. if (str_starts_with($a, 'user/-/label/')) {
  832. $tagName = substr($a, 13);
  833. } else {
  834. $user = Minz_User::name() ?? '';
  835. $prefix = 'user/' . $user . '/label/';
  836. if (str_starts_with($a, $prefix)) {
  837. $tagName = substr($a, strlen($prefix));
  838. }
  839. }
  840. if ($tagName !== '') {
  841. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  842. $tag = $tagDAO->searchByName($tagName);
  843. if ($tag === null) {
  844. $tagDAO->addTag(['name' => $tagName]);
  845. $tag = $tagDAO->searchByName($tagName);
  846. }
  847. if ($tag !== null) {
  848. foreach ($e_ids as $e_id) {
  849. $tagDAO->tagEntry($tag->id(), $e_id, true);
  850. }
  851. }
  852. }
  853. break;
  854. }
  855. }
  856. foreach ($rs as $r) {
  857. switch ($r) {
  858. case 'user/-/state/com.google/read':
  859. $entryDAO->markRead($e_ids, false);
  860. break;
  861. case 'user/-/state/com.google/starred':
  862. $entryDAO->markFavorite($e_ids, false);
  863. break;
  864. case 'user/-/state/com.google/broadcast':
  865. case 'user/-/state/com.google/like':
  866. case 'user/-/state/com.google/tracking-kept-unread':
  867. // Not supported
  868. break;
  869. default:
  870. if (str_starts_with($r, 'user/-/label/')) {
  871. $tagName = substr($r, 13);
  872. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  873. $tag = $tagDAO->searchByName($tagName);
  874. if ($tag !== null) {
  875. foreach ($e_ids as $e_id) {
  876. $tagDAO->tagEntry($tag->id(), $e_id, false);
  877. }
  878. }
  879. }
  880. break;
  881. }
  882. }
  883. exit('OK');
  884. }
  885. private static function renameTag(string $s, string $dest): never {
  886. if (str_starts_with($s, 'user/-/label/') && str_starts_with($dest, 'user/-/label/')) {
  887. $s = substr($s, 13);
  888. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  889. $dest = substr($dest, 13);
  890. $dest = htmlspecialchars($dest, ENT_COMPAT, 'UTF-8');
  891. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  892. $cat = $categoryDAO->searchByName($s);
  893. if ($cat != null) {
  894. $categoryDAO->updateCategory($cat->id(), [
  895. 'name' => $dest, 'kind' => $cat->kind(), 'attributes' => $cat->attributes()
  896. ]);
  897. exit('OK');
  898. } else {
  899. $tagDAO = FreshRSS_Factory::createTagDao();
  900. $tag = $tagDAO->searchByName($s);
  901. if ($tag != null) {
  902. $tagDAO->updateTagName($tag->id(), $dest);
  903. exit('OK');
  904. }
  905. }
  906. }
  907. self::badRequest();
  908. }
  909. private static function disableTag(string $s): never {
  910. if (str_starts_with($s, 'user/-/label/')) {
  911. $s = substr($s, 13);
  912. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  913. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  914. $cat = $categoryDAO->searchByName($s);
  915. if ($cat != null) {
  916. $feedDAO = FreshRSS_Factory::createFeedDao();
  917. $feedDAO->changeCategory($cat->id(), 0);
  918. if ($cat->id() > FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  919. $categoryDAO->deleteCategory($cat->id());
  920. }
  921. exit('OK');
  922. } else {
  923. $tagDAO = FreshRSS_Factory::createTagDao();
  924. $tag = $tagDAO->searchByName($s);
  925. if ($tag != null) {
  926. $tagDAO->deleteTag($tag->id());
  927. exit('OK');
  928. }
  929. }
  930. }
  931. self::badRequest();
  932. }
  933. /**
  934. * @param numeric-string $olderThanId
  935. */
  936. private static function markAllAsRead(string $streamId, string $olderThanId): never {
  937. $entryDAO = FreshRSS_Factory::createEntryDao();
  938. if (str_starts_with($streamId, 'feed/')) {
  939. $f_id = basename($streamId);
  940. if (!is_numeric($f_id)) {
  941. self::badRequest();
  942. }
  943. $f_id = (int)$f_id;
  944. $entryDAO->markReadFeed($f_id, $olderThanId);
  945. } elseif (str_starts_with($streamId, 'user/-/label/')) {
  946. $c_name = substr($streamId, 13);
  947. $c_name = htmlspecialchars($c_name, ENT_COMPAT, 'UTF-8');
  948. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  949. $cat = $categoryDAO->searchByName($c_name);
  950. if ($cat != null) {
  951. $entryDAO->markReadCat($cat->id(), $olderThanId);
  952. } else {
  953. $tagDAO = FreshRSS_Factory::createTagDao();
  954. $tag = $tagDAO->searchByName($c_name);
  955. if ($tag != null) {
  956. $entryDAO->markReadTag($tag->id(), $olderThanId);
  957. } else {
  958. self::badRequest();
  959. }
  960. }
  961. } elseif ($streamId === 'user/-/state/com.google/reading-list') {
  962. $entryDAO->markReadEntries($olderThanId, priorityMin: FreshRSS_Feed::PRIORITY_HIDDEN + 1);
  963. } elseif ($streamId === 'user/-/state/com.google/starred') {
  964. $entryDAO->markReadEntries($olderThanId, onlyFavorites: true, priorityMin: FreshRSS_Feed::PRIORITY_HIDDEN + 1);
  965. } elseif ($streamId === 'user/-/state/org.freshrss/main') {
  966. $entryDAO->markReadEntries($olderThanId, priorityMin: FreshRSS_Feed::PRIORITY_MAIN_STREAM);
  967. } elseif ($streamId === 'user/-/state/org.freshrss/important') {
  968. $entryDAO->markReadEntries($olderThanId, priorityMin: FreshRSS_Feed::PRIORITY_IMPORTANT);
  969. } elseif ($streamId === 'user/-/state/com.google/read') {
  970. $entryDAO->markReadEntries($olderThanId, state: FreshRSS_Entry::STATE_READ);
  971. } elseif ($streamId === 'user/-/state/com.google/unread') {
  972. $entryDAO->markReadEntries($olderThanId, state: FreshRSS_Entry::STATE_NOT_READ, priorityMin: FreshRSS_Feed::PRIORITY_HIDDEN + 1);
  973. } else {
  974. self::badRequest();
  975. }
  976. exit('OK');
  977. }
  978. public static function parse(): never {
  979. header('Access-Control-Allow-Headers: Authorization');
  980. header('Access-Control-Allow-Methods: GET, POST');
  981. header('Access-Control-Allow-Origin: *');
  982. header('Access-Control-Max-Age: 600');
  983. if (($_SERVER['REQUEST_METHOD'] ?? '') === 'OPTIONS') {
  984. self::noContent();
  985. }
  986. $pathInfo = '';
  987. if (empty($_SERVER['PATH_INFO']) || !is_string($_SERVER['PATH_INFO'])) {
  988. if (!empty($_SERVER['ORIG_PATH_INFO']) && is_string($_SERVER['ORIG_PATH_INFO'])) {
  989. // Compatibility https://php.net/reserved.variables.server
  990. $pathInfo = $_SERVER['ORIG_PATH_INFO'];
  991. }
  992. } else {
  993. $pathInfo = $_SERVER['PATH_INFO'];
  994. }
  995. $pathInfo = rawurldecode($pathInfo);
  996. $pathInfo = '' . preg_replace('%^(/api)?(/greader\.php)?%', '', $pathInfo); //Discard common errors
  997. if ($pathInfo == '' && empty($_SERVER['QUERY_STRING'])) {
  998. exit('OK');
  999. }
  1000. $pathInfos = explode('/', $pathInfo);
  1001. if (count($pathInfos) < 3) {
  1002. self::badRequest();
  1003. }
  1004. FreshRSS_Context::initSystem();
  1005. //Minz_Log::debug('----------------------------------------------------------------', API_LOG);
  1006. //Minz_Log::debug(self::debugInfo(), API_LOG);
  1007. if (!FreshRSS_Context::hasSystemConf() || !FreshRSS_Context::systemConf()->api_enabled) {
  1008. self::serviceUnavailable();
  1009. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  1010. self::checkCompatibility();
  1011. }
  1012. Minz_Session::init('FreshRSS', true);
  1013. if ($pathInfos[1] !== 'accounts') {
  1014. self::authorizationToUser();
  1015. }
  1016. if (FreshRSS_Context::hasUserConf()) {
  1017. Minz_Translate::init(FreshRSS_Context::userConf()->language);
  1018. Minz_ExtensionManager::init();
  1019. Minz_ExtensionManager::enableByList(FreshRSS_Context::userConf()->extensions_enabled, 'user');
  1020. } else {
  1021. Minz_Translate::init();
  1022. }
  1023. self::$ORIGINAL_INPUT = file_get_contents('php://input', false, null, 0, 1048576) ?: '';
  1024. if ($pathInfos[1] === 'accounts') {
  1025. if (($pathInfos[2] === 'ClientLogin') && is_string($_REQUEST['Email'] ?? null) && is_string($_REQUEST['Passwd'] ?? null)) {
  1026. self::clientLogin($_REQUEST['Email'], $_REQUEST['Passwd']);
  1027. }
  1028. } elseif (isset($pathInfos[3], $pathInfos[4]) && $pathInfos[1] === 'reader' && $pathInfos[2] === 'api' && $pathInfos[3] === '0') {
  1029. if (Minz_User::name() === null) {
  1030. self::unauthorized();
  1031. }
  1032. // ck=[unix timestamp]: Use the current Unix time here, helps Google with caching
  1033. $timestamp = is_numeric($_GET['ck'] ?? null) ? (int)$_GET['ck'] : 0;
  1034. switch ($pathInfos[4]) {
  1035. case 'stream':
  1036. /**
  1037. * xt=[exclude target]: Used to exclude certain items from the feed.
  1038. * For example, using xt=user/-/state/com.google/read will exclude items
  1039. * that the current user has marked as read, or xt=feed/[feedurl] will
  1040. * exclude items from a particular feed (obviously not useful in this request,
  1041. * but xt appears in other listing requests).
  1042. */
  1043. $exclude_target = is_string($_GET['xt'] ?? null) ? $_GET['xt'] : '';
  1044. $filter_target = is_string($_GET['it'] ?? null) ? $_GET['it'] : '';
  1045. //n=[integer] : The maximum number of results to return.
  1046. $count = is_numeric($_GET['n'] ?? null) ? (int)$_GET['n'] : 20;
  1047. //r=[d|n|o] : Sort order of item results. d or n gives items in descending date order, o in ascending order.
  1048. $order = is_string($_GET['r'] ?? null) ? $_GET['r'] : 'd';
  1049. /**
  1050. * ot=[unix timestamp] : The time from which you want to retrieve items.
  1051. * Only items that have been crawled by Google Reader after this time will be returned.
  1052. */
  1053. $start_time = is_numeric($_GET['ot'] ?? null) ? (int)$_GET['ot'] : 0;
  1054. $stop_time = is_numeric($_GET['nt'] ?? null) ? (int)$_GET['nt'] : 0;
  1055. /**
  1056. * Continuation token. If a StreamContents response does not represent
  1057. * all items in a timestamp range, it will have a continuation attribute.
  1058. * The same request can be re-issued with the value of that attribute put
  1059. * in this parameter to get more items
  1060. */
  1061. $continuation = is_string($_GET['c'] ?? null) ? trim($_GET['c']) : '';
  1062. if (!ctype_digit($continuation)) {
  1063. $continuation = '0';
  1064. }
  1065. if (isset($pathInfos[5]) && $pathInfos[5] === 'contents') {
  1066. if (!isset($pathInfos[6]) && is_string($_GET['s'] ?? null)) {
  1067. // Compatibility BazQux API https://github.com/bazqux/bazqux-api#fetching-streams
  1068. $streamIdInfos = explode('/', $_GET['s']);
  1069. foreach ($streamIdInfos as $streamIdInfo) {
  1070. $pathInfos[] = $streamIdInfo;
  1071. }
  1072. }
  1073. if (isset($pathInfos[6], $pathInfos[7])) {
  1074. if ($pathInfos[6] === 'feed') {
  1075. $include_target = $pathInfos[7];
  1076. if ($include_target !== '' && !is_numeric($include_target)) {
  1077. $include_target = empty($_SERVER['REQUEST_URI']) || !is_string($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
  1078. if (preg_match('#/reader/api/0/stream/contents/feed/([A-Za-z0-9\'!*()%$_.~+-]+)#', $include_target, $matches) === 1) {
  1079. $include_target = urldecode($matches[1]);
  1080. } else {
  1081. $include_target = '';
  1082. }
  1083. }
  1084. self::streamContents($pathInfos[6], $include_target, $start_time, $stop_time,
  1085. $count, $order, $filter_target, $exclude_target, $continuation);
  1086. } elseif (isset($pathInfos[8], $pathInfos[9]) && $pathInfos[6] === 'user') {
  1087. if ($pathInfos[8] === 'state') {
  1088. if (in_array($pathInfos[9], ['com.google', 'org.freshrss'], true) && isset($pathInfos[10])) {
  1089. if (in_array($pathInfos[10], ['reading-list', 'starred', 'main', 'important'], true)) {
  1090. $include_target = '';
  1091. self::streamContents($pathInfos[10], $include_target, $start_time, $stop_time, $count, $order,
  1092. $filter_target, $exclude_target, $continuation);
  1093. }
  1094. }
  1095. } elseif ($pathInfos[8] === 'label') {
  1096. $include_target = empty($_SERVER['REQUEST_URI']) || !is_string($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
  1097. if (preg_match('#/reader/api/0/stream/contents/user/[^/+]/label/([A-Za-z0-9\'!*()%$_.~+-]+)#', $include_target, $matches)) {
  1098. $include_target = urldecode($matches[1]);
  1099. } else {
  1100. $include_target = $pathInfos[9];
  1101. }
  1102. self::streamContents($pathInfos[8], $include_target, $start_time, $stop_time,
  1103. $count, $order, $filter_target, $exclude_target, $continuation);
  1104. }
  1105. }
  1106. } else { //EasyRSS, FeedMe
  1107. $include_target = '';
  1108. self::streamContents('reading-list', $include_target, $start_time, $stop_time,
  1109. $count, $order, $filter_target, $exclude_target, $continuation);
  1110. }
  1111. } elseif ($pathInfos[5] === 'items') {
  1112. if ($pathInfos[6] === 'ids' && is_string($_GET['s'] ?? null)) {
  1113. // StreamId for which to fetch the item IDs.
  1114. // TODO: support multiple streams
  1115. $streamId = $_GET['s'];
  1116. self::streamContentsItemsIds($streamId, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  1117. } elseif ($pathInfos[6] === 'contents' && isset($_POST['i'])) { //FeedMe
  1118. $e_ids = self::multiplePosts('i'); //item IDs
  1119. self::streamContentsItems($e_ids, $order);
  1120. }
  1121. }
  1122. break;
  1123. case 'tag':
  1124. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  1125. $output = $_GET['output'] ?? '';
  1126. if ($output !== 'json') self::notImplemented();
  1127. self::tagList();
  1128. }
  1129. break;
  1130. case 'subscription':
  1131. if (isset($pathInfos[5])) {
  1132. switch ($pathInfos[5]) {
  1133. case 'export':
  1134. self::subscriptionExport();
  1135. // Always exits
  1136. case 'import':
  1137. if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && self::$ORIGINAL_INPUT != '') {
  1138. self::subscriptionImport(self::$ORIGINAL_INPUT);
  1139. }
  1140. break;
  1141. case 'list':
  1142. $output = $_GET['output'] ?? '';
  1143. if ($output !== 'json') self::notImplemented();
  1144. self::subscriptionList();
  1145. // Always exits
  1146. case 'edit':
  1147. if (isset($_REQUEST['s'], $_REQUEST['ac'])) {
  1148. // StreamId to operate on. The parameter may be repeated to edit multiple subscriptions at once
  1149. $streamNames = empty($_POST['s']) && is_string($_GET['s'] ?? null) ? [$_GET['s']] : self::multiplePosts('s');
  1150. /* Title to use for the subscription. For the `subscribe` action,
  1151. * if not specified then the feed’s current title will be used. Can
  1152. * be used with the `edit` action to rename a subscription */
  1153. $titles = empty($_POST['t']) && is_string($_GET['t'] ?? null) ? [$_GET['t']] : self::multiplePosts('t');
  1154. // Action to perform on the given StreamId. Possible values are `subscribe`, `unsubscribe` and `edit`
  1155. $action = is_string($_REQUEST['ac'] ?? null) ? $_REQUEST['ac'] : '';
  1156. // StreamId to add the subscription to (generally a user label)
  1157. // (in FreshRSS, we do not support repeated values since a feed can only be in one category)
  1158. $add = is_string($_REQUEST['a'] ?? null) ? $_REQUEST['a'] : '';
  1159. // StreamId to remove the subscription from (generally a user label) (in FreshRSS, we do not support repeated values)
  1160. $remove = is_string($_REQUEST['r'] ?? null) ? $_REQUEST['r'] : '';
  1161. self::subscriptionEdit($streamNames, $titles, $action, $add, $remove);
  1162. }
  1163. break;
  1164. case 'quickadd': //https://github.com/theoldreader/api
  1165. if (is_string($_REQUEST['quickadd'] ?? null)) {
  1166. self::quickadd($_REQUEST['quickadd']);
  1167. }
  1168. break;
  1169. }
  1170. }
  1171. break;
  1172. case 'unread-count':
  1173. $output = $_GET['output'] ?? '';
  1174. if ($output !== 'json') self::notImplemented();
  1175. self::unreadCount();
  1176. // Always exits
  1177. case 'edit-tag': // https://web.archive.org/web/20200616071132/https://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3
  1178. $token = is_string($_POST['T'] ?? null) ? trim($_POST['T']) : '';
  1179. self::checkToken(FreshRSS_Context::userConf(), $token);
  1180. // Add (Can be repeated to add multiple tags at once): user/-/state/com.google/read user/-/state/com.google/starred
  1181. $as = self::multiplePosts('a');
  1182. // Remove (Can be repeated to remove multiple tags at once): user/-/state/com.google/read user/-/state/com.google/starred
  1183. $rs = self::multiplePosts('r');
  1184. $e_ids = self::multiplePosts('i'); //item IDs
  1185. self::editTag($e_ids, $as, $rs);
  1186. // Always exits
  1187. case 'rename-tag': //https://github.com/theoldreader/api
  1188. $token = is_string($_POST['T'] ?? null) ? trim($_POST['T']) : '';
  1189. self::checkToken(FreshRSS_Context::userConf(), $token);
  1190. $s = is_string($_POST['s'] ?? null) ? trim($_POST['s']) : ''; //user/-/label/Folder
  1191. $dest = is_string($_POST['dest'] ?? null) ? trim($_POST['dest']) : ''; //user/-/label/NewFolder
  1192. self::renameTag($s, $dest);
  1193. // Always exits
  1194. case 'disable-tag': //https://github.com/theoldreader/api
  1195. $token = is_string($_POST['T'] ?? null) ? trim($_POST['T']) : '';
  1196. self::checkToken(FreshRSS_Context::userConf(), $token);
  1197. $s_s = self::multiplePosts('s');
  1198. foreach ($s_s as $s) {
  1199. self::disableTag($s); //user/-/label/Folder
  1200. }
  1201. // Always exits
  1202. case 'mark-all-as-read':
  1203. $token = is_string($_POST['T'] ?? null) ? trim($_POST['T']) : '';
  1204. self::checkToken(FreshRSS_Context::userConf(), $token);
  1205. $streamId = is_string($_POST['s'] ?? null) ? trim($_POST['s']) : '';
  1206. $ts = is_string($_POST['ts'] ?? null) ? trim($_POST['ts']) : '0'; //Older than timestamp in nanoseconds
  1207. if (!ctype_digit($ts)) {
  1208. self::badRequest();
  1209. }
  1210. self::markAllAsRead($streamId, $ts);
  1211. // Always exits
  1212. case 'token':
  1213. self::token(FreshRSS_Context::userConf());
  1214. // Always exits
  1215. case 'user-info':
  1216. self::userInfo();
  1217. // Always exits
  1218. }
  1219. }
  1220. self::badRequest();
  1221. }
  1222. }
  1223. GReaderAPI::parse();