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.

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