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.

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