ProfileController.php 7.0 KB

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