Mirror of Quill
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.

585 lines
16 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. function require_login(&$app, $redirect=true) {
  3. $params = $app->request()->params();
  4. if(array_key_exists('token', $params)) {
  5. try {
  6. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  7. $_SESSION['user_id'] = $data->user_id;
  8. $_SESSION['me'] = $data->me;
  9. } catch(DomainException $e) {
  10. if($redirect) {
  11. header('X-Error: DomainException');
  12. $app->redirect('/', 301);
  13. } else {
  14. return false;
  15. }
  16. } catch(UnexpectedValueException $e) {
  17. if($redirect) {
  18. header('X-Error: UnexpectedValueException');
  19. $app->redirect('/', 301);
  20. } else {
  21. return false;
  22. }
  23. }
  24. }
  25. if(!array_key_exists('user_id', $_SESSION)) {
  26. if($redirect)
  27. $app->redirect('/');
  28. return false;
  29. } else {
  30. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  31. }
  32. }
  33. function generate_login_token() {
  34. return JWT::encode(array(
  35. 'user_id' => $_SESSION['user_id'],
  36. 'me' => $_SESSION['me'],
  37. 'created_at' => time()
  38. ), Config::$jwtSecret);
  39. }
  40. $app->get('/dashboard', function() use($app) {
  41. if($user=require_login($app)) {
  42. $html = render('dashboard', array(
  43. 'title' => 'Dashboard',
  44. 'authorizing' => false
  45. ));
  46. $app->response()->body($html);
  47. }
  48. });
  49. $app->get('/new', function() use($app) {
  50. if($user=require_login($app)) {
  51. $params = $app->request()->params();
  52. $entry = false;
  53. $in_reply_to = '';
  54. if(array_key_exists('reply', $params))
  55. $in_reply_to = $params['reply'];
  56. $test_response = '';
  57. if($user->last_micropub_response) {
  58. try {
  59. if(@json_decode($user->last_micropub_response)) {
  60. $d = json_decode($user->last_micropub_response);
  61. $test_response = $d->response;
  62. }
  63. } catch(Exception $e) {
  64. }
  65. }
  66. $html = render('new-post', array(
  67. 'title' => 'New Post',
  68. 'in_reply_to' => $in_reply_to,
  69. 'micropub_endpoint' => $user->micropub_endpoint,
  70. 'media_endpoint' => $user->micropub_media_endpoint,
  71. 'micropub_scope' => $user->micropub_scope,
  72. 'micropub_access_token' => $user->micropub_access_token,
  73. 'response_date' => $user->last_micropub_response_date,
  74. 'syndication_targets' => json_decode($user->syndication_targets, true),
  75. 'test_response' => $test_response,
  76. 'location_enabled' => $user->location_enabled,
  77. 'user' => $user,
  78. 'authorizing' => false
  79. ));
  80. $app->response()->body($html);
  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. $html = 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. 'authorizing' => false
  105. ));
  106. $app->response()->body($html);
  107. }
  108. });
  109. $app->get('/favorite', function() use($app) {
  110. if($user=require_login($app)) {
  111. $params = $app->request()->params();
  112. $url = '';
  113. if(array_key_exists('url', $params))
  114. $url = $params['url'];
  115. $html = render('new-favorite', array(
  116. 'title' => 'New Favorite',
  117. 'url' => $url,
  118. 'token' => generate_login_token(),
  119. 'authorizing' => false
  120. ));
  121. $app->response()->body($html);
  122. }
  123. });
  124. $app->get('/event', function() use($app) {
  125. if($user=require_login($app)) {
  126. $params = $app->request()->params();
  127. $html = render('event', array(
  128. 'title' => 'Event',
  129. 'authorizing' => false
  130. ));
  131. $app->response()->body($html);
  132. }
  133. });
  134. $app->get('/itinerary', function() use($app) {
  135. if($user=require_login($app)) {
  136. $params = $app->request()->params();
  137. $html = render('new-itinerary', array(
  138. 'title' => 'Itinerary',
  139. 'authorizing' => false
  140. ));
  141. $app->response()->body($html);
  142. }
  143. });
  144. $app->get('/photo', function() use($app) {
  145. if($user=require_login($app)) {
  146. $params = $app->request()->params();
  147. $html = render('photo', array(
  148. 'title' => 'New Photo',
  149. 'note_content' => '',
  150. 'authorizing' => false
  151. ));
  152. $app->response()->body($html);
  153. }
  154. });
  155. $app->get('/review', function() use($app) {
  156. if($user=require_login($app)) {
  157. $params = $app->request()->params();
  158. $html = render('review', array(
  159. 'title' => 'Review',
  160. 'authorizing' => false
  161. ));
  162. $app->response()->body($html);
  163. }
  164. });
  165. $app->get('/repost', function() use($app) {
  166. if($user=require_login($app)) {
  167. $params = $app->request()->params();
  168. $url = '';
  169. if(array_key_exists('url', $params))
  170. $url = $params['url'];
  171. $html = render('new-repost', array(
  172. 'title' => 'New Repost',
  173. 'url' => $url,
  174. 'token' => generate_login_token(),
  175. 'authorizing' => false
  176. ));
  177. $app->response()->body($html);
  178. }
  179. });
  180. $app->post('/prefs', function() use($app) {
  181. if($user=require_login($app)) {
  182. $params = $app->request()->params();
  183. $user->location_enabled = $params['enabled'];
  184. $user->save();
  185. }
  186. $app->response()['Content-type'] = 'application/json';
  187. $app->response()->body(json_encode(array(
  188. 'result' => 'ok'
  189. )));
  190. });
  191. $app->get('/creating-a-token-endpoint', function() use($app) {
  192. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  193. });
  194. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  195. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint', 'authorizing' => false));
  196. $app->response()->body($html);
  197. });
  198. $app->get('/docs', function() use($app) {
  199. $html = render('docs', array('title' => 'Documentation', 'authorizing' => false));
  200. $app->response()->body($html);
  201. });
  202. $app->get('/privacy', function() use($app) {
  203. $html = render('privacy', array('title' => 'Quill Privacy Policy', 'authorizing' => false));
  204. $app->response()->body($html);
  205. });
  206. $app->get('/add-to-home', function() use($app) {
  207. $params = $app->request()->params();
  208. header("Cache-Control: no-cache, must-revalidate");
  209. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  210. unset($_SESSION['add-to-home-started']);
  211. // Verify the token and sign the user in
  212. try {
  213. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  214. $_SESSION['user_id'] = $data->user_id;
  215. $_SESSION['me'] = $data->me;
  216. $app->redirect('/new', 301);
  217. } catch(DomainException $e) {
  218. header('X-Error: DomainException');
  219. $app->redirect('/', 301);
  220. } catch(UnexpectedValueException $e) {
  221. header('X-Error: UnexpectedValueException');
  222. $app->redirect('/', 301);
  223. }
  224. } else {
  225. if($user=require_login($app)) {
  226. if(array_key_exists('start', $params)) {
  227. $_SESSION['add-to-home-started'] = true;
  228. $token = JWT::encode(array(
  229. 'user_id' => $_SESSION['user_id'],
  230. 'me' => $_SESSION['me'],
  231. 'created_at' => time()
  232. ), Config::$jwtSecret);
  233. $app->redirect('/add-to-home?token='.$token, 301);
  234. } else {
  235. unset($_SESSION['add-to-home-started']);
  236. $html = render('add-to-home', array('title' => 'Quill'));
  237. $app->response()->body($html);
  238. }
  239. }
  240. }
  241. });
  242. $app->get('/email', function() use($app) {
  243. if($user=require_login($app)) {
  244. $test_response = '';
  245. if($user->last_micropub_response) {
  246. try {
  247. if(@json_decode($user->last_micropub_response)) {
  248. $d = json_decode($user->last_micropub_response);
  249. $test_response = $d->response;
  250. }
  251. } catch(Exception $e) {
  252. }
  253. }
  254. if(!$user->email_username) {
  255. $host = parse_url($user->url, PHP_URL_HOST);
  256. $user->email_username = $host . '.' . rand(100000,999999);
  257. $user->save();
  258. }
  259. $html = render('email', array(
  260. 'title' => 'Post-by-Email',
  261. 'micropub_endpoint' => $user->micropub_endpoint,
  262. 'test_response' => $test_response,
  263. 'user' => $user
  264. ));
  265. $app->response()->body($html);
  266. }
  267. });
  268. $app->get('/settings', function() use($app) {
  269. if($user=require_login($app)) {
  270. $html = render('settings', array('title' => 'Settings', 'include_facebook' => true, 'authorizing' => false));
  271. $app->response()->body($html);
  272. }
  273. });
  274. $app->post('/settings/html-content', function() use($app) {
  275. if($user=require_login($app)) {
  276. $params = $app->request()->params();
  277. $user->micropub_optin_html_content = $params['html'] ? 1 : 0;
  278. $user->save();
  279. $app->response()['Content-type'] = 'application/json';
  280. $app->response()->body(json_encode(array(
  281. 'html' => $user->micropub_optin_html_content
  282. )));
  283. }
  284. });
  285. $app->get('/settings/html-content', function() use($app) {
  286. if($user=require_login($app)) {
  287. $app->response()['Content-type'] = 'application/json';
  288. $app->response()->body(json_encode(array(
  289. 'html' => $user->micropub_optin_html_content
  290. )));
  291. }
  292. });
  293. $app->get('/favorite-popup', function() use($app) {
  294. if($user=require_login($app)) {
  295. $params = $app->request()->params();
  296. $html = $app->render('favorite-popup.php', array(
  297. 'url' => $params['url'],
  298. 'token' => $params['token']
  299. ));
  300. $app->response()->body($html);
  301. }
  302. });
  303. function create_favorite(&$user, $url) {
  304. $micropub_request = array(
  305. 'like-of' => $url
  306. );
  307. $r = micropub_post_for_user($user, $micropub_request);
  308. $facebook_id = false;
  309. $instagram_id = false;
  310. $tweet_id = false;
  311. /*
  312. // Facebook likes are posted via Javascript, so pass the FB ID to the javascript code
  313. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/(?:[^\/]+)\/posts\/(\d+)/', $url, $match)) {
  314. $facebook_id = $match[1];
  315. }
  316. if(preg_match('/https?:\/\/(?:www\.)?facebook\.com\/photo\.php\?fbid=(\d+)/', $url, $match)) {
  317. $facebook_id = $match[1];
  318. }
  319. */
  320. if(preg_match('/https?:\/\/(?:www\.)?instagram\.com\/p\/([^\/]+)/', $url, $match)) {
  321. $instagram_id = $match[1];
  322. if($user->instagram_access_token) {
  323. $instagram = instagram_client();
  324. $instagram->setAccessToken($user->instagram_access_token);
  325. $ch = curl_init('https://api.instagram.com/v1/media/shortcode/' . $instagram_id . '?access_token=' . $user->instagram_access_token);
  326. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  327. $result = json_decode(curl_exec($ch));
  328. $result = $instagram->likeMedia($result->data->id);
  329. } else {
  330. // TODO: indicate that the instagram post couldn't be liked because no access token was available
  331. }
  332. }
  333. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  334. $tweet_id = $match[1];
  335. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  336. $user->twitter_access_token, $user->twitter_token_secret);
  337. $result = $twitter->post('favorites/create', array(
  338. 'id' => $tweet_id
  339. ));
  340. }
  341. return $r;
  342. }
  343. function create_repost(&$user, $url) {
  344. $micropub_request = array(
  345. 'repost-of' => $url
  346. );
  347. $r = micropub_post_for_user($user, $micropub_request);
  348. $tweet_id = false;
  349. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  350. $tweet_id = $match[1];
  351. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  352. $user->twitter_access_token, $user->twitter_token_secret);
  353. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  354. }
  355. return $r;
  356. }
  357. $app->get('/favorite.js', function() use($app) {
  358. $app->response()->header("Content-type", "text/javascript");
  359. if($user=require_login($app, false)) {
  360. $params = $app->request()->params();
  361. if(array_key_exists('url', $params)) {
  362. $r = create_favorite($user, $params['url']);
  363. $app->response()->body($app->render('favorite-js.php', array(
  364. 'url' => $params['url'],
  365. 'like_url' => $r['location'],
  366. 'error' => $r['error'],
  367. // 'facebook_id' => $facebook_id
  368. )));
  369. } else {
  370. $app->response()->body('alert("no url");');
  371. }
  372. } else {
  373. $app->response()->body('alert("invalid token");');
  374. }
  375. });
  376. $app->post('/favorite', function() use($app) {
  377. if($user=require_login($app)) {
  378. $params = $app->request()->params();
  379. $r = create_favorite($user, $params['url']);
  380. $app->response()['Content-type'] = 'application/json';
  381. $app->response()->body(json_encode(array(
  382. 'location' => $r['location'],
  383. 'error' => $r['error']
  384. )));
  385. }
  386. });
  387. $app->post('/repost', function() use($app) {
  388. if($user=require_login($app)) {
  389. $params = $app->request()->params();
  390. $r = create_repost($user, $params['url']);
  391. $app->response()['Content-type'] = 'application/json';
  392. $app->response()->body(json_encode(array(
  393. 'location' => $r['location'],
  394. 'error' => $r['error']
  395. )));
  396. }
  397. });
  398. $app->get('/micropub/syndications', function() use($app) {
  399. if($user=require_login($app)) {
  400. $data = get_micropub_config($user, ['q'=>'syndicate-to']);
  401. $app->response()['Content-type'] = 'application/json';
  402. $app->response()->body(json_encode(array(
  403. 'targets' => $data['targets'],
  404. 'response' => $data['response']
  405. )));
  406. }
  407. });
  408. $app->post('/micropub/post', function() use($app) {
  409. if($user=require_login($app)) {
  410. $params = $app->request()->params();
  411. // Remove any blank params
  412. $params = array_filter($params, function($v){
  413. return $v !== '';
  414. });
  415. $r = micropub_post_for_user($user, $params);
  416. $app->response()['Content-type'] = 'application/json';
  417. $app->response()->body(json_encode(array(
  418. 'request' => htmlspecialchars($r['request']),
  419. 'response' => htmlspecialchars($r['response']),
  420. 'location' => $r['location'],
  421. 'error' => $r['error'],
  422. 'curlinfo' => $r['curlinfo']
  423. )));
  424. }
  425. });
  426. $app->post('/micropub/multipart', function() use($app) {
  427. if($user=require_login($app)) {
  428. // var_dump($app->request()->post());
  429. //
  430. // Since $app->request()->post() with multipart is always
  431. // empty (bug in Slim?) We're using the raw $_POST here.
  432. // PHP empties everything in $_POST if the file upload size exceeds
  433. // that is why we have to test if the variables exist first.
  434. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  435. if($file) {
  436. $error = validate_photo($file);
  437. unset($_POST['null']);
  438. if(!$error) {
  439. $file_path = $file['tmp_name'];
  440. correct_photo_rotation($file_path);
  441. $r = micropub_post_for_user($user, $_POST, $file_path);
  442. } else {
  443. $r = array('error' => $error);
  444. }
  445. } else {
  446. unset($_POST['null']);
  447. $r = micropub_post_for_user($user, $_POST);
  448. }
  449. // Populate the error if there was no location header.
  450. if(empty($r['location']) && empty($r['error'])) {
  451. $r['error'] = "No 'Location' header in response.";
  452. }
  453. $app->response()['Content-type'] = 'application/json';
  454. $app->response()->body(json_encode(array(
  455. 'response' => (isset($r['response']) ? htmlspecialchars($r['response']) : null),
  456. 'location' => (isset($r['location']) ? $r['location'] : null),
  457. 'error' => (isset($r['error']) ? $r['error'] : null),
  458. )));
  459. }
  460. });
  461. $app->post('/micropub/media', function() use($app) {
  462. if($user=require_login($app)) {
  463. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  464. $error = validate_photo($file);
  465. unset($_POST['null']);
  466. if(!$error) {
  467. $file_path = $file['tmp_name'];
  468. correct_photo_rotation($file_path);
  469. $r = micropub_media_post_for_user($user, $file_path);
  470. } else {
  471. $r = array('error' => $error);
  472. }
  473. if(empty($r['location']) && empty($r['error'])) {
  474. $r['error'] = "No 'Location' header in response.";
  475. }
  476. $app->response()['Content-type'] = 'application/json';
  477. $app->response()->body(json_encode(array(
  478. 'location' => (isset($r['location']) ? $r['location'] : null),
  479. 'error' => (isset($r['error']) ? $r['error'] : null),
  480. )));
  481. }
  482. });
  483. $app->post('/micropub/postjson', function() use($app) {
  484. if($user=require_login($app)) {
  485. $params = $app->request()->params();
  486. $r = micropub_post_for_user($user, json_decode($params['data'], true), null, true);
  487. $app->response()['Content-type'] = 'application/json';
  488. $app->response()->body(json_encode(array(
  489. 'location' => $r['location'],
  490. 'error' => $r['error'],
  491. 'response' => $r['response']
  492. )));
  493. }
  494. });