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.

619 lines
18 KiB

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