4
0

greader.php 37 KB

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