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.

324 lines
8.9 KiB

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