greader.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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 = preg_replace('%^(feed/)+%', '', $streamUrl);
  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. $pathInfo = preg_replace('%^(/api)?(/greader\.php)?%', '', $pathInfo); //Discard common errors
  838. if ($pathInfo == '') {
  839. exit('OK');
  840. }
  841. $pathInfos = explode('/', $pathInfo);
  842. if (count($pathInfos) < 3) {
  843. badRequest();
  844. }
  845. Minz_Configuration::register('system',
  846. DATA_PATH . '/config.php',
  847. FRESHRSS_PATH . '/config.default.php');
  848. FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
  849. //Minz_Log::debug('----------------------------------------------------------------', API_LOG);
  850. //Minz_Log::debug(debugInfo(), API_LOG);
  851. if (!FreshRSS_Context::$system_conf->api_enabled) {
  852. serviceUnavailable();
  853. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  854. checkCompatibility();
  855. }
  856. ini_set('session.use_cookies', '0');
  857. register_shutdown_function('session_destroy');
  858. Minz_Session::init('FreshRSS');
  859. $user = $pathInfos[1] === 'accounts' ? '' : authorizationToUser();
  860. FreshRSS_Context::$user_conf = null;
  861. if ($user !== '') {
  862. FreshRSS_Context::$user_conf = get_user_configuration($user);
  863. Minz_ExtensionManager::init();
  864. if (FreshRSS_Context::$user_conf != null) {
  865. Minz_Translate::init(FreshRSS_Context::$user_conf->language);
  866. Minz_ExtensionManager::enableByList(FreshRSS_Context::$user_conf->extensions_enabled);
  867. } else {
  868. Minz_Translate::init();
  869. }
  870. } else {
  871. Minz_Translate::init();
  872. }
  873. Minz_Session::_param('currentUser', $user);
  874. if ($pathInfos[1] === 'accounts') {
  875. if (($pathInfos[2] === 'ClientLogin') && isset($_REQUEST['Email']) && isset($_REQUEST['Passwd'])) {
  876. clientLogin($_REQUEST['Email'], $_REQUEST['Passwd']);
  877. }
  878. } elseif ($pathInfos[1] === 'reader' && $pathInfos[2] === 'api' && isset($pathInfos[3]) && $pathInfos[3] === '0' && isset($pathInfos[4])) {
  879. if ($user == '') {
  880. unauthorized();
  881. }
  882. $timestamp = isset($_GET['ck']) ? intval($_GET['ck']) : 0; //ck=[unix timestamp] : Use the current Unix time here, helps Google with caching.
  883. switch ($pathInfos[4]) {
  884. case 'stream':
  885. /* xt=[exclude target] : Used to exclude certain items from the feed.
  886. * For example, using xt=user/-/state/com.google/read will exclude items
  887. * that the current user has marked as read, or xt=feed/[feedurl] will
  888. * exclude items from a particular feed (obviously not useful in this
  889. * request, but xt appears in other listing requests). */
  890. $exclude_target = isset($_GET['xt']) ? $_GET['xt'] : '';
  891. $filter_target = isset($_GET['it']) ? $_GET['it'] : '';
  892. $count = isset($_GET['n']) ? intval($_GET['n']) : 20; //n=[integer] : The maximum number of results to return.
  893. $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.
  894. /* ot=[unix timestamp] : The time from which you want to retrieve
  895. * items. Only items that have been crawled by Google Reader after
  896. * this time will be returned. */
  897. $start_time = isset($_GET['ot']) ? intval($_GET['ot']) : 0;
  898. $stop_time = isset($_GET['nt']) ? intval($_GET['nt']) : 0;
  899. /* Continuation token. If a StreamContents response does not represent
  900. * all items in a timestamp range, it will have a continuation attribute.
  901. * The same request can be re-issued with the value of that attribute put
  902. * in this parameter to get more items */
  903. $continuation = isset($_GET['c']) ? trim($_GET['c']) : '';
  904. if (!ctype_digit($continuation)) {
  905. $continuation = '';
  906. }
  907. if (isset($pathInfos[5]) && $pathInfos[5] === 'contents' && isset($pathInfos[6])) {
  908. if (isset($pathInfos[7])) {
  909. if ($pathInfos[6] === 'feed') {
  910. $include_target = $pathInfos[7];
  911. if ($include_target != '' && !ctype_digit($include_target)) {
  912. $include_target = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
  913. if (preg_match('#/reader/api/0/stream/contents/feed/([A-Za-z0-9\'!*()%$_.~+-]+)#', $include_target, $matches) && isset($matches[1])) {
  914. $include_target = urldecode($matches[1]);
  915. } else {
  916. $include_target = '';
  917. }
  918. }
  919. streamContents($pathInfos[6], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  920. } elseif ($pathInfos[6] === 'user' && isset($pathInfos[8]) && isset($pathInfos[9])) {
  921. if ($pathInfos[8] === 'state') {
  922. if ($pathInfos[9] === 'com.google' && isset($pathInfos[10])) {
  923. if ($pathInfos[10] === 'reading-list' || $pathInfos[10] === 'starred') {
  924. $include_target = '';
  925. streamContents($pathInfos[10], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  926. }
  927. }
  928. } elseif ($pathInfos[8] === 'label') {
  929. $include_target = $pathInfos[9];
  930. streamContents($pathInfos[8], $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  931. }
  932. }
  933. } else { //EasyRSS
  934. $include_target = '';
  935. streamContents('reading-list', $include_target, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  936. }
  937. } elseif ($pathInfos[5] === 'items') {
  938. if ($pathInfos[6] === 'ids' && isset($_GET['s'])) {
  939. /* StreamId for which to fetch the item IDs. The parameter may
  940. * be repeated to fetch the item IDs from multiple streams at once
  941. * (more efficient from a backend perspective than multiple requests). */
  942. $streamId = $_GET['s'];
  943. streamContentsItemsIds($streamId, $start_time, $stop_time, $count, $order, $filter_target, $exclude_target, $continuation);
  944. } elseif ($pathInfos[6] === 'contents' && isset($_POST['i'])) { //FeedMe
  945. $e_ids = multiplePosts('i'); //item IDs
  946. streamContentsItems($e_ids, $order);
  947. }
  948. }
  949. break;
  950. case 'tag':
  951. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  952. $output = isset($_GET['output']) ? $_GET['output'] : '';
  953. if ($output !== 'json') notImplemented();
  954. tagList();
  955. }
  956. break;
  957. case 'subscription':
  958. if (isset($pathInfos[5])) {
  959. switch ($pathInfos[5]) {
  960. case 'list':
  961. $output = isset($_GET['output']) ? $_GET['output'] : '';
  962. if ($output !== 'json') notImplemented();
  963. subscriptionList();
  964. break;
  965. case 'edit':
  966. if (isset($_REQUEST['s']) && isset($_REQUEST['ac'])) {
  967. //StreamId to operate on. The parameter may be repeated to edit multiple subscriptions at once
  968. $streamNames = empty($_POST['s']) && isset($_GET['s']) ? array($_GET['s']) : multiplePosts('s');
  969. /* Title to use for the subscription. For the `subscribe` action,
  970. * if not specified then the feed's current title will be used. Can
  971. * be used with the `edit` action to rename a subscription */
  972. $titles = empty($_POST['t']) && isset($_GET['t']) ? array($_GET['t']) : multiplePosts('t');
  973. $action = $_REQUEST['ac']; //Action to perform on the given StreamId. Possible values are `subscribe`, `unsubscribe` and `edit`
  974. $add = isset($_REQUEST['a']) ? $_REQUEST['a'] : ''; //StreamId to add the subscription to (generally a user label)
  975. $remove = isset($_REQUEST['r']) ? $_REQUEST['r'] : ''; //StreamId to remove the subscription from (generally a user label)
  976. subscriptionEdit($streamNames, $titles, $action, $add, $remove);
  977. }
  978. break;
  979. case 'quickadd': //https://github.com/theoldreader/api
  980. if (isset($_REQUEST['quickadd'])) {
  981. quickadd($_REQUEST['quickadd']);
  982. }
  983. break;
  984. }
  985. }
  986. break;
  987. case 'unread-count':
  988. $output = isset($_GET['output']) ? $_GET['output'] : '';
  989. if ($output !== 'json') notImplemented();
  990. $all = isset($_GET['all']) ? $_GET['all'] : '';
  991. unreadCount($all);
  992. break;
  993. case 'edit-tag': //http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/
  994. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  995. checkToken(FreshRSS_Context::$user_conf, $token);
  996. $a = isset($_POST['a']) ? $_POST['a'] : ''; //Add: user/-/state/com.google/read user/-/state/com.google/starred
  997. $r = isset($_POST['r']) ? $_POST['r'] : ''; //Remove: user/-/state/com.google/read user/-/state/com.google/starred
  998. $e_ids = multiplePosts('i'); //item IDs
  999. editTag($e_ids, $a, $r);
  1000. break;
  1001. case 'rename-tag': //https://github.com/theoldreader/api
  1002. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1003. checkToken(FreshRSS_Context::$user_conf, $token);
  1004. $s = isset($_POST['s']) ? $_POST['s'] : ''; //user/-/label/Folder
  1005. $dest = isset($_POST['dest']) ? $_POST['dest'] : ''; //user/-/label/NewFolder
  1006. renameTag($s, $dest);
  1007. break;
  1008. case 'disable-tag': //https://github.com/theoldreader/api
  1009. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1010. checkToken(FreshRSS_Context::$user_conf, $token);
  1011. $s_s = multiplePosts('s');
  1012. foreach ($s_s as $s) {
  1013. disableTag($s); //user/-/label/Folder
  1014. }
  1015. break;
  1016. case 'mark-all-as-read':
  1017. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  1018. checkToken(FreshRSS_Context::$user_conf, $token);
  1019. $streamId = $_POST['s']; //StreamId
  1020. $ts = isset($_POST['ts']) ? $_POST['ts'] : '0'; //Older than timestamp in nanoseconds
  1021. if (!ctype_digit($ts)) {
  1022. $ts = '0';
  1023. }
  1024. markAllAsRead($streamId, $ts);
  1025. break;
  1026. case 'token':
  1027. token(FreshRSS_Context::$user_conf);
  1028. break;
  1029. case 'user-info':
  1030. userInfo();
  1031. break;
  1032. }
  1033. }
  1034. badRequest();