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.

334 lines
9.5 KiB

9 years ago
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 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. unset($_SESSION['add-to-home-started']);
  83. // Verify the token and sign the user in
  84. try {
  85. $data = JWT::decode($params['token'], Config::$jwtSecret);
  86. $_SESSION['user_id'] = $data->user_id;
  87. $_SESSION['me'] = $data->me;
  88. $app->redirect('/new', 301);
  89. } catch(DomainException $e) {
  90. header('X-Error: DomainException');
  91. $app->redirect('/', 301);
  92. } catch(UnexpectedValueException $e) {
  93. header('X-Error: UnexpectedValueException');
  94. $app->redirect('/', 301);
  95. }
  96. } else {
  97. if($user=require_login($app)) {
  98. if(array_key_exists('start', $params)) {
  99. $_SESSION['add-to-home-started'] = true;
  100. $token = JWT::encode(array(
  101. 'user_id' => $_SESSION['user_id'],
  102. 'me' => $_SESSION['me'],
  103. 'created_at' => time()
  104. ), Config::$jwtSecret);
  105. $app->redirect('/add-to-home?token='.$token, 301);
  106. } else {
  107. unset($_SESSION['add-to-home-started']);
  108. $html = render('add-to-home', array('title' => 'Teacup'));
  109. $app->response()->body($html);
  110. }
  111. }
  112. }
  113. });
  114. $app->post('/post', function() use($app) {
  115. if($user=require_login($app)) {
  116. $params = $app->request()->params();
  117. // Remove any blank params
  118. $params = array_filter($params, function($v){
  119. return $v !== '';
  120. });
  121. // Store the post in the database
  122. $entry = ORM::for_table('entries')->create();
  123. $entry->user_id = $user->id;
  124. $entry->published = date('Y-m-d H:i:s');
  125. if(k($params, 'location') && $location=parse_geo_uri($params['location'])) {
  126. $entry->latitude = $location['latitude'];
  127. $entry->longitude = $location['longitude'];
  128. if($timezone=get_timezone($location['latitude'], $location['longitude'])) {
  129. $entry->timezone = $timezone->getName();
  130. $entry->tz_offset = $timezone->getOffset(new DateTime());
  131. }
  132. } else {
  133. $entry->timezone = 'UTC';
  134. $entry->tz_offset = 0;
  135. }
  136. if(k($params, 'drank')) {
  137. $entry->content = trim($params['drank']);
  138. $type = 'drink';
  139. $verb = 'drank';
  140. } elseif(k($params, 'drink')) {
  141. $entry->content = trim($params['drink']);
  142. $type = 'drink';
  143. $verb = 'drank';
  144. } elseif(k($params, 'eat')) {
  145. $entry->content = trim($params['eat']);
  146. $type = 'eat';
  147. $verb = 'ate';
  148. } elseif(k($params, 'custom_drink')) {
  149. $entry->content = trim($params['custom_drink']);
  150. $type = 'drink';
  151. $verb = 'drank';
  152. } elseif(k($params, 'custom_eat')) {
  153. $entry->content = trim($params['custom_eat']);
  154. $type = 'eat';
  155. $verb = 'ate';
  156. }
  157. $entry->type = $type;
  158. $entry->save();
  159. // Send to the micropub endpoint if one is defined, and store the result
  160. $url = false;
  161. if($user->micropub_endpoint) {
  162. $text_content = 'Just ' . $verb . ': ' . $entry->content;
  163. $mp_request = array(
  164. 'h' => 'entry',
  165. 'p3k-food' => $entry->content,
  166. 'p3k-type' => $type,
  167. 'location' => k($params, 'location'),
  168. 'summary' => $text_content
  169. );
  170. $r = micropub_post($user->micropub_endpoint, $mp_request, $user->access_token);
  171. $request = $r['request'];
  172. $response = $r['response'];
  173. $entry->micropub_response = $response;
  174. // Check the response and look for a "Location" header containing the URL
  175. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  176. $url = $match[1];
  177. $user->micropub_success = 1;
  178. $entry->micropub_success = 1;
  179. $entry->canonical_url = $url;
  180. } else {
  181. $entry->micropub_success = 0;
  182. }
  183. $entry->save();
  184. }
  185. if($url) {
  186. $app->redirect($url);
  187. } else {
  188. // TODO: Redirect to an error page or show an error on the teacup post page
  189. $url = Config::$base_url . $user->url . '/' . $entry->id;
  190. $app->redirect($url);
  191. }
  192. }
  193. });
  194. $app->get('/options', function() use($app) {
  195. if($user=require_login($app)) {
  196. $params = $app->request()->params();
  197. $options = get_entry_options($user->id, k($params,'latitude'), k($params,'longitude'));
  198. $html = partial('partials/entry-buttons', ['options'=>$options]);
  199. $app->response()->body($html);
  200. }
  201. });
  202. $app->get('/map.png', function() use($app) {
  203. $params = $app->request()->params();
  204. $url = get_static_map($_SERVER['QUERY_STRING']);
  205. $ch = curl_init($url);
  206. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  207. $img = curl_exec($ch);
  208. $app->response()['Content-type'] = 'image/png';
  209. $app->response()->body($img);
  210. });
  211. $app->get('/teacup.appcache', function() use($app) {
  212. $content = partial('appcache');
  213. $app->response()['Content-type'] = 'text/cache-manifest';
  214. $app->response()->body($content);
  215. });
  216. $app->get('/:domain', function($domain) use($app) {
  217. $params = $app->request()->params();
  218. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  219. if(!$user) {
  220. $app->notFound();
  221. return;
  222. }
  223. $per_page = 10;
  224. $entries = ORM::for_table('entries')->where('user_id', $user->id);
  225. if(array_key_exists('before', $params)) {
  226. $entries->where_lte('id', $params['before']);
  227. }
  228. $entries = $entries->limit($per_page)->order_by_desc('published')->find_many();
  229. if(count($entries) > 1) {
  230. $older = ORM::for_table('entries')->where('user_id', $user->id)
  231. ->where_lt('id', $entries[count($entries)-1]->id)->order_by_desc('published')->find_one();
  232. } else {
  233. $older = null;
  234. }
  235. if(count($entries) > 1) {
  236. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  237. ->where_gte('id', $entries[0]->id)->order_by_asc('published')->offset($per_page)->find_one();
  238. } else {
  239. $newer = null;
  240. }
  241. if(!$newer) {
  242. // no new entry was found at the specific offset, so find the newest post to link to instead
  243. $newer = ORM::for_table('entries')->where('user_id', $user->id)
  244. ->order_by_desc('published')->limit(1)->find_one();
  245. if($newer && $newer->id == $entries[0]->id)
  246. $newer = false;
  247. }
  248. $html = render('entries', array(
  249. 'title' => 'Teacup',
  250. 'entries' => $entries,
  251. 'user' => $user,
  252. 'older' => ($older ? $older->id : false),
  253. 'newer' => ($newer ? $newer->id : false)
  254. ));
  255. $app->response()->body($html);
  256. })->conditions(array(
  257. 'domain' => '[a-zA-Z0-9\.-]+\.[a-z]+'
  258. ));
  259. $app->get('/:domain/:entry', function($domain, $entry_id) use($app) {
  260. $user = ORM::for_table('users')->where('url', $domain)->find_one();
  261. if(!$user) {
  262. $app->notFound();
  263. return;
  264. }
  265. $entry = ORM::for_table('entries')->where('user_id', $user->id)->where('id', $entry_id)->find_one();
  266. if(!$entry) {
  267. $app->notFound();
  268. return;
  269. }
  270. $html = render('entry', array(
  271. 'title' => 'Teacup',
  272. 'entry' => $entry,
  273. 'user' => $user
  274. ));
  275. $app->response()->body($html);
  276. })->conditions(array(
  277. 'domain' => '[a-zA-Z0-9\.-]+\.[a-z]+',
  278. 'entry' => '\d+'
  279. ));