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.

993 lines
28 KiB

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