greader.php 38 KB

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