ProfileController.php 9.2 KB

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