1
0

ProfileController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. abort_if(!$user, 404);
  147. return redirect($user->url());
  148. }
  149. protected function privateProfileCheck(Profile $profile, $loggedIn)
  150. {
  151. if (! Auth::check()) {
  152. return true;
  153. }
  154. $user = Auth::user()->profile;
  155. if ($user->id == $profile->id || ! $profile->is_private) {
  156. return false;
  157. }
  158. $follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
  159. if ($follows == false) {
  160. return true;
  161. }
  162. return false;
  163. }
  164. public static function accountCheck(Profile $profile)
  165. {
  166. switch ($profile->status) {
  167. case 'disabled':
  168. case 'suspended':
  169. case 'delete':
  170. return view('profile.disabled');
  171. break;
  172. default:
  173. break;
  174. }
  175. return abort(404);
  176. }
  177. protected function blockedProfileCheck(Profile $profile)
  178. {
  179. $pid = Auth::user()->profile->id;
  180. $blocks = UserFilter::whereUserId($profile->id)
  181. ->whereFilterType('block')
  182. ->whereFilterableType('App\Profile')
  183. ->pluck('filterable_id')
  184. ->toArray();
  185. if (in_array($pid, $blocks)) {
  186. return true;
  187. }
  188. return false;
  189. }
  190. public function showActivityPub(Request $request, $user)
  191. {
  192. abort_if(! config_cache('federation.activitypub.enabled'), 404);
  193. abort_if(! $user, 404, 'Not found');
  194. abort_if($user->domain, 404);
  195. return Cache::remember('pf:activitypub:user-object:by-id:'.$user->id, 1800, function () use ($user) {
  196. $fractal = new Fractal\Manager();
  197. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  198. $res = $fractal->createData($resource)->toArray();
  199. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  200. });
  201. }
  202. public function showAtomFeed(Request $request, $user)
  203. {
  204. abort_if(! config('federation.atom.enabled'), 404);
  205. $pid = AccountService::usernameToId($user);
  206. abort_if(! $pid, 404);
  207. $profile = AccountService::get($pid, true);
  208. abort_if(! $profile || $profile['locked'] || ! $profile['local'], 404);
  209. $aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile['id'], 86400, function () use ($profile) {
  210. $uid = User::whereProfileId($profile['id'])->first();
  211. if (! $uid) {
  212. return true;
  213. }
  214. $exists = AccountInterstitial::whereUserId($uid->id)->where('is_spam', 1)->count();
  215. if ($exists) {
  216. return true;
  217. }
  218. return false;
  219. });
  220. abort_if($aiCheck, 404);
  221. $enabled = Cache::remember('profile:atom:enabled:'.$profile['id'], 84600, function () use ($profile) {
  222. $uid = User::whereProfileId($profile['id'])->first();
  223. if (! $uid) {
  224. return false;
  225. }
  226. $settings = UserSetting::whereUserId($uid->id)->first();
  227. if (! $settings) {
  228. return false;
  229. }
  230. return $settings->show_atom;
  231. });
  232. abort_if(! $enabled, 404);
  233. $data = Cache::remember('pf:atom:user-feed:by-id:'.$profile['id'], 14400, function () use ($pid, $profile) {
  234. $items = Status::whereProfileId($pid)
  235. ->whereScope('public')
  236. ->whereIn('type', ['photo', 'photo:album'])
  237. ->orderByDesc('id')
  238. ->take(10)
  239. ->get()
  240. ->map(function ($status) {
  241. return StatusService::get($status->id, true);
  242. })
  243. ->filter(function ($status) {
  244. return $status &&
  245. isset($status['account']) &&
  246. isset($status['media_attachments']) &&
  247. count($status['media_attachments']);
  248. })
  249. ->values();
  250. $permalink = config('app.url')."/users/{$profile['username']}.atom";
  251. $headers = ['Content-Type' => 'application/atom+xml'];
  252. if ($items && $items->count()) {
  253. $headers['Last-Modified'] = now()->parse($items->first()['created_at'])->toRfc7231String();
  254. }
  255. return compact('items', 'permalink', 'headers');
  256. });
  257. abort_if(! $data || ! isset($data['items']) || ! isset($data['permalink']), 404);
  258. return response()
  259. ->view('atom.user',
  260. [
  261. 'profile' => $profile,
  262. 'items' => $data['items'],
  263. 'permalink' => $data['permalink'],
  264. ]
  265. )
  266. ->withHeaders($data['headers']);
  267. }
  268. public function meRedirect()
  269. {
  270. abort_if(! Auth::check(), 404);
  271. return redirect(Auth::user()->url());
  272. }
  273. public function embed(Request $request, $username)
  274. {
  275. $res = view('profile.embed-removed');
  276. if (! (bool) config_cache('instance.embed.profile')) {
  277. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  278. }
  279. if (strlen($username) > 15 || strlen($username) < 2) {
  280. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  281. }
  282. $profile = $this->getCachedUser($username);
  283. if (! $profile || $profile->is_private) {
  284. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  285. }
  286. $aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile->id, 86400, function () use ($profile) {
  287. $exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count();
  288. if ($exists) {
  289. return true;
  290. }
  291. return false;
  292. });
  293. if ($aiCheck) {
  294. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  295. }
  296. if (AccountService::canEmbed($profile->user_id) == false) {
  297. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  298. }
  299. $profile = AccountService::get($profile->id);
  300. $res = view('profile.embed', compact('profile'));
  301. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  302. }
  303. public function stories(Request $request, $username)
  304. {
  305. abort_if(! config_cache('instance.stories.enabled') || ! $request->user(), 404);
  306. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  307. $pid = $profile->id;
  308. $authed = Auth::user()->profile_id;
  309. abort_if($pid != $authed && ! FollowerService::follows($authed, $pid), 404);
  310. $exists = Story::whereProfileId($pid)
  311. ->whereActive(true)
  312. ->exists();
  313. abort_unless($exists, 404);
  314. return view('profile.story', compact('pid', 'profile'));
  315. }
  316. }