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.

888 lines
26 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
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
  1. <?php
  2. use Abraham\TwitterOAuth\TwitterOAuth;
  3. use IndieWeb\DateFormatter;
  4. function require_login(&$app, $redirect=true) {
  5. $params = $app->request()->params();
  6. if(array_key_exists('token', $params)) {
  7. try {
  8. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  9. $_SESSION['user_id'] = $data->user_id;
  10. $_SESSION['me'] = $data->me;
  11. } catch(DomainException $e) {
  12. if($redirect) {
  13. header('X-Error: DomainException');
  14. $app->redirect('/', 302);
  15. } else {
  16. return false;
  17. }
  18. } catch(UnexpectedValueException $e) {
  19. if($redirect) {
  20. header('X-Error: UnexpectedValueException');
  21. $app->redirect('/', 302);
  22. } else {
  23. return false;
  24. }
  25. }
  26. }
  27. if(!array_key_exists('user_id', $_SESSION)) {
  28. if($redirect)
  29. $app->redirect('/', 302);
  30. return false;
  31. } else {
  32. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  33. }
  34. }
  35. function generate_login_token($opts=[]) {
  36. return JWT::encode(array_merge([
  37. 'user_id' => $_SESSION['user_id'],
  38. 'me' => $_SESSION['me'],
  39. 'created_at' => time()
  40. ], $opts), Config::$jwtSecret);
  41. }
  42. $app->get('/dashboard', function() use($app) {
  43. if($user=require_login($app)) {
  44. render('dashboard', array(
  45. 'title' => 'Dashboard',
  46. 'authorizing' => false,
  47. 'user' => $user,
  48. ));
  49. }
  50. });
  51. $app->get('/new', function() use($app) {
  52. if($user=require_login($app)) {
  53. $params = $app->request()->params();
  54. $entry = false;
  55. $in_reply_to = '';
  56. if(array_key_exists('reply', $params))
  57. $in_reply_to = $params['reply'];
  58. render('new-post', array(
  59. 'title' => 'New Post',
  60. 'in_reply_to' => $in_reply_to,
  61. 'micropub_endpoint' => $user->micropub_endpoint,
  62. 'media_endpoint' => $user->micropub_media_endpoint,
  63. 'micropub_scope' => $user->micropub_scope,
  64. 'micropub_access_token' => $user->micropub_access_token,
  65. 'response_date' => $user->last_micropub_response_date,
  66. 'syndication_targets' => json_decode($user->syndication_targets, true),
  67. 'supported_visibility' => json_decode($user->supported_visibility, true),
  68. 'location_enabled' => $user->location_enabled,
  69. 'user' => $user,
  70. 'authorizing' => false
  71. ));
  72. }
  73. });
  74. $app->get('/new/last-photo.json', function() use($app) {
  75. if($user=require_login($app)) {
  76. $url = null;
  77. if($user->micropub_media_endpoint) {
  78. // Request the last file uploaded from the media endpoint
  79. $response = micropub_get($user->micropub_media_endpoint, ['q'=>'last'], $user->micropub_access_token);
  80. if(isset($response['data']['url'])) {
  81. $url = $response['data']['url'];
  82. }
  83. }
  84. $app->response()['Content-type'] = 'application/json';
  85. $app->response()->body(json_encode(array(
  86. 'url' => $url
  87. )));
  88. }
  89. });
  90. $app->get('/bookmark', function() use($app) {
  91. if($user=require_login($app)) {
  92. $params = $app->request()->params();
  93. $url = '';
  94. $name = '';
  95. $content = '';
  96. $tags = '';
  97. if(array_key_exists('url', $params))
  98. $url = $params['url'];
  99. if(array_key_exists('name', $params))
  100. $name = $params['name'];
  101. if(array_key_exists('content', $params))
  102. $content = $params['content'];
  103. render('new-bookmark', array(
  104. 'title' => 'New Bookmark',
  105. 'bookmark_url' => $url,
  106. 'bookmark_name' => $name,
  107. 'bookmark_content' => $content,
  108. 'bookmark_tags' => $tags,
  109. 'token' => generate_login_token(),
  110. 'syndication_targets' => json_decode($user->syndication_targets, true),
  111. 'user' => $user,
  112. 'authorizing' => false
  113. ));
  114. }
  115. });
  116. $app->get('/favorite', function() use($app) {
  117. if($user=require_login($app)) {
  118. $params = $app->request()->params();
  119. $like_of = '';
  120. if(array_key_exists('url', $params))
  121. $like_of = $params['url'];
  122. // Check if there was a login token in the query string and whether it has autosubmit=true
  123. $autosubmit = false;
  124. if(array_key_exists('token', $params)) {
  125. try {
  126. $data = JWT::decode($params['token'], Config::$jwtSecret, ['HS256']);
  127. if(isset($data->autosubmit) && $data->autosubmit) {
  128. // Only allow this token to be used for the user who created it
  129. if($data->user_id == $_SESSION['user_id']) {
  130. $autosubmit = true;
  131. }
  132. }
  133. } catch(Exception $e) {
  134. }
  135. }
  136. if(array_key_exists('edit', $params)) {
  137. $edit_data = get_micropub_source($user, $params['edit'], 'like-of');
  138. $url = $params['edit'];
  139. if(isset($edit_data['like-of'])) {
  140. $like_of = $edit_data['like-of'][0];
  141. }
  142. } else {
  143. $edit_data = false;
  144. $url = false;
  145. }
  146. render('new-favorite', array(
  147. 'title' => 'New Favorite',
  148. 'like_of' => $like_of,
  149. 'token' => generate_login_token(['autosubmit'=>true]),
  150. 'authorizing' => false,
  151. 'autosubmit' => $autosubmit,
  152. 'url' => $url
  153. ));
  154. }
  155. });
  156. $app->get('/event', function() use($app) {
  157. if($user=require_login($app)) {
  158. $params = $app->request()->params();
  159. render('event', array(
  160. 'title' => 'Event',
  161. 'authorizing' => false
  162. ));
  163. }
  164. });
  165. $app->get('/itinerary', function() use($app) {
  166. if($user=require_login($app)) {
  167. $params = $app->request()->params();
  168. render('new-itinerary', array(
  169. 'title' => 'Itinerary',
  170. 'authorizing' => false
  171. ));
  172. }
  173. });
  174. $app->get('/photo', function() use($app) {
  175. if($user=require_login($app)) {
  176. $params = $app->request()->params();
  177. render('photo', array(
  178. 'title' => 'New Photo',
  179. 'note_content' => '',
  180. 'authorizing' => false
  181. ));
  182. }
  183. });
  184. $app->get('/review', function() use($app) {
  185. if($user=require_login($app)) {
  186. $params = $app->request()->params();
  187. render('review', array(
  188. 'title' => 'Review',
  189. 'authorizing' => false
  190. ));
  191. }
  192. });
  193. $app->get('/repost', function() use($app) {
  194. if($user=require_login($app)) {
  195. $params = $app->request()->params();
  196. $repost_of = '';
  197. if(array_key_exists('url', $params))
  198. $repost_of = $params['url'];
  199. if(array_key_exists('edit', $params)) {
  200. $edit_data = get_micropub_source($user, $params['edit'], 'repost-of');
  201. $url = $params['edit'];
  202. if(isset($edit_data['repost-of'])) {
  203. $repost = $edit_data['repost-of'][0];
  204. if(is_string($edit_data['repost-of'][0])) {
  205. $repost_of = $repost;
  206. } elseif(is_array($repost)) {
  207. if(array_key_exists('type', $repost) && in_array('h-cite', $repost)) {
  208. if(array_key_exists('url', $repost['properties'])) {
  209. $repost_of = $repost['properties']['url'][0];
  210. }
  211. } else {
  212. // Error
  213. }
  214. } else {
  215. // Error: don't know what type of post this is
  216. }
  217. }
  218. } else {
  219. $edit_data = false;
  220. $url = false;
  221. }
  222. render('new-repost', array(
  223. 'title' => 'New Repost',
  224. 'repost_of' => $repost_of,
  225. 'token' => generate_login_token(),
  226. 'authorizing' => false,
  227. 'url' => $url,
  228. ));
  229. }
  230. });
  231. $app->post('/prefs', function() use($app) {
  232. if($user=require_login($app)) {
  233. $params = $app->request()->params();
  234. $user->location_enabled = $params['enabled'];
  235. $user->save();
  236. }
  237. $app->response()['Content-type'] = 'application/json';
  238. $app->response()->body(json_encode(array(
  239. 'result' => 'ok'
  240. )));
  241. });
  242. $app->post('/prefs/timezone', function() use($app) {
  243. // Called when the interface finds the user's location.
  244. // Look up the timezone for this location and store it as their default.
  245. $timezone = false;
  246. if($user=require_login($app)) {
  247. $params = $app->request()->params();
  248. $timezone = p3k\Timezone::timezone_for_location($params['latitude'], $params['longitude']);
  249. if($timezone) {
  250. $user->default_timezone = $timezone;
  251. $user->save();
  252. }
  253. }
  254. $app->response()['Content-type'] = 'application/json';
  255. $app->response()->body(json_encode(array(
  256. 'result' => 'ok',
  257. 'timezone' => $timezone,
  258. )));
  259. });
  260. $app->get('/add-to-home', function() use($app) {
  261. $params = $app->request()->params();
  262. header("Cache-Control: no-cache, must-revalidate");
  263. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  264. unset($_SESSION['add-to-home-started']);
  265. // Verify the token and sign the user in
  266. try {
  267. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  268. $_SESSION['user_id'] = $data->user_id;
  269. $_SESSION['me'] = $data->me;
  270. $app->redirect('/new', 302);
  271. } catch(DomainException $e) {
  272. header('X-Error: DomainException');
  273. $app->redirect('/', 302);
  274. } catch(UnexpectedValueException $e) {
  275. header('X-Error: UnexpectedValueException');
  276. $app->redirect('/', 302);
  277. }
  278. } else {
  279. if($user=require_login($app)) {
  280. if(array_key_exists('start', $params)) {
  281. $_SESSION['add-to-home-started'] = true;
  282. $token = JWT::encode(array(
  283. 'user_id' => $_SESSION['user_id'],
  284. 'me' => $_SESSION['me'],
  285. 'created_at' => time()
  286. ), Config::$jwtSecret);
  287. $app->redirect('/add-to-home?token='.$token, 302);
  288. } else {
  289. unset($_SESSION['add-to-home-started']);
  290. render('add-to-home', array('title' => 'Quill'));
  291. }
  292. }
  293. }
  294. });
  295. $app->get('/email', function() use($app) {
  296. if($user=require_login($app)) {
  297. if(!$user->email_username) {
  298. $host = parse_url($user->url, PHP_URL_HOST);
  299. $user->email_username = $host . '.' . rand(100000,999999);
  300. $user->save();
  301. }
  302. render('email', array(
  303. 'title' => 'Post-by-Email',
  304. 'micropub_endpoint' => $user->micropub_endpoint,
  305. 'user' => $user
  306. ));
  307. }
  308. });
  309. $app->get('/settings', function() use($app) {
  310. if($user=require_login($app)) {
  311. render('settings', [
  312. 'title' => 'Settings',
  313. 'user' => $user,
  314. 'syndication_targets' => json_decode($user->syndication_targets, true),
  315. 'authorizing' => false
  316. ]);
  317. }
  318. });
  319. $app->post('/settings/save', function() use($app) {
  320. if($user=require_login($app)) {
  321. $params = $app->request()->params();
  322. if(array_key_exists('html_content', $params))
  323. $user->micropub_optin_html_content = $params['html_content'] ? 1 : 0;
  324. if(array_key_exists('slug_field', $params) && $params['slug_field'])
  325. $user->micropub_slug_field = $params['slug_field'];
  326. if(array_key_exists('syndicate_field', $params) && $params['syndicate_field']) {
  327. if(in_array($params['syndicate_field'], ['syndicate-to','mp-syndicate-to']))
  328. $user->micropub_syndicate_field = $params['syndicate_field'];
  329. }
  330. $user->save();
  331. $app->response()['Content-type'] = 'application/json';
  332. $app->response()->body(json_encode(array(
  333. 'result' => 'ok'
  334. )));
  335. }
  336. });
  337. $app->post('/settings/html-content', function() use($app) {
  338. if($user=require_login($app)) {
  339. $params = $app->request()->params();
  340. $user->micropub_optin_html_content = $params['html'] ? 1 : 0;
  341. $user->save();
  342. $app->response()['Content-type'] = 'application/json';
  343. $app->response()->body(json_encode(array(
  344. 'html' => $user->micropub_optin_html_content
  345. )));
  346. }
  347. });
  348. $app->get('/settings/html-content', function() use($app) {
  349. if($user=require_login($app)) {
  350. $app->response()['Content-type'] = 'application/json';
  351. $app->response()->body(json_encode(array(
  352. 'html' => $user->micropub_optin_html_content
  353. )));
  354. }
  355. });
  356. function create_favorite(&$user, $url) {
  357. $tweet_id = false;
  358. $twitter_syndication = false;
  359. // POSSE favorites to Twitter
  360. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  361. $tweet_id = $match[1];
  362. $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
  363. $user->twitter_access_token, $user->twitter_token_secret);
  364. $result = $twitter->post('favorites/create', array(
  365. 'id' => $tweet_id
  366. ));
  367. if(property_exists($result, 'id_str')) {
  368. $twitter_syndication = 'https://twitter.com/'.$user->twitter_username.'/status/'.$result->id_str;
  369. }
  370. }
  371. $micropub_request = array(
  372. 'like-of' => $url
  373. );
  374. if($twitter_syndication) {
  375. $micropub_request['syndication'] = $twitter_syndication;
  376. }
  377. $r = micropub_post_for_user($user, $micropub_request);
  378. return $r;
  379. }
  380. function edit_favorite(&$user, $post_url, $like_of) {
  381. $micropub_request = [
  382. 'action' => 'update',
  383. 'url' => $post_url,
  384. 'replace' => [
  385. 'like-of' => [$like_of]
  386. ]
  387. ];
  388. $r = micropub_post_for_user($user, $micropub_request, null, true);
  389. return $r;
  390. }
  391. function create_repost(&$user, $url) {
  392. $tweet_id = false;
  393. $twitter_syndication = false;
  394. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  395. $tweet_id = $match[1];
  396. $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
  397. $user->twitter_access_token, $user->twitter_token_secret);
  398. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  399. if(property_exists($result, 'id_str')) {
  400. $twitter_syndication = 'https://twitter.com/'.$user->twitter_username.'/status/'.$result->id_str;
  401. }
  402. }
  403. $micropub_request = array(
  404. 'repost-of' => $url
  405. );
  406. if($twitter_syndication) {
  407. $micropub_request['syndication'] = $twitter_syndication;
  408. }
  409. $r = micropub_post_for_user($user, $micropub_request);
  410. return $r;
  411. }
  412. function edit_repost(&$user, $post_url, $repost_of) {
  413. $micropub_request = [
  414. 'action' => 'update',
  415. 'url' => $post_url,
  416. 'replace' => [
  417. 'repost-of' => [$repost_of]
  418. ]
  419. ];
  420. $r = micropub_post_for_user($user, $micropub_request, null, true);
  421. return $r;
  422. }
  423. $app->post('/favorite', function() use($app) {
  424. if($user=require_login($app)) {
  425. $params = $app->request()->params();
  426. $error = false;
  427. if(isset($params['edit']) && $params['edit']) {
  428. $r = edit_favorite($user, $params['edit'], $params['like_of']);
  429. if(isset($r['location']) && $r['location'])
  430. $location = $r['location'];
  431. elseif(in_array($r['code'], [200,201,204]))
  432. $location = $params['edit'];
  433. elseif(in_array($r['code'], [401,403])) {
  434. $location = false;
  435. $error = 'Your Micropub endpoint denied the request. Check that Quill is authorized to update posts.';
  436. } else {
  437. $location = false;
  438. $error = 'Your Micropub endpoint did not return a location header or a recognized response code';
  439. }
  440. } else {
  441. $r = create_favorite($user, $params['like_of']);
  442. $location = $r['location'];
  443. }
  444. $app->response()['Content-type'] = 'application/json';
  445. $app->response()->body(json_encode(array(
  446. 'location' => $location,
  447. 'error' => $r['error'],
  448. 'error_details' => $error,
  449. )));
  450. }
  451. });
  452. $app->post('/repost', function() use($app) {
  453. if($user=require_login($app)) {
  454. $params = $app->request()->params();
  455. $error = false;
  456. if(isset($params['edit']) && $params['edit']) {
  457. $r = edit_repost($user, $params['edit'], $params['repost_of']);
  458. if(isset($r['location']) && $r['location'])
  459. $location = $r['location'];
  460. elseif(in_array($r['code'], [200,201,204]))
  461. $location = $params['edit'];
  462. elseif(in_array($r['code'], [401,403])) {
  463. $location = false;
  464. $error = 'Your Micropub endpoint denied the request. Check that Quill is authorized to update posts.';
  465. } else {
  466. $location = false;
  467. $error = 'Your Micropub endpoint did not return a location header or a recognized response code';
  468. }
  469. } else {
  470. $r = create_repost($user, $params['repost_of']);
  471. $location = $r['location'];
  472. }
  473. $app->response()['Content-type'] = 'application/json';
  474. $app->response()->body(json_encode(array(
  475. 'location' => $location,
  476. 'error' => $r['error'],
  477. 'error_details' => $error,
  478. )));
  479. }
  480. });
  481. $app->get('/code', function() use($app) {
  482. if($user=require_login($app)) {
  483. $params = $app->request()->params();
  484. $edit_data = ['content'=>'','name'=>''];
  485. if(array_key_exists('edit', $params)) {
  486. $source = get_micropub_source($user, $params['edit'], ['content','name']);
  487. if(isset($source['content']) && is_array($source['content']) && isset($source['content'][0]))
  488. $edit_data['content'] = $source['content'][0];
  489. if(isset($source['name']) && is_array($source['name']) && isset($source['name'][0]))
  490. $edit_data['name'] = $source['name'][0];
  491. $url = $params['edit'];
  492. } else {
  493. $url = false;
  494. }
  495. $languages = [
  496. 'php' => ['php'],
  497. 'ruby' => ['rb'],
  498. 'python' => ['py'],
  499. 'perl' => ['pl'],
  500. 'javascript' => ['js'],
  501. 'html' => ['html','htm'],
  502. 'css' => ['css'],
  503. 'bash' => ['sh'],
  504. 'nginx' => ['conf'],
  505. 'apache' => [],
  506. 'text' => ['txt'],
  507. ];
  508. ksort($languages);
  509. $language_map = [];
  510. foreach($languages as $lang=>$exts) {
  511. foreach($exts as $ext)
  512. $language_map[$ext] = $lang;
  513. }
  514. render('new-code', array(
  515. 'title' => 'New Code Snippet',
  516. 'url' => $url,
  517. 'edit_data' => $edit_data,
  518. 'token' => generate_login_token(),
  519. 'languages' => $languages,
  520. 'language_map' => $language_map,
  521. 'my_hostname' => parse_url($user->url, PHP_URL_HOST),
  522. 'authorizing' => false,
  523. ));
  524. }
  525. });
  526. $app->post('/code', function() use($app) {
  527. if($user=require_login($app)) {
  528. $params = $app->request()->params();
  529. $error = false;
  530. if(isset($params['edit']) && $params['edit']) {
  531. $micropub_request = [
  532. 'action' => 'update',
  533. 'url' => $params['edit'],
  534. 'replace' => [
  535. 'content' => [$params['content']]
  536. ]
  537. ];
  538. if(isset($params['name']) && $params['name']) {
  539. $micropub_request['replace']['name'] = [$params['name']];
  540. }
  541. $r = micropub_post_for_user($user, $micropub_request, null, true);
  542. if(isset($r['location']) && $r['location'])
  543. $location = $r['location'];
  544. elseif(in_array($r['code'], [200,201,204]))
  545. $location = $params['edit'];
  546. elseif(in_array($r['code'], [401,403])) {
  547. $location = false;
  548. $error = 'Your Micropub endpoint denied the request. Check that Quill is authorized to update posts.';
  549. } else {
  550. $location = false;
  551. $error = 'Your Micropub endpoint did not return a location header or a recognized response code';
  552. }
  553. } else {
  554. $micropub_request = array(
  555. 'p3k-content-type' => 'code/' . $params['language'],
  556. 'content' => $params['content'],
  557. );
  558. if(isset($params['name']) && $params['name'])
  559. $micropub_request['name'] = $params['name'];
  560. $r = micropub_post_for_user($user, $micropub_request);
  561. $location = $r['location'];
  562. }
  563. $app->response()['Content-type'] = 'application/json';
  564. $app->response()->body(json_encode(array(
  565. 'location' => $location,
  566. 'error' => $r['error'],
  567. 'error_details' => $error,
  568. )));
  569. }
  570. });
  571. $app->get('/reply/preview', function() use($app) {
  572. if($user=require_login($app)) {
  573. $params = $app->request()->params();
  574. if(!isset($params['url']) || !$params['url']) {
  575. return '';
  576. }
  577. $reply_url = trim($params['url']);
  578. if(preg_match('/twtr\.io\/([0-9a-z]+)/i', $reply_url, $match)) {
  579. $twtr = 'https://twitter.com/_/status/' . sxg_to_num($match[1]);
  580. $ch = curl_init($twtr);
  581. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  582. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  583. curl_exec($ch);
  584. $expanded_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  585. if($expanded_url) $reply_url = $expanded_url;
  586. }
  587. $entry = false;
  588. $xray_opts = [];
  589. if(preg_match('/twitter\.com\/(?:[^\/]+)\/statuse?s?\/(.+)/', $reply_url, $match)) {
  590. if($user->twitter_access_token) {
  591. $xray_opts['twitter_api_key'] = Config::$twitterClientID;
  592. $xray_opts['twitter_api_secret'] = Config::$twitterClientSecret;
  593. $xray_opts['twitter_access_token'] = $user->twitter_access_token;
  594. $xray_opts['twitter_access_token_secret'] = $user->twitter_token_secret;
  595. }
  596. }
  597. // Pass to X-Ray to see if it can expand the entry
  598. $xray = new p3k\XRay();
  599. $xray->http = new p3k\HTTP('Quill ('.Config::$base_url.')');
  600. $data = $xray->parse($reply_url, $xray_opts);
  601. if($data && isset($data['data'])) {
  602. if($data['data']['type'] == 'entry') {
  603. $entry = $data['data'];
  604. } elseif($data['data']['type'] == 'event') {
  605. $entry = $data['data'];
  606. $content = '';
  607. if(isset($entry['start']) && isset($entry['end'])) {
  608. $formatted = DateFormatter::format($entry['start'], $entry['end'], false);
  609. if($formatted)
  610. $content .= $formatted;
  611. else {
  612. $start = new DateTime($entry['start']);
  613. $end = new DateTime($entry['end']);
  614. if($start && $end)
  615. $content .= 'from '.$start->format('Y-m-d g:ia').' to '.$end->format('Y-m-d g:ia');
  616. }
  617. } elseif(isset($entry['start'])) {
  618. $formatted = DateFormatter::format($entry['start'], false, false);
  619. if($formatted)
  620. $content .= $formatted;
  621. else {
  622. $start = new DateTime($entry['start']);
  623. if($start)
  624. $content .= $start->format('Y-m-d g:ia');
  625. }
  626. }
  627. $entry['content']['text'] = $content;
  628. }
  629. // Create a nickname based on the author URL
  630. if($entry && array_key_exists('author', $entry)) {
  631. if($entry['author']['url']) {
  632. if(!isset($entry['author']['nickname']) || !$entry['author']['nickname'])
  633. $entry['author']['nickname'] = display_url($entry['author']['url']);
  634. }
  635. }
  636. }
  637. $mentions = [];
  638. if($entry) {
  639. if(array_key_exists('author', $entry) && isset($entry['author']['nickname'])) {
  640. // Find all @-names in the post, as well as the author name
  641. $mentions[] = strtolower($entry['author']['nickname']);
  642. }
  643. if(isset($entry['content']) && $entry['content'] && isset($entry['content']['text'])) {
  644. if(preg_match_all('/(^|(?<=[\s\/]))@([a-z0-9_]+([a-z0-9_\.]*)?)/i', $entry['content']['text'], $matches)) {
  645. foreach($matches[0] as $nick) {
  646. if(trim($nick,'@') != $user->twitter_username && trim($nick,'@') != display_url($user->url))
  647. $mentions[] = strtolower(trim($nick,'@'));
  648. }
  649. }
  650. }
  651. $mentions = array_values(array_unique($mentions));
  652. }
  653. $syndications = [];
  654. if($entry && isset($entry['syndication'])) {
  655. foreach($entry['syndication'] as $s) {
  656. $host = parse_url($s, PHP_URL_HOST);
  657. switch($host) {
  658. case 'twitter.com':
  659. case 'www.twitter.com':
  660. $icon = 'twitter.ico'; break;
  661. case 'facebook.com':
  662. case 'www.facebook.com':
  663. $icon = 'facebook.ico'; break;
  664. case 'github.com':
  665. case 'www.github.com':
  666. $icon = 'github.ico'; break;
  667. default:
  668. $icon = 'default.png'; break;
  669. }
  670. $syndications[] = [
  671. 'url' => $s,
  672. 'icon' => $icon
  673. ];
  674. }
  675. }
  676. $app->response()['Content-type'] = 'application/json';
  677. $app->response()->body(json_encode([
  678. 'canonical_reply_url' => $reply_url,
  679. 'entry' => $entry,
  680. 'mentions' => $mentions,
  681. 'syndications' => $syndications,
  682. ]));
  683. }
  684. });
  685. $app->get('/edit', function() use($app) {
  686. if($user=require_login($app)) {
  687. $params = $app->request()->params();
  688. if(!isset($params['url']) || !$params['url']) {
  689. $app->response()->body('no URL specified');
  690. }
  691. // Query the micropub endpoint for the source properties
  692. $source = micropub_get($user->micropub_endpoint, [
  693. 'q' => 'source',
  694. 'url' => $params['url']
  695. ], $user->micropub_access_token);
  696. $data = $source['data'];
  697. if(array_key_exists('error', $data)) {
  698. render('edit/error', [
  699. 'title' => 'Error',
  700. 'summary' => 'Your Micropub endpoint returned an error:',
  701. 'error' => $data['error'],
  702. 'error_description' => $data['error_description']
  703. ]);
  704. return;
  705. }
  706. if(!array_key_exists('properties', $data) || !array_key_exists('type', $data)) {
  707. render('edit/error', [
  708. 'title' => 'Error',
  709. 'summary' => '',
  710. 'error' => 'Invalid Response',
  711. 'error_description' => 'Your endpoint did not return "properties" and "type" in the response.'
  712. ]);
  713. return;
  714. }
  715. // Start checking for content types
  716. $type = $data['type'][0];
  717. $error = false;
  718. $url = false;
  719. if($type == 'h-review') {
  720. $url = '/review';
  721. } elseif($type == 'h-event') {
  722. $url = '/event';
  723. } elseif($type != 'h-entry') {
  724. $error = 'This type of post is not supported by any of Quill\'s editing interfaces. Type: '.$type;
  725. } else {
  726. if(array_key_exists('bookmark-of', $data['properties'])) {
  727. $url = '/bookmark';
  728. } elseif(array_key_exists('like-of', $data['properties'])) {
  729. $url = '/favorite';
  730. } elseif(array_key_exists('repost-of', $data['properties'])) {
  731. $url = '/repost';
  732. }
  733. }
  734. if($error) {
  735. render('edit/error', [
  736. 'title' => 'Error',
  737. 'summary' => '',
  738. 'error' => 'There was a problem!',
  739. 'error_description' => $error
  740. ]);
  741. return;
  742. }
  743. // Until all interfaces are complete, show an error here for unsupported ones
  744. if(!in_array($url, ['/favorite','/repost','/code'])) {
  745. render('edit/error', [
  746. 'title' => 'Not Yet Supported',
  747. 'summary' => '',
  748. 'error' => 'Not Yet Supported',
  749. 'error_description' => 'Editing is not yet supported for this type of post.'
  750. ]);
  751. return;
  752. }
  753. $app->redirect($url . '?edit=' . $params['url'], 302);
  754. }
  755. });
  756. $app->get('/airport-info', function() use($app){
  757. if($user=require_login($app)) {
  758. $params = $app->request()->params();
  759. if(!isset($params['code'])) return;
  760. $ch = curl_init('https://atlas.p3k.io/api/timezone?airport='.urlencode($params['code']));
  761. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  762. $data = json_decode(curl_exec($ch), true);
  763. if(!$data)
  764. $response = ['error' => 'unknown'];
  765. else {
  766. $response = $data;
  767. }
  768. $app->response()['Content-type'] = 'application/json';
  769. $app->response()->body(json_encode($response));
  770. }
  771. });