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.

977 lines
28 KiB

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