ProfileController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use Cache;
  6. use View;
  7. use App\Follower;
  8. use App\FollowRequest;
  9. use App\Profile;
  10. use App\Story;
  11. use App\User;
  12. use App\UserFilter;
  13. use League\Fractal;
  14. use App\Util\Lexer\Nickname;
  15. use App\Util\Webfinger\Webfinger;
  16. use App\Transformer\ActivityPub\ProfileOutbox;
  17. use App\Transformer\ActivityPub\ProfileTransformer;
  18. class ProfileController extends Controller
  19. {
  20. public function show(Request $request, $username)
  21. {
  22. $user = Profile::whereNull('domain')
  23. ->whereNull('status')
  24. ->whereUsername($username)
  25. ->firstOrFail();
  26. if($request->wantsJson() && config('federation.activitypub.enabled')) {
  27. return $this->showActivityPub($request, $user);
  28. }
  29. return $this->buildProfile($request, $user);
  30. }
  31. protected function buildProfile(Request $request, $user)
  32. {
  33. $username = $user->username;
  34. $loggedIn = Auth::check();
  35. $isPrivate = false;
  36. $isBlocked = false;
  37. if(!$loggedIn) {
  38. $key = 'profile:settings:' . $user->id;
  39. $ttl = now()->addHours(6);
  40. $settings = Cache::remember($key, $ttl, function() use($user) {
  41. return $user->user->settings;
  42. });
  43. if ($user->is_private == true) {
  44. abort(404);
  45. }
  46. $owner = false;
  47. $is_following = false;
  48. $is_admin = $user->user->is_admin;
  49. $profile = $user;
  50. $settings = [
  51. 'crawlable' => $settings->crawlable,
  52. 'following' => [
  53. 'count' => $settings->show_profile_following_count,
  54. 'list' => $settings->show_profile_following
  55. ],
  56. 'followers' => [
  57. 'count' => $settings->show_profile_follower_count,
  58. 'list' => $settings->show_profile_followers
  59. ]
  60. ];
  61. return view('profile.show', compact('profile', 'settings'));
  62. } else {
  63. $key = 'profile:settings:' . $user->id;
  64. $ttl = now()->addHours(6);
  65. $settings = Cache::remember($key, $ttl, function() use($user) {
  66. return $user->user->settings;
  67. });
  68. if ($user->is_private == true) {
  69. $isPrivate = $this->privateProfileCheck($user, $loggedIn);
  70. }
  71. $isBlocked = $this->blockedProfileCheck($user);
  72. $owner = $loggedIn && Auth::id() === $user->user_id;
  73. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  74. if ($isPrivate == true || $isBlocked == true) {
  75. $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
  76. ->whereFollowingId($user->id)
  77. ->exists() : false;
  78. return view('profile.private', compact('user', 'is_following', 'requested'));
  79. }
  80. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  81. $profile = $user;
  82. $settings = [
  83. 'crawlable' => $settings->crawlable,
  84. 'following' => [
  85. 'count' => $settings->show_profile_following_count,
  86. 'list' => $settings->show_profile_following
  87. ],
  88. 'followers' => [
  89. 'count' => $settings->show_profile_follower_count,
  90. 'list' => $settings->show_profile_followers
  91. ]
  92. ];
  93. return view('profile.show', compact('profile', 'settings'));
  94. }
  95. }
  96. public function permalinkRedirect(Request $request, $username)
  97. {
  98. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  99. if ($request->wantsJson() && config('federation.activitypub.enabled')) {
  100. return $this->showActivityPub($request, $user);
  101. }
  102. return redirect($user->url());
  103. }
  104. protected function privateProfileCheck(Profile $profile, $loggedIn)
  105. {
  106. if (!Auth::check()) {
  107. return true;
  108. }
  109. $user = Auth::user()->profile;
  110. if($user->id == $profile->id || !$profile->is_private) {
  111. return false;
  112. }
  113. $follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
  114. if ($follows == false) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. public static function accountCheck(Profile $profile)
  120. {
  121. switch ($profile->status) {
  122. case 'disabled':
  123. case 'suspended':
  124. case 'delete':
  125. return view('profile.disabled');
  126. break;
  127. default:
  128. break;
  129. }
  130. return abort(404);
  131. }
  132. protected function blockedProfileCheck(Profile $profile)
  133. {
  134. $pid = Auth::user()->profile->id;
  135. $blocks = UserFilter::whereUserId($profile->id)
  136. ->whereFilterType('block')
  137. ->whereFilterableType('App\Profile')
  138. ->pluck('filterable_id')
  139. ->toArray();
  140. if (in_array($pid, $blocks)) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. public function showActivityPub(Request $request, $user)
  146. {
  147. abort_if(!config('federation.activitypub.enabled'), 404);
  148. abort_if($user->domain, 404);
  149. $fractal = new Fractal\Manager();
  150. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  151. $res = $fractal->createData($resource)->toArray();
  152. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  153. }
  154. public function showAtomFeed(Request $request, $user)
  155. {
  156. abort_if(!config('federation.atom.enabled'), 404);
  157. $profile = $user = Profile::whereNull('status')->whereNull('domain')->whereUsername($user)->whereIsPrivate(false)->firstOrFail();
  158. if($profile->status != null) {
  159. return $this->accountCheck($profile);
  160. }
  161. if($profile->is_private || Auth::check()) {
  162. $blocked = $this->blockedProfileCheck($profile);
  163. $check = $this->privateProfileCheck($profile, null);
  164. if($check || $blocked) {
  165. return redirect($profile->url());
  166. }
  167. }
  168. $items = $profile->statuses()->whereHas('media')->whereIn('visibility',['public', 'unlisted'])->orderBy('created_at', 'desc')->take(10)->get();
  169. return response()->view('atom.user', compact('profile', 'items'))
  170. ->header('Content-Type', 'application/atom+xml');
  171. }
  172. public function meRedirect()
  173. {
  174. abort_if(!Auth::check(), 404);
  175. return redirect(Auth::user()->url());
  176. }
  177. public function embed(Request $request, $username)
  178. {
  179. $res = view('profile.embed-removed');
  180. if(strlen($username) > 15 || strlen($username) < 2) {
  181. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  182. }
  183. $profile = Profile::whereUsername($username)
  184. ->whereIsPrivate(false)
  185. ->whereNull('status')
  186. ->whereNull('domain')
  187. ->first();
  188. if(!$profile) {
  189. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  190. }
  191. $content = Cache::remember('profile:embed:'.$profile->id, now()->addHours(12), function() use($profile) {
  192. return View::make('profile.embed')->with(compact('profile'))->render();
  193. });
  194. return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  195. }
  196. public function stories(Request $request, $username)
  197. {
  198. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  199. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  200. $pid = $profile->id;
  201. $authed = Auth::user()->profile;
  202. abort_if($pid != $authed->id && $profile->followedBy($authed) == false, 404);
  203. $exists = Story::whereProfileId($pid)
  204. ->where('expires_at', '>', now())
  205. ->count();
  206. abort_unless($exists > 0, 404);
  207. return view('profile.story', compact('pid', 'profile'));
  208. }
  209. }