greader.php 36 KB

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