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.

987 lines
28 KiB

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