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.

678 lines
19 KiB

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