1
0

ProfileController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use Cache;
  6. use DB;
  7. use View;
  8. use App\Follower;
  9. use App\FollowRequest;
  10. use App\Profile;
  11. use App\Story;
  12. use App\User;
  13. use App\UserFilter;
  14. use League\Fractal;
  15. use App\Services\AccountService;
  16. use App\Services\FollowerService;
  17. use App\Services\StatusService;
  18. use App\Util\Lexer\Nickname;
  19. use App\Util\Webfinger\Webfinger;
  20. use App\Transformer\ActivityPub\ProfileOutbox;
  21. use App\Transformer\ActivityPub\ProfileTransformer;
  22. class ProfileController extends Controller
  23. {
  24. public function show(Request $request, $username)
  25. {
  26. // redirect authed users to Metro 2.0
  27. if($request->user()) {
  28. // unless they force static view
  29. if(!$request->has('fs') || $request->input('fs') != '1') {
  30. $pid = AccountService::usernameToId($username);
  31. if($pid) {
  32. return redirect('/i/web/profile/' . $pid);
  33. }
  34. }
  35. }
  36. $user = Profile::whereNull('domain')
  37. ->whereNull('status')
  38. ->whereUsername($username)
  39. ->firstOrFail();
  40. if($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  41. return $this->showActivityPub($request, $user);
  42. }
  43. return $this->buildProfile($request, $user);
  44. }
  45. protected function buildProfile(Request $request, $user)
  46. {
  47. $username = $user->username;
  48. $loggedIn = Auth::check();
  49. $isPrivate = false;
  50. $isBlocked = false;
  51. if(!$loggedIn) {
  52. $key = 'profile:settings:' . $user->id;
  53. $ttl = now()->addHours(6);
  54. $settings = Cache::remember($key, $ttl, function() use($user) {
  55. return $user->user->settings;
  56. });
  57. if ($user->is_private == true) {
  58. $profile = null;
  59. return view('profile.private', compact('user'));
  60. }
  61. $owner = false;
  62. $is_following = false;
  63. $profile = $user;
  64. $settings = [
  65. 'crawlable' => $settings->crawlable,
  66. 'following' => [
  67. 'count' => $settings->show_profile_following_count,
  68. 'list' => $settings->show_profile_following
  69. ],
  70. 'followers' => [
  71. 'count' => $settings->show_profile_follower_count,
  72. 'list' => $settings->show_profile_followers
  73. ]
  74. ];
  75. return view('profile.show', compact('profile', 'settings'));
  76. } else {
  77. $key = 'profile:settings:' . $user->id;
  78. $ttl = now()->addHours(6);
  79. $settings = Cache::remember($key, $ttl, function() use($user) {
  80. return $user->user->settings;
  81. });
  82. if ($user->is_private == true) {
  83. $isPrivate = $this->privateProfileCheck($user, $loggedIn);
  84. }
  85. $isBlocked = $this->blockedProfileCheck($user);
  86. $owner = $loggedIn && Auth::id() === $user->user_id;
  87. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  88. if ($isPrivate == true || $isBlocked == true) {
  89. $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
  90. ->whereFollowingId($user->id)
  91. ->exists() : false;
  92. return view('profile.private', compact('user', 'is_following', 'requested'));
  93. }
  94. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  95. $profile = $user;
  96. $settings = [
  97. 'crawlable' => $settings->crawlable,
  98. 'following' => [
  99. 'count' => $settings->show_profile_following_count,
  100. 'list' => $settings->show_profile_following
  101. ],
  102. 'followers' => [
  103. 'count' => $settings->show_profile_follower_count,
  104. 'list' => $settings->show_profile_followers
  105. ]
  106. ];
  107. return view('profile.show', compact('profile', 'settings'));
  108. }
  109. }
  110. public function permalinkRedirect(Request $request, $username)
  111. {
  112. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  113. if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  114. return $this->showActivityPub($request, $user);
  115. }
  116. return redirect($user->url());
  117. }
  118. protected function privateProfileCheck(Profile $profile, $loggedIn)
  119. {
  120. if (!Auth::check()) {
  121. return true;
  122. }
  123. $user = Auth::user()->profile;
  124. if($user->id == $profile->id || !$profile->is_private) {
  125. return false;
  126. }
  127. $follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
  128. if ($follows == false) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. public static function accountCheck(Profile $profile)
  134. {
  135. switch ($profile->status) {
  136. case 'disabled':
  137. case 'suspended':
  138. case 'delete':
  139. return view('profile.disabled');
  140. break;
  141. default:
  142. break;
  143. }
  144. return abort(404);
  145. }
  146. protected function blockedProfileCheck(Profile $profile)
  147. {
  148. $pid = Auth::user()->profile->id;
  149. $blocks = UserFilter::whereUserId($profile->id)
  150. ->whereFilterType('block')
  151. ->whereFilterableType('App\Profile')
  152. ->pluck('filterable_id')
  153. ->toArray();
  154. if (in_array($pid, $blocks)) {
  155. return true;
  156. }
  157. return false;
  158. }
  159. public function showActivityPub(Request $request, $user)
  160. {
  161. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  162. abort_if($user->domain, 404);
  163. return Cache::remember('pf:activitypub:user-object:by-id:' . $user->id, 3600, function() use($user) {
  164. $fractal = new Fractal\Manager();
  165. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  166. $res = $fractal->createData($resource)->toArray();
  167. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  168. });
  169. }
  170. public function showAtomFeed(Request $request, $user)
  171. {
  172. abort_if(!config('federation.atom.enabled'), 404);
  173. $pid = AccountService::usernameToId($user);
  174. abort_if(!$pid, 404);
  175. $profile = AccountService::get($pid, true);
  176. abort_if(!$profile || $profile['locked'] || !$profile['local'], 404);
  177. $data = Cache::remember('pf:atom:user-feed:by-id:' . $profile['id'], 86400, function() use($pid, $profile) {
  178. $items = DB::table('statuses')
  179. ->whereProfileId($pid)
  180. ->whereVisibility('public')
  181. ->whereType('photo')
  182. ->orderByDesc('id')
  183. ->take(10)
  184. ->get()
  185. ->map(function($status) {
  186. return StatusService::get($status->id);
  187. })
  188. ->filter(function($status) {
  189. return $status &&
  190. isset($status['account']) &&
  191. isset($status['media_attachments']) &&
  192. count($status['media_attachments']);
  193. })
  194. ->values();
  195. $permalink = config('app.url') . "/users/{$profile['username']}.atom";
  196. $headers = ['Content-Type' => 'application/atom+xml'];
  197. if($items && $items->count()) {
  198. $headers['Last-Modified'] = now()->parse($items->first()['created_at'])->toRfc7231String();
  199. }
  200. return compact('items', 'permalink', 'headers');
  201. });
  202. abort_if(!$data, 404);
  203. return response()
  204. ->view('atom.user',
  205. [
  206. 'profile' => $profile,
  207. 'items' => $data['items'],
  208. 'permalink' => $data['permalink']
  209. ]
  210. )
  211. ->withHeaders($data['headers']);
  212. }
  213. public function meRedirect()
  214. {
  215. abort_if(!Auth::check(), 404);
  216. return redirect(Auth::user()->url());
  217. }
  218. public function embed(Request $request, $username)
  219. {
  220. $res = view('profile.embed-removed');
  221. if(!config('instance.embed.profile')) {
  222. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  223. }
  224. if(strlen($username) > 15 || strlen($username) < 2) {
  225. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  226. }
  227. $profile = Profile::whereUsername($username)
  228. ->whereIsPrivate(false)
  229. ->whereNull('status')
  230. ->whereNull('domain')
  231. ->first();
  232. if(!$profile) {
  233. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  234. }
  235. if(AccountService::canEmbed($profile->user_id) == false) {
  236. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  237. }
  238. $profile = AccountService::get($profile->id);
  239. $res = view('profile.embed', compact('profile'));
  240. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  241. }
  242. public function stories(Request $request, $username)
  243. {
  244. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  245. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  246. $pid = $profile->id;
  247. $authed = Auth::user()->profile_id;
  248. abort_if($pid != $authed && !FollowerService::follows($authed, $pid), 404);
  249. $exists = Story::whereProfileId($pid)
  250. ->whereActive(true)
  251. ->exists();
  252. abort_unless($exists, 404);
  253. return view('profile.story', compact('pid', 'profile'));
  254. }
  255. }