1
0

ProfileController.php 9.6 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\Status;
  14. use App\User;
  15. use App\UserSetting;
  16. use App\UserFilter;
  17. use League\Fractal;
  18. use App\Services\AccountService;
  19. use App\Services\FollowerService;
  20. use App\Services\StatusService;
  21. use App\Util\Lexer\Nickname;
  22. use App\Util\Webfinger\Webfinger;
  23. use App\Transformer\ActivityPub\ProfileOutbox;
  24. use App\Transformer\ActivityPub\ProfileTransformer;
  25. class ProfileController extends Controller
  26. {
  27. public function show(Request $request, $username)
  28. {
  29. // redirect authed users to Metro 2.0
  30. if($request->user()) {
  31. // unless they force static view
  32. if(!$request->has('fs') || $request->input('fs') != '1') {
  33. $pid = AccountService::usernameToId($username);
  34. if($pid) {
  35. return redirect('/i/web/profile/' . $pid);
  36. }
  37. }
  38. }
  39. $user = Profile::whereNull('domain')
  40. ->whereNull('status')
  41. ->whereUsername($username)
  42. ->firstOrFail();
  43. if($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  44. return $this->showActivityPub($request, $user);
  45. }
  46. $aiCheck = Cache::remember('profile:ai-check:spam-login:' . $user->id, 86400, function() use($user) {
  47. $exists = AccountInterstitial::whereUserId($user->user_id)->where('is_spam', 1)->count();
  48. if($exists) {
  49. return true;
  50. }
  51. return false;
  52. });
  53. if($aiCheck) {
  54. return redirect('/login');
  55. }
  56. return $this->buildProfile($request, $user);
  57. }
  58. protected function buildProfile(Request $request, $user)
  59. {
  60. $username = $user->username;
  61. $loggedIn = Auth::check();
  62. $isPrivate = false;
  63. $isBlocked = false;
  64. if(!$loggedIn) {
  65. $key = 'profile:settings:' . $user->id;
  66. $ttl = now()->addHours(6);
  67. $settings = Cache::remember($key, $ttl, function() use($user) {
  68. return $user->user->settings;
  69. });
  70. if ($user->is_private == true) {
  71. $profile = null;
  72. return view('profile.private', compact('user'));
  73. }
  74. $owner = false;
  75. $is_following = false;
  76. $profile = $user;
  77. $settings = [
  78. 'crawlable' => $settings->crawlable,
  79. 'following' => [
  80. 'count' => $settings->show_profile_following_count,
  81. 'list' => $settings->show_profile_following
  82. ],
  83. 'followers' => [
  84. 'count' => $settings->show_profile_follower_count,
  85. 'list' => $settings->show_profile_followers
  86. ]
  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. return view('profile.show', compact('profile', 'settings'));
  121. }
  122. }
  123. public function permalinkRedirect(Request $request, $username)
  124. {
  125. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  126. if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  127. return $this->showActivityPub($request, $user);
  128. }
  129. return redirect($user->url());
  130. }
  131. protected function privateProfileCheck(Profile $profile, $loggedIn)
  132. {
  133. if (!Auth::check()) {
  134. return true;
  135. }
  136. $user = Auth::user()->profile;
  137. if($user->id == $profile->id || !$profile->is_private) {
  138. return false;
  139. }
  140. $follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
  141. if ($follows == false) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. public static function accountCheck(Profile $profile)
  147. {
  148. switch ($profile->status) {
  149. case 'disabled':
  150. case 'suspended':
  151. case 'delete':
  152. return view('profile.disabled');
  153. break;
  154. default:
  155. break;
  156. }
  157. return abort(404);
  158. }
  159. protected function blockedProfileCheck(Profile $profile)
  160. {
  161. $pid = Auth::user()->profile->id;
  162. $blocks = UserFilter::whereUserId($profile->id)
  163. ->whereFilterType('block')
  164. ->whereFilterableType('App\Profile')
  165. ->pluck('filterable_id')
  166. ->toArray();
  167. if (in_array($pid, $blocks)) {
  168. return true;
  169. }
  170. return false;
  171. }
  172. public function showActivityPub(Request $request, $user)
  173. {
  174. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  175. abort_if($user->domain, 404);
  176. return Cache::remember('pf:activitypub:user-object:by-id:' . $user->id, 3600, function() use($user) {
  177. $fractal = new Fractal\Manager();
  178. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  179. $res = $fractal->createData($resource)->toArray();
  180. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  181. });
  182. }
  183. public function showAtomFeed(Request $request, $user)
  184. {
  185. abort_if(!config('federation.atom.enabled'), 404);
  186. $pid = AccountService::usernameToId($user);
  187. abort_if(!$pid, 404);
  188. $profile = AccountService::get($pid, true);
  189. abort_if(!$profile || $profile['locked'] || !$profile['local'], 404);
  190. $aiCheck = Cache::remember('profile:ai-check:spam-login:' . $profile['id'], 86400, function() use($profile) {
  191. $uid = User::whereProfileId($profile['id'])->first();
  192. if(!$uid) {
  193. return true;
  194. }
  195. $exists = AccountInterstitial::whereUserId($uid->id)->where('is_spam', 1)->count();
  196. if($exists) {
  197. return true;
  198. }
  199. return false;
  200. });
  201. abort_if($aiCheck, 404);
  202. $enabled = Cache::remember('profile:atom:enabled:' . $profile['id'], 84600, function() use ($profile) {
  203. $uid = User::whereProfileId($profile['id'])->first();
  204. if(!$uid) {
  205. return false;
  206. }
  207. $settings = UserSetting::whereUserId($uid->id)->first();
  208. if(!$settings) {
  209. return false;
  210. }
  211. return $settings->show_atom;
  212. });
  213. abort_if(!$enabled, 404);
  214. $data = Cache::remember('pf:atom:user-feed:by-id:' . $profile['id'], 900, function() use($pid, $profile) {
  215. $items = Status::whereProfileId($pid)
  216. ->whereScope('public')
  217. ->whereIn('type', ['photo', 'photo:album'])
  218. ->orderByDesc('id')
  219. ->take(10)
  220. ->get()
  221. ->map(function($status) {
  222. return StatusService::get($status->id, true);
  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. }