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.

677 lines
19 KiB

8 years ago
8 years ago
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, array('HS256'));
  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('/dashboard', function() use($app) {
  41. if($user=require_login($app)) {
  42. $html = render('dashboard', array(
  43. 'title' => 'Dashboard',
  44. 'authorizing' => false
  45. ));
  46. $app->response()->body($html);
  47. }
  48. });
  49. $app->get('/new', function() use($app) {
  50. if($user=require_login($app)) {
  51. $params = $app->request()->params();
  52. $entry = false;
  53. $in_reply_to = '';
  54. if(array_key_exists('reply', $params))
  55. $in_reply_to = $params['reply'];
  56. $test_response = '';
  57. if($user->last_micropub_response) {
  58. try {
  59. if(@json_decode($user->last_micropub_response)) {
  60. $d = json_decode($user->last_micropub_response);
  61. $test_response = $d->response;
  62. }
  63. } catch(Exception $e) {
  64. }
  65. }
  66. $html = render('new-post', array(
  67. 'title' => 'New Post',
  68. 'in_reply_to' => $in_reply_to,
  69. 'micropub_endpoint' => $user->micropub_endpoint,
  70. 'micropub_scope' => $user->micropub_scope,
  71. 'micropub_access_token' => $user->micropub_access_token,
  72. 'response_date' => $user->last_micropub_response_date,
  73. 'syndication_targets' => json_decode($user->syndication_targets, true),
  74. 'test_response' => $test_response,
  75. 'location_enabled' => $user->location_enabled,
  76. 'user' => $user,
  77. 'authorizing' => false
  78. ));
  79. $app->response()->body($html);
  80. }
  81. });
  82. $app->get('/bookmark', function() use($app) {
  83. if($user=require_login($app)) {
  84. $params = $app->request()->params();
  85. $url = '';
  86. $name = '';
  87. $content = '';
  88. $tags = '';
  89. if(array_key_exists('url', $params))
  90. $url = $params['url'];
  91. if(array_key_exists('name', $params))
  92. $name = $params['name'];
  93. if(array_key_exists('content', $params))
  94. $content = $params['content'];
  95. $html = render('new-bookmark', array(
  96. 'title' => 'New Bookmark',
  97. 'bookmark_url' => $url,
  98. 'bookmark_name' => $name,
  99. 'bookmark_content' => $content,
  100. 'bookmark_tags' => $tags,
  101. 'token' => generate_login_token(),
  102. 'syndication_targets' => json_decode($user->syndication_targets, true),
  103. 'authorizing' => false
  104. ));
  105. $app->response()->body($html);
  106. }
  107. });
  108. $app->get('/favorite', function() use($app) {
  109. if($user=require_login($app)) {
  110. $params = $app->request()->params();
  111. $url = '';
  112. if(array_key_exists('url', $params))
  113. $url = $params['url'];
  114. $html = render('new-favorite', array(
  115. 'title' => 'New Favorite',
  116. 'url' => $url,
  117. 'token' => generate_login_token(),
  118. 'authorizing' => false
  119. ));
  120. $app->response()->body($html);
  121. }
  122. });
  123. $app->get('/event', function() use($app) {
  124. if($user=require_login($app)) {
  125. $params = $app->request()->params();
  126. $html = render('event', array(
  127. 'title' => 'Event',
  128. 'authorizing' => false
  129. ));
  130. $app->response()->body($html);
  131. }
  132. });
  133. $app->get('/itinerary', function() use($app) {
  134. if($user=require_login($app)) {
  135. $params = $app->request()->params();
  136. $html = render('new-itinerary', array(
  137. 'title' => 'Itinerary',
  138. 'authorizing' => false
  139. ));
  140. $app->response()->body($html);
  141. }
  142. });
  143. $app->get('/photo', function() use($app) {
  144. if($user=require_login($app)) {
  145. $params = $app->request()->params();
  146. $html = render('photo', array(
  147. 'title' => 'New Photo',
  148. 'note_content' => '',
  149. 'authorizing' => false
  150. ));
  151. $app->response()->body($html);
  152. }
  153. });
  154. $app->get('/repost', function() use($app) {
  155. if($user=require_login($app)) {
  156. $params = $app->request()->params();
  157. $url = '';
  158. if(array_key_exists('url', $params))
  159. $url = $params['url'];
  160. $html = render('new-repost', array(
  161. 'title' => 'New Repost',
  162. 'url' => $url,
  163. 'token' => generate_login_token(),
  164. 'authorizing' => false
  165. ));
  166. $app->response()->body($html);
  167. }
  168. });
  169. $app->post('/prefs', function() use($app) {
  170. if($user=require_login($app)) {
  171. $params = $app->request()->params();
  172. $user->location_enabled = $params['enabled'];
  173. $user->save();
  174. }
  175. $app->response()->body(json_encode(array(
  176. 'result' => 'ok'
  177. )));
  178. });
  179. $app->get('/creating-a-token-endpoint', function() use($app) {
  180. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  181. });
  182. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  183. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint', 'authorizing' => false));
  184. $app->response()->body($html);
  185. });
  186. $app->get('/docs', function() use($app) {
  187. $html = render('docs', array('title' => 'Documentation', 'authorizing' => false));
  188. $app->response()->body($html);
  189. });
  190. $app->get('/privacy', function() use($app) {
  191. $html = render('privacy', array('title' => 'Quill Privacy Policy', 'authorizing' => false));
  192. $app->response()->body($html);
  193. });
  194. $app->get('/add-to-home', function() use($app) {
  195. $params = $app->request()->params();
  196. header("Cache-Control: no-cache, must-revalidate");
  197. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  198. unset($_SESSION['add-to-home-started']);
  199. // Verify the token and sign the user in
  200. try {
  201. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  202. $_SESSION['user_id'] = $data->user_id;
  203. $_SESSION['me'] = $data->me;
  204. $app->redirect('/new', 301);
  205. } catch(DomainException $e) {
  206. header('X-Error: DomainException');
  207. $app->redirect('/', 301);
  208. } catch(UnexpectedValueException $e) {
  209. header('X-Error: UnexpectedValueException');
  210. $app->redirect('/', 301);
  211. }
  212. } else {
  213. if($user=require_login($app)) {
  214. if(array_key_exists('start', $params)) {
  215. $_SESSION['add-to-home-started'] = true;
  216. $token = JWT::encode(array(
  217. 'user_id' => $_SESSION['user_id'],
  218. 'me' => $_SESSION['me'],
  219. 'created_at' => time()
  220. ), Config::$jwtSecret);
  221. $app->redirect('/add-to-home?token='.$token, 301);
  222. } else {
  223. unset($_SESSION['add-to-home-started']);
  224. $html = render('add-to-home', array('title' => 'Quill'));
  225. $app->response()->body($html);
  226. }
  227. }
  228. }
  229. });
  230. $app->get('/email', function() use($app) {
  231. if($user=require_login($app)) {
  232. $test_response = '';
  233. if($user->last_micropub_response) {
  234. try {
  235. if(@json_decode($user->last_micropub_response)) {
  236. $d = json_decode($user->last_micropub_response);
  237. $test_response = $d->response;
  238. }
  239. } catch(Exception $e) {
  240. }
  241. }
  242. if(!$user->email_username) {
  243. $host = parse_url($user->url, PHP_URL_HOST);
  244. $user->email_username = $host . '.' . rand(100000,999999);
  245. $user->save();
  246. }
  247. $html = render('email', array(
  248. 'title' => 'Post-by-Email',
  249. 'micropub_endpoint' => $user->micropub_endpoint,
  250. 'test_response' => $test_response,
  251. 'user' => $user
  252. ));
  253. $app->response()->body($html);
  254. }
  255. });
  256. $app->get('/settings', function() use($app) {
  257. if($user=require_login($app)) {
  258. $html = render('settings', array('title' => 'Settings', 'include_facebook' => true, 'authorizing' => false));
  259. $app->response()->body($html);
  260. }
  261. });
  262. $app->post('/settings/html-content', function() use($app) {
  263. if($user=require_login($app)) {
  264. $params = $app->request()->params();
  265. $user->micropub_optin_html_content = $params['html'] ? 1 : 0;
  266. $user->save();
  267. $app->response()->body(json_encode(array(
  268. 'html' => $user->micropub_optin_html_content
  269. )));
  270. }
  271. });
  272. $app->get('/settings/html-content', function() use($app) {
  273. if($user=require_login($app)) {
  274. $app->response()->body(json_encode(array(
  275. 'html' => $user->micropub_optin_html_content
  276. )));
  277. }
  278. });
  279. $app->get('/favorite-popup', function() use($app) {
  280. if($user=require_login($app)) {
  281. $params = $app->request()->params();
  282. $html = $app->render('favorite-popup.php', array(
  283. 'url' => $params['url'],
  284. 'token' => $params['token']
  285. ));
  286. $app->response()->body($html);
  287. }
  288. });
  289. function create_favorite(&$user, $url) {
  290. $micropub_request = array(
  291. 'like-of' => $url
  292. );
  293. $r = micropub_post_for_user($user, $micropub_request);
  294. $facebook_id = false;
  295. $instagram_id = false;
  296. $tweet_id = false;
  297. /*
  298. // Facebook likes are posted via Javascript, so pass the FB ID to the javascript code
  299. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/(?:[^\/]+)\/posts\/(\d+)/', $url, $match)) {
  300. $facebook_id = $match[1];
  301. }
  302. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/photo\.php\?fbid=(\d+)/', $url, $match)) {
  303. $facebook_id = $match[1];
  304. }
  305. */
  306. if(preg_match('/https?:\/\/(?:www\.)?instagram\.com\/p\/([^\/]+)/', $url, $match)) {
  307. $instagram_id = $match[1];
  308. if($user->instagram_access_token) {
  309. $instagram = instagram_client();
  310. $instagram->setAccessToken($user->instagram_access_token);
  311. $ch = curl_init('https://api.instagram.com/v1/media/shortcode/' . $instagram_id . '?access_token=' . $user->instagram_access_token);
  312. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  313. $result = json_decode(curl_exec($ch));
  314. $result = $instagram->likeMedia($result->data->id);
  315. } else {
  316. // TODO: indicate that the instagram post couldn't be liked because no access token was available
  317. }
  318. }
  319. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  320. $tweet_id = $match[1];
  321. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  322. $user->twitter_access_token, $user->twitter_token_secret);
  323. $result = $twitter->post('favorites/create', array(
  324. 'id' => $tweet_id
  325. ));
  326. }
  327. return $r;
  328. }
  329. function create_repost(&$user, $url) {
  330. $micropub_request = array(
  331. 'repost-of' => $url
  332. );
  333. $r = micropub_post_for_user($user, $micropub_request);
  334. $tweet_id = false;
  335. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  336. $tweet_id = $match[1];
  337. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  338. $user->twitter_access_token, $user->twitter_token_secret);
  339. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  340. }
  341. return $r;
  342. }
  343. $app->get('/favorite.js', function() use($app) {
  344. $app->response()->header("Content-type", "text/javascript");
  345. if($user=require_login($app, false)) {
  346. $params = $app->request()->params();
  347. if(array_key_exists('url', $params)) {
  348. $r = create_favorite($user, $params['url']);
  349. $app->response()->body($app->render('favorite-js.php', array(
  350. 'url' => $params['url'],
  351. 'like_url' => $r['location'],
  352. 'error' => $r['error'],
  353. // 'facebook_id' => $facebook_id
  354. )));
  355. } else {
  356. $app->response()->body('alert("no url");');
  357. }
  358. } else {
  359. $app->response()->body('alert("invalid token");');
  360. }
  361. });
  362. $app->post('/favorite', function() use($app) {
  363. if($user=require_login($app)) {
  364. $params = $app->request()->params();
  365. $r = create_favorite($user, $params['url']);
  366. $app->response()->body(json_encode(array(
  367. 'location' => $r['location'],
  368. 'error' => $r['error']
  369. )));
  370. }
  371. });
  372. $app->post('/repost', function() use($app) {
  373. if($user=require_login($app)) {
  374. $params = $app->request()->params();
  375. $r = create_repost($user, $params['url']);
  376. $app->response()->body(json_encode(array(
  377. 'location' => $r['location'],
  378. 'error' => $r['error']
  379. )));
  380. }
  381. });
  382. $app->get('/micropub/syndications', function() use($app) {
  383. if($user=require_login($app)) {
  384. $data = get_syndication_targets($user);
  385. $app->response()->body(json_encode(array(
  386. 'targets' => $data['targets'],
  387. 'response' => $data['response']
  388. )));
  389. }
  390. });
  391. $app->post('/micropub/post', function() use($app) {
  392. if($user=require_login($app)) {
  393. $params = $app->request()->params();
  394. // Remove any blank params
  395. $params = array_filter($params, function($v){
  396. return $v !== '';
  397. });
  398. $r = micropub_post_for_user($user, $params);
  399. $app->response()->body(json_encode(array(
  400. 'request' => htmlspecialchars($r['request']),
  401. 'response' => htmlspecialchars($r['response']),
  402. 'location' => $r['location'],
  403. 'error' => $r['error'],
  404. 'curlinfo' => $r['curlinfo']
  405. )));
  406. }
  407. });
  408. $app->post('/micropub/multipart', function() use($app) {
  409. if($user=require_login($app)) {
  410. // var_dump($app->request()->post());
  411. //
  412. // Since $app->request()->post() with multipart is always
  413. // empty (bug in Slim?) We're using the raw $_POST here.
  414. // PHP empties everything in $_POST if the file upload size exceeds
  415. // that is why we have to test if the variables exist first.
  416. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  417. if($file) {
  418. $error = validate_photo($file);
  419. unset($_POST['null']);
  420. if(!$error) {
  421. $file_path = $file['tmp_name'];
  422. correct_photo_rotation($file_path);
  423. $r = micropub_post_for_user($user, $_POST, $file_path);
  424. } else {
  425. $r = array('error' => $error);
  426. }
  427. } else {
  428. unset($_POST['null']);
  429. $r = micropub_post_for_user($user, $_POST);
  430. }
  431. // Populate the error if there was no location header.
  432. if(empty($r['location']) && empty($r['error'])) {
  433. $r['error'] = "No 'Location' header in response.";
  434. }
  435. $app->response()->body(json_encode(array(
  436. 'response' => (isset($r['response']) ? htmlspecialchars($r['response']) : null),
  437. 'location' => (isset($r['location']) ? $r['location'] : null),
  438. 'error' => (isset($r['error']) ? $r['error'] : null),
  439. )));
  440. }
  441. });
  442. $app->post('/micropub/postjson', function() use($app) {
  443. if($user=require_login($app)) {
  444. $params = $app->request()->params();
  445. $r = micropub_post_for_user($user, json_decode($params['data'], true), null, true);
  446. $app->response()->body(json_encode(array(
  447. 'location' => $r['location'],
  448. 'error' => $r['error'],
  449. 'response' => $r['response']
  450. )));
  451. }
  452. });
  453. /*
  454. $app->post('/auth/facebook', function() use($app) {
  455. if($user=require_login($app, false)) {
  456. $params = $app->request()->params();
  457. // User just auth'd with facebook, store the access token
  458. $user->facebook_access_token = $params['fb_token'];
  459. $user->save();
  460. $app->response()->body(json_encode(array(
  461. 'result' => 'ok'
  462. )));
  463. } else {
  464. $app->response()->body(json_encode(array(
  465. 'result' => 'error'
  466. )));
  467. }
  468. });
  469. */
  470. $app->post('/auth/twitter', function() use($app) {
  471. if($user=require_login($app, false)) {
  472. $params = $app->request()->params();
  473. // User just auth'd with twitter, store the access token
  474. $user->twitter_access_token = $params['twitter_token'];
  475. $user->twitter_token_secret = $params['twitter_secret'];
  476. $user->save();
  477. $app->response()->body(json_encode(array(
  478. 'result' => 'ok'
  479. )));
  480. } else {
  481. $app->response()->body(json_encode(array(
  482. 'result' => 'error'
  483. )));
  484. }
  485. });
  486. function getTwitterLoginURL(&$twitter) {
  487. $request_token = $twitter->getRequestToken(Config::$base_url . 'auth/twitter/callback');
  488. $_SESSION['twitter_auth'] = $request_token;
  489. return $twitter->getAuthorizeURL($request_token['oauth_token']);
  490. }
  491. $app->get('/auth/twitter', function() use($app) {
  492. $params = $app->request()->params();
  493. if($user=require_login($app, false)) {
  494. // If there is an existing Twitter token, check if it is valid
  495. // Otherwise, generate a Twitter login link
  496. $twitter_login_url = false;
  497. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  498. $user->twitter_access_token, $user->twitter_token_secret);
  499. if(array_key_exists('login', $params)) {
  500. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret);
  501. $twitter_login_url = getTwitterLoginURL($twitter);
  502. } else {
  503. if($user->twitter_access_token) {
  504. if ($twitter->get('account/verify_credentials')) {
  505. $app->response()->body(json_encode(array(
  506. 'result' => 'ok'
  507. )));
  508. return;
  509. } else {
  510. // If the existing twitter token is not valid, generate a login link
  511. $twitter_login_url = getTwitterLoginURL($twitter);
  512. }
  513. } else {
  514. $twitter_login_url = getTwitterLoginURL($twitter);
  515. }
  516. }
  517. $app->response()->body(json_encode(array(
  518. 'url' => $twitter_login_url
  519. )));
  520. } else {
  521. $app->response()->body(json_encode(array(
  522. 'result' => 'error'
  523. )));
  524. }
  525. });
  526. $app->get('/auth/twitter/callback', function() use($app) {
  527. if($user=require_login($app)) {
  528. $params = $app->request()->params();
  529. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  530. $_SESSION['twitter_auth']['oauth_token'], $_SESSION['twitter_auth']['oauth_token_secret']);
  531. $credentials = $twitter->getAccessToken($params['oauth_verifier']);
  532. $user->twitter_access_token = $credentials['oauth_token'];
  533. $user->twitter_token_secret = $credentials['oauth_token_secret'];
  534. $user->twitter_username = $credentials['screen_name'];
  535. $user->save();
  536. $app->redirect('/settings');
  537. }
  538. });
  539. $app->get('/auth/instagram', function() use($app) {
  540. if($user=require_login($app, false)) {
  541. $instagram = instagram_client();
  542. // If there is an existing Instagram auth token, check if it's valid
  543. if($user->instagram_access_token) {
  544. $instagram->setAccessToken($user->instagram_access_token);
  545. $igUser = $instagram->getUser();
  546. if($igUser && $igUser->meta->code == 200) {
  547. $app->response()->body(json_encode(array(
  548. 'result' => 'ok',
  549. 'username' => $igUser->data->username,
  550. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  551. )));
  552. return;
  553. }
  554. }
  555. $app->response()->body(json_encode(array(
  556. 'result' => 'error',
  557. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  558. )));
  559. }
  560. });
  561. $app->get('/auth/instagram/callback', function() use($app) {
  562. if($user=require_login($app)) {
  563. $params = $app->request()->params();
  564. $instagram = instagram_client();
  565. $data = $instagram->getOAuthToken($params['code']);
  566. $user->instagram_access_token = $data->access_token;
  567. $user->save();
  568. $app->redirect('/settings');
  569. }
  570. });