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.

583 lines
17 KiB

8 years ago
8 years ago
8 years ago
8 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. ));
  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->get('/photo', function() use($app) {
  112. if($user=require_login($app)) {
  113. $params = $app->request()->params();
  114. $html = render('photo', array(
  115. 'title' => 'New Photo',
  116. 'note_content' => '',
  117. 'authorizing' => false
  118. ));
  119. $app->response()->body($html);
  120. }
  121. });
  122. $app->get('/repost', function() use($app) {
  123. if($user=require_login($app)) {
  124. $params = $app->request()->params();
  125. $url = '';
  126. if(array_key_exists('url', $params))
  127. $url = $params['url'];
  128. $html = render('new-repost', array(
  129. 'title' => 'New Repost',
  130. 'url' => $url,
  131. 'token' => generate_login_token()
  132. ));
  133. $app->response()->body($html);
  134. }
  135. });
  136. $app->post('/prefs', function() use($app) {
  137. if($user=require_login($app)) {
  138. $params = $app->request()->params();
  139. $user->location_enabled = $params['enabled'];
  140. $user->save();
  141. }
  142. $app->response()->body(json_encode(array(
  143. 'result' => 'ok'
  144. )));
  145. });
  146. $app->get('/creating-a-token-endpoint', function() use($app) {
  147. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  148. });
  149. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  150. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint'));
  151. $app->response()->body($html);
  152. });
  153. $app->get('/docs', function() use($app) {
  154. $html = render('docs', array('title' => 'Documentation'));
  155. $app->response()->body($html);
  156. });
  157. $app->get('/privacy', function() use($app) {
  158. $html = render('privacy', array('title' => 'Quill Privacy Policy'));
  159. $app->response()->body($html);
  160. });
  161. $app->get('/add-to-home', function() use($app) {
  162. $params = $app->request()->params();
  163. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  164. // Verify the token and sign the user in
  165. try {
  166. $data = JWT::decode($params['token'], Config::$jwtSecret);
  167. $_SESSION['user_id'] = $data->user_id;
  168. $_SESSION['me'] = $data->me;
  169. $app->redirect('/new', 301);
  170. } catch(DomainException $e) {
  171. header('X-Error: DomainException');
  172. $app->redirect('/', 301);
  173. } catch(UnexpectedValueException $e) {
  174. header('X-Error: UnexpectedValueException');
  175. $app->redirect('/', 301);
  176. }
  177. } else {
  178. if($user=require_login($app)) {
  179. if(array_key_exists('start', $params)) {
  180. $_SESSION['add-to-home-started'] = true;
  181. $token = JWT::encode(array(
  182. 'user_id' => $_SESSION['user_id'],
  183. 'me' => $_SESSION['me'],
  184. 'created_at' => time()
  185. ), Config::$jwtSecret);
  186. $app->redirect('/add-to-home?token='.$token, 301);
  187. } else {
  188. unset($_SESSION['add-to-home-started']);
  189. $html = render('add-to-home', array('title' => 'Quill'));
  190. $app->response()->body($html);
  191. }
  192. }
  193. }
  194. });
  195. $app->get('/settings', function() use($app) {
  196. if($user=require_login($app)) {
  197. $html = render('settings', array('title' => 'Settings', 'include_facebook' => true));
  198. $app->response()->body($html);
  199. }
  200. });
  201. $app->get('/favorite-popup', function() use($app) {
  202. if($user=require_login($app)) {
  203. $params = $app->request()->params();
  204. $html = $app->render('favorite-popup.php', array(
  205. 'url' => $params['url'],
  206. 'token' => $params['token']
  207. ));
  208. $app->response()->body($html);
  209. }
  210. });
  211. function create_favorite(&$user, $url) {
  212. $micropub_request = array(
  213. 'like-of' => $url
  214. );
  215. $r = micropub_post_for_user($user, $micropub_request);
  216. $facebook_id = false;
  217. $instagram_id = false;
  218. $tweet_id = false;
  219. /*
  220. // Facebook likes are posted via Javascript, so pass the FB ID to the javascript code
  221. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/(?:[^\/]+)\/posts\/(\d+)/', $url, $match)) {
  222. $facebook_id = $match[1];
  223. }
  224. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/photo\.php\?fbid=(\d+)/', $url, $match)) {
  225. $facebook_id = $match[1];
  226. }
  227. */
  228. if(preg_match('/https?:\/\/(?:www\.)?instagram\.com\/p\/([^\/]+)/', $url, $match)) {
  229. $instagram_id = $match[1];
  230. if($user->instagram_access_token) {
  231. $instagram = instagram_client();
  232. $instagram->setAccessToken($user->instagram_access_token);
  233. $ch = curl_init('https://api.instagram.com/v1/media/shortcode/' . $instagram_id . '?access_token=' . $user->instagram_access_token);
  234. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  235. $result = json_decode(curl_exec($ch));
  236. $result = $instagram->likeMedia($result->data->id);
  237. } else {
  238. // TODO: indicate that the instagram post couldn't be liked because no access token was available
  239. }
  240. }
  241. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  242. $tweet_id = $match[1];
  243. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  244. $user->twitter_access_token, $user->twitter_token_secret);
  245. $result = $twitter->post('favorites/create', array(
  246. 'id' => $tweet_id
  247. ));
  248. }
  249. return $r;
  250. }
  251. function create_photo(&$user, $params, $file) {
  252. $error = validate_photo($file);
  253. if(!$error) {
  254. $file_path = $file['tmp_name'];
  255. $micropub_request = array('content' => $params['note_content']);
  256. $r = micropub_post_for_user($user, $micropub_request, $file_path);
  257. } else {
  258. $r = array('error' => $error);
  259. }
  260. return $r;
  261. }
  262. function create_repost(&$user, $url) {
  263. $micropub_request = array(
  264. 'repost-of' => $url
  265. );
  266. $r = micropub_post_for_user($user, $micropub_request);
  267. $tweet_id = false;
  268. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  269. $tweet_id = $match[1];
  270. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  271. $user->twitter_access_token, $user->twitter_token_secret);
  272. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  273. }
  274. return $r;
  275. }
  276. $app->get('/favorite.js', function() use($app) {
  277. $app->response()->header("Content-type", "text/javascript");
  278. if($user=require_login($app, false)) {
  279. $params = $app->request()->params();
  280. if(array_key_exists('url', $params)) {
  281. $r = create_favorite($user, $params['url']);
  282. $app->response()->body($app->render('favorite-js.php', array(
  283. 'url' => $params['url'],
  284. 'like_url' => $r['location'],
  285. 'error' => $r['error'],
  286. // 'facebook_id' => $facebook_id
  287. )));
  288. } else {
  289. $app->response()->body('alert("no url");');
  290. }
  291. } else {
  292. $app->response()->body('alert("invalid token");');
  293. }
  294. });
  295. $app->post('/favorite', function() use($app) {
  296. if($user=require_login($app)) {
  297. $params = $app->request()->params();
  298. $r = create_favorite($user, $params['url']);
  299. $app->response()->body(json_encode(array(
  300. 'location' => $r['location'],
  301. 'error' => $r['error']
  302. )));
  303. }
  304. });
  305. $app->post('/photo', function() use($app) {
  306. if($user=require_login($app)) {
  307. // var_dump($app->request()->post());
  308. //
  309. // Since $app->request()->post() with multipart is always
  310. // empty (bug in Slim?) We're using the raw $_POST here
  311. // until this gets fixed.
  312. // PHP empties everything in $_POST if the file upload size exceeds
  313. // that is why we have to test if the variables exist first.
  314. $note_content = isset($_POST['note_content']) ? $_POST['note_content'] : null;
  315. $params = array('note_content' => $note_content);
  316. $file = isset($_FILES['note_photo']) ? $_FILES['note_photo'] : null;
  317. $r = create_photo($user, $params, $file);
  318. // Populate the error if there was no location header.
  319. if(empty($r['location']) && empty($r['error'])) {
  320. $r['error'] = "No 'Location' header in response.";
  321. }
  322. $html = render('photo', array(
  323. 'title' => 'Photo posted',
  324. 'note_content' => $params['note_content'],
  325. 'location' => (isset($r['location']) ? $r['location'] : null),
  326. 'error' => (isset($r['error']) ? $r['error'] : null),
  327. 'response' => (isset($r['response']) ? htmlspecialchars($r['response']) : null),
  328. 'authorizing' => false
  329. ));
  330. $app->response()->body($html);
  331. }
  332. });
  333. $app->post('/repost', function() use($app) {
  334. if($user=require_login($app)) {
  335. $params = $app->request()->params();
  336. $r = create_repost($user, $params['url']);
  337. $app->response()->body(json_encode(array(
  338. 'location' => $r['location'],
  339. 'error' => $r['error']
  340. )));
  341. }
  342. });
  343. $app->get('/micropub/syndications', function() use($app) {
  344. if($user=require_login($app)) {
  345. $data = get_syndication_targets($user);
  346. $app->response()->body(json_encode(array(
  347. 'targets' => $data['targets'],
  348. 'response' => $data['response']
  349. )));
  350. }
  351. });
  352. $app->post('/micropub/post', function() use($app) {
  353. if($user=require_login($app)) {
  354. $params = $app->request()->params();
  355. // Remove any blank params
  356. $params = array_filter($params, function($v){
  357. return $v !== '';
  358. });
  359. $r = micropub_post_for_user($user, $params);
  360. $app->response()->body(json_encode(array(
  361. 'request' => htmlspecialchars($r['request']),
  362. 'response' => htmlspecialchars($r['response']),
  363. 'location' => $r['location'],
  364. 'error' => $r['error'],
  365. 'curlinfo' => $r['curlinfo']
  366. )));
  367. }
  368. });
  369. /*
  370. $app->post('/auth/facebook', function() use($app) {
  371. if($user=require_login($app, false)) {
  372. $params = $app->request()->params();
  373. // User just auth'd with facebook, store the access token
  374. $user->facebook_access_token = $params['fb_token'];
  375. $user->save();
  376. $app->response()->body(json_encode(array(
  377. 'result' => 'ok'
  378. )));
  379. } else {
  380. $app->response()->body(json_encode(array(
  381. 'result' => 'error'
  382. )));
  383. }
  384. });
  385. */
  386. $app->post('/auth/twitter', function() use($app) {
  387. if($user=require_login($app, false)) {
  388. $params = $app->request()->params();
  389. // User just auth'd with twitter, store the access token
  390. $user->twitter_access_token = $params['twitter_token'];
  391. $user->twitter_token_secret = $params['twitter_secret'];
  392. $user->save();
  393. $app->response()->body(json_encode(array(
  394. 'result' => 'ok'
  395. )));
  396. } else {
  397. $app->response()->body(json_encode(array(
  398. 'result' => 'error'
  399. )));
  400. }
  401. });
  402. function getTwitterLoginURL(&$twitter) {
  403. $request_token = $twitter->getRequestToken(Config::$base_url . 'auth/twitter/callback');
  404. $_SESSION['twitter_auth'] = $request_token;
  405. return $twitter->getAuthorizeURL($request_token['oauth_token']);
  406. }
  407. $app->get('/auth/twitter', function() use($app) {
  408. $params = $app->request()->params();
  409. if($user=require_login($app, false)) {
  410. // If there is an existing Twitter token, check if it is valid
  411. // Otherwise, generate a Twitter login link
  412. $twitter_login_url = false;
  413. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  414. $user->twitter_access_token, $user->twitter_token_secret);
  415. if(array_key_exists('login', $params)) {
  416. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret);
  417. $twitter_login_url = getTwitterLoginURL($twitter);
  418. } else {
  419. if($user->twitter_access_token) {
  420. if ($twitter->get('account/verify_credentials')) {
  421. $app->response()->body(json_encode(array(
  422. 'result' => 'ok'
  423. )));
  424. return;
  425. } else {
  426. // If the existing twitter token is not valid, generate a login link
  427. $twitter_login_url = getTwitterLoginURL($twitter);
  428. }
  429. } else {
  430. $twitter_login_url = getTwitterLoginURL($twitter);
  431. }
  432. }
  433. $app->response()->body(json_encode(array(
  434. 'url' => $twitter_login_url
  435. )));
  436. } else {
  437. $app->response()->body(json_encode(array(
  438. 'result' => 'error'
  439. )));
  440. }
  441. });
  442. $app->get('/auth/twitter/callback', function() use($app) {
  443. if($user=require_login($app)) {
  444. $params = $app->request()->params();
  445. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  446. $_SESSION['twitter_auth']['oauth_token'], $_SESSION['twitter_auth']['oauth_token_secret']);
  447. $credentials = $twitter->getAccessToken($params['oauth_verifier']);
  448. $user->twitter_access_token = $credentials['oauth_token'];
  449. $user->twitter_token_secret = $credentials['oauth_token_secret'];
  450. $user->twitter_username = $credentials['screen_name'];
  451. $user->save();
  452. $app->redirect('/settings');
  453. }
  454. });
  455. $app->get('/auth/instagram', function() use($app) {
  456. if($user=require_login($app, false)) {
  457. $instagram = instagram_client();
  458. // If there is an existing Instagram auth token, check if it's valid
  459. if($user->instagram_access_token) {
  460. $instagram->setAccessToken($user->instagram_access_token);
  461. $igUser = $instagram->getUser();
  462. if($igUser && $igUser->meta->code == 200) {
  463. $app->response()->body(json_encode(array(
  464. 'result' => 'ok',
  465. 'username' => $igUser->data->username,
  466. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  467. )));
  468. return;
  469. }
  470. }
  471. $app->response()->body(json_encode(array(
  472. 'result' => 'error',
  473. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  474. )));
  475. }
  476. });
  477. $app->get('/auth/instagram/callback', function() use($app) {
  478. if($user=require_login($app)) {
  479. $params = $app->request()->params();
  480. $instagram = instagram_client();
  481. $data = $instagram->getOAuthToken($params['code']);
  482. $user->instagram_access_token = $data->access_token;
  483. $user->save();
  484. $app->redirect('/settings');
  485. }
  486. });