greader.php 36 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. if (strpos($e_id, '/') !== false) {
  671. $e_id = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  672. }
  673. $e_ids[$i] = $e_id;
  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 (strpos($e_id, '/') !== false) {
  690. $e_id = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  691. }
  692. $e_ids[$i] = $e_id;
  693. }
  694. $entryDAO = FreshRSS_Factory::createEntryDao();
  695. $tagDAO = FreshRSS_Factory::createTagDao();
  696. switch ($a) {
  697. case 'user/-/state/com.google/read':
  698. $entryDAO->markRead($e_ids, true);
  699. break;
  700. case 'user/-/state/com.google/starred':
  701. $entryDAO->markFavorite($e_ids, true);
  702. break;
  703. /*case 'user/-/state/com.google/tracking-kept-unread':
  704. break;
  705. case 'user/-/state/com.google/like':
  706. break;
  707. case 'user/-/state/com.google/broadcast':
  708. break;*/
  709. default:
  710. $tagName = '';
  711. if (strpos($a, 'user/-/label/') === 0) {
  712. $tagName = substr($a, 13);
  713. } else {
  714. $user = Minz_Session::param('currentUser', '_');
  715. $prefix = 'user/' . $user . '/label/';
  716. if (strpos($a, $prefix) === 0) {
  717. $tagName = substr($a, strlen($prefix));
  718. }
  719. }
  720. if ($tagName != '') {
  721. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  722. $tag = $tagDAO->searchByName($tagName);
  723. if ($tag == null) {
  724. $tagDAO->addTag(array('name' => $tagName));
  725. $tag = $tagDAO->searchByName($tagName);
  726. }
  727. if ($tag != null) {
  728. foreach ($e_ids as $e_id) {
  729. $tagDAO->tagEntry($tag->id(), $e_id, true);
  730. }
  731. }
  732. }
  733. break;
  734. }
  735. switch ($r) {
  736. case 'user/-/state/com.google/read':
  737. $entryDAO->markRead($e_ids, false);
  738. break;
  739. case 'user/-/state/com.google/starred':
  740. $entryDAO->markFavorite($e_ids, false);
  741. break;
  742. default:
  743. if (strpos($r, 'user/-/label/') === 0) {
  744. $tagName = substr($r, 13);
  745. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  746. $tag = $tagDAO->searchByName($tagName);
  747. if ($tag != null) {
  748. foreach ($e_ids as $e_id) {
  749. $tagDAO->tagEntry($tag->id(), $e_id, false);
  750. }
  751. }
  752. }
  753. break;
  754. }
  755. exit('OK');
  756. }
  757. function renameTag($s, $dest) {
  758. if ($s != '' && strpos($s, 'user/-/label/') === 0 &&
  759. $dest != '' && strpos($dest, 'user/-/label/') === 0) {
  760. $s = substr($s, 13);
  761. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  762. $dest = substr($dest, 13);
  763. $dest = htmlspecialchars($dest, ENT_COMPAT, 'UTF-8');
  764. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  765. $cat = $categoryDAO->searchByName($s);
  766. if ($cat != null) {
  767. $categoryDAO->updateCategory($cat->id(), array('name' => $dest));
  768. exit('OK');
  769. } else {
  770. $tagDAO = FreshRSS_Factory::createTagDao();
  771. $tag = $tagDAO->searchByName($s);
  772. if ($tag != null) {
  773. $tagDAO->updateTag($tag->id(), array('name' => $dest));
  774. exit('OK');
  775. }
  776. }
  777. }
  778. badRequest();
  779. }
  780. function disableTag($s) {
  781. if ($s != '' && strpos($s, 'user/-/label/') === 0) {
  782. $s = substr($s, 13);
  783. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  784. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  785. $cat = $categoryDAO->searchByName($s);
  786. if ($cat != null) {
  787. $feedDAO = FreshRSS_Factory::createFeedDao();
  788. $feedDAO->changeCategory($cat->id(), 0);
  789. if ($cat->id() > 1) {
  790. $categoryDAO->deleteCategory($cat->id());
  791. }
  792. exit('OK');
  793. } else {
  794. $tagDAO = FreshRSS_Factory::createTagDao();
  795. $tag = $tagDAO->searchByName($s);
  796. if ($tag != null) {
  797. $tagDAO->deleteTag($tag->id());
  798. exit('OK');
  799. }
  800. }
  801. }
  802. badRequest();
  803. }
  804. function markAllAsRead($streamId, $olderThanId) {
  805. $entryDAO = FreshRSS_Factory::createEntryDao();
  806. if (strpos($streamId, 'feed/') === 0) {
  807. $f_id = basename($streamId);
  808. $entryDAO->markReadFeed($f_id, $olderThanId);
  809. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  810. $c_name = substr($streamId, 13);
  811. $c_name = htmlspecialchars($c_name, ENT_COMPAT, 'UTF-8');
  812. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  813. $cat = $categoryDAO->searchByName($c_name);
  814. if ($cat != null) {
  815. $entryDAO->markReadCat($cat->id(), $olderThanId);
  816. } else {
  817. $tagDAO = FreshRSS_Factory::createTagDao();
  818. $tag = $tagDAO->searchByName($c_name);
  819. if ($tag != null) {
  820. $entryDAO->markReadTag($tag->id(), $olderThanId);
  821. }
  822. }
  823. } elseif ($streamId === 'user/-/state/com.google/reading-list') {
  824. $entryDAO->markReadEntries($olderThanId, false, -1);
  825. }
  826. exit('OK');
  827. }
  828. $pathInfo = empty($_SERVER['PATH_INFO']) ? '' : urldecode($_SERVER['PATH_INFO']);
  829. if ($pathInfo == '') {
  830. exit('OK');
  831. }
  832. $pathInfos = explode('/', $pathInfo);
  833. if (count($pathInfos) < 3) {
  834. badRequest();
  835. }
  836. Minz_Configuration::register('system',
  837. DATA_PATH . '/config.php',
  838. FRESHRSS_PATH . '/config.default.php');
  839. FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
  840. //Minz_Log::debug('----------------------------------------------------------------', API_LOG);
  841. //Minz_Log::debug(debugInfo(), API_LOG);
  842. if (!FreshRSS_Context::$system_conf->api_enabled) {
  843. serviceUnavailable();
  844. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  845. checkCompatibility();
  846. }
  847. ini_set('session.use_cookies', '0');
  848. register_shutdown_function('session_destroy');
  849. Minz_Session::init('FreshRSS');
  850. $user = authorizationToUser();
  851. FreshRSS_Context::$user_conf = null;
  852. if ($user !== '') {
  853. FreshRSS_Context::$user_conf = get_user_configuration($user);
  854. Minz_ExtensionManager::init();
  855. Minz_Translate::init(FreshRSS_Context::$user_conf->language);
  856. if (FreshRSS_Context::$user_conf != null) {
  857. Minz_ExtensionManager::enableByList(FreshRSS_Context::$user_conf->extensions_enabled);
  858. }
  859. } else {
  860. Minz_Translate::init();
  861. }
  862. Minz_Session::_param('currentUser', $user);
  863. if ($pathInfos[1] === 'accounts') {
  864. if (($pathInfos[2] === 'ClientLogin') && isset($_REQUEST['Email']) && isset($_REQUEST['Passwd'])) {
  865. clientLogin($_REQUEST['Email'], $_REQUEST['Passwd']);
  866. }
  867. } elseif ($pathInfos[1] === 'reader' && $pathInfos[2] === 'api' && isset($pathInfos[3]) && $pathInfos[3] === '0' && isset($pathInfos[4])) {
  868. if ($user == '') {
  869. unauthorized();
  870. }
  871. $timestamp = isset($_GET['ck']) ? intval($_GET['ck']) : 0; //ck=[unix timestamp] : Use the current Unix time here, helps Google with caching.
  872. switch ($pathInfos[4]) {
  873. case 'stream':
  874. /* xt=[exclude target] : Used to exclude certain items from the feed.
  875. * For example, using xt=user/-/state/com.google/read will exclude items
  876. * that the current user has marked as read, or xt=feed/[feedurl] will
  877. * exclude items from a particular feed (obviously not useful in this
  878. * request, but xt appears in other listing requests). */
  879. $exclude_target = isset($_GET['xt']) ? $_GET['xt'] : '';
  880. $filter_target = isset($_GET['it']) ? $_GET['it'] : '';
  881. $count = isset($_GET['n']) ? intval($_GET['n']) : 20; //n=[integer] : The maximum number of results to return.
  882. $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.
  883. /* ot=[unix timestamp] : The time from which you want to retrieve
  884. * items. Only items that have been crawled by Google Reader after
  885. * this time will be returned. */
  886. $start_time = isset($_GET['ot']) ? intval($_GET['ot']) : 0;
  887. $stop_time = isset($_GET['nt']) ? intval($_GET['nt']) : 0;
  888. /* Continuation token. If a StreamContents response does not represent
  889. * all items in a timestamp range, it will have a continuation attribute.
  890. * The same request can be re-issued with the value of that attribute put
  891. * in this parameter to get more items */
  892. $continuation = isset($_GET['c']) ? trim($_GET['c']) : '';
  893. if (!ctype_digit($continuation)) {
  894. $continuation = '';
  895. }
  896. if (isset($pathInfos[5]) && $pathInfos[5] === 'contents' && isset($pathInfos[6])) {
  897. if (isset($pathInfos[7])) {
  898. if ($pathInfos[6] === 'feed') {
  899. $include_target = $pathInfos[7];
  900. if ($include_target != '' && !ctype_digit($include_target)) {
  901. $include_target = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
  902. if (preg_match('#/reader/api/0/stream/contents/feed/([A-Za-z0-9\'!*()%$_.~+-]+)#', $include_target, $matches) && isset($matches[1])) {
  903. $include_target = urldecode($matches[1]);
  904. } else {
  905. $include_target = '';
  906. }
  907. }
  908. streamContents($pathInfos[6], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  909. } elseif ($pathInfos[6] === 'user' && isset($pathInfos[8]) && isset($pathInfos[9])) {
  910. if ($pathInfos[8] === 'state') {
  911. if ($pathInfos[9] === 'com.google' && isset($pathInfos[10])) {
  912. if ($pathInfos[10] === 'reading-list' || $pathInfos[10] === 'starred') {
  913. $include_target = '';
  914. streamContents($pathInfos[10], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  915. }
  916. }
  917. } elseif ($pathInfos[8] === 'label') {
  918. $include_target = $pathInfos[9];
  919. streamContents($pathInfos[8], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  920. }
  921. }
  922. } else { //EasyRSS
  923. $include_target = '';
  924. streamContents('reading-list', $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  925. }
  926. } elseif ($pathInfos[5] === 'items') {
  927. if ($pathInfos[6] === 'ids' && isset($_GET['s'])) {
  928. /* StreamId for which to fetch the item IDs. The parameter may
  929. * be repeated to fetch the item IDs from multiple streams at once
  930. * (more efficient from a backend perspective than multiple requests). */
  931. $streamId = $_GET['s'];
  932. streamContentsItemsIds($streamId, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  933. } elseif ($pathInfos[6] === 'contents' && isset($_POST['i'])) { //FeedMe
  934. $e_ids = multiplePosts('i'); //item IDs
  935. streamContentsItems($e_ids, $order);
  936. }
  937. }
  938. break;
  939. case 'tag':
  940. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  941. $output = isset($_GET['output']) ? $_GET['output'] : '';
  942. if ($output !== 'json') notImplemented();
  943. tagList();
  944. }
  945. break;
  946. case 'subscription':
  947. if (isset($pathInfos[5])) {
  948. switch ($pathInfos[5]) {
  949. case 'list':
  950. $output = isset($_GET['output']) ? $_GET['output'] : '';
  951. if ($output !== 'json') notImplemented();
  952. subscriptionList();
  953. break;
  954. case 'edit':
  955. if (isset($_REQUEST['s']) && isset($_REQUEST['ac'])) {
  956. //StreamId to operate on. The parameter may be repeated to edit multiple subscriptions at once
  957. $streamNames = empty($_POST['s']) && isset($_GET['s']) ? array($_GET['s']) : multiplePosts('s');
  958. /* Title to use for the subscription. For the `subscribe` action,
  959. * if not specified then the feed's current title will be used. Can
  960. * be used with the `edit` action to rename a subscription */
  961. $titles = empty($_POST['t']) && isset($_GET['t']) ? array($_GET['t']) : multiplePosts('t');
  962. $action = $_REQUEST['ac']; //Action to perform on the given StreamId. Possible values are `subscribe`, `unsubscribe` and `edit`
  963. $add = isset($_REQUEST['a']) ? $_REQUEST['a'] : ''; //StreamId to add the subscription to (generally a user label)
  964. $remove = isset($_REQUEST['r']) ? $_REQUEST['r'] : ''; //StreamId to remove the subscription from (generally a user label)
  965. subscriptionEdit($streamNames, $titles, $action, $add, $remove);
  966. }
  967. break;
  968. case 'quickadd': //https://github.com/theoldreader/api
  969. if (isset($_GET['quickadd'])) {
  970. quickadd($_GET['quickadd']);
  971. }
  972. break;
  973. }
  974. }
  975. break;
  976. case 'unread-count':
  977. $output = isset($_GET['output']) ? $_GET['output'] : '';
  978. if ($output !== 'json') notImplemented();
  979. $all = isset($_GET['all']) ? $_GET['all'] : '';
  980. unreadCount($all);
  981. break;
  982. case 'edit-tag': //http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/
  983. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  984. checkToken(FreshRSS_Context::$user_conf, $token);
  985. $a = isset($_POST['a']) ? $_POST['a'] : ''; //Add: user/-/state/com.google/read user/-/state/com.google/starred
  986. $r = isset($_POST['r']) ? $_POST['r'] : ''; //Remove: user/-/state/com.google/read user/-/state/com.google/starred
  987. $e_ids = multiplePosts('i'); //item IDs
  988. editTag($e_ids, $a, $r);
  989. break;
  990. case 'rename-tag': //https://github.com/theoldreader/api
  991. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  992. checkToken(FreshRSS_Context::$user_conf, $token);
  993. $s = isset($_POST['s']) ? $_POST['s'] : ''; //user/-/label/Folder
  994. $dest = isset($_POST['dest']) ? $_POST['dest'] : ''; //user/-/label/NewFolder
  995. renameTag($s, $dest);
  996. break;
  997. case 'disable-tag': //https://github.com/theoldreader/api
  998. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  999. checkToken(FreshRSS_Context::$user_conf, $token);
  1000. $s_s = multiplePosts('s');
  1001. foreach ($s_s as $s) {
  1002. disableTag($s); //user/-/label/Folder
  1003. }
  1004. break;
  1005. case 'mark-all-as-read':
  1006. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1007. checkToken(FreshRSS_Context::$user_conf, $token);
  1008. $streamId = $_POST['s']; //StreamId
  1009. $ts = isset($_POST['ts']) ? $_POST['ts'] : '0'; //Older than timestamp in nanoseconds
  1010. if (!ctype_digit($ts)) {
  1011. $ts = '0';
  1012. }
  1013. markAllAsRead($streamId, $ts);
  1014. break;
  1015. case 'token':
  1016. token(FreshRSS_Context::$user_conf);
  1017. break;
  1018. case 'user-info':
  1019. userInfo();
  1020. break;
  1021. }
  1022. }
  1023. badRequest();