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.

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