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