8889841cControllers/Controller.php000066600000011405150516025250011714 0ustar00channelManager = $channelManager; } public function onOpen(ConnectionInterface $connection, RequestInterface $request = null) { $this->request = $request; $this->contentLength = $this->findContentLength($request->getHeaders()); $this->requestBuffer = (string) $request->getBody(); $this->checkContentLength($connection); } protected function findContentLength(array $headers): int { return Collection::make($headers)->first(function ($values, $header) { return strtolower($header) === 'content-length'; })[0] ?? 0; } public function onMessage(ConnectionInterface $from, $msg) { $this->requestBuffer .= $msg; $this->checkContentLength($from); } protected function checkContentLength(ConnectionInterface $connection) { if (strlen($this->requestBuffer) === $this->contentLength) { $serverRequest = (new ServerRequest( $this->request->getMethod(), $this->request->getUri(), $this->request->getHeaders(), $this->requestBuffer, $this->request->getProtocolVersion() ))->withQueryParams(QueryParameters::create($this->request)->all()); $laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest)); $this ->ensureValidAppId($laravelRequest->appId) ->ensureValidSignature($laravelRequest); $response = new JsonResponse($this($laravelRequest)); $content = $response->content(); $response->header('Content-Length', strlen($content)); $connection->send($response); $connection->close(); } } public function onClose(ConnectionInterface $connection) { } public function onError(ConnectionInterface $connection, Exception $exception) { if (! $exception instanceof HttpException) { return; } $responseData = json_encode([ 'error' => $exception->getMessage(), ]); $response = new Response($exception->getStatusCode(), [ 'Content-Type' => 'application/json', 'Content-Length' => strlen($responseData), ], $responseData); $connection->send(Message::toString($response)); $connection->close(); } public function ensureValidAppId(string $appId) { if (! App::findById($appId)) { throw new HttpException(401, "Unknown app id `{$appId}` provided."); } return $this; } protected function ensureValidSignature(Request $request) { /* * The `auth_signature` & `body_md5` parameters are not included when calculating the `auth_signature` value. * * The `appId`, `appKey` & `channelName` parameters are actually route paramaters and are never supplied by the client. */ $params = Arr::except($request->query(), ['auth_signature', 'body_md5', 'appId', 'appKey', 'channelName']); if ($request->getContent() !== '') { $params['body_md5'] = md5($request->getContent()); } ksort($params); $signature = "{$request->getMethod()}\n/{$request->path()}\n".Pusher::array_implode('=', '&', $params); $authSignature = hash_hmac('sha256', $signature, App::findById($request->get('appId'))->secret); if ($authSignature !== $request->get('auth_signature')) { throw new HttpException(401, 'Invalid auth signature provided.'); } return $this; } abstract public function __invoke(Request $request); } Controllers/FetchChannelsController.php000066600000002633150516025250014345 0ustar00has('info')) { $attributes = explode(',', trim($request->info)); if (in_array('user_count', $attributes) && ! Str::startsWith($request->filter_by_prefix, 'presence-')) { throw new HttpException(400, 'Request must be limited to presence channels in order to fetch user_count'); } } $channels = Collection::make($this->channelManager->getChannels($request->appId)); if ($request->has('filter_by_prefix')) { $channels = $channels->filter(function ($channel, $channelName) use ($request) { return Str::startsWith($channelName, $request->filter_by_prefix); }); } return [ 'channels' => $channels->map(function ($channel) use ($attributes) { $info = new \stdClass; if (in_array('user_count', $attributes)) { $info->user_count = count($channel->getUsers()); } return $info; })->toArray() ?: new \stdClass, ]; } } Controllers/FetchChannelController.php000066600000001014150516025250014152 0ustar00channelManager->find($request->appId, $request->channelName); if (is_null($channel)) { throw new HttpException(404, "Unknown channel `{$request->channelName}`."); } return $channel->toArray(); } } Controllers/TriggerEventController.php000066600000002570150516025250014245 0ustar00ensureValidSignature($request); $payload = $request->json(); if ($payload->has('channel')) { $channels = [$payload->get('channel')]; } else { $channels = $payload->all()['channels'] ?? []; if (is_string($channels)) { $channels = [$channels]; } } foreach ($channels as $channelName) { $channel = $this->channelManager->find($request->appId, $channelName); optional($channel)->broadcastToEveryoneExcept([ 'channel' => $channelName, 'event' => $request->json()->get('name'), 'data' => $request->json()->get('data'), ], $request->json()->get('socket_id')); DashboardLogger::apiMessage( $request->appId, $channelName, $request->json()->get('name'), $request->json()->get('data') ); StatisticsLogger::apiMessage($request->appId); } return (object) []; } } Controllers/FetchUsersController.php000066600000001662150516025250013714 0ustar00channelManager->find($request->appId, $request->channelName); if (is_null($channel)) { throw new HttpException(404, 'Unknown channel "'.$request->channelName.'"'); } if (! $channel instanceof PresenceChannel) { throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"'); } return [ 'users' => Collection::make($channel->getUsers())->keys()->map(function ($userId) { return ['id' => $userId]; })->values(), ]; } }