You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

526 lines
15 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. <?php
  2. function require_login(&$app, $redirect=true) {
  3. $params = $app->request()->params();
  4. if(array_key_exists('token', $params)) {
  5. try {
  6. $data = JWT::decode($params['token'], Config::$jwtSecret);
  7. $_SESSION['user_id'] = $data->user_id;
  8. $_SESSION['me'] = $data->me;
  9. } catch(DomainException $e) {
  10. if($redirect) {
  11. header('X-Error: DomainException');
  12. $app->redirect('/', 301);
  13. } else {
  14. return false;
  15. }
  16. } catch(UnexpectedValueException $e) {
  17. if($redirect) {
  18. header('X-Error: UnexpectedValueException');
  19. $app->redirect('/', 301);
  20. } else {
  21. return false;
  22. }
  23. }
  24. }
  25. if(!array_key_exists('user_id', $_SESSION)) {
  26. if($redirect)
  27. $app->redirect('/');
  28. return false;
  29. } else {
  30. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  31. }
  32. }
  33. function generate_login_token() {
  34. return JWT::encode(array(
  35. 'user_id' => $_SESSION['user_id'],
  36. 'me' => $_SESSION['me'],
  37. 'created_at' => time()
  38. ), Config::$jwtSecret);
  39. }
  40. $app->get('/new', function() use($app) {
  41. if($user=require_login($app)) {
  42. $params = $app->request()->params();
  43. $entry = false;
  44. $photo_url = false;
  45. $in_reply_to = '';
  46. if(array_key_exists('reply', $params))
  47. $in_reply_to = $params['reply'];
  48. $test_response = '';
  49. if($user->last_micropub_response) {
  50. try {
  51. if(@json_decode($user->last_micropub_response)) {
  52. $d = json_decode($user->last_micropub_response);
  53. $test_response = $d->response;
  54. }
  55. } catch(Exception $e) {
  56. }
  57. }
  58. $html = render('new-post', array(
  59. 'title' => 'New Post',
  60. 'in_reply_to' => $in_reply_to,
  61. 'micropub_endpoint' => $user->micropub_endpoint,
  62. 'micropub_scope' => $user->micropub_scope,
  63. 'micropub_access_token' => $user->micropub_access_token,
  64. 'response_date' => $user->last_micropub_response_date,
  65. 'syndication_targets' => json_decode($user->syndication_targets, true),
  66. 'test_response' => $test_response,
  67. 'location_enabled' => $user->location_enabled,
  68. 'authorizing' => false
  69. ));
  70. $app->response()->body($html);
  71. }
  72. });
  73. $app->get('/bookmark', function() use($app) {
  74. if($user=require_login($app)) {
  75. $params = $app->request()->params();
  76. $url = '';
  77. $name = '';
  78. $content = '';
  79. $tags = '';
  80. if(array_key_exists('url', $params))
  81. $url = $params['url'];
  82. if(array_key_exists('name', $params))
  83. $name = $params['name'];
  84. if(array_key_exists('content', $params))
  85. $content = $params['content'];
  86. $html = render('new-bookmark', array(
  87. 'title' => 'New Bookmark',
  88. 'bookmark_url' => $url,
  89. 'bookmark_name' => $name,
  90. 'bookmark_content' => $content,
  91. 'bookmark_tags' => $tags,
  92. 'token' => generate_login_token(),
  93. 'syndication_targets' => json_decode($user->syndication_targets, true),
  94. 'authorizing' => false
  95. ));
  96. $app->response()->body($html);
  97. }
  98. });
  99. $app->get('/favorite', function() use($app) {
  100. if($user=require_login($app)) {
  101. $params = $app->request()->params();
  102. $url = '';
  103. if(array_key_exists('url', $params))
  104. $url = $params['url'];
  105. $html = render('new-favorite', array(
  106. 'title' => 'New Favorite',
  107. 'url' => $url,
  108. 'token' => generate_login_token(),
  109. 'authorizing' => false
  110. ));
  111. $app->response()->body($html);
  112. }
  113. });
  114. $app->get('/repost', function() use($app) {
  115. if($user=require_login($app)) {
  116. $params = $app->request()->params();
  117. $url = '';
  118. if(array_key_exists('url', $params))
  119. $url = $params['url'];
  120. $html = render('new-repost', array(
  121. 'title' => 'New Repost',
  122. 'url' => $url,
  123. 'token' => generate_login_token(),
  124. 'authorizing' => false
  125. ));
  126. $app->response()->body($html);
  127. }
  128. });
  129. $app->post('/prefs', function() use($app) {
  130. if($user=require_login($app)) {
  131. $params = $app->request()->params();
  132. $user->location_enabled = $params['enabled'];
  133. $user->save();
  134. }
  135. $app->response()->body(json_encode(array(
  136. 'result' => 'ok'
  137. )));
  138. });
  139. $app->get('/creating-a-token-endpoint', function() use($app) {
  140. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  141. });
  142. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  143. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint', 'authorizing' => false));
  144. $app->response()->body($html);
  145. });
  146. $app->get('/docs', function() use($app) {
  147. $html = render('docs', array('title' => 'Documentation', 'authorizing' => false));
  148. $app->response()->body($html);
  149. });
  150. $app->get('/privacy', function() use($app) {
  151. $html = render('privacy', array('title' => 'Quill Privacy Policy', 'authorizing' => false));
  152. $app->response()->body($html);
  153. });
  154. $app->get('/add-to-home', function() use($app) {
  155. $params = $app->request()->params();
  156. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  157. // Verify the token and sign the user in
  158. try {
  159. $data = JWT::decode($params['token'], Config::$jwtSecret);
  160. $_SESSION['user_id'] = $data->user_id;
  161. $_SESSION['me'] = $data->me;
  162. $app->redirect('/new', 301);
  163. } catch(DomainException $e) {
  164. header('X-Error: DomainException');
  165. $app->redirect('/', 301);
  166. } catch(UnexpectedValueException $e) {
  167. header('X-Error: UnexpectedValueException');
  168. $app->redirect('/', 301);
  169. }
  170. } else {
  171. if($user=require_login($app)) {
  172. if(array_key_exists('start', $params)) {
  173. $_SESSION['add-to-home-started'] = true;
  174. $token = JWT::encode(array(
  175. 'user_id' => $_SESSION['user_id'],
  176. 'me' => $_SESSION['me'],
  177. 'created_at' => time()
  178. ), Config::$jwtSecret);
  179. $app->redirect('/add-to-home?token='.$token, 301);
  180. } else {
  181. unset($_SESSION['add-to-home-started']);
  182. $html = render('add-to-home', array('title' => 'Quill'));
  183. $app->response()->body($html);
  184. }
  185. }
  186. }
  187. });
  188. $app->get('/settings', function() use($app) {
  189. if($user=require_login($app)) {
  190. $html = render('settings', array('title' => 'Settings', 'include_facebook' => true, 'authorizing' => false));
  191. $app->response()->body($html);
  192. }
  193. });
  194. $app->get('/favorite-popup', function() use($app) {
  195. if($user=require_login($app)) {
  196. $params = $app->request()->params();
  197. $html = $app->render('favorite-popup.php', array(
  198. 'url' => $params['url'],
  199. 'token' => $params['token']
  200. ));
  201. $app->response()->body($html);
  202. }
  203. });
  204. function create_favorite(&$user, $url) {
  205. $micropub_request = array(
  206. 'like-of' => $url
  207. );
  208. $r = micropub_post_for_user($user, $micropub_request);
  209. $facebook_id = false;
  210. $instagram_id = false;
  211. $tweet_id = false;
  212. /*
  213. // Facebook likes are posted via Javascript, so pass the FB ID to the javascript code
  214. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/(?:[^\/]+)\/posts\/(\d+)/', $url, $match)) {
  215. $facebook_id = $match[1];
  216. }
  217. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/photo\.php\?fbid=(\d+)/', $url, $match)) {
  218. $facebook_id = $match[1];
  219. }
  220. */
  221. if(preg_match('/https?:\/\/(?:www\.)?instagram\.com\/p\/([^\/]+)/', $url, $match)) {
  222. $instagram_id = $match[1];
  223. if($user->instagram_access_token) {
  224. $instagram = instagram_client();
  225. $instagram->setAccessToken($user->instagram_access_token);
  226. $ch = curl_init('https://api.instagram.com/v1/media/shortcode/' . $instagram_id . '?access_token=' . $user->instagram_access_token);
  227. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  228. $result = json_decode(curl_exec($ch));
  229. $result = $instagram->likeMedia($result->data->id);
  230. } else {
  231. // TODO: indicate that the instagram post couldn't be liked because no access token was available
  232. }
  233. }
  234. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  235. $tweet_id = $match[1];
  236. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  237. $user->twitter_access_token, $user->twitter_token_secret);
  238. $result = $twitter->post('favorites/create', array(
  239. 'id' => $tweet_id
  240. ));
  241. }
  242. return $r;
  243. }
  244. function create_repost(&$user, $url) {
  245. $micropub_request = array(
  246. 'repost-of' => $url
  247. );
  248. $r = micropub_post_for_user($user, $micropub_request);
  249. $tweet_id = false;
  250. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  251. $tweet_id = $match[1];
  252. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  253. $user->twitter_access_token, $user->twitter_token_secret);
  254. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  255. }
  256. return $r;
  257. }
  258. $app->get('/favorite.js', function() use($app) {
  259. $app->response()->header("Content-type", "text/javascript");
  260. if($user=require_login($app, false)) {
  261. $params = $app->request()->params();
  262. if(array_key_exists('url', $params)) {
  263. $r = create_favorite($user, $params['url']);
  264. $app->response()->body($app->render('favorite-js.php', array(
  265. 'url' => $params['url'],
  266. 'like_url' => $r['location'],
  267. 'error' => $r['error'],
  268. // 'facebook_id' => $facebook_id
  269. )));
  270. } else {
  271. $app->response()->body('alert("no url");');
  272. }
  273. } else {
  274. $app->response()->body('alert("invalid token");');
  275. }
  276. });
  277. $app->post('/favorite', function() use($app) {
  278. if($user=require_login($app)) {
  279. $params = $app->request()->params();
  280. $r = create_favorite($user, $params['url']);
  281. $app->response()->body(json_encode(array(
  282. 'location' => $r['location'],
  283. 'error' => $r['error']
  284. )));
  285. }
  286. });
  287. $app->post('/repost', function() use($app) {
  288. if($user=require_login($app)) {
  289. $params = $app->request()->params();
  290. $r = create_repost($user, $params['url']);
  291. $app->response()->body(json_encode(array(
  292. 'location' => $r['location'],
  293. 'error' => $r['error']
  294. )));
  295. }
  296. });
  297. $app->get('/micropub/syndications', function() use($app) {
  298. if($user=require_login($app)) {
  299. $data = get_syndication_targets($user);
  300. $app->response()->body(json_encode(array(
  301. 'targets' => $data['targets'],
  302. 'response' => $data['response']
  303. )));
  304. }
  305. });
  306. $app->post('/micropub/post', function() use($app) {
  307. if($user=require_login($app)) {
  308. $params = $app->request()->params();
  309. // Remove any blank params
  310. $params = array_filter($params, function($v){
  311. return $v !== '';
  312. });
  313. $r = micropub_post_for_user($user, $params);
  314. $app->response()->body(json_encode(array(
  315. 'request' => htmlspecialchars($r['request']),
  316. 'response' => htmlspecialchars($r['response']),
  317. 'location' => $r['location'],
  318. 'error' => $r['error'],
  319. 'curlinfo' => $r['curlinfo']
  320. )));
  321. }
  322. });
  323. /*
  324. $app->post('/auth/facebook', function() use($app) {
  325. if($user=require_login($app, false)) {
  326. $params = $app->request()->params();
  327. // User just auth'd with facebook, store the access token
  328. $user->facebook_access_token = $params['fb_token'];
  329. $user->save();
  330. $app->response()->body(json_encode(array(
  331. 'result' => 'ok'
  332. )));
  333. } else {
  334. $app->response()->body(json_encode(array(
  335. 'result' => 'error'
  336. )));
  337. }
  338. });
  339. */
  340. $app->post('/auth/twitter', function() use($app) {
  341. if($user=require_login($app, false)) {
  342. $params = $app->request()->params();
  343. // User just auth'd with twitter, store the access token
  344. $user->twitter_access_token = $params['twitter_token'];
  345. $user->twitter_token_secret = $params['twitter_secret'];
  346. $user->save();
  347. $app->response()->body(json_encode(array(
  348. 'result' => 'ok'
  349. )));
  350. } else {
  351. $app->response()->body(json_encode(array(
  352. 'result' => 'error'
  353. )));
  354. }
  355. });
  356. function getTwitterLoginURL(&$twitter) {
  357. $request_token = $twitter->getRequestToken(Config::$base_url . 'auth/twitter/callback');
  358. $_SESSION['twitter_auth'] = $request_token;
  359. return $twitter->getAuthorizeURL($request_token['oauth_token']);
  360. }
  361. $app->get('/auth/twitter', function() use($app) {
  362. $params = $app->request()->params();
  363. if($user=require_login($app, false)) {
  364. // If there is an existing Twitter token, check if it is valid
  365. // Otherwise, generate a Twitter login link
  366. $twitter_login_url = false;
  367. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  368. $user->twitter_access_token, $user->twitter_token_secret);
  369. if(array_key_exists('login', $params)) {
  370. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret);
  371. $twitter_login_url = getTwitterLoginURL($twitter);
  372. } else {
  373. if($user->twitter_access_token) {
  374. if ($twitter->get('account/verify_credentials')) {
  375. $app->response()->body(json_encode(array(
  376. 'result' => 'ok'
  377. )));
  378. return;
  379. } else {
  380. // If the existing twitter token is not valid, generate a login link
  381. $twitter_login_url = getTwitterLoginURL($twitter);
  382. }
  383. } else {
  384. $twitter_login_url = getTwitterLoginURL($twitter);
  385. }
  386. }
  387. $app->response()->body(json_encode(array(
  388. 'url' => $twitter_login_url
  389. )));
  390. } else {
  391. $app->response()->body(json_encode(array(
  392. 'result' => 'error'
  393. )));
  394. }
  395. });
  396. $app->get('/auth/twitter/callback', function() use($app) {
  397. if($user=require_login($app)) {
  398. $params = $app->request()->params();
  399. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  400. $_SESSION['twitter_auth']['oauth_token'], $_SESSION['twitter_auth']['oauth_token_secret']);
  401. $credentials = $twitter->getAccessToken($params['oauth_verifier']);
  402. $user->twitter_access_token = $credentials['oauth_token'];
  403. $user->twitter_token_secret = $credentials['oauth_token_secret'];
  404. $user->twitter_username = $credentials['screen_name'];
  405. $user->save();
  406. $app->redirect('/settings');
  407. }
  408. });
  409. $app->get('/auth/instagram', function() use($app) {
  410. if($user=require_login($app, false)) {
  411. $instagram = instagram_client();
  412. // If there is an existing Instagram auth token, check if it's valid
  413. if($user->instagram_access_token) {
  414. $instagram->setAccessToken($user->instagram_access_token);
  415. $igUser = $instagram->getUser();
  416. if($igUser && $igUser->meta->code == 200) {
  417. $app->response()->body(json_encode(array(
  418. 'result' => 'ok',
  419. 'username' => $igUser->data->username,
  420. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  421. )));
  422. return;
  423. }
  424. }
  425. $app->response()->body(json_encode(array(
  426. 'result' => 'error',
  427. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  428. )));
  429. }
  430. });
  431. $app->get('/auth/instagram/callback', function() use($app) {
  432. if($user=require_login($app)) {
  433. $params = $app->request()->params();
  434. $instagram = instagram_client();
  435. $data = $instagram->getOAuthToken($params['code']);
  436. $user->instagram_access_token = $data->access_token;
  437. $user->save();
  438. $app->redirect('/settings');
  439. }
  440. });