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.

617 lines
18 KiB

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