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.

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