ProfileController.php 13 KB

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