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.

321 lines
9.2 KiB

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. // 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. $verb = 'drank';
  139. } elseif(k($params, 'drink')) {
  140. $entry->content = trim($params['drink']);
  141. $type = 'drink';
  142. $verb = 'drank';
  143. } elseif(k($params, 'eat')) {
  144. $entry->content = trim($params['eat']);
  145. $type = 'eat';
  146. $verb = 'ate';
  147. } elseif(k($params, 'custom_drink')) {
  148. $entry->content = trim($params['custom_drink']);
  149. $type = 'drink';
  150. $verb = 'drank';
  151. } elseif(k($params, 'custom_eat')) {
  152. $entry->content = trim($params['custom_eat']);
  153. $type = 'eat';
  154. $verb = 'ate';
  155. }
  156. $entry->type = $type;
  157. $entry->save();
  158. // Send to the micropub endpoint if one is defined, and store the result
  159. $url = false;
  160. if($user->micropub_endpoint) {
  161. $text_content = 'Just ' . $verb . ': ' . $entry->content;
  162. $mp_request = array(
  163. 'h' => 'entry',
  164. 'p3k-food' => $entry->content,
  165. 'p3k-type' => $type,
  166. 'location' => k($params, 'location'),
  167. 'summary' => $text_content
  168. );
  169. $r = micropub_post($user->micropub_endpoint, $mp_request, $user->access_token);
  170. $request = $r['request'];
  171. $response = $r['response'];
  172. $entry->micropub_response = $response;
  173. // Check the response and look for a "Location" header containing the URL
  174. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  175. $url = $match[1];
  176. $user->micropub_success = 1;
  177. $entry->micropub_success = 1;
  178. $entry->canonical_url = $url;
  179. } else {
  180. $entry->micropub_success = 0;
  181. }
  182. $entry->save();
  183. }
  184. if($url) {
  185. $app->redirect($url);
  186. } else {
  187. // TODO: Redirect to an error page or show an error on the teacup post page
  188. $url = Config::$base_url . $user->url . '/' . $entry->id;
  189. $app->redirect($url);
  190. }
  191. }
  192. });
  193. $app->get('/new/options', function() use($app) {
  194. if($user=require_login($app)) {
  195. $params = $app->request()->params();
  196. $options = get_entry_options($user->id, k($params,'latitude'), k($params,'longitude'));
  197. $html = partial('partials/entry-buttons', ['options'=>$options]);
  198. $app->response()->body($html);
  199. }
  200. });
  201. $app->get('/map.png', function() use($app) {
  202. $params = $app->request()->params();
  203. $url = static_map($params['latitude'], $params['longitude']);
  204. $ch = curl_init($url);
  205. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  206. $img = curl_exec($ch);
  207. $app->response()['Content-type'] = 'image/png';
  208. $app->response()->body($img);
  209. });
  210. $app->get('/:domain', function($domain) use($app) {
  211. $params = $app->request()->params();
  212. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  213. if(!$user) {
  214. $app->notFound();
  215. return;
  216. }
  217. $per_page = 10;
  218. $entries = ORM::for_table('entries')->where('user_id', $user->id);
  219. if(array_key_exists('before', $params)) {
  220. $entries->where_lte('id', $params['before']);
  221. }
  222. $entries = $entries->limit($per_page)->order_by_desc('published')->find_many();
  223. $older = ORM::for_table('entries')->where('user_id', $user->id)
  224. ->where_lt('id', $entries[count($entries)-1]->id)->order_by_desc('published')->find_one();
  225. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  226. ->where_gte('id', $entries[0]->id)->order_by_asc('published')->offset($per_page)->find_one();
  227. if(!$newer) {
  228. // no new entry was found at the specific offset, so find the newest post to link to instead
  229. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  230. ->order_by_desc('published')->limit(1)->find_one();
  231. if($newer->id == $entries[0]->id)
  232. $newer = false;
  233. }
  234. $html = render('entries', array(
  235. 'title' => 'Teacup',
  236. 'entries' => $entries,
  237. 'user' => $user,
  238. 'older' => ($older ? $older->id : false),
  239. 'newer' => ($newer ? $newer->id : false)
  240. ));
  241. $app->response()->body($html);
  242. })->conditions(array(
  243. 'domain' => '[a-zA-Z0-9\.-]+\.[a-z]+'
  244. ));
  245. $app->get('/:domain/:entry', function($domain, $entry_id) use($app) {
  246. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  247. if(!$user) {
  248. $app->notFound();
  249. return;
  250. }
  251. $entry = ORM::for_table('entries')->where('user_id', $user->id)->where('id', $entry_id)->find_one();
  252. if(!$entry) {
  253. $app->notFound();
  254. return;
  255. }
  256. $html = render('entry', array(
  257. 'title' => 'Teacup',
  258. 'entry' => $entry,
  259. 'user' => $user
  260. ));
  261. $app->response()->body($html);
  262. })->conditions(array(
  263. 'domain' => '[a-zA-Z0-9\.-]+\.[a-z]+',
  264. 'entry' => '\d+'
  265. ));