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.

273 lines
7.7 KiB

  1. <?php
  2. function require_login(&$app) {
  3. $params = $app->request()->params();
  4. if(array_key_exists('token', $params)) {
  5. try {
  6. $data = JWT::decode($params['token'], Config::$jwtSecret);
  7. $_SESSION['user_id'] = $data->user_id;
  8. $_SESSION['me'] = $data->me;
  9. } catch(DomainException $e) {
  10. header('X-Error: DomainException');
  11. $app->redirect('/', 301);
  12. } catch(UnexpectedValueException $e) {
  13. header('X-Error: UnexpectedValueException');
  14. $app->redirect('/', 301);
  15. }
  16. }
  17. if(!array_key_exists('user_id', $_SESSION)) {
  18. $app->redirect('/');
  19. return false;
  20. } else {
  21. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  22. }
  23. }
  24. function generate_login_token() {
  25. return JWT::encode(array(
  26. 'user_id' => $_SESSION['user_id'],
  27. 'me' => $_SESSION['me'],
  28. 'created_at' => time()
  29. ), Config::$jwtSecret);
  30. }
  31. $app->get('/new', function() use($app) {
  32. if($user=require_login($app)) {
  33. $entry = false;
  34. $photo_url = false;
  35. $html = render('new-post', array(
  36. 'title' => 'New Post',
  37. 'micropub_endpoint' => $user->micropub_endpoint,
  38. 'token_scope' => $user->token_scope,
  39. 'access_token' => $user->access_token,
  40. 'response_date' => $user->last_micropub_response_date,
  41. 'location_enabled' => $user->location_enabled
  42. ));
  43. $app->response()->body($html);
  44. }
  45. });
  46. $app->get('/new/options.json', function() use($app) {
  47. $app->response()['Content-Type'] = 'application/json';
  48. $app->response()->body(json_encode(array(
  49. 'sections' => array(
  50. array(
  51. 'title' => 'Caffeine',
  52. 'items' => array_map(function($e){ return array('title'=>$e); }, caffeine_options())
  53. ),
  54. array(
  55. 'title' => 'Alcohol',
  56. 'items' => array_map(function($e){ return array('title'=>$e); }, alcohol_options())
  57. )
  58. )
  59. )));
  60. });
  61. $app->post('/prefs', function() use($app) {
  62. if($user=require_login($app)) {
  63. $params = $app->request()->params();
  64. $user->location_enabled = $params['enabled'];
  65. $user->save();
  66. }
  67. $app->response()->body(json_encode(array(
  68. 'result' => 'ok'
  69. )));
  70. });
  71. $app->get('/creating-a-token-endpoint', function() use($app) {
  72. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  73. });
  74. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  75. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint'));
  76. $app->response()->body($html);
  77. });
  78. $app->get('/docs', function() use($app) {
  79. $html = render('docs', array('title' => 'Documentation'));
  80. $app->response()->body($html);
  81. });
  82. $app->get('/add-to-home', function() use($app) {
  83. $params = $app->request()->params();
  84. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  85. // Verify the token and sign the user in
  86. try {
  87. $data = JWT::decode($params['token'], Config::$jwtSecret);
  88. $_SESSION['user_id'] = $data->user_id;
  89. $_SESSION['me'] = $data->me;
  90. $app->redirect('/new', 301);
  91. } catch(DomainException $e) {
  92. header('X-Error: DomainException');
  93. $app->redirect('/', 301);
  94. } catch(UnexpectedValueException $e) {
  95. header('X-Error: UnexpectedValueException');
  96. $app->redirect('/', 301);
  97. }
  98. } else {
  99. if($user=require_login($app)) {
  100. if(array_key_exists('start', $params)) {
  101. $_SESSION['add-to-home-started'] = true;
  102. $token = JWT::encode(array(
  103. 'user_id' => $_SESSION['user_id'],
  104. 'me' => $_SESSION['me'],
  105. 'created_at' => time()
  106. ), Config::$jwtSecret);
  107. $app->redirect('/add-to-home?token='.$token, 301);
  108. } else {
  109. unset($_SESSION['add-to-home-started']);
  110. $html = render('add-to-home', array('title' => 'Teacup'));
  111. $app->response()->body($html);
  112. }
  113. }
  114. }
  115. });
  116. $app->post('/post', function() use($app) {
  117. if($user=require_login($app)) {
  118. $params = $app->request()->params();
  119. // Remove any blank params
  120. $params = array_filter($params, function($v){
  121. return $v !== '';
  122. });
  123. // Store the post in the database
  124. $entry = ORM::for_table('entries')->create();
  125. $entry->user_id = $user->id;
  126. $entry->published = date('Y-m-d H:i:s');
  127. if(k($params, 'location') && $location=parse_geo_uri($params['location'])) {
  128. $entry->latitude = $location['latitude'];
  129. $entry->longitude = $location['longitude'];
  130. if($timezone=get_timezone($location['latitude'], $location['longitude'])) {
  131. $entry->timezone = $timezone->getName();
  132. $entry->tz_offset = $timezone->getOffset(new DateTime());
  133. }
  134. } else {
  135. $entry->timezone = 'UTC';
  136. $entry->tz_offset = 0;
  137. }
  138. if(k($params, 'drank')) {
  139. $entry->content = $params['drank'];
  140. $type = 'drink';
  141. } elseif(k($params, 'custom_drank')) {
  142. $entry->content = $params['custom_drank'];
  143. $type = 'drink';
  144. } elseif(k($params, 'custom_ate')) {
  145. $entry->content = $params['custom_ate'];
  146. $type = 'eat';
  147. }
  148. $entry->save();
  149. // Send to the micropub endpoint if one is defined, and store the result
  150. if($user->micropub_endpoint) {
  151. $mp_request = array(
  152. 'h' => 'entry',
  153. 'p3k-food' => $entry->content,
  154. 'p3k-type' => $type,
  155. 'location' => k($params, 'location')
  156. );
  157. $r = micropub_post($user->micropub_endpoint, $mp_request, $user->access_token);
  158. $request = $r['request'];
  159. $response = $r['response'];
  160. $entry->micropub_response = $response;
  161. // Check the response and look for a "Location" header containing the URL
  162. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  163. $url = $match[1];
  164. $user->micropub_success = 1;
  165. $entry->micropub_success = 1;
  166. $entry->canonical_url = $url;
  167. } else {
  168. $entry->micropub_success = 0;
  169. }
  170. $entry->save();
  171. } else {
  172. $url = Config::$base_url . $user->url . '/' . $entry->id;
  173. }
  174. $app->redirect($url);
  175. }
  176. });
  177. $app->get('/:domain', function($domain) use($app) {
  178. $params = $app->request()->params();
  179. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  180. if(!$user) {
  181. $app->notFound();
  182. return;
  183. }
  184. $per_page = 10;
  185. $entries = ORM::for_table('entries')->where('user_id', $user->id);
  186. if(array_key_exists('before', $params)) {
  187. $entries->where_lte('id', $params['before']);
  188. }
  189. $entries = $entries->limit($per_page)->order_by_desc('published')->find_many();
  190. $older = ORM::for_table('entries')->where('user_id', $user->id)
  191. ->where_lt('id', $entries[count($entries)-1]->id)->order_by_desc('published')->find_one();
  192. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  193. ->where_gte('id', $entries[0]->id)->order_by_asc('published')->offset($per_page)->find_one();
  194. if(!$newer) {
  195. // no new entry was found at the specific offset, so find the newest post to link to instead
  196. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  197. ->order_by_desc('published')->limit(1)->find_one();
  198. if($newer->id == $entries[0]->id)
  199. $newer = false;
  200. }
  201. $html = render('entries', array(
  202. 'title' => 'Teacup',
  203. 'entries' => $entries,
  204. 'user' => $user,
  205. 'older' => ($older ? $older->id : false),
  206. 'newer' => ($newer ? $newer->id : false)
  207. ));
  208. $app->response()->body($html);
  209. });
  210. $app->get('/:domain/:entry', function($domain, $entry_id) use($app) {
  211. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  212. if(!$user) {
  213. $app->notFound();
  214. return;
  215. }
  216. $entry = ORM::for_table('entries')->where('user_id', $user->id)->where('id', $entry_id)->find_one();
  217. if(!$entry) {
  218. $app->notFound();
  219. return;
  220. }
  221. $html = render('entry', array(
  222. 'title' => 'Teacup',
  223. 'entry' => $entry,
  224. 'user' => $user
  225. ));
  226. $app->response()->body($html);
  227. });