greader.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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', false, null, -1, 1048576);
  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'), date('c') . "\t" . $text . "\n", 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()");
  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()");
  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()");
  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()");
  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()");
  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 authorizationToUser() {
  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. Minz_Log::warning('Invalid API user ' . $user . ': configuration cannot be found.');
  140. unauthorized();
  141. }
  142. $system_conf = Minz_Configuration::get('system');
  143. if ($headerAuthX[1] === sha1($system_conf->salt . $user . $conf->apiPasswordHash)) {
  144. return $user;
  145. } else {
  146. logMe('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1]);
  147. Minz_Log::warning('Invalid API authorisation for user ' . $user . ': ' . $headerAuthX[1]);
  148. unauthorized();
  149. }
  150. } else {
  151. badRequest();
  152. }
  153. }
  154. }
  155. return '';
  156. }
  157. function clientLogin($email, $pass) { //http://web.archive.org/web/20130604091042/http://undoc.in/clientLogin.html
  158. //logMe('clientLogin(' . $email . ")");
  159. if (ctype_alnum($email)) {
  160. if (!function_exists('password_verify')) {
  161. include_once(LIB_PATH . '/password_compat.php');
  162. }
  163. $conf = get_user_configuration($email);
  164. if (is_null($conf)) {
  165. Minz_Log::warning('Invalid API user ' . $email . ': configuration cannot be found.');
  166. unauthorized();
  167. }
  168. if ($conf->apiPasswordHash != '' && password_verify($pass, $conf->apiPasswordHash)) {
  169. header('Content-Type: text/plain; charset=UTF-8');
  170. $system_conf = Minz_Configuration::get('system');
  171. $auth = $email . '/' . sha1($system_conf->salt . $email . $conf->apiPasswordHash);
  172. echo 'SID=', $auth, "\n",
  173. 'Auth=', $auth, "\n";
  174. exit();
  175. } else {
  176. Minz_Log::warning('Password API mismatch for user ' . $email);
  177. unauthorized();
  178. }
  179. } else {
  180. badRequest();
  181. }
  182. die();
  183. }
  184. function token($conf) {
  185. //http://blog.martindoms.com/2009/08/15/using-the-google-reader-api-part-1/
  186. //https://github.com/ericmann/gReader-Library/blob/master/greader.class.php
  187. $user = Minz_Session::param('currentUser', '_');
  188. //logMe('token('. $user . ")"); //TODO: Implement real token that expires
  189. $system_conf = Minz_Configuration::get('system');
  190. $token = str_pad(sha1($system_conf->salt . $user . $conf->apiPasswordHash), 57, 'Z'); //Must have 57 characters
  191. echo $token, "\n";
  192. exit();
  193. }
  194. function checkToken($conf, $token) {
  195. //http://code.google.com/p/google-reader-api/wiki/ActionToken
  196. $user = Minz_Session::param('currentUser', '_');
  197. //logMe('checkToken(' . $token . ")");
  198. $system_conf = Minz_Configuration::get('system');
  199. if ($token === str_pad(sha1($system_conf->salt . $user . $conf->apiPasswordHash), 57, 'Z')) {
  200. return true;
  201. }
  202. unauthorized();
  203. }
  204. function tagList() {
  205. //logMe("tagList()");
  206. header('Content-Type: application/json; charset=UTF-8');
  207. $pdo = new MyPDO();
  208. $stm = $pdo->prepare('SELECT c.name FROM `%_category` c');
  209. $stm->execute();
  210. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  211. $tags = array(
  212. array('id' => 'user/-/state/com.google/starred'),
  213. //array('id' => 'user/-/state/com.google/broadcast', 'sortid' => '2'),
  214. );
  215. foreach ($res as $cName) {
  216. $tags[] = array(
  217. 'id' => 'user/-/label/' . $cName,
  218. //'sortid' => $cName,
  219. );
  220. }
  221. echo json_encode(array('tags' => $tags)), "\n";
  222. exit();
  223. }
  224. function subscriptionList() {
  225. //logMe("subscriptionList()");
  226. header('Content-Type: application/json; charset=UTF-8');
  227. $pdo = new MyPDO();
  228. $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
  229. INNER JOIN `%_category` c ON c.id = f.category');
  230. $stm->execute();
  231. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  232. $subscriptions = array();
  233. foreach ($res as $line) {
  234. $subscriptions[] = array(
  235. 'id' => 'feed/' . $line['id'],
  236. 'title' => $line['name'],
  237. 'categories' => array(
  238. array(
  239. 'id' => 'user/-/label/' . $line['c_name'],
  240. 'label' => $line['c_name'],
  241. ),
  242. ),
  243. //'sortid' => $line['name'],
  244. //'firstitemmsec' => 0,
  245. 'url' => $line['url'],
  246. 'htmlUrl' => $line['website'],
  247. //'iconUrl' => '',
  248. );
  249. }
  250. echo json_encode(array('subscriptions' => $subscriptions)), "\n";
  251. exit();
  252. }
  253. function unreadCount() { //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#unread-count
  254. //logMe("unreadCount()");
  255. header('Content-Type: application/json; charset=UTF-8');
  256. $totalUnreads = 0;
  257. $totalLastUpdate = 0;
  258. $categoryDAO = new FreshRSS_CategoryDAO();
  259. foreach ($categoryDAO->listCategories(true, true) as $cat) {
  260. $catLastUpdate = 0;
  261. foreach ($cat->feeds() as $feed) {
  262. $lastUpdate = $feed->lastUpdate();
  263. $unreadcounts[] = array(
  264. 'id' => 'feed/' . $feed->id(),
  265. 'count' => $feed->nbNotRead(),
  266. 'newestItemTimestampUsec' => $lastUpdate . '000000',
  267. );
  268. if ($catLastUpdate < $lastUpdate) {
  269. $catLastUpdate = $lastUpdate;
  270. }
  271. }
  272. $unreadcounts[] = array(
  273. 'id' => 'user/-/label/' . $cat->name(),
  274. 'count' => $cat->nbNotRead(),
  275. 'newestItemTimestampUsec' => $catLastUpdate . '000000',
  276. );
  277. $totalUnreads += $cat->nbNotRead();
  278. if ($totalLastUpdate < $catLastUpdate) {
  279. $totalLastUpdate = $catLastUpdate;
  280. }
  281. }
  282. $unreadcounts[] = array(
  283. 'id' => 'user/-/state/com.google/reading-list',
  284. 'count' => $totalUnreads,
  285. 'newestItemTimestampUsec' => $totalLastUpdate . '000000',
  286. );
  287. echo json_encode(array(
  288. 'max' => $totalUnreads,
  289. 'unreadcounts' => $unreadcounts,
  290. )), "\n";
  291. exit();
  292. }
  293. function streamContents($path, $include_target, $start_time, $count, $order, $exclude_target, $continuation) {
  294. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  295. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  296. //logMe("streamContents($path, $include_target, $start_time, $count, $order, $exclude_target, $continuation)");
  297. header('Content-Type: application/json; charset=UTF-8');
  298. $feedDAO = FreshRSS_Factory::createFeedDao();
  299. $arrayFeedCategoryNames = $feedDAO->arrayFeedCategoryNames();
  300. switch ($path) {
  301. case 'reading-list':
  302. $type = 'A';
  303. break;
  304. case 'starred':
  305. $type = 's';
  306. break;
  307. case 'feed':
  308. $type = 'f';
  309. break;
  310. case 'label':
  311. $type = 'c';
  312. $categoryDAO = new FreshRSS_CategoryDAO();
  313. $cat = $categoryDAO->searchByName($include_target);
  314. $include_target = $cat == null ? -1 : $cat->id();
  315. break;
  316. default:
  317. $type = 'A';
  318. break;
  319. }
  320. switch ($exclude_target) {
  321. case 'user/-/state/com.google/read':
  322. $state = FreshRSS_Entry::STATE_NOT_READ;
  323. break;
  324. case 'user/-/state/com.google/unread':
  325. $state = FreshRSS_Entry::STATE_READ;
  326. break;
  327. default:
  328. $state = FreshRSS_Entry::STATE_ALL;
  329. break;
  330. }
  331. if (!empty($continuation)) {
  332. $count++; //Shift by one element
  333. }
  334. $entryDAO = FreshRSS_Factory::createEntryDao();
  335. $entries = $entryDAO->listWhere($type, $include_target, $state, $order === 'o' ? 'ASC' : 'DESC', $count, $continuation, new FreshRSS_Search(''), $start_time);
  336. $items = array();
  337. foreach ($entries as $entry) {
  338. $f_id = $entry->feed();
  339. if (isset($arrayFeedCategoryNames[$f_id])) {
  340. $c_name = $arrayFeedCategoryNames[$f_id]['c_name'];
  341. $f_name = $arrayFeedCategoryNames[$f_id]['name'];
  342. } else {
  343. $c_name = '_';
  344. $f_name = '_';
  345. }
  346. $item = array(
  347. 'id' => /*'tag:google.com,2005:reader/item/' .*/ dec2hex($entry->id()), //64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId
  348. 'crawlTimeMsec' => substr($entry->id(), 0, -3),
  349. 'timestampUsec' => $entry->id(), //EasyRSS
  350. 'published' => $entry->date(true),
  351. 'title' => $entry->title(),
  352. 'summary' => array('content' => $entry->content()),
  353. 'alternate' => array(
  354. array('href' => $entry->link()),
  355. ),
  356. 'categories' => array(
  357. 'user/-/state/com.google/reading-list',
  358. 'user/-/label/' . $c_name,
  359. ),
  360. 'origin' => array(
  361. 'streamId' => 'feed/' . $f_id,
  362. 'title' => $f_name, //EasyRSS
  363. //'htmlUrl' => $line['f_website'],
  364. ),
  365. );
  366. if ($entry->author() != '') {
  367. $item['author'] = $entry->author();
  368. }
  369. if ($entry->isRead()) {
  370. $item['categories'][] = 'user/-/state/com.google/read';
  371. }
  372. if ($entry->isFavorite()) {
  373. $item['categories'][] = 'user/-/state/com.google/starred';
  374. }
  375. $items[] = $item;
  376. }
  377. if (!empty($continuation)) {
  378. array_shift($items); //Discard first element that was already sent in the previous response
  379. }
  380. $response = array(
  381. 'id' => 'user/-/state/com.google/reading-list',
  382. 'updated' => time(),
  383. 'items' => $items,
  384. );
  385. if ((count($entries) >= $count) && (!empty($entry))) {
  386. $response['continuation'] = $entry->id();
  387. }
  388. echo json_encode($response), "\n";
  389. exit();
  390. }
  391. function streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target) {
  392. //http://code.google.com/p/google-reader-api/wiki/ApiStreamItemsIds
  393. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  394. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  395. //logMe("streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target)");
  396. $type = 'A';
  397. $id = '';
  398. if ($streamId === 'user/-/state/com.google/reading-list') {
  399. $type = 'A';
  400. } elseif ('user/-/state/com.google/starred') {
  401. $type = 's';
  402. } elseif (strpos($streamId, 'feed/') === 0) {
  403. $type = 'f';
  404. $id = basename($streamId);
  405. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  406. $type = 'c';
  407. $c_name = basename($streamId);
  408. $categoryDAO = new FreshRSS_CategoryDAO();
  409. $cat = $categoryDAO->searchByName($c_name);
  410. $id = $cat == null ? -1 : $cat->id();
  411. }
  412. switch ($exclude_target) {
  413. case 'user/-/state/com.google/read':
  414. $state = FreshRSS_Entry::STATE_NOT_READ;
  415. break;
  416. default:
  417. $state = FreshRSS_Entry::STATE_ALL;
  418. break;
  419. }
  420. $entryDAO = FreshRSS_Factory::createEntryDao();
  421. $ids = $entryDAO->listIdsWhere($type, $id, $state, $order === 'o' ? 'ASC' : 'DESC', $count, '', new FreshRSS_Search(''), $start_time);
  422. if (empty($ids)) { //For News+ bug https://github.com/noinnion/newsplus/issues/84#issuecomment-57834632
  423. $ids[] = 0;
  424. }
  425. $itemRefs = array();
  426. foreach ($ids as $id) {
  427. $itemRefs[] = array(
  428. 'id' => $id, //64-bit decimal
  429. );
  430. }
  431. echo json_encode(array(
  432. 'itemRefs' => $itemRefs,
  433. )), "\n";
  434. exit();
  435. }
  436. function editTag($e_ids, $a, $r) {
  437. //logMe("editTag()");
  438. foreach ($e_ids as $i => $e_id) {
  439. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  440. }
  441. $entryDAO = FreshRSS_Factory::createEntryDao();
  442. switch ($a) {
  443. case 'user/-/state/com.google/read':
  444. $entryDAO->markRead($e_ids, true);
  445. break;
  446. case 'user/-/state/com.google/starred':
  447. $entryDAO->markFavorite($e_ids, true);
  448. break;
  449. /*case 'user/-/state/com.google/tracking-kept-unread':
  450. break;
  451. case 'user/-/state/com.google/like':
  452. break;
  453. case 'user/-/state/com.google/broadcast':
  454. break;*/
  455. }
  456. switch ($r) {
  457. case 'user/-/state/com.google/read':
  458. $entryDAO->markRead($e_ids, false);
  459. break;
  460. case 'user/-/state/com.google/starred':
  461. $entryDAO->markFavorite($e_ids, false);
  462. break;
  463. }
  464. echo 'OK';
  465. exit();
  466. }
  467. function markAllAsRead($streamId, $olderThanId) {
  468. //logMe("markAllAsRead($streamId, $olderThanId)");
  469. $entryDAO = FreshRSS_Factory::createEntryDao();
  470. if (strpos($streamId, 'feed/') === 0) {
  471. $f_id = basename($streamId);
  472. $entryDAO->markReadFeed($f_id, $olderThanId);
  473. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  474. $c_name = basename($streamId);
  475. $categoryDAO = new FreshRSS_CategoryDAO();
  476. $cat = $categoryDAO->searchByName($c_name);
  477. $entryDAO->markReadCat($cat === null ? -1 : $cat->id(), $olderThanId);
  478. } elseif ($streamId === 'user/-/state/com.google/reading-list') {
  479. $entryDAO->markReadEntries($olderThanId, false, -1);
  480. }
  481. echo 'OK';
  482. exit();
  483. }
  484. //logMe('----------------------------------------------------------------');
  485. //logMe(debugInfo());
  486. $pathInfo = empty($_SERVER['PATH_INFO']) ? '/Error' : urldecode($_SERVER['PATH_INFO']);
  487. $pathInfos = explode('/', $pathInfo);
  488. Minz_Configuration::register('system',
  489. DATA_PATH . '/config.php',
  490. DATA_PATH . '/config.default.php');
  491. $system_conf = Minz_Configuration::get('system');
  492. if (!$system_conf->api_enabled) {
  493. serviceUnavailable();
  494. }
  495. Minz_Session::init('FreshRSS');
  496. $user = authorizationToUser();
  497. $conf = null;
  498. if ($user !== '') {
  499. $conf = get_user_configuration($user);
  500. }
  501. //logMe('User => ' . $user);
  502. Minz_Session::_param('currentUser', $user);
  503. if (count($pathInfos) < 3) {
  504. badRequest();
  505. }
  506. elseif ($pathInfos[1] === 'accounts') {
  507. if (($pathInfos[2] === 'ClientLogin') && isset($_REQUEST['Email']) && isset($_REQUEST['Passwd'])) {
  508. clientLogin($_REQUEST['Email'], $_REQUEST['Passwd']);
  509. }
  510. }
  511. elseif ($pathInfos[1] === 'reader' && $pathInfos[2] === 'api' && isset($pathInfos[3]) && $pathInfos[3] === '0' && isset($pathInfos[4])) {
  512. if ($user == '') {
  513. unauthorized();
  514. }
  515. $timestamp = isset($_GET['ck']) ? intval($_GET['ck']) : 0; //ck=[unix timestamp] : Use the current Unix time here, helps Google with caching.
  516. switch ($pathInfos[4]) {
  517. case 'stream':
  518. $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).
  519. $count = isset($_GET['n']) ? intval($_GET['n']) : 20; //n=[integer] : The maximum number of results to return.
  520. $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.
  521. $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.
  522. $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
  523. if (isset($pathInfos[5]) && $pathInfos[5] === 'contents' && isset($pathInfos[6])) {
  524. if (isset($pathInfos[7])) {
  525. if ($pathInfos[6] === 'feed') {
  526. $include_target = $pathInfos[7];
  527. StreamContents($pathInfos[6], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  528. } elseif ($pathInfos[6] === 'user' && isset($pathInfos[8]) && isset($pathInfos[9])) {
  529. if ($pathInfos[8] === 'state') {
  530. if ($pathInfos[9] === 'com.google' && isset($pathInfos[10])) {
  531. if ($pathInfos[10] === 'reading-list' || $pathInfos[10] === 'starred') {
  532. $include_target = '';
  533. streamContents($pathInfos[10], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  534. }
  535. }
  536. } elseif ($pathInfos[8] === 'label') {
  537. $include_target = $pathInfos[9];
  538. streamContents($pathInfos[8], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  539. }
  540. }
  541. } else { //EasyRSS
  542. $include_target = '';
  543. streamContents('reading-list', $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  544. }
  545. } elseif ($pathInfos[5] === 'items') {
  546. if ($pathInfos[6] === 'ids' && isset($_GET['s'])) {
  547. $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).
  548. streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target);
  549. }
  550. }
  551. break;
  552. case 'tag':
  553. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  554. $output = isset($_GET['output']) ? $_GET['output'] : '';
  555. if ($output !== 'json') notImplemented();
  556. tagList($_GET['output']);
  557. }
  558. break;
  559. case 'subscription':
  560. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  561. $output = isset($_GET['output']) ? $_GET['output'] : '';
  562. if ($output !== 'json') notImplemented();
  563. subscriptionList($_GET['output']);
  564. }
  565. break;
  566. case 'unread-count':
  567. $output = isset($_GET['output']) ? $_GET['output'] : '';
  568. if ($output !== 'json') notImplemented();
  569. $all = isset($_GET['all']) ? $_GET['all'] : '';
  570. unreadCount($all);
  571. break;
  572. case 'edit-tag': //http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/
  573. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  574. checkToken($conf, $token);
  575. $a = isset($_POST['a']) ? $_POST['a'] : ''; //Add: user/-/state/com.google/read user/-/state/com.google/starred
  576. $r = isset($_POST['r']) ? $_POST['r'] : ''; //Remove: user/-/state/com.google/read user/-/state/com.google/starred
  577. $e_ids = multiplePosts('i'); //item IDs
  578. editTag($e_ids, $a, $r);
  579. break;
  580. case 'mark-all-as-read':
  581. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  582. checkToken($conf, $token);
  583. $streamId = $_POST['s']; //StreamId
  584. $ts = isset($_POST['ts']) ? $_POST['ts'] : '0'; //Older than timestamp in nanoseconds
  585. if (!ctype_digit($ts)) {
  586. $ts = '0';
  587. }
  588. markAllAsRead($streamId, $ts);
  589. break;
  590. case 'token':
  591. token($conf);
  592. break;
  593. }
  594. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  595. checkCompatibility();
  596. }
  597. badRequest();