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.

516 lines
14 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. <?php
  2. use Abraham\TwitterOAuth\TwitterOAuth;
  3. function require_login(&$app, $redirect=true) {
  4. $params = $app->request()->params();
  5. if(array_key_exists('token', $params)) {
  6. try {
  7. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  8. $_SESSION['user_id'] = $data->user_id;
  9. $_SESSION['me'] = $data->me;
  10. } catch(DomainException $e) {
  11. if($redirect) {
  12. header('X-Error: DomainException');
  13. $app->redirect('/', 302);
  14. } else {
  15. return false;
  16. }
  17. } catch(UnexpectedValueException $e) {
  18. if($redirect) {
  19. header('X-Error: UnexpectedValueException');
  20. $app->redirect('/', 302);
  21. } else {
  22. return false;
  23. }
  24. }
  25. }
  26. if(!array_key_exists('user_id', $_SESSION)) {
  27. if($redirect)
  28. $app->redirect('/', 302);
  29. return false;
  30. } else {
  31. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  32. }
  33. }
  34. function generate_login_token($opts=[]) {
  35. return JWT::encode(array_merge([
  36. 'user_id' => $_SESSION['user_id'],
  37. 'me' => $_SESSION['me'],
  38. 'created_at' => time()
  39. ], $opts), Config::$jwtSecret);
  40. }
  41. $app->get('/dashboard', function() use($app) {
  42. if($user=require_login($app)) {
  43. render('dashboard', array(
  44. 'title' => 'Dashboard',
  45. 'authorizing' => false
  46. ));
  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. 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. }
  81. });
  82. $app->get('/bookmark', function() use($app) {
  83. if($user=require_login($app)) {
  84. $params = $app->request()->params();
  85. $url = '';
  86. $name = '';
  87. $content = '';
  88. $tags = '';
  89. if(array_key_exists('url', $params))
  90. $url = $params['url'];
  91. if(array_key_exists('name', $params))
  92. $name = $params['name'];
  93. if(array_key_exists('content', $params))
  94. $content = $params['content'];
  95. render('new-bookmark', array(
  96. 'title' => 'New Bookmark',
  97. 'bookmark_url' => $url,
  98. 'bookmark_name' => $name,
  99. 'bookmark_content' => $content,
  100. 'bookmark_tags' => $tags,
  101. 'token' => generate_login_token(),
  102. 'syndication_targets' => json_decode($user->syndication_targets, true),
  103. 'user' => $user,
  104. 'authorizing' => false
  105. ));
  106. }
  107. });
  108. $app->get('/favorite', function() use($app) {
  109. if($user=require_login($app)) {
  110. $params = $app->request()->params();
  111. $url = '';
  112. if(array_key_exists('url', $params))
  113. $url = $params['url'];
  114. // Check if there was a login token in the query string and whether it has autosubmit=true
  115. $autosubmit = false;
  116. if(array_key_exists('token', $params)) {
  117. try {
  118. $data = JWT::decode($params['token'], Config::$jwtSecret, ['HS256']);
  119. $autosubmit = isset($data->autosubmit) && $data->autosubmit;
  120. } catch(Exception $e) {
  121. }
  122. }
  123. render('new-favorite', array(
  124. 'title' => 'New Favorite',
  125. 'url' => $url,
  126. 'token' => generate_login_token(['autosubmit'=>true]),
  127. 'authorizing' => false,
  128. 'autosubmit' => $autosubmit
  129. ));
  130. }
  131. });
  132. $app->get('/event', function() use($app) {
  133. if($user=require_login($app)) {
  134. $params = $app->request()->params();
  135. render('event', array(
  136. 'title' => 'Event',
  137. 'authorizing' => false
  138. ));
  139. }
  140. });
  141. $app->get('/itinerary', function() use($app) {
  142. if($user=require_login($app)) {
  143. $params = $app->request()->params();
  144. render('new-itinerary', array(
  145. 'title' => 'Itinerary',
  146. 'authorizing' => false
  147. ));
  148. }
  149. });
  150. $app->get('/photo', function() use($app) {
  151. if($user=require_login($app)) {
  152. $params = $app->request()->params();
  153. render('photo', array(
  154. 'title' => 'New Photo',
  155. 'note_content' => '',
  156. 'authorizing' => false
  157. ));
  158. }
  159. });
  160. $app->get('/review', function() use($app) {
  161. if($user=require_login($app)) {
  162. $params = $app->request()->params();
  163. render('review', array(
  164. 'title' => 'Review',
  165. 'authorizing' => false
  166. ));
  167. }
  168. });
  169. $app->get('/repost', function() use($app) {
  170. if($user=require_login($app)) {
  171. $params = $app->request()->params();
  172. $url = '';
  173. if(array_key_exists('url', $params))
  174. $url = $params['url'];
  175. render('new-repost', array(
  176. 'title' => 'New Repost',
  177. 'url' => $url,
  178. 'token' => generate_login_token(),
  179. 'authorizing' => false
  180. ));
  181. }
  182. });
  183. $app->post('/prefs', function() use($app) {
  184. if($user=require_login($app)) {
  185. $params = $app->request()->params();
  186. $user->location_enabled = $params['enabled'];
  187. $user->save();
  188. }
  189. $app->response()['Content-type'] = 'application/json';
  190. $app->response()->body(json_encode(array(
  191. 'result' => 'ok'
  192. )));
  193. });
  194. $app->post('/prefs/timezone', function() use($app) {
  195. // Called when the interface finds the user's location.
  196. // Look up the timezone for this location and store it as their default.
  197. $timezone = false;
  198. if($user=require_login($app)) {
  199. $params = $app->request()->params();
  200. $timezone = p3k\Timezone::timezone_for_location($params['latitude'], $params['longitude']);
  201. if($timezone) {
  202. $user->default_timezone = $timezone;
  203. $user->save();
  204. }
  205. }
  206. $app->response()['Content-type'] = 'application/json';
  207. $app->response()->body(json_encode(array(
  208. 'result' => 'ok',
  209. 'timezone' => $timezone,
  210. )));
  211. });
  212. $app->get('/add-to-home', function() use($app) {
  213. $params = $app->request()->params();
  214. header("Cache-Control: no-cache, must-revalidate");
  215. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  216. unset($_SESSION['add-to-home-started']);
  217. // Verify the token and sign the user in
  218. try {
  219. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  220. $_SESSION['user_id'] = $data->user_id;
  221. $_SESSION['me'] = $data->me;
  222. $app->redirect('/new', 302);
  223. } catch(DomainException $e) {
  224. header('X-Error: DomainException');
  225. $app->redirect('/', 302);
  226. } catch(UnexpectedValueException $e) {
  227. header('X-Error: UnexpectedValueException');
  228. $app->redirect('/', 302);
  229. }
  230. } else {
  231. if($user=require_login($app)) {
  232. if(array_key_exists('start', $params)) {
  233. $_SESSION['add-to-home-started'] = true;
  234. $token = JWT::encode(array(
  235. 'user_id' => $_SESSION['user_id'],
  236. 'me' => $_SESSION['me'],
  237. 'created_at' => time()
  238. ), Config::$jwtSecret);
  239. $app->redirect('/add-to-home?token='.$token, 302);
  240. } else {
  241. unset($_SESSION['add-to-home-started']);
  242. render('add-to-home', array('title' => 'Quill'));
  243. }
  244. }
  245. }
  246. });
  247. $app->get('/email', function() use($app) {
  248. if($user=require_login($app)) {
  249. $test_response = '';
  250. if($user->last_micropub_response) {
  251. try {
  252. if(@json_decode($user->last_micropub_response)) {
  253. $d = json_decode($user->last_micropub_response);
  254. $test_response = $d->response;
  255. }
  256. } catch(Exception $e) {
  257. }
  258. }
  259. if(!$user->email_username) {
  260. $host = parse_url($user->url, PHP_URL_HOST);
  261. $user->email_username = $host . '.' . rand(100000,999999);
  262. $user->save();
  263. }
  264. render('email', array(
  265. 'title' => 'Post-by-Email',
  266. 'micropub_endpoint' => $user->micropub_endpoint,
  267. 'test_response' => $test_response,
  268. 'user' => $user
  269. ));
  270. }
  271. });
  272. $app->get('/settings', function() use($app) {
  273. if($user=require_login($app)) {
  274. render('settings', [
  275. 'title' => 'Settings',
  276. 'user' => $user,
  277. 'authorizing' => false
  278. ]);
  279. }
  280. });
  281. $app->post('/settings/save', function() use($app) {
  282. if($user=require_login($app)) {
  283. $params = $app->request()->params();
  284. if(array_key_exists('html_content', $params))
  285. $user->micropub_optin_html_content = $params['html_content'] ? 1 : 0;
  286. if(array_key_exists('slug_field', $params) && $params['slug_field'])
  287. $user->micropub_slug_field = $params['slug_field'];
  288. if(array_key_exists('syndicate_field', $params) && $params['syndicate_field']) {
  289. if(in_array($params['syndicate_field'], ['syndicate-to','mp-syndicate-to']))
  290. $user->micropub_syndicate_field = $params['syndicate_field'];
  291. }
  292. $user->save();
  293. $app->response()['Content-type'] = 'application/json';
  294. $app->response()->body(json_encode(array(
  295. 'result' => 'ok'
  296. )));
  297. }
  298. });
  299. $app->post('/settings/html-content', function() use($app) {
  300. if($user=require_login($app)) {
  301. $params = $app->request()->params();
  302. $user->micropub_optin_html_content = $params['html'] ? 1 : 0;
  303. $user->save();
  304. $app->response()['Content-type'] = 'application/json';
  305. $app->response()->body(json_encode(array(
  306. 'html' => $user->micropub_optin_html_content
  307. )));
  308. }
  309. });
  310. $app->get('/settings/html-content', function() use($app) {
  311. if($user=require_login($app)) {
  312. $app->response()['Content-type'] = 'application/json';
  313. $app->response()->body(json_encode(array(
  314. 'html' => $user->micropub_optin_html_content
  315. )));
  316. }
  317. });
  318. function create_favorite(&$user, $url) {
  319. $micropub_request = array(
  320. 'like-of' => $url
  321. );
  322. $r = micropub_post_for_user($user, $micropub_request);
  323. $tweet_id = false;
  324. // POSSE favorites to Twitter
  325. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  326. $tweet_id = $match[1];
  327. $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
  328. $user->twitter_access_token, $user->twitter_token_secret);
  329. $result = $twitter->post('favorites/create', array(
  330. 'id' => $tweet_id
  331. ));
  332. }
  333. return $r;
  334. }
  335. function create_repost(&$user, $url) {
  336. $micropub_request = array(
  337. 'repost-of' => $url
  338. );
  339. $r = micropub_post_for_user($user, $micropub_request);
  340. $tweet_id = false;
  341. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  342. $tweet_id = $match[1];
  343. $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
  344. $user->twitter_access_token, $user->twitter_token_secret);
  345. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  346. }
  347. return $r;
  348. }
  349. $app->post('/favorite', function() use($app) {
  350. if($user=require_login($app)) {
  351. $params = $app->request()->params();
  352. $r = create_favorite($user, $params['url']);
  353. $app->response()['Content-type'] = 'application/json';
  354. $app->response()->body(json_encode(array(
  355. 'location' => $r['location'],
  356. 'error' => $r['error']
  357. )));
  358. }
  359. });
  360. $app->post('/repost', function() use($app) {
  361. if($user=require_login($app)) {
  362. $params = $app->request()->params();
  363. $r = create_repost($user, $params['url']);
  364. $app->response()['Content-type'] = 'application/json';
  365. $app->response()->body(json_encode(array(
  366. 'location' => $r['location'],
  367. 'error' => $r['error']
  368. )));
  369. }
  370. });
  371. $app->get('/reply/preview', function() use($app) {
  372. if($user=require_login($app)) {
  373. $params = $app->request()->params();
  374. if(!isset($params['url']) || !$params['url']) {
  375. return '';
  376. }
  377. $reply_url = trim($params['url']);
  378. if(preg_match('/twtr\.io\/([0-9a-z]+)/i', $reply_url, $match)) {
  379. $twtr = 'https://twitter.com/_/status/' . sxg_to_num($match[1]);
  380. $ch = curl_init($twtr);
  381. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  382. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  383. curl_exec($ch);
  384. $expanded_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  385. if($expanded_url) $reply_url = $expanded_url;
  386. }
  387. $entry = false;
  388. $xray = [
  389. 'url' => $reply_url
  390. ];
  391. if(preg_match('/twitter\.com\/(?:[^\/]+)\/statuse?s?\/(.+)/', $reply_url, $match)) {
  392. if($user->twitter_access_token) {
  393. $xray['twitter_api_key'] = Config::$twitterClientID;
  394. $xray['twitter_api_secret'] = Config::$twitterClientSecret;
  395. $xray['twitter_access_token'] = $user->twitter_access_token;
  396. $xray['twitter_access_token_secret'] = $user->twitter_token_secret;
  397. }
  398. }
  399. // Pass to X-Ray to see if it can expand the entry
  400. $ch = curl_init('https://xray.p3k.io/parse');
  401. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  402. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($xray));
  403. $response = curl_exec($ch);
  404. $data = @json_decode($response, true);
  405. if($data && isset($data['data']) && $data['data']['type'] == 'entry') {
  406. $entry = $data['data'];
  407. // Create a nickname based on the author URL
  408. if(array_key_exists('author', $entry)) {
  409. if($entry['author']['url']) {
  410. if(!isset($entry['author']['nickname']) || !$entry['author']['nickname'])
  411. $entry['author']['nickname'] = display_url($entry['author']['url']);
  412. }
  413. }
  414. }
  415. $mentions = [];
  416. if($entry) {
  417. if(array_key_exists('author', $entry)) {
  418. // Find all @-names in the post, as well as the author name
  419. $mentions[] = strtolower($entry['author']['nickname']);
  420. }
  421. if(preg_match_all('/(^|(?<=[\s\/]))@([a-z0-9_]+([a-z0-9_\.]*)?)/i', $entry['content']['text'], $matches)) {
  422. foreach($matches[0] as $nick) {
  423. if(trim($nick,'@') != $user->twitter_username && trim($nick,'@') != display_url($user->url))
  424. $mentions[] = strtolower(trim($nick,'@'));
  425. }
  426. }
  427. $mentions = array_values(array_unique($mentions));
  428. }
  429. $app->response()['Content-type'] = 'application/json';
  430. $app->response()->body(json_encode([
  431. 'canonical_reply_url' => $reply_url,
  432. 'entry' => $entry,
  433. 'mentions' => $mentions
  434. ]));
  435. }
  436. });