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.

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