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.

720 lines
21 KiB

9 years ago
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. $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. 'media_endpoint' => $user->micropub_media_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()['Content-type'] = 'application/json';
  177. $app->response()->body(json_encode(array(
  178. 'result' => 'ok'
  179. )));
  180. });
  181. $app->get('/creating-a-token-endpoint', function() use($app) {
  182. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  183. });
  184. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  185. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint', 'authorizing' => false));
  186. $app->response()->body($html);
  187. });
  188. $app->get('/docs', function() use($app) {
  189. $html = render('docs', array('title' => 'Documentation', 'authorizing' => false));
  190. $app->response()->body($html);
  191. });
  192. $app->get('/privacy', function() use($app) {
  193. $html = render('privacy', array('title' => 'Quill Privacy Policy', 'authorizing' => false));
  194. $app->response()->body($html);
  195. });
  196. $app->get('/add-to-home', function() use($app) {
  197. $params = $app->request()->params();
  198. header("Cache-Control: no-cache, must-revalidate");
  199. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  200. unset($_SESSION['add-to-home-started']);
  201. // Verify the token and sign the user in
  202. try {
  203. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  204. $_SESSION['user_id'] = $data->user_id;
  205. $_SESSION['me'] = $data->me;
  206. $app->redirect('/new', 301);
  207. } catch(DomainException $e) {
  208. header('X-Error: DomainException');
  209. $app->redirect('/', 301);
  210. } catch(UnexpectedValueException $e) {
  211. header('X-Error: UnexpectedValueException');
  212. $app->redirect('/', 301);
  213. }
  214. } else {
  215. if($user=require_login($app)) {
  216. if(array_key_exists('start', $params)) {
  217. $_SESSION['add-to-home-started'] = true;
  218. $token = JWT::encode(array(
  219. 'user_id' => $_SESSION['user_id'],
  220. 'me' => $_SESSION['me'],
  221. 'created_at' => time()
  222. ), Config::$jwtSecret);
  223. $app->redirect('/add-to-home?token='.$token, 301);
  224. } else {
  225. unset($_SESSION['add-to-home-started']);
  226. $html = render('add-to-home', array('title' => 'Quill'));
  227. $app->response()->body($html);
  228. }
  229. }
  230. }
  231. });
  232. $app->get('/email', function() use($app) {
  233. if($user=require_login($app)) {
  234. $test_response = '';
  235. if($user->last_micropub_response) {
  236. try {
  237. if(@json_decode($user->last_micropub_response)) {
  238. $d = json_decode($user->last_micropub_response);
  239. $test_response = $d->response;
  240. }
  241. } catch(Exception $e) {
  242. }
  243. }
  244. if(!$user->email_username) {
  245. $host = parse_url($user->url, PHP_URL_HOST);
  246. $user->email_username = $host . '.' . rand(100000,999999);
  247. $user->save();
  248. }
  249. $html = render('email', array(
  250. 'title' => 'Post-by-Email',
  251. 'micropub_endpoint' => $user->micropub_endpoint,
  252. 'test_response' => $test_response,
  253. 'user' => $user
  254. ));
  255. $app->response()->body($html);
  256. }
  257. });
  258. $app->get('/settings', function() use($app) {
  259. if($user=require_login($app)) {
  260. $html = render('settings', array('title' => 'Settings', 'include_facebook' => true, 'authorizing' => false));
  261. $app->response()->body($html);
  262. }
  263. });
  264. $app->post('/settings/html-content', function() use($app) {
  265. if($user=require_login($app)) {
  266. $params = $app->request()->params();
  267. $user->micropub_optin_html_content = $params['html'] ? 1 : 0;
  268. $user->save();
  269. $app->response()['Content-type'] = 'application/json';
  270. $app->response()->body(json_encode(array(
  271. 'html' => $user->micropub_optin_html_content
  272. )));
  273. }
  274. });
  275. $app->get('/settings/html-content', function() use($app) {
  276. if($user=require_login($app)) {
  277. $app->response()['Content-type'] = 'application/json';
  278. $app->response()->body(json_encode(array(
  279. 'html' => $user->micropub_optin_html_content
  280. )));
  281. }
  282. });
  283. $app->get('/favorite-popup', function() use($app) {
  284. if($user=require_login($app)) {
  285. $params = $app->request()->params();
  286. $html = $app->render('favorite-popup.php', array(
  287. 'url' => $params['url'],
  288. 'token' => $params['token']
  289. ));
  290. $app->response()->body($html);
  291. }
  292. });
  293. function create_favorite(&$user, $url) {
  294. $micropub_request = array(
  295. 'like-of' => $url
  296. );
  297. $r = micropub_post_for_user($user, $micropub_request);
  298. $facebook_id = false;
  299. $instagram_id = false;
  300. $tweet_id = false;
  301. /*
  302. // Facebook likes are posted via Javascript, so pass the FB ID to the javascript code
  303. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/(?:[^\/]+)\/posts\/(\d+)/', $url, $match)) {
  304. $facebook_id = $match[1];
  305. }
  306. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/photo\.php\?fbid=(\d+)/', $url, $match)) {
  307. $facebook_id = $match[1];
  308. }
  309. */
  310. if(preg_match('/https?:\/\/(?:www\.)?instagram\.com\/p\/([^\/]+)/', $url, $match)) {
  311. $instagram_id = $match[1];
  312. if($user->instagram_access_token) {
  313. $instagram = instagram_client();
  314. $instagram->setAccessToken($user->instagram_access_token);
  315. $ch = curl_init('https://api.instagram.com/v1/media/shortcode/' . $instagram_id . '?access_token=' . $user->instagram_access_token);
  316. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  317. $result = json_decode(curl_exec($ch));
  318. $result = $instagram->likeMedia($result->data->id);
  319. } else {
  320. // TODO: indicate that the instagram post couldn't be liked because no access token was available
  321. }
  322. }
  323. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  324. $tweet_id = $match[1];
  325. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  326. $user->twitter_access_token, $user->twitter_token_secret);
  327. $result = $twitter->post('favorites/create', array(
  328. 'id' => $tweet_id
  329. ));
  330. }
  331. return $r;
  332. }
  333. function create_repost(&$user, $url) {
  334. $micropub_request = array(
  335. 'repost-of' => $url
  336. );
  337. $r = micropub_post_for_user($user, $micropub_request);
  338. $tweet_id = false;
  339. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  340. $tweet_id = $match[1];
  341. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  342. $user->twitter_access_token, $user->twitter_token_secret);
  343. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  344. }
  345. return $r;
  346. }
  347. $app->get('/favorite.js', function() use($app) {
  348. $app->response()->header("Content-type", "text/javascript");
  349. if($user=require_login($app, false)) {
  350. $params = $app->request()->params();
  351. if(array_key_exists('url', $params)) {
  352. $r = create_favorite($user, $params['url']);
  353. $app->response()->body($app->render('favorite-js.php', array(
  354. 'url' => $params['url'],
  355. 'like_url' => $r['location'],
  356. 'error' => $r['error'],
  357. // 'facebook_id' => $facebook_id
  358. )));
  359. } else {
  360. $app->response()->body('alert("no url");');
  361. }
  362. } else {
  363. $app->response()->body('alert("invalid token");');
  364. }
  365. });
  366. $app->post('/favorite', function() use($app) {
  367. if($user=require_login($app)) {
  368. $params = $app->request()->params();
  369. $r = create_favorite($user, $params['url']);
  370. $app->response()['Content-type'] = 'application/json';
  371. $app->response()->body(json_encode(array(
  372. 'location' => $r['location'],
  373. 'error' => $r['error']
  374. )));
  375. }
  376. });
  377. $app->post('/repost', function() use($app) {
  378. if($user=require_login($app)) {
  379. $params = $app->request()->params();
  380. $r = create_repost($user, $params['url']);
  381. $app->response()['Content-type'] = 'application/json';
  382. $app->response()->body(json_encode(array(
  383. 'location' => $r['location'],
  384. 'error' => $r['error']
  385. )));
  386. }
  387. });
  388. $app->get('/micropub/syndications', function() use($app) {
  389. if($user=require_login($app)) {
  390. $data = get_micropub_config($user, ['q'=>'syndicate-to']);
  391. $app->response()['Content-type'] = 'application/json';
  392. $app->response()->body(json_encode(array(
  393. 'targets' => $data['targets'],
  394. 'response' => $data['response']
  395. )));
  396. }
  397. });
  398. $app->post('/micropub/post', function() use($app) {
  399. if($user=require_login($app)) {
  400. $params = $app->request()->params();
  401. // Remove any blank params
  402. $params = array_filter($params, function($v){
  403. return $v !== '';
  404. });
  405. $r = micropub_post_for_user($user, $params);
  406. $app->response()['Content-type'] = 'application/json';
  407. $app->response()->body(json_encode(array(
  408. 'request' => htmlspecialchars($r['request']),
  409. 'response' => htmlspecialchars($r['response']),
  410. 'location' => $r['location'],
  411. 'error' => $r['error'],
  412. 'curlinfo' => $r['curlinfo']
  413. )));
  414. }
  415. });
  416. $app->post('/micropub/multipart', function() use($app) {
  417. if($user=require_login($app)) {
  418. // var_dump($app->request()->post());
  419. //
  420. // Since $app->request()->post() with multipart is always
  421. // empty (bug in Slim?) We're using the raw $_POST here.
  422. // PHP empties everything in $_POST if the file upload size exceeds
  423. // that is why we have to test if the variables exist first.
  424. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  425. if($file) {
  426. $error = validate_photo($file);
  427. unset($_POST['null']);
  428. if(!$error) {
  429. $file_path = $file['tmp_name'];
  430. correct_photo_rotation($file_path);
  431. $r = micropub_post_for_user($user, $_POST, $file_path);
  432. } else {
  433. $r = array('error' => $error);
  434. }
  435. } else {
  436. unset($_POST['null']);
  437. $r = micropub_post_for_user($user, $_POST);
  438. }
  439. // Populate the error if there was no location header.
  440. if(empty($r['location']) && empty($r['error'])) {
  441. $r['error'] = "No 'Location' header in response.";
  442. }
  443. $app->response()['Content-type'] = 'application/json';
  444. $app->response()->body(json_encode(array(
  445. 'response' => (isset($r['response']) ? htmlspecialchars($r['response']) : null),
  446. 'location' => (isset($r['location']) ? $r['location'] : null),
  447. 'error' => (isset($r['error']) ? $r['error'] : null),
  448. )));
  449. }
  450. });
  451. $app->post('/micropub/media', function() use($app) {
  452. if($user=require_login($app)) {
  453. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  454. $error = validate_photo($file);
  455. unset($_POST['null']);
  456. if(!$error) {
  457. $file_path = $file['tmp_name'];
  458. correct_photo_rotation($file_path);
  459. $r = micropub_media_post_for_user($user, $file_path);
  460. } else {
  461. $r = array('error' => $error);
  462. }
  463. if(empty($r['location']) && empty($r['error'])) {
  464. $r['error'] = "No 'Location' header in response.";
  465. }
  466. $app->response()['Content-type'] = 'application/json';
  467. $app->response()->body(json_encode(array(
  468. 'location' => (isset($r['location']) ? $r['location'] : null),
  469. 'error' => (isset($r['error']) ? $r['error'] : null),
  470. )));
  471. }
  472. });
  473. $app->post('/micropub/postjson', function() use($app) {
  474. if($user=require_login($app)) {
  475. $params = $app->request()->params();
  476. $r = micropub_post_for_user($user, json_decode($params['data'], true), null, true);
  477. $app->response()['Content-type'] = 'application/json';
  478. $app->response()->body(json_encode(array(
  479. 'location' => $r['location'],
  480. 'error' => $r['error'],
  481. 'response' => $r['response']
  482. )));
  483. }
  484. });
  485. /*
  486. $app->post('/auth/facebook', function() use($app) {
  487. if($user=require_login($app, false)) {
  488. $params = $app->request()->params();
  489. // User just auth'd with facebook, store the access token
  490. $user->facebook_access_token = $params['fb_token'];
  491. $user->save();
  492. $app->response()->body(json_encode(array(
  493. 'result' => 'ok'
  494. )));
  495. } else {
  496. $app->response()->body(json_encode(array(
  497. 'result' => 'error'
  498. )));
  499. }
  500. });
  501. */
  502. $app->post('/auth/twitter', function() use($app) {
  503. if($user=require_login($app, false)) {
  504. $params = $app->request()->params();
  505. // User just auth'd with twitter, store the access token
  506. $user->twitter_access_token = $params['twitter_token'];
  507. $user->twitter_token_secret = $params['twitter_secret'];
  508. $user->save();
  509. $app->response()['Content-type'] = 'application/json';
  510. $app->response()->body(json_encode(array(
  511. 'result' => 'ok'
  512. )));
  513. } else {
  514. $app->response()['Content-type'] = 'application/json';
  515. $app->response()->body(json_encode(array(
  516. 'result' => 'error'
  517. )));
  518. }
  519. });
  520. function getTwitterLoginURL(&$twitter) {
  521. $request_token = $twitter->getRequestToken(Config::$base_url . 'auth/twitter/callback');
  522. $_SESSION['twitter_auth'] = $request_token;
  523. return $twitter->getAuthorizeURL($request_token['oauth_token']);
  524. }
  525. $app->get('/auth/twitter', function() use($app) {
  526. $params = $app->request()->params();
  527. if($user=require_login($app, false)) {
  528. // If there is an existing Twitter token, check if it is valid
  529. // Otherwise, generate a Twitter login link
  530. $twitter_login_url = false;
  531. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  532. $user->twitter_access_token, $user->twitter_token_secret);
  533. if(array_key_exists('login', $params)) {
  534. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret);
  535. $twitter_login_url = getTwitterLoginURL($twitter);
  536. } else {
  537. if($user->twitter_access_token) {
  538. if ($twitter->get('account/verify_credentials')) {
  539. $app->response()['Content-type'] = 'application/json';
  540. $app->response()->body(json_encode(array(
  541. 'result' => 'ok'
  542. )));
  543. return;
  544. } else {
  545. // If the existing twitter token is not valid, generate a login link
  546. $twitter_login_url = getTwitterLoginURL($twitter);
  547. }
  548. } else {
  549. $twitter_login_url = getTwitterLoginURL($twitter);
  550. }
  551. }
  552. $app->response()['Content-type'] = 'application/json';
  553. $app->response()->body(json_encode(array(
  554. 'url' => $twitter_login_url
  555. )));
  556. } else {
  557. $app->response()['Content-type'] = 'application/json';
  558. $app->response()->body(json_encode(array(
  559. 'result' => 'error'
  560. )));
  561. }
  562. });
  563. $app->get('/auth/twitter/callback', function() use($app) {
  564. if($user=require_login($app)) {
  565. $params = $app->request()->params();
  566. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  567. $_SESSION['twitter_auth']['oauth_token'], $_SESSION['twitter_auth']['oauth_token_secret']);
  568. $credentials = $twitter->getAccessToken($params['oauth_verifier']);
  569. $user->twitter_access_token = $credentials['oauth_token'];
  570. $user->twitter_token_secret = $credentials['oauth_token_secret'];
  571. $user->twitter_username = $credentials['screen_name'];
  572. $user->save();
  573. $app->redirect('/settings');
  574. }
  575. });
  576. $app->get('/auth/instagram', function() use($app) {
  577. if($user=require_login($app, false)) {
  578. $instagram = instagram_client();
  579. // If there is an existing Instagram auth token, check if it's valid
  580. if($user->instagram_access_token) {
  581. $instagram->setAccessToken($user->instagram_access_token);
  582. $igUser = $instagram->getUser();
  583. if($igUser && $igUser->meta->code == 200) {
  584. $app->response()['Content-type'] = 'application/json';
  585. $app->response()->body(json_encode(array(
  586. 'result' => 'ok',
  587. 'username' => $igUser->data->username,
  588. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  589. )));
  590. return;
  591. }
  592. }
  593. $app->response()['Content-type'] = 'application/json';
  594. $app->response()->body(json_encode(array(
  595. 'result' => 'error',
  596. 'url' => $instagram->getLoginUrl(array('basic','likes'))
  597. )));
  598. }
  599. });
  600. $app->get('/auth/instagram/callback', function() use($app) {
  601. if($user=require_login($app)) {
  602. $params = $app->request()->params();
  603. $instagram = instagram_client();
  604. $data = $instagram->getOAuthToken($params['code']);
  605. $user->instagram_access_token = $data->access_token;
  606. $user->save();
  607. $app->redirect('/settings');
  608. }
  609. });