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.

338 lines
9.7 KiB

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