greader.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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'), 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. default:
  325. $state = FreshRSS_Entry::STATE_ALL;
  326. break;
  327. }
  328. if (!empty($continuation)) {
  329. $count++; //Shift by one element
  330. }
  331. $entryDAO = FreshRSS_Factory::createEntryDao();
  332. $entries = $entryDAO->listWhere($type, $include_target, $state, $order === 'o' ? 'ASC' : 'DESC', $count, $continuation, new FreshRSS_Search(''), $start_time);
  333. $items = array();
  334. foreach ($entries as $entry) {
  335. $f_id = $entry->feed();
  336. if (isset($arrayFeedCategoryNames[$f_id])) {
  337. $c_name = $arrayFeedCategoryNames[$f_id]['c_name'];
  338. $f_name = $arrayFeedCategoryNames[$f_id]['name'];
  339. } else {
  340. $c_name = '_';
  341. $f_name = '_';
  342. }
  343. $item = array(
  344. 'id' => /*'tag:google.com,2005:reader/item/' .*/ dec2hex($entry->id()), //64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId
  345. 'crawlTimeMsec' => substr($entry->id(), 0, -3),
  346. 'timestampUsec' => $entry->id(), //EasyRSS
  347. 'published' => $entry->date(true),
  348. 'title' => $entry->title(),
  349. 'summary' => array('content' => $entry->content()),
  350. 'alternate' => array(
  351. array('href' => $entry->link()),
  352. ),
  353. 'categories' => array(
  354. 'user/-/state/com.google/reading-list',
  355. 'user/-/label/' . $c_name,
  356. ),
  357. 'origin' => array(
  358. 'streamId' => 'feed/' . $f_id,
  359. 'title' => $f_name, //EasyRSS
  360. //'htmlUrl' => $line['f_website'],
  361. ),
  362. );
  363. if ($entry->author() != '') {
  364. $item['author'] = $entry->author();
  365. }
  366. if ($entry->isRead()) {
  367. $item['categories'][] = 'user/-/state/com.google/read';
  368. }
  369. if ($entry->isFavorite()) {
  370. $item['categories'][] = 'user/-/state/com.google/starred';
  371. }
  372. $items[] = $item;
  373. }
  374. if (!empty($continuation)) {
  375. array_shift($items); //Discard first element that was already sent in the previous response
  376. }
  377. $response = array(
  378. 'id' => 'user/-/state/com.google/reading-list',
  379. 'updated' => time(),
  380. 'items' => $items,
  381. );
  382. if ((count($entries) >= $count) && (!empty($entry))) {
  383. $response['continuation'] = $entry->id();
  384. }
  385. echo json_encode($response), "\n";
  386. exit();
  387. }
  388. function streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target) {
  389. //http://code.google.com/p/google-reader-api/wiki/ApiStreamItemsIds
  390. //http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
  391. //http://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2/#feed
  392. //logMe("streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target)");
  393. $type = 'A';
  394. $id = '';
  395. if ($streamId === 'user/-/state/com.google/reading-list') {
  396. $type = 'A';
  397. } elseif ('user/-/state/com.google/starred') {
  398. $type = 's';
  399. } elseif (strpos($streamId, 'feed/') === 0) {
  400. $type = 'f';
  401. $id = basename($streamId);
  402. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  403. $type = 'c';
  404. $c_name = basename($streamId);
  405. $categoryDAO = new FreshRSS_CategoryDAO();
  406. $cat = $categoryDAO->searchByName($c_name);
  407. $id = $cat == null ? -1 : $cat->id();
  408. }
  409. switch ($exclude_target) {
  410. case 'user/-/state/com.google/read':
  411. $state = FreshRSS_Entry::STATE_NOT_READ;
  412. break;
  413. default:
  414. $state = FreshRSS_Entry::STATE_ALL;
  415. break;
  416. }
  417. $entryDAO = FreshRSS_Factory::createEntryDao();
  418. $ids = $entryDAO->listIdsWhere($type, $id, $state, $order === 'o' ? 'ASC' : 'DESC', $count, '', new FreshRSS_Search(''), $start_time);
  419. if (empty($ids)) { //For News+ bug https://github.com/noinnion/newsplus/issues/84#issuecomment-57834632
  420. $ids[] = 0;
  421. }
  422. $itemRefs = array();
  423. foreach ($ids as $id) {
  424. $itemRefs[] = array(
  425. 'id' => $id, //64-bit decimal
  426. );
  427. }
  428. echo json_encode(array(
  429. 'itemRefs' => $itemRefs,
  430. )), "\n";
  431. exit();
  432. }
  433. function editTag($e_ids, $a, $r) {
  434. //logMe("editTag()");
  435. foreach ($e_ids as $i => $e_id) {
  436. $e_ids[$i] = hex2dec(basename($e_id)); //Strip prefix 'tag:google.com,2005:reader/item/'
  437. }
  438. $entryDAO = FreshRSS_Factory::createEntryDao();
  439. switch ($a) {
  440. case 'user/-/state/com.google/read':
  441. $entryDAO->markRead($e_ids, true);
  442. break;
  443. case 'user/-/state/com.google/starred':
  444. $entryDAO->markFavorite($e_ids, true);
  445. break;
  446. /*case 'user/-/state/com.google/tracking-kept-unread':
  447. break;
  448. case 'user/-/state/com.google/like':
  449. break;
  450. case 'user/-/state/com.google/broadcast':
  451. break;*/
  452. }
  453. switch ($r) {
  454. case 'user/-/state/com.google/read':
  455. $entryDAO->markRead($e_ids, false);
  456. break;
  457. case 'user/-/state/com.google/starred':
  458. $entryDAO->markFavorite($e_ids, false);
  459. break;
  460. }
  461. echo 'OK';
  462. exit();
  463. }
  464. function markAllAsRead($streamId, $olderThanId) {
  465. //logMe("markAllAsRead($streamId, $olderThanId)");
  466. $entryDAO = FreshRSS_Factory::createEntryDao();
  467. if (strpos($streamId, 'feed/') === 0) {
  468. $f_id = basename($streamId);
  469. $entryDAO->markReadFeed($f_id, $olderThanId);
  470. } elseif (strpos($streamId, 'user/-/label/') === 0) {
  471. $c_name = basename($streamId);
  472. $categoryDAO = new FreshRSS_CategoryDAO();
  473. $cat = $categoryDAO->searchByName($c_name);
  474. $entryDAO->markReadCat($cat === null ? -1 : $cat->id(), $olderThanId);
  475. } elseif ($streamId === 'user/-/state/com.google/reading-list') {
  476. $entryDAO->markReadEntries($olderThanId, false, -1);
  477. }
  478. echo 'OK';
  479. exit();
  480. }
  481. //logMe('----------------------------------------------------------------');
  482. //logMe(debugInfo());
  483. $pathInfo = empty($_SERVER['PATH_INFO']) ? '/Error' : urldecode($_SERVER['PATH_INFO']);
  484. $pathInfos = explode('/', $pathInfo);
  485. Minz_Configuration::register('system',
  486. DATA_PATH . '/config.php',
  487. DATA_PATH . '/config.default.php');
  488. $system_conf = Minz_Configuration::get('system');
  489. if (!$system_conf->api_enabled) {
  490. serviceUnavailable();
  491. }
  492. Minz_Session::init('FreshRSS');
  493. $user = authorizationToUser();
  494. $conf = null;
  495. if ($user !== '') {
  496. $conf = get_user_configuration($user);
  497. }
  498. //logMe('User => ' . $user);
  499. Minz_Session::_param('currentUser', $user);
  500. if (count($pathInfos) < 3) {
  501. badRequest();
  502. }
  503. elseif ($pathInfos[1] === 'accounts') {
  504. if (($pathInfos[2] === 'ClientLogin') && isset($_REQUEST['Email']) && isset($_REQUEST['Passwd'])) {
  505. clientLogin($_REQUEST['Email'], $_REQUEST['Passwd']);
  506. }
  507. }
  508. elseif ($pathInfos[1] === 'reader' && $pathInfos[2] === 'api' && isset($pathInfos[3]) && $pathInfos[3] === '0' && isset($pathInfos[4])) {
  509. if ($user == '') {
  510. unauthorized();
  511. }
  512. $timestamp = isset($_GET['ck']) ? intval($_GET['ck']) : 0; //ck=[unix timestamp] : Use the current Unix time here, helps Google with caching.
  513. switch ($pathInfos[4]) {
  514. case 'stream':
  515. $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).
  516. $count = isset($_GET['n']) ? intval($_GET['n']) : 20; //n=[integer] : The maximum number of results to return.
  517. $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.
  518. $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.
  519. $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
  520. if (isset($pathInfos[5]) && $pathInfos[5] === 'contents' && isset($pathInfos[6])) {
  521. if (isset($pathInfos[7])) {
  522. if ($pathInfos[6] === 'feed') {
  523. $include_target = $pathInfos[7];
  524. StreamContents($pathInfos[6], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  525. } elseif ($pathInfos[6] === 'user' && isset($pathInfos[8]) && isset($pathInfos[9])) {
  526. if ($pathInfos[8] === 'state') {
  527. if ($pathInfos[9] === 'com.google' && isset($pathInfos[10])) {
  528. if ($pathInfos[10] === 'reading-list' || $pathInfos[10] === 'starred') {
  529. $include_target = '';
  530. streamContents($pathInfos[10], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  531. }
  532. }
  533. } elseif ($pathInfos[8] === 'label') {
  534. $include_target = $pathInfos[9];
  535. streamContents($pathInfos[8], $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  536. }
  537. }
  538. } else { //EasyRSS
  539. $include_target = '';
  540. streamContents('reading-list', $include_target, $start_time, $count, $order, $exclude_target, $continuation);
  541. }
  542. } elseif ($pathInfos[5] === 'items') {
  543. if ($pathInfos[6] === 'ids' && isset($_GET['s'])) {
  544. $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).
  545. streamContentsItemsIds($streamId, $start_time, $count, $order, $exclude_target);
  546. }
  547. }
  548. break;
  549. case 'tag':
  550. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  551. $output = isset($_GET['output']) ? $_GET['output'] : '';
  552. if ($output !== 'json') notImplemented();
  553. tagList($_GET['output']);
  554. }
  555. break;
  556. case 'subscription':
  557. if (isset($pathInfos[5]) && $pathInfos[5] === 'list') {
  558. $output = isset($_GET['output']) ? $_GET['output'] : '';
  559. if ($output !== 'json') notImplemented();
  560. subscriptionList($_GET['output']);
  561. }
  562. break;
  563. case 'unread-count':
  564. $output = isset($_GET['output']) ? $_GET['output'] : '';
  565. if ($output !== 'json') notImplemented();
  566. $all = isset($_GET['all']) ? $_GET['all'] : '';
  567. unreadCount($all);
  568. break;
  569. case 'edit-tag': //http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/
  570. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  571. checkToken($conf, $token);
  572. $a = isset($_POST['a']) ? $_POST['a'] : ''; //Add: user/-/state/com.google/read user/-/state/com.google/starred
  573. $r = isset($_POST['r']) ? $_POST['r'] : ''; //Remove: user/-/state/com.google/read user/-/state/com.google/starred
  574. $e_ids = multiplePosts('i'); //item IDs
  575. editTag($e_ids, $a, $r);
  576. break;
  577. case 'mark-all-as-read':
  578. $token = isset($_POST['T']) ? trim($_POST['T']) : '';
  579. checkToken($conf, $token);
  580. $streamId = $_POST['s']; //StreamId
  581. $ts = isset($_POST['ts']) ? $_POST['ts'] : '0'; //Older than timestamp in nanoseconds
  582. if (!ctype_digit($ts)) {
  583. $ts = '0';
  584. }
  585. markAllAsRead($streamId, $ts);
  586. break;
  587. case 'token':
  588. token($conf);
  589. break;
  590. }
  591. } elseif ($pathInfos[1] === 'check' && $pathInfos[2] === 'compatibility') {
  592. checkCompatibility();
  593. }
  594. badRequest();