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.

504 lines
14 KiB

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