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.

474 lines
14 KiB

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