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.

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