greader.php 36 KB

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