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. */
  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 (!FreshRSS_Context::$user_conf->enabled) {
  146. Minz_Log::warning('Invalid API user ' . $user . ': configuration cannot be found.');
  147. unauthorized();
  148. }
  149. if ($headerAuthX[1] === sha1(FreshRSS_Context::$system_conf->salt . $user . FreshRSS_Context::$user_conf->apiPasswordHash)) {
  150. return $user;
  151. } else {
  152. Minz_Log::warning('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1], API_LOG);
  153. Minz_Log::warning('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1]);
  154. unauthorized();
  155. }
  156. } else {
  157. badRequest();
  158. }
  159. }
  160. }
  161. return '';
  162. }
  163. function clientLogin($email, $pass) { //http://web.archive.org/web/20130604091042/http://undoc.in/clientLogin.html
  164. if (FreshRSS_user_Controller::checkUsername($email)) {
  165. FreshRSS_Context::$user_conf = get_user_configuration($email);
  166. if (FreshRSS_Context::$user_conf == null) {
  167. Minz_Log::warning('Invalid API user ' . $email . ': configuration cannot be found.');
  168. unauthorized();
  169. }
  170. if (FreshRSS_Context::$user_conf->apiPasswordHash != '' && password_verify($pass, FreshRSS_Context::$user_conf->apiPasswordHash)) {
  171. header('Content-Type: text/plain; charset=UTF-8');
  172. $auth = $email . '/' . sha1(FreshRSS_Context::$system_conf->salt . $email . FreshRSS_Context::$user_conf->apiPasswordHash);
  173. echo 'SID=', $auth, "\n",
  174. 'LSID=null', "\n", //Vienna RSS
  175. 'Auth=', $auth, "\n";
  176. exit();
  177. } else {
  178. Minz_Log::warning('Password API mismatch for user ' . $email);
  179. unauthorized();
  180. }
  181. } else {
  182. badRequest();
  183. }
  184. die();
  185. }
  186. function token($conf) {
  187. //http://blog.martindoms.com/2009/08/15/using-the-google-reader-api-part-1/
  188. //https://github.com/ericmann/gReader-Library/blob/master/greader.class.php
  189. $user = Minz_Session::param('currentUser', '_');
  190. //Minz_Log::debug('token('. $user . ')', API_LOG); //TODO: Implement real token that expires
  191. $token = str_pad(sha1(FreshRSS_Context::$system_conf->salt . $user . $conf->apiPasswordHash), 57, 'Z'); //Must have 57 characters
  192. echo $token, "\n";
  193. exit();
  194. }
  195. function checkToken($conf, $token) {
  196. //http://code.google.com/p/google-reader-api/wiki/ActionToken
  197. $user = Minz_Session::param('currentUser', '_');
  198. if ($user !== '_' && ( //TODO: Check security consequences
  199. $token == '' || //FeedMe
  200. $token === 'x')) { //Reeder
  201. return true;
  202. }
  203. if ($token === str_pad(sha1(FreshRSS_Context::$system_conf->salt . $user . $conf->apiPasswordHash), 57, 'Z')) {
  204. return true;
  205. }
  206. Minz_Log::warning('Invalid POST token: ' . $token, API_LOG);
  207. unauthorized();
  208. }
  209. function userInfo() { //https://github.com/theoldreader/api#user-info
  210. $user = Minz_Session::param('currentUser', '_');
  211. exit(json_encode(array(
  212. 'userId' => $user,
  213. 'userName' => $user,
  214. 'userProfileId' => $user,
  215. 'userEmail' => FreshRSS_Context::$user_conf->mail_login,
  216. ), JSON_OPTIONS));
  217. }
  218. function tagList() {
  219. header('Content-Type: application/json; charset=UTF-8');
  220. $tags = array(
  221. array('id' => 'user/-/state/com.google/starred'),
  222. //array('id' => 'user/-/state/com.google/broadcast', 'sortid' => '2'),
  223. );
  224. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  225. $categories = $categoryDAO->listCategories(true, false);
  226. foreach ($categories as $cat) {
  227. $tags[] = array(
  228. 'id' => 'user/-/label/' . htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  229. //'sortid' => $cat->name(),
  230. 'type' => 'folder', //Inoreader
  231. );
  232. }
  233. $tagDAO = FreshRSS_Factory::createTagDao();
  234. $labels = $tagDAO->listTags(true);
  235. foreach ($labels as $label) {
  236. $tags[] = array(
  237. 'id' => 'user/-/label/' . htmlspecialchars_decode($label->name(), ENT_QUOTES),
  238. //'sortid' => $label->name(),
  239. 'type' => 'tag', //Inoreader
  240. 'unread_count' => $label->nbUnread(), //Inoreader
  241. );
  242. }
  243. echo json_encode(array('tags' => $tags), JSON_OPTIONS), "\n";
  244. exit();
  245. }
  246. function subscriptionList() {
  247. header('Content-Type: application/json; charset=UTF-8');
  248. $salt = FreshRSS_Context::$system_conf->salt;
  249. $faviconsUrl = Minz_Url::display('/f.php?', '', true);
  250. $faviconsUrl = str_replace('/api/greader.php/reader/api/0/subscription', '', $faviconsUrl); //Security if base_url is not set properly
  251. $subscriptions = array();
  252. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  253. foreach ($categoryDAO->listCategories(true, true) as $cat) {
  254. foreach ($cat->feeds() as $feed) {
  255. $subscriptions[] = [
  256. 'id' => 'feed/' . $feed->id(),
  257. 'title' => escapeToUnicodeAlternative($feed->name(), true),
  258. 'categories' => [
  259. [
  260. 'id' => 'user/-/label/' . htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  261. 'label' => htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  262. ],
  263. ],
  264. //'sortid' => $feed->name(),
  265. //'firstitemmsec' => 0,
  266. 'url' => htmlspecialchars_decode($feed->url(), ENT_QUOTES),
  267. 'htmlUrl' => htmlspecialchars_decode($feed->website(), ENT_QUOTES),
  268. 'iconUrl' => $faviconsUrl . hash('crc32b', $salt . $feed->url()),
  269. ];
  270. }
  271. }
  272. echo json_encode(array('subscriptions' => $subscriptions), JSON_OPTIONS), "\n";
  273. exit();
  274. }
  275. function subscriptionEdit($streamNames, $titles, $action, $add = '', $remove = '') {
  276. //https://github.com/mihaip/google-reader-api/blob/master/wiki/ApiSubscriptionEdit.wiki
  277. switch ($action) {
  278. case 'subscribe':
  279. case 'unsubscribe':
  280. case 'edit':
  281. break;
  282. default:
  283. badRequest();
  284. }
  285. $addCatId = 0;
  286. $categoryDAO = null;
  287. if ($add != '' || $remove != '') {
  288. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  289. }
  290. $c_name = '';
  291. if ($add != '' && strpos($add, 'user/') === 0) { //user/-/label/Example ; user/username/label/Example
  292. if (strpos($add, 'user/-/label/') === 0) {
  293. $c_name = substr($add, 13);
  294. } else {
  295. $user = Minz_Session::param('currentUser', '_');
  296. $prefix = 'user/' . $user . '/label/';
  297. if (strpos($add, $prefix) === 0) {
  298. $c_name = substr($add, strlen($prefix));
  299. } else {
  300. $c_name = '';
  301. }
  302. }
  303. $c_name = htmlspecialchars($c_name, ENT_COMPAT, 'UTF-8');
  304. $cat = $categoryDAO->searchByName($c_name);
  305. $addCatId = $cat == null ? 0 : $cat->id();
  306. } elseif ($remove != '' && strpos($remove, 'user/-/label/') === 0) {
  307. $addCatId = 1; //Default category
  308. }
  309. $feedDAO = FreshRSS_Factory::createFeedDao();
  310. if (!is_array($streamNames) || count($streamNames) < 1) {
  311. badRequest();
  312. }
  313. for ($i = count($streamNames) - 1; $i >= 0; $i--) {
  314. $streamUrl = $streamNames[$i]; //feed/http://example.net/sample.xml ; feed/338
  315. if (strpos($streamUrl, 'feed/') === 0) {
  316. $streamUrl = substr($streamUrl, 5);
  317. $feedId = 0;
  318. if (ctype_digit($streamUrl)) {
  319. if ($action === 'subscribe') {
  320. continue;
  321. }
  322. $feedId = $streamUrl;
  323. } else {
  324. $streamUrl = htmlspecialchars($streamUrl, ENT_COMPAT, 'UTF-8');
  325. $feed = $feedDAO->searchByUrl($streamUrl);
  326. $feedId = $feed == null ? -1 : $feed->id();
  327. }
  328. $title = isset($titles[$i]) ? $titles[$i] : '';
  329. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
  330. switch ($action) {
  331. case 'subscribe':
  332. if ($feedId <= 0) {
  333. $http_auth = '';
  334. try {
  335. $feed = FreshRSS_feed_Controller::addFeed($streamUrl, $title, $addCatId, $c_name, $http_auth);
  336. continue 2;
  337. } catch (Exception $e) {
  338. Minz_Log::error('subscriptionEdit error subscribe: ' . $e->getMessage(), API_LOG);
  339. }
  340. }
  341. badRequest();
  342. break;
  343. case 'unsubscribe':
  344. if (!($feedId > 0 && FreshRSS_feed_Controller::deleteFeed($feedId))) {
  345. badRequest();
  346. }
  347. break;
  348. case 'edit':
  349. if ($feedId > 0) {
  350. if ($addCatId > 0 || $c_name != '') {
  351. FreshRSS_feed_Controller::moveFeed($feedId, $addCatId, $c_name);
  352. }
  353. if ($title != '') {
  354. FreshRSS_feed_Controller::renameFeed($feedId, $title);
  355. }
  356. } else {
  357. badRequest();
  358. }
  359. break;
  360. }
  361. }
  362. }
  363. exit('OK');
  364. }
  365. function quickadd($url) {
  366. try {
  367. $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
  368. if (substr($url, 0, 5) === 'feed/') {
  369. $url = substr($url, 5);
  370. }
  371. $feed = FreshRSS_feed_Controller::addFeed($url);
  372. exit(json_encode(array(
  373. 'numResults' => 1,
  374. 'query' => $feed->url(),
  375. 'streamId' => 'feed/' . $feed->id(),
  376. 'streamName' => $feed->name(),
  377. ), JSON_OPTIONS));
  378. } catch (Exception $e) {
  379. Minz_Log::error('quickadd error: ' . $e->getMessage(), API_LOG);
  380. die(json_encode(array(
  381. 'numResults' => 0,
  382. 'error' => $e->getMessage(),
  383. ), JSON_OPTIONS));
  384. }
  385. }
  386. function unreadCount() { //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#unread-count
  387. header('Content-Type: application/json; charset=UTF-8');
  388. $totalUnreads = 0;
  389. $totalLastUpdate = 0;
  390. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  391. $feedDAO = FreshRSS_Factory::createFeedDao();
  392. $feedsNewestItemUsec = $feedDAO->listFeedsNewestItemUsec();
  393. foreach ($categoryDAO->listCategories(true, true) as $cat) {
  394. $catLastUpdate = 0;
  395. foreach ($cat->feeds() as $feed) {
  396. $lastUpdate = isset($feedsNewestItemUsec['f_' . $feed->id()]) ? $feedsNewestItemUsec['f_' . $feed->id()] : 0;
  397. $unreadcounts[] = array(
  398. 'id' => 'feed/' . $feed->id(),
  399. 'count' => $feed->nbNotRead(),
  400. 'newestItemTimestampUsec' => '' . $lastUpdate,
  401. );
  402. if ($catLastUpdate < $lastUpdate) {
  403. $catLastUpdate = $lastUpdate;
  404. }
  405. }
  406. $unreadcounts[] = array(
  407. 'id' => 'user/-/label/' . htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  408. 'count' => $cat->nbNotRead(),
  409. 'newestItemTimestampUsec' => '' . $catLastUpdate,
  410. );
  411. $totalUnreads += $cat->nbNotRead();
  412. if ($totalLastUpdate < $catLastUpdate) {
  413. $totalLastUpdate = $catLastUpdate;
  414. }
  415. }
  416. $tagDAO = FreshRSS_Factory::createTagDao();
  417. $tagsNewestItemUsec = $tagDAO->listTagsNewestItemUsec();
  418. foreach ($tagDAO->listTags(true) as $label) {
  419. $lastUpdate = isset($tagsNewestItemUsec['t_' . $label->id()]) ? $tagsNewestItemUsec['t_' . $label->id()] : 0;
  420. $unreadcounts[] = array(
  421. 'id' => 'user/-/label/' . htmlspecialchars_decode($label->name(), ENT_QUOTES),
  422. 'count' => $label->nbUnread(),
  423. 'newestItemTimestampUsec' => '' . $lastUpdate,
  424. );
  425. }
  426. $unreadcounts[] = array(
  427. 'id' => 'user/-/state/com.google/reading-list',
  428. 'count' => $totalUnreads,
  429. 'newestItemTimestampUsec' => '' . $totalLastUpdate,
  430. );
  431. echo json_encode(array(
  432. 'max' => $totalUnreads,
  433. 'unreadcounts' => $unreadcounts,
  434. ), JSON_OPTIONS), "\n";
  435. exit();
  436. }
  437. function entriesToArray($entries) {
  438. if (empty($entries)) {
  439. return array();
  440. }
  441. $feedDAO = FreshRSS_Factory::createFeedDao();
  442. $arrayFeedCategoryNames = $feedDAO->arrayFeedCategoryNames();
  443. $tagDAO = FreshRSS_Factory::createTagDao();
  444. $entryIdsTagNames = $tagDAO->getEntryIdsTagNames($entries);
  445. if ($entryIdsTagNames == false) {
  446. $entryIdsTagNames = array();
  447. }
  448. $items = array();
  449. foreach ($entries as $item) {
  450. $entry = Minz_ExtensionManager::callHook('entry_before_display', $item);
  451. if ($entry == null) {
  452. continue;
  453. }
  454. $f_id = $entry->feed();
  455. if (isset($arrayFeedCategoryNames[$f_id])) {
  456. $c_name = $arrayFeedCategoryNames[$f_id]['c_name'];
  457. $f_name = $arrayFeedCategoryNames[$f_id]['name'];
  458. } else {
  459. $c_name = '_';
  460. $f_name = '_';
  461. }
  462. $item = array(
  463. 'id' => 'tag:google.com,2005:reader/item/' . dec2hex($entry->id()), //64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId
  464. 'crawlTimeMsec' => substr($entry->dateAdded(true, true), 0, -3),
  465. 'timestampUsec' => '' . $entry->dateAdded(true, true), //EasyRSS & Reeder
  466. 'published' => $entry->date(true),
  467. 'title' => escapeToUnicodeAlternative($entry->title(), false),
  468. 'summary' => array('content' => $entry->content()),
  469. 'alternate' => array(
  470. array('href' => htmlspecialchars_decode($entry->link(), ENT_QUOTES)),
  471. ),
  472. 'categories' => array(
  473. 'user/-/state/com.google/reading-list',
  474. 'user/-/label/' . htmlspecialchars_decode($c_name, ENT_QUOTES),
  475. ),
  476. 'origin' => array(
  477. 'streamId' => 'feed/' . $f_id,
  478. 'title' => escapeToUnicodeAlternative($f_name, true), //EasyRSS
  479. //'htmlUrl' => $line['f_website'],
  480. ),
  481. );
  482. foreach ($entry->enclosures() as $enclosure) {
  483. if (!empty($enclosure['url']) && !empty($enclosure['type'])) {
  484. $media = [
  485. 'href' => $enclosure['url'],
  486. 'type' => $enclosure['type'],
  487. ];
  488. if (!empty($enclosure['length'])) {
  489. $media['length'] = intval($enclosure['length']);
  490. }
  491. $item['enclosure'][] = $media;
  492. }
  493. }
  494. $author = $entry->authors(true);
  495. $author = trim($author, '; ');
  496. if ($author != '') {
  497. $item['author'] = escapeToUnicodeAlternative($author, false);
  498. }
  499. if ($entry->isRead()) {
  500. $item['categories'][] = 'user/-/state/com.google/read';
  501. }
  502. if ($entry->isFavorite()) {
  503. $item['categories'][] = 'user/-/state/com.google/starred';
  504. }
  505. $tagNames = isset($entryIdsTagNames['e_' . $entry->id()]) ? $entryIdsTagNames['e_' . $entry->id()] : array();
  506. foreach ($tagNames as $tagName) {
  507. $item['categories'][] = 'user/-/label/' . htmlspecialchars_decode($tagName, ENT_QUOTES);
  508. }
  509. $items[] = $item;
  510. }
  511. return $items;
  512. }
  513. function streamContentsFilters($type, $streamId, $filter_target, $exclude_target, $start_time, $stop_time) {
  514. switch ($type) {
  515. case 'f': //feed
  516. if ($streamId != '' && !ctype_digit($streamId)) {
  517. $feedDAO = FreshRSS_Factory::createFeedDao();
  518. $streamId = htmlspecialchars($streamId, ENT_COMPAT, 'UTF-8');
  519. $feed = $feedDAO->searchByUrl($streamId);
  520. $streamId = $feed == null ? -1 : $feed->id();
  521. }
  522. break;
  523. case 'c': //category or label
  524. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  525. $streamId = htmlspecialchars($streamId, ENT_COMPAT, 'UTF-8');
  526. $cat = $categoryDAO->searchByName($streamId);
  527. if ($cat != null) {
  528. $type = 'c';
  529. $streamId = $cat->id();
  530. } else {
  531. $tagDAO = FreshRSS_Factory::createTagDao();
  532. $tag = $tagDAO->searchByName($streamId);
  533. if ($tag != null) {
  534. $type = 't';
  535. $streamId = $tag->id();
  536. } else {
  537. $type = 'A';
  538. $streamId = -1;
  539. }
  540. }
  541. break;
  542. }
  543. switch ($filter_target) {
  544. case 'user/-/state/com.google/read':
  545. $state = FreshRSS_Entry::STATE_READ;
  546. break;
  547. case 'user/-/state/com.google/unread':
  548. $state = FreshRSS_Entry::STATE_NOT_READ;
  549. break;
  550. case 'user/-/state/com.google/starred':
  551. $state = FreshRSS_Entry::STATE_FAVORITE;
  552. break;
  553. default:
  554. $state = FreshRSS_Entry::STATE_ALL;
  555. break;
  556. }
  557. switch ($exclude_target) {
  558. case 'user/-/state/com.google/read':
  559. $state &= FreshRSS_Entry::STATE_NOT_READ;
  560. break;
  561. case 'user/-/state/com.google/unread':
  562. $state &= FreshRSS_Entry::STATE_READ;
  563. break;
  564. case 'user/-/state/com.google/starred':
  565. $state &= FreshRSS_Entry::STATE_NOT_FAVORITE;
  566. break;
  567. }
  568. $searches = new FreshRSS_BooleanSearch('');
  569. if ($start_time != '') {
  570. $search = new FreshRSS_Search('');
  571. $search->setMinDate($start_time);
  572. $searches->add($search);
  573. }
  574. if ($stop_time != '') {
  575. $search = new FreshRSS_Search('');
  576. $search->setMaxDate($stop_time);
  577. $searches->add($search);
  578. }
  579. return array($type, $streamId, $state, $searches);
  580. }
  581. function streamContents($path, $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation) {
  582. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  583. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  584. header('Content-Type: application/json; charset=UTF-8');
  585. switch ($path) {
  586. case 'reading-list':
  587. $type = 'A';
  588. break;
  589. case 'starred':
  590. $type = 's';
  591. break;
  592. case 'feed':
  593. $type = 'f';
  594. break;
  595. case 'label':
  596. $type = 'c';
  597. break;
  598. default:
  599. $type = 'A';
  600. break;
  601. }
  602. list($type, $include_target, $state, $searches) = streamContentsFilters($type, $include_target, $filter_target, $exclude_target, $start_time, $stop_time);
  603. if ($continuation != '') {
  604. $count++; //Shift by one element
  605. }
  606. $entryDAO = FreshRSS_Factory::createEntryDao();
  607. $entries = $entryDAO->listWhere($type, $include_target, $state, $order === 'o' ? 'ASC' : 'DESC', $count, $continuation, $searches);
  608. $entries = iterator_to_array($entries); //TODO: Improve
  609. $items = entriesToArray($entries);
  610. if ($continuation != '') {
  611. array_shift($items); //Discard first element that was already sent in the previous response
  612. $count--;
  613. }
  614. $response = array(
  615. 'id' => 'user/-/state/com.google/reading-list',
  616. 'updated' => time(),
  617. 'items' => $items,
  618. );
  619. if (count($entries) >= $count) {
  620. $entry = end($entries);
  621. if ($entry != false) {
  622. $response['continuation'] = $entry->id();
  623. }
  624. }
  625. echo json_encode($response, JSON_OPTIONS), "\n";
  626. exit();
  627. }
  628. function streamContentsItemsIds($streamId, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation) {
  629. //http://code.google.com/p/google-reader-api/wiki/ApiStreamItemsIds
  630. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  631. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  632. $type = 'A';
  633. $id = '';
  634. if ($streamId === 'user/-/state/com.google/reading-list') {
  635. $type = 'A';
  636. } elseif ($streamId === 'user/-/state/com.google/starred') {
  637. $type = 's';
  638. } elseif (strpos($streamId, 'feed/') === 0) {
  639. $type = 'f';
  640. $streamId = substr($streamId, 5);
  641. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  642. $type = 'c';
  643. $streamId = substr($streamId, 13);
  644. }
  645. list($type, $id, $state, $searches) = streamContentsFilters($type, $streamId, $filter_target, $exclude_target, $start_time, $stop_time);
  646. if ($continuation != '') {
  647. $count++; //Shift by one element
  648. }
  649. $entryDAO = FreshRSS_Factory::createEntryDao();
  650. $ids = $entryDAO->listIdsWhere($type, $id, $state, $order === 'o' ? 'ASC' : 'DESC', $count, $continuation, $searches);
  651. if ($continuation != '') {
  652. array_shift($ids); //Discard first element that was already sent in the previous response
  653. $count--;
  654. }
  655. if (empty($ids) && isset($_GET['client']) && $_GET['client'] === 'newsplus') {
  656. $ids[] = 0; //For News+ bug https://github.com/noinnion/newsplus/issues/84#issuecomment-57834632
  657. }
  658. $itemRefs = array();
  659. foreach ($ids as $id) {
  660. $itemRefs[] = array(
  661. 'id' => '' . $id, //64-bit decimal
  662. );
  663. }
  664. $response = array(
  665. 'itemRefs' => $itemRefs,
  666. );
  667. if (count($ids) >= $count) {
  668. $id = end($ids);
  669. if ($id != false) {
  670. $response['continuation'] = $id;
  671. }
  672. }
  673. echo json_encode($response, JSON_OPTIONS), "\n";
  674. exit();
  675. }
  676. function streamContentsItems($e_ids, $order) {
  677. header('Content-Type: application/json; charset=UTF-8');
  678. foreach ($e_ids as $i => $e_id) {
  679. // https://feedhq.readthedocs.io/en/latest/api/terminology.html#items
  680. if (!ctype_digit($e_id) || $e_id[0] === '0') {
  681. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  682. }
  683. }
  684. $entryDAO = FreshRSS_Factory::createEntryDao();
  685. $entries = $entryDAO->listByIds($e_ids, $order === 'o' ? 'ASC' : 'DESC');
  686. $entries = iterator_to_array($entries); //TODO: Improve
  687. $items = entriesToArray($entries);
  688. $response = array(
  689. 'id' => 'user/-/state/com.google/reading-list',
  690. 'updated' => time(),
  691. 'items' => $items,
  692. );
  693. echo json_encode($response, JSON_OPTIONS), "\n";
  694. exit();
  695. }
  696. function editTag($e_ids, $a, $r) {
  697. foreach ($e_ids as $i => $e_id) {
  698. if (!ctype_digit($e_id) || $e_id[0] === '0') {
  699. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  700. }
  701. }
  702. $entryDAO = FreshRSS_Factory::createEntryDao();
  703. $tagDAO = FreshRSS_Factory::createTagDao();
  704. switch ($a) {
  705. case 'user/-/state/com.google/read':
  706. $entryDAO->markRead($e_ids, true);
  707. break;
  708. case 'user/-/state/com.google/starred':
  709. $entryDAO->markFavorite($e_ids, true);
  710. break;
  711. /*case 'user/-/state/com.google/tracking-kept-unread':
  712. break;
  713. case 'user/-/state/com.google/like':
  714. break;
  715. case 'user/-/state/com.google/broadcast':
  716. break;*/
  717. default:
  718. $tagName = '';
  719. if (strpos($a, 'user/-/label/') === 0) {
  720. $tagName = substr($a, 13);
  721. } else {
  722. $user = Minz_Session::param('currentUser', '_');
  723. $prefix = 'user/' . $user . '/label/';
  724. if (strpos($a, $prefix) === 0) {
  725. $tagName = substr($a, strlen($prefix));
  726. }
  727. }
  728. if ($tagName != '') {
  729. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  730. $tag = $tagDAO->searchByName($tagName);
  731. if ($tag == null) {
  732. $tagDAO->addTag(array('name' => $tagName));
  733. $tag = $tagDAO->searchByName($tagName);
  734. }
  735. if ($tag != null) {
  736. foreach ($e_ids as $e_id) {
  737. $tagDAO->tagEntry($tag->id(), $e_id, true);
  738. }
  739. }
  740. }
  741. break;
  742. }
  743. switch ($r) {
  744. case 'user/-/state/com.google/read':
  745. $entryDAO->markRead($e_ids, false);
  746. break;
  747. case 'user/-/state/com.google/starred':
  748. $entryDAO->markFavorite($e_ids, false);
  749. break;
  750. default:
  751. if (strpos($r, 'user/-/label/') === 0) {
  752. $tagName = substr($r, 13);
  753. $tagName = htmlspecialchars($tagName, ENT_COMPAT, 'UTF-8');
  754. $tag = $tagDAO->searchByName($tagName);
  755. if ($tag != null) {
  756. foreach ($e_ids as $e_id) {
  757. $tagDAO->tagEntry($tag->id(), $e_id, false);
  758. }
  759. }
  760. }
  761. break;
  762. }
  763. exit('OK');
  764. }
  765. function renameTag($s, $dest) {
  766. if ($s != '' && strpos($s, 'user/-/label/') === 0 &&
  767. $dest != '' && strpos($dest, 'user/-/label/') === 0) {
  768. $s = substr($s, 13);
  769. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  770. $dest = substr($dest, 13);
  771. $dest = htmlspecialchars($dest, ENT_COMPAT, 'UTF-8');
  772. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  773. $cat = $categoryDAO->searchByName($s);
  774. if ($cat != null) {
  775. $categoryDAO->updateCategory($cat->id(), array('name' => $dest));
  776. exit('OK');
  777. } else {
  778. $tagDAO = FreshRSS_Factory::createTagDao();
  779. $tag = $tagDAO->searchByName($s);
  780. if ($tag != null) {
  781. $tagDAO->updateTag($tag->id(), array('name' => $dest));
  782. exit('OK');
  783. }
  784. }
  785. }
  786. badRequest();
  787. }
  788. function disableTag($s) {
  789. if ($s != '' && strpos($s, 'user/-/label/') === 0) {
  790. $s = substr($s, 13);
  791. $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  792. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  793. $cat = $categoryDAO->searchByName($s);
  794. if ($cat != null) {
  795. $feedDAO = FreshRSS_Factory::createFeedDao();
  796. $feedDAO->changeCategory($cat->id(), 0);
  797. if ($cat->id() > 1) {
  798. $categoryDAO->deleteCategory($cat->id());
  799. }
  800. exit('OK');
  801. } else {
  802. $tagDAO = FreshRSS_Factory::createTagDao();
  803. $tag = $tagDAO->searchByName($s);
  804. if ($tag != null) {
  805. $tagDAO->deleteTag($tag->id());
  806. exit('OK');
  807. }
  808. }
  809. }
  810. badRequest();
  811. }
  812. function markAllAsRead($streamId, $olderThanId) {
  813. $entryDAO = FreshRSS_Factory::createEntryDao();
  814. if (strpos($streamId, 'feed/') === 0) {
  815. $f_id = basename($streamId);
  816. $entryDAO->markReadFeed($f_id, $olderThanId);
  817. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  818. $c_name = substr($streamId, 13);
  819. $c_name = htmlspecialchars($c_name, ENT_COMPAT, 'UTF-8');
  820. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  821. $cat = $categoryDAO->searchByName($c_name);
  822. if ($cat != null) {
  823. $entryDAO->markReadCat($cat->id(), $olderThanId);
  824. } else {
  825. $tagDAO = FreshRSS_Factory::createTagDao();
  826. $tag = $tagDAO->searchByName($c_name);
  827. if ($tag != null) {
  828. $entryDAO->markReadTag($tag->id(), $olderThanId);
  829. }
  830. }
  831. } elseif ($streamId === 'user/-/state/com.google/reading-list') {
  832. $entryDAO->markReadEntries($olderThanId, false, -1);
  833. }
  834. exit('OK');
  835. }
  836. $pathInfo = empty($_SERVER['PATH_INFO']) ? '' : urldecode($_SERVER['PATH_INFO']);
  837. if ($pathInfo == '') {
  838. exit('OK');
  839. }
  840. $pathInfos = explode('/', $pathInfo);
  841. if (count($pathInfos) < 3) {
  842. badRequest();
  843. }
  844. Minz_Configuration::register('system',
  845. DATA_PATH . '/config.php',
  846. FRESHRSS_PATH . '/config.default.php');
  847. FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
  848. //Minz_Log::debug('----------------------------------------------------------------', API_LOG);
  849. //Minz_Log::debug(debugInfo(), API_LOG);
  850. if (!FreshRSS_Context::$system_conf->api_enabled) {
  851. serviceUnavailable();
  852. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  853. checkCompatibility();
  854. }
  855. ini_set('session.use_cookies', '0');
  856. register_shutdown_function('session_destroy');
  857. Minz_Session::init('FreshRSS');
  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();