greader.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <?php
  2. /**
  3. == Description ==
  4. Server-side API compatible with Google Reader API layer 2
  5. for the FreshRSS project http://freshrss.org
  6. == Credits ==
  7. * 2014-03: Released by Alexandre Alapetite http://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. */
  20. require('../../constants.php');
  21. require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader
  22. $ORIGINAL_INPUT = file_get_contents('php://input');
  23. if (PHP_INT_SIZE < 8) { //32-bit
  24. function dec2hex($dec) {
  25. return str_pad(gmp_strval(gmp_init($dec, 10), 16), 16, '0', STR_PAD_LEFT);
  26. }
  27. function hex2dec($hex) {
  28. return gmp_strval(gmp_init($hex, 16), 10);
  29. }
  30. } else { //64-bit
  31. function dec2hex($dec) { //http://code.google.com/p/google-reader-api/wiki/ItemId
  32. return str_pad(dechex($dec), 16, '0', STR_PAD_LEFT);
  33. }
  34. function hex2dec($hex) {
  35. return hexdec($hex);
  36. }
  37. }
  38. function headerVariable($headerName, $varName) {
  39. $header = '';
  40. $upName = 'HTTP_' . strtoupper($headerName);
  41. if (isset($_SERVER[$upName])) {
  42. $header = $_SERVER[$upName];
  43. } elseif (function_exists('getallheaders')) {
  44. $ALL_HEADERS = getallheaders();
  45. if (isset($ALL_HEADERS[$headerName])) {
  46. $header = $ALL_HEADERS[$headerName];
  47. }
  48. }
  49. parse_str($header, $pairs);
  50. return isset($pairs[$varName]) ? $pairs[$varName] : null;
  51. }
  52. function multiplePosts($name) { //https://bugs.php.net/bug.php?id=51633
  53. global $ORIGINAL_INPUT;
  54. $inputs = explode('&', $ORIGINAL_INPUT);
  55. $result = array();
  56. $prefix = $name . '=';
  57. $prefixLength = strlen($prefix);
  58. foreach ($inputs as $input) {
  59. if (strpos($input, $prefix) === 0) {
  60. $result[] = urldecode(substr($input, $prefixLength));
  61. }
  62. }
  63. return $result;
  64. }
  65. class MyPDO extends Minz_ModelPdo {
  66. function prepare($sql) {
  67. return $this->bd->prepare(str_replace('%_', $this->prefix, $sql));
  68. }
  69. }
  70. function logMe($text) {
  71. file_put_contents(join_path(USERS_PATH, '_', 'log_api.txt'), $text, FILE_APPEND);
  72. }
  73. function debugInfo() {
  74. if (function_exists('getallheaders')) {
  75. $ALL_HEADERS = getallheaders();
  76. } else { //nginx http://php.net/getallheaders#84262
  77. $ALL_HEADERS = '';
  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(array('date' => date('c'), 'headers' => $ALL_HEADERS, '_SERVER' => $_SERVER, '_GET' => $_GET, '_POST' => $_POST, '_COOKIE' => $_COOKIE, 'INPUT' => $ORIGINAL_INPUT), true);
  86. }
  87. function badRequest() {
  88. logMe("badRequest()\n");
  89. logMe(debugInfo());
  90. header('HTTP/1.1 400 Bad Request');
  91. header('Content-Type: text/plain; charset=UTF-8');
  92. die('Bad Request!');
  93. }
  94. function unauthorized() {
  95. logMe("unauthorized()\n");
  96. logMe(debugInfo());
  97. header('HTTP/1.1 401 Unauthorized');
  98. header('Content-Type: text/plain; charset=UTF-8');
  99. header('Google-Bad-Token: true');
  100. die('Unauthorized!');
  101. }
  102. function notImplemented() {
  103. logMe("notImplemented()\n");
  104. logMe(debugInfo());
  105. header('HTTP/1.1 501 Not Implemented');
  106. header('Content-Type: text/plain; charset=UTF-8');
  107. die('Not Implemented!');
  108. }
  109. function serviceUnavailable() {
  110. logMe("serviceUnavailable()\n");
  111. header('HTTP/1.1 503 Service Unavailable');
  112. header('Content-Type: text/plain; charset=UTF-8');
  113. die('Service Unavailable!');
  114. }
  115. function checkCompatibility() {
  116. logMe("checkCompatibility()\n");
  117. header('Content-Type: text/plain; charset=UTF-8');
  118. if (PHP_INT_SIZE < 8 && !function_exists('gmp_init')) {
  119. die('FAIL 64-bit or GMP extension!');
  120. }
  121. if ((!array_key_exists('HTTP_AUTHORIZATION', $_SERVER)) && //Apache mod_rewrite trick should be fine
  122. (empty($_SERVER['SERVER_SOFTWARE']) || (stripos($_SERVER['SERVER_SOFTWARE'], 'nginx') === false)) && //nginx should be fine
  123. (empty($_SERVER['SERVER_SOFTWARE']) || (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') === false)) && //lighttpd should be fine
  124. ((!function_exists('getallheaders')) || (stripos(php_sapi_name(), 'cgi') !== false))) { //Main problem is Apache/CGI mode
  125. die('FAIL getallheaders! (probably)');
  126. }
  127. echo 'PASS';
  128. exit();
  129. }
  130. function authorizationToUserConf() {
  131. $headerAuth = headerVariable('Authorization', 'GoogleLogin_auth'); //Input is 'GoogleLogin auth', but PHP replaces spaces by '_' http://php.net/language.variables.external
  132. if ($headerAuth != '') {
  133. $headerAuthX = explode('/', $headerAuth, 2);
  134. if (count($headerAuthX) === 2) {
  135. $user = $headerAuthX[0];
  136. if (ctype_alnum($user)) {
  137. $conf = get_user_configuration($user);
  138. if (is_null($conf)) {
  139. unauthorized();
  140. }
  141. $system_conf = Minz_Configuration::get('system');
  142. if ($headerAuthX[1] === sha1($system_conf->salt . $conf->user . $conf->apiPasswordHash)) {
  143. return $conf;
  144. } else {
  145. logMe('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1] . "\n");
  146. Minz_Log::warning('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1]);
  147. unauthorized();
  148. }
  149. } else {
  150. badRequest();
  151. }
  152. }
  153. }
  154. return null;
  155. }
  156. function clientLogin($email, $pass) { //http://web.archive.org/web/20130604091042/http://undoc.in/clientLogin.html
  157. logMe('clientLogin(' . $email . ")\n");
  158. if (ctype_alnum($email)) {
  159. if (!function_exists('password_verify')) {
  160. include_once(LIB_PATH . '/password_compat.php');
  161. }
  162. $conf = get_user_configuration($email);
  163. if (is_null($conf)) {
  164. unauthorized();
  165. }
  166. if ($conf->apiPasswordHash != '' && password_verify($pass, $conf->apiPasswordHash)) {
  167. header('Content-Type: text/plain; charset=UTF-8');
  168. $system_conf = Minz_Configuration::get('system');
  169. $auth = $email . '/' . sha1($system_conf->salt . $conf->user . $conf->apiPasswordHash);
  170. echo 'SID=', $auth, "\n",
  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. logMe('token('. $conf->user . ")\n"); //TODO: Implement real token that expires
  186. $system_conf = Minz_Configuration::get('system');
  187. $token = str_pad(sha1($system_conf->salt . $conf->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. logMe('checkToken(' . $token . ")\n");
  194. $system_conf = Minz_Configuration::get('system');
  195. if ($token === str_pad(sha1($system_conf->salt . $conf->user . $conf->apiPasswordHash), 57, 'Z')) {
  196. return true;
  197. }
  198. unauthorized();
  199. }
  200. function tagList() {
  201. logMe("tagList()\n");
  202. header('Content-Type: application/json; charset=UTF-8');
  203. $pdo = new MyPDO();
  204. $stm = $pdo->prepare('SELECT c.name FROM `%_category` c');
  205. $stm->execute();
  206. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  207. $tags = array(
  208. array('id' => 'user/-/state/com.google/starred'),
  209. //array('id' => 'user/-/state/com.google/broadcast', 'sortid' => '2'),
  210. );
  211. foreach ($res as $cName) {
  212. $tags[] = array(
  213. 'id' => 'user/-/label/' . $cName,
  214. //'sortid' => $cName,
  215. );
  216. }
  217. echo json_encode(array('tags' => $tags)), "\n";
  218. exit();
  219. }
  220. function subscriptionList() {
  221. logMe("subscriptionList()\n");
  222. header('Content-Type: application/json; charset=UTF-8');
  223. $pdo = new MyPDO();
  224. $stm = $pdo->prepare('SELECT f.id, f.name, f.url, f.website, c.id as c_id, c.name as c_name FROM `%_feed` f
  225. INNER JOIN `%_category` c ON c.id = f.category');
  226. $stm->execute();
  227. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  228. $subscriptions = array();
  229. foreach ($res as $line) {
  230. $subscriptions[] = array(
  231. 'id' => 'feed/' . $line['id'],
  232. 'title' => $line['name'],
  233. 'categories' => array(
  234. array(
  235. 'id' => 'user/-/label/' . $line['c_name'],
  236. 'label' => $line['c_name'],
  237. ),
  238. ),
  239. //'sortid' => $line['name'],
  240. //'firstitemmsec' => 0,
  241. 'url' => $line['url'],
  242. 'htmlUrl' => $line['website'],
  243. //'iconUrl' => '',
  244. );
  245. }
  246. echo json_encode(array('subscriptions' => $subscriptions)), "\n";
  247. exit();
  248. }
  249. function unreadCount() { //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#unread-count
  250. logMe("unreadCount()\n");
  251. header('Content-Type: application/json; charset=UTF-8');
  252. $totalUnreads = 0;
  253. $totalLastUpdate = 0;
  254. $categoryDAO = new FreshRSS_CategoryDAO();
  255. foreach ($categoryDAO->listCategories(true, true) as $cat) {
  256. $catLastUpdate = 0;
  257. foreach ($cat->feeds() as $feed) {
  258. $lastUpdate = $feed->lastUpdate();
  259. $unreadcounts[] = array(
  260. 'id' => 'feed/' . $feed->id(),
  261. 'count' => $feed->nbNotRead(),
  262. 'newestItemTimestampUsec' => $lastUpdate . '000000',
  263. );
  264. if ($catLastUpdate < $lastUpdate) {
  265. $catLastUpdate = $lastUpdate;
  266. }
  267. }
  268. $unreadcounts[] = array(
  269. 'id' => 'user/-/label/' . $cat->name(),
  270. 'count' => $cat->nbNotRead(),
  271. 'newestItemTimestampUsec' => $catLastUpdate . '000000',
  272. );
  273. $totalUnreads += $cat->nbNotRead();
  274. if ($totalLastUpdate < $catLastUpdate) {
  275. $totalLastUpdate = $catLastUpdate;
  276. }
  277. }
  278. $unreadcounts[] = array(
  279. 'id' => 'user/-/state/com.google/reading-list',
  280. 'count' => $totalUnreads,
  281. 'newestItemTimestampUsec' => $totalLastUpdate . '000000',
  282. );
  283. echo json_encode(array(
  284. 'max' => $totalUnreads,
  285. 'unreadcounts' => $unreadcounts,
  286. )), "\n";
  287. exit();
  288. }
  289. function streamContents($path, $include_target, $start_time, $count, $order, $exclude_target, $continuation) {
  290. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  291. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  292. logMe("streamContents($path, $include_target, $start_time, $count, $order, $exclude_target, $continuation)\n");
  293. header('Content-Type: application/json; charset=UTF-8');
  294. $feedDAO = FreshRSS_Factory::createFeedDao();
  295. $arrayFeedCategoryNames = $feedDAO->arrayFeedCategoryNames();
  296. switch ($path) {
  297. case 'reading-list':
  298. $type = 'A';
  299. break;
  300. case 'starred':
  301. $type = 's';
  302. break;
  303. case 'feed':
  304. $type = 'f';
  305. break;
  306. case 'label':
  307. $type = 'c';
  308. $categoryDAO = new FreshRSS_CategoryDAO();
  309. $cat = $categoryDAO->searchByName($include_target);
  310. $include_target = $cat == null ? -1 : $cat->id();
  311. break;
  312. default:
  313. $type = 'A';
  314. break;
  315. }
  316. switch ($exclude_target) {
  317. case 'user/-/state/com.google/read':
  318. $state = FreshRSS_Entry::STATE_NOT_READ;
  319. break;
  320. default:
  321. $state = FreshRSS_Entry::STATE_ALL;
  322. break;
  323. }
  324. if (!empty($continuation)) {
  325. $count++; //Shift by one element
  326. }
  327. $entryDAO = FreshRSS_Factory::createEntryDao();
  328. $entries = $entryDAO->listWhere($type, $include_target, $state, $order === 'o' ? 'ASC' : 'DESC', $count, $continuation, '', $start_time);
  329. $items = array();
  330. foreach ($entries as $entry) {
  331. $f_id = $entry->feed();
  332. if (isset($arrayFeedCategoryNames[$f_id])) {
  333. $c_name = $arrayFeedCategoryNames[$f_id]['c_name'];
  334. $f_name = $arrayFeedCategoryNames[$f_id]['name'];
  335. } else {
  336. $c_name = '_';
  337. $f_name = '_';
  338. }
  339. $item = array(
  340. 'id' => /*'tag:google.com,2005:reader/item/' .*/ dec2hex($entry->id()), //64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId
  341. 'crawlTimeMsec' => substr($entry->id(), 0, -3),
  342. 'timestampUsec' => $entry->id(), //EasyRSS
  343. 'published' => $entry->date(true),
  344. 'title' => $entry->title(),
  345. 'summary' => array('content' => $entry->content()),
  346. 'alternate' => array(
  347. array('href' => $entry->link()),
  348. ),
  349. 'categories' => array(
  350. 'user/-/state/com.google/reading-list',
  351. 'user/-/label/' . $c_name,
  352. ),
  353. 'origin' => array(
  354. 'streamId' => 'feed/' . $f_id,
  355. 'title' => $f_name, //EasyRSS
  356. //'htmlUrl' => $line['f_website'],
  357. ),
  358. );
  359. if ($entry->author() != '') {
  360. $item['author'] = $entry->author();
  361. }
  362. if ($entry->isRead()) {
  363. $item['categories'][] = 'user/-/state/com.google/read';
  364. }
  365. if ($entry->isFavorite()) {
  366. $item['categories'][] = 'user/-/state/com.google/starred';
  367. }
  368. $items[] = $item;
  369. }
  370. if (!empty($continuation)) {
  371. array_shift($items); //Discard first element that was already sent in the previous response
  372. }
  373. $response = array(
  374. 'id' => 'user/-/state/com.google/reading-list',
  375. 'updated' => time(),
  376. 'items' => $items,
  377. );
  378. if ((count($entries) >= $count) && (!empty($entry))) {
  379. $response['continuation'] = $entry->id();
  380. }
  381. echo json_encode($response), "\n";
  382. exit();
  383. }
  384. function streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target) {
  385. //http://code.google.com/p/google-reader-api/wiki/ApiStreamItemsIds
  386. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  387. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  388. logMe("streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target)\n");
  389. $type = 'A';
  390. $id = '';
  391. if ($streamId === 'user/-/state/com.google/reading-list') {
  392. $type = 'A';
  393. } elseif ('user/-/state/com.google/starred') {
  394. $type = 's';
  395. } elseif (strpos($streamId, 'feed/') === 0) {
  396. $type = 'f';
  397. $id = basename($streamId);
  398. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  399. $type = 'c';
  400. $c_name = basename($streamId);
  401. $categoryDAO = new FreshRSS_CategoryDAO();
  402. $cat = $categoryDAO->searchByName($c_name);
  403. $id = $cat == null ? -1 : $cat->id();
  404. }
  405. switch ($exclude_target) {
  406. case 'user/-/state/com.google/read':
  407. $state = FreshRSS_Entry::STATE_NOT_READ;
  408. break;
  409. default:
  410. $state = FreshRSS_Entry::STATE_ALL;
  411. break;
  412. }
  413. $entryDAO = FreshRSS_Factory::createEntryDao();
  414. $ids = $entryDAO->listIdsWhere($type, $id, $state, $order === 'o' ? 'ASC' : 'DESC', $count, '', '', $start_time);
  415. $itemRefs = array();
  416. foreach ($ids as $id) {
  417. $itemRefs[] = array(
  418. 'id' => $id, //64-bit decimal
  419. );
  420. }
  421. echo json_encode(array(
  422. 'itemRefs' => $itemRefs,
  423. )), "\n";
  424. exit();
  425. }
  426. function editTag($e_ids, $a, $r) {
  427. logMe("editTag()\n");
  428. foreach ($e_ids as $i => $e_id) {
  429. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  430. }
  431. $entryDAO = FreshRSS_Factory::createEntryDao();
  432. switch ($a) {
  433. case 'user/-/state/com.google/read':
  434. $entryDAO->markRead($e_ids, true);
  435. break;
  436. case 'user/-/state/com.google/starred':
  437. $entryDAO->markFavorite($e_ids, true);
  438. break;
  439. /*case 'user/-/state/com.google/tracking-kept-unread':
  440. break;
  441. case 'user/-/state/com.google/like':
  442. break;
  443. case 'user/-/state/com.google/broadcast':
  444. break;*/
  445. }
  446. switch ($r) {
  447. case 'user/-/state/com.google/read':
  448. $entryDAO->markRead($e_ids, false);
  449. break;
  450. case 'user/-/state/com.google/starred':
  451. $entryDAO->markFavorite($e_ids, false);
  452. break;
  453. }
  454. echo 'OK';
  455. exit();
  456. }
  457. function markAllAsRead($streamId, $olderThanId) {
  458. logMe("markAllAsRead($streamId, $olderThanId)\n");
  459. $entryDAO = FreshRSS_Factory::createEntryDao();
  460. if (strpos($streamId, 'feed/') === 0) {
  461. $f_id = basename($streamId);
  462. $entryDAO->markReadFeed($f_id, $olderThanId);
  463. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  464. $c_name = basename($streamId);
  465. $categoryDAO = new FreshRSS_CategoryDAO();
  466. $cat = $categoryDAO->searchByName($c_name);
  467. $entryDAO->markReadCat($cat === null ? -1 : $cat->id(), $olderThanId);
  468. } elseif ($streamId === 'user/-/state/com.google/reading-list') {
  469. $entryDAO->markReadEntries($olderThanId, false, -1);
  470. }
  471. echo 'OK';
  472. exit();
  473. }
  474. logMe('----------------------------------------------------------------'."\n");
  475. //logMe(debugInfo());
  476. $pathInfo = empty($_SERVER['PATH_INFO']) ? '/Error' : urldecode($_SERVER['PATH_INFO']);
  477. $pathInfos = explode('/', $pathInfo);
  478. Minz_Configuration::register('system',
  479. DATA_PATH . '/config.php',
  480. DATA_PATH . '/config.default.php');
  481. $system_conf = Minz_Configuration::get('system');
  482. if (!$system_conf->api_enabled) {
  483. serviceUnavailable();
  484. }
  485. Minz_Session::init('FreshRSS');
  486. $conf = authorizationToUserConf();
  487. $user = $conf == null ? '' : $conf->user;
  488. logMe('User => ' . $user . "\n");
  489. Minz_Session::_param('currentUser', $user);
  490. if (count($pathInfos) < 3) {
  491. badRequest();
  492. }
  493. elseif ($pathInfos[1] === 'accounts') {
  494. if (($pathInfos[2] === 'ClientLogin') && isset($_REQUEST['Email']) && isset($_REQUEST['Passwd'])) {
  495. clientLogin($_REQUEST['Email'], $_REQUEST['Passwd']);
  496. }
  497. }
  498. elseif ($pathInfos[1] === 'reader' && $pathInfos[2] === 'api' && isset($pathInfos[3]) && $pathInfos[3] === '0' && isset($pathInfos[4])) {
  499. if ($user == '') {
  500. unauthorized();
  501. }
  502. $timestamp = isset($_GET['ck']) ? intval($_GET['ck']) : 0; //ck=[unix timestamp] : Use the current Unix time here, helps Google with caching.
  503. switch ($pathInfos[4]) {
  504. case 'stream':
  505. $exclude_target = isset($_GET['xt']) ? $_GET['xt'] : ''; //xt=[exclude target] : Used to exclude certain items from the feed. For example, using xt=user/-/state/com.google/read will exclude items that the current user has marked as read, or xt=feed/[feedurl] will exclude items from a particular feed (obviously not useful in this request, but xt appears in other listing requests).
  506. $count = isset($_GET['n']) ? intval($_GET['n']) : 20; //n=[integer] : The maximum number of results to return.
  507. $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.
  508. $start_time = isset($_GET['ot']) ? intval($_GET['ot']) : 0; //ot=[unix timestamp] : The time from which you want to retrieve items. Only items that have been crawled by Google Reader after this time will be returned.
  509. $continuation = isset($_GET['c']) ? $_GET['c'] : ''; //Continuation token. If a StreamContents response does not represent all items in a timestamp range, it will have a continuation attribute. The same request can be re-issued with the value of that attribute put in this parameter to get more items
  510. if (isset($pathInfos[5]) && $pathInfos[5] === 'contents' && isset($pathInfos[6])) {
  511. if (isset($pathInfos[7])) {
  512. if ($pathInfos[6] === 'feed') {
  513. $include_target = $pathInfos[7];
  514. StreamContents($pathInfos[6], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  515. } elseif ($pathInfos[6] === 'user' && isset($pathInfos[8]) && isset($pathInfos[9])) {
  516. if ($pathInfos[8] === 'state') {
  517. if ($pathInfos[9] === 'com.google' && isset($pathInfos[10])) {
  518. if ($pathInfos[10] === 'reading-list' || $pathInfos[10] === 'starred') {
  519. $include_target = '';
  520. streamContents($pathInfos[10], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  521. }
  522. }
  523. } elseif ($pathInfos[8] === 'label') {
  524. $include_target = $pathInfos[9];
  525. streamContents($pathInfos[8], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  526. }
  527. }
  528. } else { //EasyRSS
  529. $include_target = '';
  530. streamContents('reading-list', $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  531. }
  532. } elseif ($pathInfos[5] === 'items') {
  533. if ($pathInfos[6] === 'ids' && isset($_GET['s'])) {
  534. $streamId = $_GET['s']; //StreamId for which to fetch the item IDs. The parameter may be repeated to fetch the item IDs from multiple streams at once (more efficient from a backend perspective than multiple requests).
  535. streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target);
  536. }
  537. }
  538. break;
  539. case 'tag':
  540. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  541. $output = isset($_GET['output']) ? $_GET['output'] : '';
  542. if ($output !== 'json') notImplemented();
  543. tagList($_GET['output']);
  544. }
  545. break;
  546. case 'subscription':
  547. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  548. $output = isset($_GET['output']) ? $_GET['output'] : '';
  549. if ($output !== 'json') notImplemented();
  550. subscriptionList($_GET['output']);
  551. }
  552. break;
  553. case 'unread-count':
  554. $output = isset($_GET['output']) ? $_GET['output'] : '';
  555. if ($output !== 'json') notImplemented();
  556. $all = isset($_GET['all']) ? $_GET['all'] : '';
  557. unreadCount($all);
  558. break;
  559. case 'edit-tag': //http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/
  560. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  561. checkToken($conf, $token);
  562. $a = isset($_POST['a']) ? $_POST['a'] : ''; //Add: user/-/state/com.google/read user/-/state/com.google/starred
  563. $r = isset($_POST['r']) ? $_POST['r'] : ''; //Remove: user/-/state/com.google/read user/-/state/com.google/starred
  564. $e_ids = multiplePosts('i'); //item IDs
  565. editTag($e_ids, $a, $r);
  566. break;
  567. case 'mark-all-as-read':
  568. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  569. checkToken($conf, $token);
  570. $streamId = $_POST['s']; //StreamId
  571. $ts = isset($_POST['ts']) ? $_POST['ts'] : '0'; //Older than timestamp in nanoseconds
  572. if (!ctype_digit($ts)) {
  573. $ts = '0';
  574. }
  575. markAllAsRead($streamId, $ts);
  576. break;
  577. case 'token':
  578. Token($conf);
  579. break;
  580. }
  581. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  582. checkCompatibility();
  583. }
  584. badRequest();