1
0

ProfileController.php 13 KB

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