ProfileController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\Follower;
  9. use App\FollowRequest;
  10. use App\Profile;
  11. use App\Story;
  12. use App\User;
  13. use App\UserFilter;
  14. use League\Fractal;
  15. use App\Services\AccountService;
  16. use App\Services\FollowerService;
  17. use App\Services\StatusService;
  18. use App\Util\Lexer\Nickname;
  19. use App\Util\Webfinger\Webfinger;
  20. use App\Transformer\ActivityPub\ProfileOutbox;
  21. use App\Transformer\ActivityPub\ProfileTransformer;
  22. class ProfileController extends Controller
  23. {
  24. public function show(Request $request, $username)
  25. {
  26. $user = Profile::whereNull('domain')
  27. ->whereNull('status')
  28. ->whereUsername($username)
  29. ->firstOrFail();
  30. if($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  31. return $this->showActivityPub($request, $user);
  32. }
  33. return $this->buildProfile($request, $user);
  34. }
  35. protected function buildProfile(Request $request, $user)
  36. {
  37. $username = $user->username;
  38. $loggedIn = Auth::check();
  39. $isPrivate = false;
  40. $isBlocked = false;
  41. if(!$loggedIn) {
  42. $key = 'profile:settings:' . $user->id;
  43. $ttl = now()->addHours(6);
  44. $settings = Cache::remember($key, $ttl, function() use($user) {
  45. return $user->user->settings;
  46. });
  47. if ($user->is_private == true) {
  48. $profile = null;
  49. return view('profile.private', compact('user'));
  50. }
  51. $owner = false;
  52. $is_following = false;
  53. $profile = $user;
  54. $settings = [
  55. 'crawlable' => $settings->crawlable,
  56. 'following' => [
  57. 'count' => $settings->show_profile_following_count,
  58. 'list' => $settings->show_profile_following
  59. ],
  60. 'followers' => [
  61. 'count' => $settings->show_profile_follower_count,
  62. 'list' => $settings->show_profile_followers
  63. ]
  64. ];
  65. $ui = $request->has('ui') && $request->input('ui') == 'memory' ? 'profile.memory' : 'profile.show';
  66. return view($ui, compact('profile', 'settings'));
  67. } else {
  68. $key = 'profile:settings:' . $user->id;
  69. $ttl = now()->addHours(6);
  70. $settings = Cache::remember($key, $ttl, function() use($user) {
  71. return $user->user->settings;
  72. });
  73. if ($user->is_private == true) {
  74. $isPrivate = $this->privateProfileCheck($user, $loggedIn);
  75. }
  76. $isBlocked = $this->blockedProfileCheck($user);
  77. $owner = $loggedIn && Auth::id() === $user->user_id;
  78. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  79. if ($isPrivate == true || $isBlocked == true) {
  80. $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
  81. ->whereFollowingId($user->id)
  82. ->exists() : false;
  83. return view('profile.private', compact('user', 'is_following', 'requested'));
  84. }
  85. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  86. $profile = $user;
  87. $settings = [
  88. 'crawlable' => $settings->crawlable,
  89. 'following' => [
  90. 'count' => $settings->show_profile_following_count,
  91. 'list' => $settings->show_profile_following
  92. ],
  93. 'followers' => [
  94. 'count' => $settings->show_profile_follower_count,
  95. 'list' => $settings->show_profile_followers
  96. ]
  97. ];
  98. $ui = $request->has('ui') && $request->input('ui') == 'memory' ? 'profile.memory' : 'profile.show';
  99. return view($ui, compact('profile', 'settings'));
  100. }
  101. }
  102. public function permalinkRedirect(Request $request, $username)
  103. {
  104. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  105. if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  106. return $this->showActivityPub($request, $user);
  107. }
  108. return redirect($user->url());
  109. }
  110. protected function privateProfileCheck(Profile $profile, $loggedIn)
  111. {
  112. if (!Auth::check()) {
  113. return true;
  114. }
  115. $user = Auth::user()->profile;
  116. if($user->id == $profile->id || !$profile->is_private) {
  117. return false;
  118. }
  119. $follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
  120. if ($follows == false) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. public static function accountCheck(Profile $profile)
  126. {
  127. switch ($profile->status) {
  128. case 'disabled':
  129. case 'suspended':
  130. case 'delete':
  131. return view('profile.disabled');
  132. break;
  133. default:
  134. break;
  135. }
  136. return abort(404);
  137. }
  138. protected function blockedProfileCheck(Profile $profile)
  139. {
  140. $pid = Auth::user()->profile->id;
  141. $blocks = UserFilter::whereUserId($profile->id)
  142. ->whereFilterType('block')
  143. ->whereFilterableType('App\Profile')
  144. ->pluck('filterable_id')
  145. ->toArray();
  146. if (in_array($pid, $blocks)) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. public function showActivityPub(Request $request, $user)
  152. {
  153. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  154. abort_if($user->domain, 404);
  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. public function showAtomFeed(Request $request, $user)
  161. {
  162. abort_if(!config('federation.atom.enabled'), 404);
  163. $pid = AccountService::usernameToId($user);
  164. abort_if(!$pid, 404);
  165. $profile = AccountService::get($pid);
  166. abort_if(!$profile || $profile['locked'] || !$profile['local'], 404);
  167. $items = DB::table('statuses')
  168. ->whereProfileId($pid)
  169. ->whereVisibility('public')
  170. ->whereType('photo')
  171. ->latest()
  172. ->take(10)
  173. ->get()
  174. ->map(function($status) {
  175. return StatusService::get($status->id);
  176. })
  177. ->filter(function($status) {
  178. return $status &&
  179. isset($status['account']) &&
  180. isset($status['media_attachments']) &&
  181. count($status['media_attachments']);
  182. })
  183. ->values();
  184. $permalink = config('app.url') . "/users/{$profile['username']}.atom";
  185. return response()
  186. ->view('atom.user', compact('profile', 'items', 'permalink'))
  187. ->header('Content-Type', 'application/atom+xml');
  188. }
  189. public function meRedirect()
  190. {
  191. abort_if(!Auth::check(), 404);
  192. return redirect(Auth::user()->url());
  193. }
  194. public function embed(Request $request, $username)
  195. {
  196. $res = view('profile.embed-removed');
  197. if(strlen($username) > 15 || strlen($username) < 2) {
  198. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  199. }
  200. $profile = Profile::whereUsername($username)
  201. ->whereIsPrivate(false)
  202. ->whereNull('status')
  203. ->whereNull('domain')
  204. ->first();
  205. if(!$profile) {
  206. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  207. }
  208. if(AccountService::canEmbed($profile->user_id) == false) {
  209. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  210. }
  211. $profile = AccountService::get($profile->id);
  212. $res = view('profile.embed', compact('profile'));
  213. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  214. }
  215. public function stories(Request $request, $username)
  216. {
  217. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  218. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  219. $pid = $profile->id;
  220. $authed = Auth::user()->profile_id;
  221. abort_if($pid != $authed && !FollowerService::follows($authed, $pid), 404);
  222. $exists = Story::whereProfileId($pid)
  223. ->whereActive(true)
  224. ->exists();
  225. abort_unless($exists, 404);
  226. return view('profile.story', compact('pid', 'profile'));
  227. }
  228. }