Mirror of Quill
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.

662 lines
19 KiB

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