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.

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