greader.php 22 KB

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