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.

302 lines
8.4 KiB

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