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.

933 lines
27 KiB

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