greader.php 37 KB

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