ProfileController.php 9.5 KB

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