greader.php 38 KB

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