Mirror of Quill
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.

502 lines
14 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. $user->micropub_syndicate_field = $params['syndicate_field'];
  280. $user->save();
  281. $app->response()['Content-type'] = 'application/json';
  282. $app->response()->body(json_encode(array(
  283. 'result' => 'ok'
  284. )));
  285. }
  286. });
  287. $app->post('/settings/html-content', function() use($app) {
  288. if($user=require_login($app)) {
  289. $params = $app->request()->params();
  290. $user->micropub_optin_html_content = $params['html'] ? 1 : 0;
  291. $user->save();
  292. $app->response()['Content-type'] = 'application/json';
  293. $app->response()->body(json_encode(array(
  294. 'html' => $user->micropub_optin_html_content
  295. )));
  296. }
  297. });
  298. $app->get('/settings/html-content', function() use($app) {
  299. if($user=require_login($app)) {
  300. $app->response()['Content-type'] = 'application/json';
  301. $app->response()->body(json_encode(array(
  302. 'html' => $user->micropub_optin_html_content
  303. )));
  304. }
  305. });
  306. function create_favorite(&$user, $url) {
  307. $micropub_request = array(
  308. 'like-of' => $url
  309. );
  310. $r = micropub_post_for_user($user, $micropub_request);
  311. $tweet_id = false;
  312. // POSSE favorites to Twitter
  313. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  314. $tweet_id = $match[1];
  315. $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
  316. $user->twitter_access_token, $user->twitter_token_secret);
  317. $result = $twitter->post('favorites/create', array(
  318. 'id' => $tweet_id
  319. ));
  320. }
  321. return $r;
  322. }
  323. function create_repost(&$user, $url) {
  324. $micropub_request = array(
  325. 'repost-of' => $url
  326. );
  327. $r = micropub_post_for_user($user, $micropub_request);
  328. $tweet_id = false;
  329. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  330. $tweet_id = $match[1];
  331. $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
  332. $user->twitter_access_token, $user->twitter_token_secret);
  333. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  334. }
  335. return $r;
  336. }
  337. $app->post('/favorite', function() use($app) {
  338. if($user=require_login($app)) {
  339. $params = $app->request()->params();
  340. $r = create_favorite($user, $params['url']);
  341. $app->response()['Content-type'] = 'application/json';
  342. $app->response()->body(json_encode(array(
  343. 'location' => $r['location'],
  344. 'error' => $r['error']
  345. )));
  346. }
  347. });
  348. $app->post('/repost', function() use($app) {
  349. if($user=require_login($app)) {
  350. $params = $app->request()->params();
  351. $r = create_repost($user, $params['url']);
  352. $app->response()['Content-type'] = 'application/json';
  353. $app->response()->body(json_encode(array(
  354. 'location' => $r['location'],
  355. 'error' => $r['error']
  356. )));
  357. }
  358. });
  359. $app->get('/reply/preview', function() use($app) {
  360. if($user=require_login($app)) {
  361. $params = $app->request()->params();
  362. if(!isset($params['url']) || !$params['url']) {
  363. return '';
  364. }
  365. $reply_url = trim($params['url']);
  366. if(preg_match('/twtr\.io\/([0-9a-z]+)/i', $reply_url, $match)) {
  367. $twtr = 'https://twitter.com/_/status/' . sxg_to_num($match[1]);
  368. $ch = curl_init($twtr);
  369. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  370. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  371. curl_exec($ch);
  372. $expanded_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  373. if($expanded_url) $reply_url = $expanded_url;
  374. }
  375. $entry = false;
  376. $xray = [
  377. 'url' => $reply_url
  378. ];
  379. if(preg_match('/twitter\.com\/(?:[^\/]+)\/statuse?s?\/(.+)/', $reply_url, $match)) {
  380. if($user->twitter_access_token) {
  381. $xray['twitter_api_key'] = Config::$twitterClientID;
  382. $xray['twitter_api_secret'] = Config::$twitterClientSecret;
  383. $xray['twitter_access_token'] = $user->twitter_access_token;
  384. $xray['twitter_access_token_secret'] = $user->twitter_token_secret;
  385. }
  386. }
  387. // Pass to X-Ray to see if it can expand the entry
  388. $ch = curl_init('https://xray.p3k.io/parse');
  389. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  390. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($xray));
  391. $response = curl_exec($ch);
  392. $data = @json_decode($response, true);
  393. if($data && isset($data['data']) && $data['data']['type'] == 'entry') {
  394. $entry = $data['data'];
  395. // Create a nickname based on the author URL
  396. if(array_key_exists('author', $entry)) {
  397. if($entry['author']['url']) {
  398. if(!isset($entry['author']['nickname']) || !$entry['author']['nickname'])
  399. $entry['author']['nickname'] = display_url($entry['author']['url']);
  400. }
  401. }
  402. }
  403. $mentions = [];
  404. if($entry) {
  405. if(array_key_exists('author', $entry)) {
  406. // Find all @-names in the post, as well as the author name
  407. $mentions[] = strtolower($entry['author']['nickname']);
  408. }
  409. if(preg_match_all('/(^|(?<=[\s\/]))@([a-z0-9_]+([a-z0-9_\.]*)?)/i', $entry['content']['text'], $matches)) {
  410. foreach($matches[0] as $nick) {
  411. if(trim($nick,'@') != $user->twitter_username && trim($nick,'@') != display_url($user->url))
  412. $mentions[] = strtolower(trim($nick,'@'));
  413. }
  414. }
  415. $mentions = array_values(array_unique($mentions));
  416. }
  417. $app->response()['Content-type'] = 'application/json';
  418. $app->response()->body(json_encode([
  419. 'canonical_reply_url' => $reply_url,
  420. 'entry' => $entry,
  421. 'mentions' => $mentions
  422. ]));
  423. }
  424. });