ProfileController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. if(config('database.default') == 'pgsql') {
  23. $username = strtolower($username);
  24. }
  25. $user = Profile::whereNull('domain')
  26. ->whereNull('status')
  27. ->whereUsername($username)
  28. ->firstOrFail();
  29. if($request->wantsJson() && config('federation.activitypub.enabled')) {
  30. return $this->showActivityPub($request, $user);
  31. }
  32. return $this->buildProfile($request, $user);
  33. }
  34. protected function buildProfile(Request $request, $user)
  35. {
  36. $username = $user->username;
  37. $loggedIn = Auth::check();
  38. $isPrivate = false;
  39. $isBlocked = false;
  40. if(!$loggedIn) {
  41. $key = 'profile:settings:' . $user->id;
  42. $ttl = now()->addHours(6);
  43. $settings = Cache::remember($key, $ttl, function() use($user) {
  44. return $user->user->settings;
  45. });
  46. if ($user->is_private == true) {
  47. abort(404);
  48. }
  49. $owner = false;
  50. $is_following = false;
  51. $is_admin = $user->user->is_admin;
  52. $profile = $user;
  53. $settings = [
  54. 'crawlable' => $settings->crawlable,
  55. 'following' => [
  56. 'count' => $settings->show_profile_following_count,
  57. 'list' => $settings->show_profile_following
  58. ],
  59. 'followers' => [
  60. 'count' => $settings->show_profile_follower_count,
  61. 'list' => $settings->show_profile_followers
  62. ]
  63. ];
  64. return view('profile.show', compact('profile', 'settings'));
  65. } else {
  66. $key = 'profile:settings:' . $user->id;
  67. $ttl = now()->addHours(6);
  68. $settings = Cache::remember($key, $ttl, function() use($user) {
  69. return $user->user->settings;
  70. });
  71. if ($user->is_private == true) {
  72. $isPrivate = $this->privateProfileCheck($user, $loggedIn);
  73. }
  74. $isBlocked = $this->blockedProfileCheck($user);
  75. $owner = $loggedIn && Auth::id() === $user->user_id;
  76. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  77. if ($isPrivate == true || $isBlocked == true) {
  78. $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
  79. ->whereFollowingId($user->id)
  80. ->exists() : false;
  81. return view('profile.private', compact('user', 'is_following', 'requested'));
  82. }
  83. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  84. $profile = $user;
  85. $settings = [
  86. 'crawlable' => $settings->crawlable,
  87. 'following' => [
  88. 'count' => $settings->show_profile_following_count,
  89. 'list' => $settings->show_profile_following
  90. ],
  91. 'followers' => [
  92. 'count' => $settings->show_profile_follower_count,
  93. 'list' => $settings->show_profile_followers
  94. ]
  95. ];
  96. return view('profile.show', 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('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('federation.activitypub.enabled'), 404);
  151. abort_if($user->domain, 404);
  152. $key = 'profile:ap:' . $user->id;
  153. $ttl = now()->addHours(6);
  154. return Cache::remember($key, $ttl, function() use($user) {
  155. $fractal = new Fractal\Manager();
  156. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  157. $res = $fractal->createData($resource)->toArray();
  158. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  159. });
  160. }
  161. public function showAtomFeed(Request $request, $user)
  162. {
  163. abort_if(!config('federation.atom.enabled'), 404);
  164. $profile = $user = Profile::whereNull('status')->whereNull('domain')->whereUsername($user)->whereIsPrivate(false)->firstOrFail();
  165. if($profile->status != null) {
  166. return $this->accountCheck($profile);
  167. }
  168. if($profile->is_private || Auth::check()) {
  169. $blocked = $this->blockedProfileCheck($profile);
  170. $check = $this->privateProfileCheck($profile, null);
  171. if($check || $blocked) {
  172. return redirect($profile->url());
  173. }
  174. }
  175. $items = $profile->statuses()->whereHas('media')->whereIn('visibility',['public', 'unlisted'])->orderBy('created_at', 'desc')->take(10)->get();
  176. return response()->view('atom.user', compact('profile', 'items'))
  177. ->header('Content-Type', 'application/atom+xml');
  178. }
  179. public function meRedirect()
  180. {
  181. abort_if(!Auth::check(), 404);
  182. return redirect(Auth::user()->url());
  183. }
  184. public function embed(Request $request, $username)
  185. {
  186. $res = view('profile.embed-removed');
  187. if(strlen($username) > 15 || strlen($username) < 2) {
  188. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  189. }
  190. $profile = Profile::whereUsername($username)
  191. ->whereIsPrivate(false)
  192. ->whereNull('status')
  193. ->whereNull('domain')
  194. ->first();
  195. if(!$profile) {
  196. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  197. }
  198. $content = Cache::remember('profile:embed:'.$profile->id, now()->addHours(12), function() use($profile) {
  199. return View::make('profile.embed')->with(compact('profile'))->render();
  200. });
  201. return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  202. }
  203. public function stories(Request $request, $username)
  204. {
  205. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  206. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  207. $pid = $profile->id;
  208. $authed = Auth::user()->profile;
  209. abort_if($pid != $authed->id && $profile->followedBy($authed) == false, 404);
  210. $exists = Story::whereProfileId($pid)
  211. ->where('expires_at', '>', now())
  212. ->count();
  213. abort_unless($exists > 0, 404);
  214. return view('profile.story', compact('pid'));
  215. }
  216. }