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.

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