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.

287 lines
8.0 KiB

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 get_login(&$app) {
  25. if(array_key_exists('user_id', $_SESSION)) {
  26. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  27. } else {
  28. return false;
  29. }
  30. }
  31. function generate_login_token() {
  32. return JWT::encode(array(
  33. 'user_id' => $_SESSION['user_id'],
  34. 'me' => $_SESSION['me'],
  35. 'created_at' => time()
  36. ), Config::$jwtSecret);
  37. }
  38. $app->get('/new', function() use($app) {
  39. if($user=require_login($app)) {
  40. $entry = false;
  41. $photo_url = false;
  42. $html = render('new-post', array(
  43. 'title' => 'New Post',
  44. 'micropub_endpoint' => $user->micropub_endpoint,
  45. 'token_scope' => $user->token_scope,
  46. 'access_token' => $user->access_token,
  47. 'response_date' => $user->last_micropub_response_date,
  48. 'location_enabled' => $user->location_enabled
  49. ));
  50. $app->response()->body($html);
  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, 'drink')) {
  134. $entry->content = $params['drink'];
  135. $type = 'drink';
  136. } elseif(k($params, 'custom_drank')) {
  137. $entry->content = $params['custom_drank'];
  138. $type = 'drink';
  139. } elseif(k($params, 'custom_ate')) {
  140. $entry->content = $params['custom_ate'];
  141. $type = 'eat';
  142. }
  143. $entry->save();
  144. // Send to the micropub endpoint if one is defined, and store the result
  145. $url = false;
  146. if($user->micropub_endpoint) {
  147. $mp_request = array(
  148. 'h' => 'entry',
  149. 'p3k-food' => $entry->content,
  150. 'p3k-type' => $type,
  151. 'location' => k($params, 'location')
  152. );
  153. $r = micropub_post($user->micropub_endpoint, $mp_request, $user->access_token);
  154. $request = $r['request'];
  155. $response = $r['response'];
  156. $entry->micropub_response = $response;
  157. // Check the response and look for a "Location" header containing the URL
  158. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  159. $url = $match[1];
  160. $user->micropub_success = 1;
  161. $entry->micropub_success = 1;
  162. $entry->canonical_url = $url;
  163. } else {
  164. $entry->micropub_success = 0;
  165. }
  166. $entry->save();
  167. }
  168. if($url) {
  169. $app->redirect($url);
  170. } else {
  171. // TODO: Redirect to an error page or show an error on the teacup post page
  172. $url = Config::$base_url . $user->url . '/' . $entry->id;
  173. $app->redirect($url);
  174. }
  175. }
  176. });
  177. $app->get('/map.png', function() use($app) {
  178. $params = $app->request()->params();
  179. $url = static_map($params['latitude'], $params['longitude']);
  180. $ch = curl_init($url);
  181. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  182. $img = curl_exec($ch);
  183. $app->response()['Content-type'] = 'image/png';
  184. $app->response()->body($img);
  185. });
  186. $app->get('/:domain', function($domain) use($app) {
  187. $params = $app->request()->params();
  188. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  189. if(!$user) {
  190. $app->notFound();
  191. return;
  192. }
  193. $per_page = 10;
  194. $entries = ORM::for_table('entries')->where('user_id', $user->id);
  195. if(array_key_exists('before', $params)) {
  196. $entries->where_lte('id', $params['before']);
  197. }
  198. $entries = $entries->limit($per_page)->order_by_desc('published')->find_many();
  199. $older = ORM::for_table('entries')->where('user_id', $user->id)
  200. ->where_lt('id', $entries[count($entries)-1]->id)->order_by_desc('published')->find_one();
  201. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  202. ->where_gte('id', $entries[0]->id)->order_by_asc('published')->offset($per_page)->find_one();
  203. if(!$newer) {
  204. // no new entry was found at the specific offset, so find the newest post to link to instead
  205. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  206. ->order_by_desc('published')->limit(1)->find_one();
  207. if($newer->id == $entries[0]->id)
  208. $newer = false;
  209. }
  210. $html = render('entries', array(
  211. 'title' => 'Teacup',
  212. 'entries' => $entries,
  213. 'user' => $user,
  214. 'older' => ($older ? $older->id : false),
  215. 'newer' => ($newer ? $newer->id : false)
  216. ));
  217. $app->response()->body($html);
  218. });
  219. $app->get('/:domain/:entry', function($domain, $entry_id) use($app) {
  220. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  221. if(!$user) {
  222. $app->notFound();
  223. return;
  224. }
  225. $entry = ORM::for_table('entries')->where('user_id', $user->id)->where('id', $entry_id)->find_one();
  226. if(!$entry) {
  227. $app->notFound();
  228. return;
  229. }
  230. $html = render('entry', array(
  231. 'title' => 'Teacup',
  232. 'entry' => $entry,
  233. 'user' => $user
  234. ));
  235. $app->response()->body($html);
  236. });