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.

847 lines
24 KiB

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