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.

509 lines
14 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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('/add-to-home', function() use($app) {
  192. $params = $app->request()->params();
  193. header("Cache-Control: no-cache, must-revalidate");
  194. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  195. unset($_SESSION['add-to-home-started']);
  196. // Verify the token and sign the user in
  197. try {
  198. $data = JWT::decode($params['token'], Config::$jwtSecret, array('HS256'));
  199. $_SESSION['user_id'] = $data->user_id;
  200. $_SESSION['me'] = $data->me;
  201. $app->redirect('/new', 301);
  202. } catch(DomainException $e) {
  203. header('X-Error: DomainException');
  204. $app->redirect('/', 301);
  205. } catch(UnexpectedValueException $e) {
  206. header('X-Error: UnexpectedValueException');
  207. $app->redirect('/', 301);
  208. }
  209. } else {
  210. if($user=require_login($app)) {
  211. if(array_key_exists('start', $params)) {
  212. $_SESSION['add-to-home-started'] = true;
  213. $token = JWT::encode(array(
  214. 'user_id' => $_SESSION['user_id'],
  215. 'me' => $_SESSION['me'],
  216. 'created_at' => time()
  217. ), Config::$jwtSecret);
  218. $app->redirect('/add-to-home?token='.$token, 301);
  219. } else {
  220. unset($_SESSION['add-to-home-started']);
  221. $html = render('add-to-home', array('title' => 'Quill'));
  222. $app->response()->body($html);
  223. }
  224. }
  225. }
  226. });
  227. $app->get('/email', function() use($app) {
  228. if($user=require_login($app)) {
  229. $test_response = '';
  230. if($user->last_micropub_response) {
  231. try {
  232. if(@json_decode($user->last_micropub_response)) {
  233. $d = json_decode($user->last_micropub_response);
  234. $test_response = $d->response;
  235. }
  236. } catch(Exception $e) {
  237. }
  238. }
  239. if(!$user->email_username) {
  240. $host = parse_url($user->url, PHP_URL_HOST);
  241. $user->email_username = $host . '.' . rand(100000,999999);
  242. $user->save();
  243. }
  244. $html = render('email', array(
  245. 'title' => 'Post-by-Email',
  246. 'micropub_endpoint' => $user->micropub_endpoint,
  247. 'test_response' => $test_response,
  248. 'user' => $user
  249. ));
  250. $app->response()->body($html);
  251. }
  252. });
  253. $app->get('/settings', function() use($app) {
  254. if($user=require_login($app)) {
  255. $html = render('settings', [
  256. 'title' => 'Settings',
  257. 'user' => $user,
  258. 'authorizing' => false
  259. ]);
  260. $app->response()->body($html);
  261. }
  262. });
  263. $app->post('/settings/html-content', function() use($app) {
  264. if($user=require_login($app)) {
  265. $params = $app->request()->params();
  266. $user->micropub_optin_html_content = $params['html'] ? 1 : 0;
  267. $user->save();
  268. $app->response()['Content-type'] = 'application/json';
  269. $app->response()->body(json_encode(array(
  270. 'html' => $user->micropub_optin_html_content
  271. )));
  272. }
  273. });
  274. $app->get('/settings/html-content', function() use($app) {
  275. if($user=require_login($app)) {
  276. $app->response()['Content-type'] = 'application/json';
  277. $app->response()->body(json_encode(array(
  278. 'html' => $user->micropub_optin_html_content
  279. )));
  280. }
  281. });
  282. function create_favorite(&$user, $url) {
  283. $micropub_request = array(
  284. 'like-of' => $url
  285. );
  286. $r = micropub_post_for_user($user, $micropub_request);
  287. $tweet_id = false;
  288. // POSSE favorites to Twitter
  289. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  290. $tweet_id = $match[1];
  291. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  292. $user->twitter_access_token, $user->twitter_token_secret);
  293. $result = $twitter->post('favorites/create', array(
  294. 'id' => $tweet_id
  295. ));
  296. }
  297. return $r;
  298. }
  299. function create_repost(&$user, $url) {
  300. $micropub_request = array(
  301. 'repost-of' => $url
  302. );
  303. $r = micropub_post_for_user($user, $micropub_request);
  304. $tweet_id = false;
  305. if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
  306. $tweet_id = $match[1];
  307. $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
  308. $user->twitter_access_token, $user->twitter_token_secret);
  309. $result = $twitter->post('statuses/retweet/'.$tweet_id);
  310. }
  311. return $r;
  312. }
  313. $app->post('/favorite', function() use($app) {
  314. if($user=require_login($app)) {
  315. $params = $app->request()->params();
  316. $r = create_favorite($user, $params['url']);
  317. $app->response()['Content-type'] = 'application/json';
  318. $app->response()->body(json_encode(array(
  319. 'location' => $r['location'],
  320. 'error' => $r['error']
  321. )));
  322. }
  323. });
  324. $app->post('/repost', function() use($app) {
  325. if($user=require_login($app)) {
  326. $params = $app->request()->params();
  327. $r = create_repost($user, $params['url']);
  328. $app->response()['Content-type'] = 'application/json';
  329. $app->response()->body(json_encode(array(
  330. 'location' => $r['location'],
  331. 'error' => $r['error']
  332. )));
  333. }
  334. });
  335. $app->get('/micropub/syndications', function() use($app) {
  336. if($user=require_login($app)) {
  337. $data = get_micropub_config($user, ['q'=>'syndicate-to']);
  338. $app->response()['Content-type'] = 'application/json';
  339. $app->response()->body(json_encode(array(
  340. 'targets' => $data['targets'],
  341. 'response' => $data['response']
  342. )));
  343. }
  344. });
  345. $app->post('/micropub/post', function() use($app) {
  346. if($user=require_login($app)) {
  347. $params = $app->request()->params();
  348. // Remove any blank params
  349. $params = array_filter($params, function($v){
  350. return $v !== '';
  351. });
  352. $r = micropub_post_for_user($user, $params);
  353. $app->response()['Content-type'] = 'application/json';
  354. $app->response()->body(json_encode(array(
  355. 'request' => htmlspecialchars($r['request']),
  356. 'response' => htmlspecialchars($r['response']),
  357. 'location' => $r['location'],
  358. 'error' => $r['error'],
  359. 'curlinfo' => $r['curlinfo']
  360. )));
  361. }
  362. });
  363. $app->post('/micropub/multipart', function() use($app) {
  364. if($user=require_login($app)) {
  365. // var_dump($app->request()->post());
  366. //
  367. // Since $app->request()->post() with multipart is always
  368. // empty (bug in Slim?) We're using the raw $_POST here.
  369. // PHP empties everything in $_POST if the file upload size exceeds
  370. // that is why we have to test if the variables exist first.
  371. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  372. if($file) {
  373. $error = validate_photo($file);
  374. unset($_POST['null']);
  375. if(!$error) {
  376. $file_path = $file['tmp_name'];
  377. correct_photo_rotation($file_path);
  378. $r = micropub_post_for_user($user, $_POST, $file_path);
  379. } else {
  380. $r = array('error' => $error);
  381. }
  382. } else {
  383. unset($_POST['null']);
  384. $r = micropub_post_for_user($user, $_POST);
  385. }
  386. // Populate the error if there was no location header.
  387. if(empty($r['location']) && empty($r['error'])) {
  388. $r['error'] = "No 'Location' header in response.";
  389. }
  390. $app->response()['Content-type'] = 'application/json';
  391. $app->response()->body(json_encode(array(
  392. 'response' => (isset($r['response']) ? htmlspecialchars($r['response']) : null),
  393. 'location' => (isset($r['location']) ? $r['location'] : null),
  394. 'error' => (isset($r['error']) ? $r['error'] : null),
  395. )));
  396. }
  397. });
  398. $app->post('/micropub/media', function() use($app) {
  399. if($user=require_login($app)) {
  400. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  401. $error = validate_photo($file);
  402. unset($_POST['null']);
  403. if(!$error) {
  404. $file_path = $file['tmp_name'];
  405. correct_photo_rotation($file_path);
  406. $r = micropub_media_post_for_user($user, $file_path);
  407. } else {
  408. $r = array('error' => $error);
  409. }
  410. if(empty($r['location']) && empty($r['error'])) {
  411. $r['error'] = "No 'Location' header in response.";
  412. }
  413. $app->response()['Content-type'] = 'application/json';
  414. $app->response()->body(json_encode(array(
  415. 'location' => (isset($r['location']) ? $r['location'] : null),
  416. 'error' => (isset($r['error']) ? $r['error'] : null),
  417. )));
  418. }
  419. });
  420. $app->post('/micropub/postjson', function() use($app) {
  421. if($user=require_login($app)) {
  422. $params = $app->request()->params();
  423. $r = micropub_post_for_user($user, json_decode($params['data'], true), null, true);
  424. $app->response()['Content-type'] = 'application/json';
  425. $app->response()->body(json_encode(array(
  426. 'location' => $r['location'],
  427. 'error' => $r['error'],
  428. 'response' => $r['response']
  429. )));
  430. }
  431. });