greader.php 37 KB

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