diff --git a/.pubnub.yml b/.pubnub.yml index 80eaa75f..7ea565d9 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,8 +1,13 @@ name: php -version: 6.2.1 +version: 6.3.0 schema: 1 scm: github.com/pubnub/php changelog: + - date: 2024-06-18 + version: v6.3.0 + changes: + - type: feature + text: "Added support for file sharing operations." - date: 2024-06-11 version: v6.2.1 changes: @@ -417,8 +422,8 @@ sdks: - x86-64 - distribution-type: library distribution-repository: GitHub release - package-name: php-6.2.1.zip - location: https://github.com/pubnub/php/releases/tag/v6.2.1 + package-name: php-6.3.0.zip + location: https://github.com/pubnub/php/releases/tag/v6.3.0 requires: - name: rmccue/requests min-version: 1.0.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 45ad0199..80353f89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v6.3.0 +June 18 2024 + +#### Added +- Added support for file sharing operations. + ## v6.2.1 June 11 2024 diff --git a/README.md b/README.md index 3524e8f3..e9e0e05a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your { "require": { - "pubnub/pubnub": "6.2.1" + "pubnub/pubnub": "6.3.0" } } ``` diff --git a/composer.json b/composer.json index 50d1e63a..0a714dcb 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "keywords": ["api", "real-time", "realtime", "real time", "ajax", "push"], "homepage": "http://www.pubnub.com/", "license": "proprietary", - "version": "6.2.1", + "version": "6.3.0", "authors": [ { "name": "PubNub", diff --git a/examples/FileSharing.php b/examples/FileSharing.php new file mode 100644 index 00000000..b855bbd4 --- /dev/null +++ b/examples/FileSharing.php @@ -0,0 +1,73 @@ +setSubscribeKey(getenv('SUBSCRIBE_KEY', 'demo')); +$config->setPublishKey(getenv('PUBLISH_KEY', 'demo')); +$config->setUserId('example'); + +$pubnub = new PubNub($config); + +// Sending file +$fileHandle = fopen(__DIR__ . DIRECTORY_SEPARATOR . $fileName, "r"); +$sendFileResult = $pubnub->sendFile() + ->channel($channelName) + ->fileName($fileName) + ->message("Hello from PHP SDK") + ->fileHandle($fileHandle) + ->sync(); + +fclose($fileHandle); + +// Listing files in the channel +$channelFiles = $pubnub->listFiles()->channel($channelName)->sync(); +$fileCount = $channelFiles->getCount(); +if ($fileCount > 0) { + print("There are {$fileCount} files in the channel {$channelName}\n"); + foreach ($channelFiles->getFiles() as $idx => $file) { + print("File[{$idx}]: {$file->getName()} with ID: {$file->getId()}," + . "size {$file->getSize()}, created at: {$file->getCreationTime()}\n"); + } +} else { + print("There are no files in the channel {$channelName}\n"); +} + +$file = $channelFiles->getFiles()[0]; + +print('Getting download URL for the file...'); +$downloadUrl = $pubnub->getFileDownloadUrl() + ->channel($channelName) + ->fileId($file->getId()) + ->fileName($file->getName()) + ->sync(); + +printf("To download the file use the following URL: %s\n", $downloadUrl->getFileUrl()); + +print("Downloading file..."); +$downloadFile = $pubnub->downloadFile() + ->channel($channelName) + ->fileId($file->getId()) + ->fileName($file->getName()) + ->sync(); +file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . $file->getName(), $downloadFile->getFileContent()); +print("done. File saved as: {$file->getName()}\n"); + +// deleting file +$deleteFile = $pubnub->deleteFile() + ->channel($channelName) + ->fileId($file->getId()) + ->fileName($file->getName()) + ->sync(); + +if ($deleteFile->getStatus() === 200) { + print("File deleted successfully\n"); +} else { + print("Failed to delete file\n"); +} diff --git a/src/PubNub/Endpoints/Access/Audit.php b/src/PubNub/Endpoints/Access/Audit.php index 9a0d6579..40ad01e3 100644 --- a/src/PubNub/Endpoints/Access/Audit.php +++ b/src/PubNub/Endpoints/Access/Audit.php @@ -94,12 +94,12 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNAccessManagerAuditResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNAccessManagerAuditResult::fromJson($json['payload']); + return PNAccessManagerAuditResult::fromJson($result['payload']); } /** diff --git a/src/PubNub/Endpoints/Access/Grant.php b/src/PubNub/Endpoints/Access/Grant.php index 1850d798..d8d90ff4 100644 --- a/src/PubNub/Endpoints/Access/Grant.php +++ b/src/PubNub/Endpoints/Access/Grant.php @@ -285,12 +285,12 @@ public function sync() } /** - * @param array $json + * @param array $result * @return PNAccessManagerGrantResult */ - public function createResponse($json) + public function createResponse($result) { - return PNAccessManagerGrantResult::fromJson($json['payload']); + return PNAccessManagerGrantResult::fromJson($result['payload']); } /** diff --git a/src/PubNub/Endpoints/ChannelGroups/AddChannelToChannelGroup.php b/src/PubNub/Endpoints/ChannelGroups/AddChannelToChannelGroup.php index 0263076f..dcc6c3d7 100644 --- a/src/PubNub/Endpoints/ChannelGroups/AddChannelToChannelGroup.php +++ b/src/PubNub/Endpoints/ChannelGroups/AddChannelToChannelGroup.php @@ -79,10 +79,10 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNChannelGroupsAddChannelResult */ - protected function createResponse($json) + protected function createResponse($result) { return new PNChannelGroupsAddChannelResult(); } diff --git a/src/PubNub/Endpoints/ChannelGroups/ListChannelsInChannelGroup.php b/src/PubNub/Endpoints/ChannelGroups/ListChannelsInChannelGroup.php index f536efa4..fd729fc2 100644 --- a/src/PubNub/Endpoints/ChannelGroups/ListChannelsInChannelGroup.php +++ b/src/PubNub/Endpoints/ChannelGroups/ListChannelsInChannelGroup.php @@ -78,12 +78,12 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNChannelGroupsListChannelsResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNChannelGroupsListChannelsResult::fromPayload(static::fetchPayload($json)); + return PNChannelGroupsListChannelsResult::fromPayload(static::fetchPayload($result)); } /** diff --git a/src/PubNub/Endpoints/ChannelGroups/RemoveChannelFromChannelGroup.php b/src/PubNub/Endpoints/ChannelGroups/RemoveChannelFromChannelGroup.php index 6c9bae78..a0722bd7 100644 --- a/src/PubNub/Endpoints/ChannelGroups/RemoveChannelFromChannelGroup.php +++ b/src/PubNub/Endpoints/ChannelGroups/RemoveChannelFromChannelGroup.php @@ -99,10 +99,10 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNChannelGroupsRemoveChannelResult */ - protected function createResponse($json) + protected function createResponse($result) { return new PNChannelGroupsRemoveChannelResult(); } diff --git a/src/PubNub/Endpoints/ChannelGroups/RemoveChannelGroup.php b/src/PubNub/Endpoints/ChannelGroups/RemoveChannelGroup.php index 1082a57d..6140e4e5 100644 --- a/src/PubNub/Endpoints/ChannelGroups/RemoveChannelGroup.php +++ b/src/PubNub/Endpoints/ChannelGroups/RemoveChannelGroup.php @@ -78,10 +78,10 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNChannelGroupsRemoveGroupResult */ - protected function createResponse($json) + protected function createResponse($result) { return new PNChannelGroupsRemoveGroupResult(); } diff --git a/src/PubNub/Endpoints/Endpoint.php b/src/PubNub/Endpoints/Endpoint.php index 1334a06a..9db615aa 100755 --- a/src/PubNub/Endpoints/Endpoint.php +++ b/src/PubNub/Endpoints/Endpoint.php @@ -25,15 +25,21 @@ abstract class Endpoint { + protected const RESPONSE_IS_JSON = true; + /** @var PubNub */ protected $pubnub; /** @var PNEnvelope */ protected $envelope; + protected $customHost = null; + /** @var array */ protected static $cachedTransports = []; + protected $followRedirects = true; + public function __construct(PubNub $pubnubInstance) { $this->pubnub = $pubnubInstance; @@ -42,10 +48,10 @@ public function __construct(PubNub $pubnubInstance) abstract protected function validateParams(); /** - * @param array $json Decoded json + * @param array $result Decoded json * @return mixed */ - abstract protected function createResponse($json); + abstract protected function createResponse($result); /** * @return int @@ -321,20 +327,18 @@ protected function invokeRequestAndCacheIt() */ protected function requestOptions() { - $options = [ + return [ 'timeout' => $this->getRequestTimeout(), 'connect_timeout' => $this->getConnectTimeout(), - 'transport' => $this->getDefaultTransport(), + 'transport' => $this->getTransport(), 'useragent' => 'PHP/' . PHP_VERSION, + 'follow_redirects' => $this->followRedirects, ]; + } - $transport = $this->pubnub->getConfiguration()->getTransport(); - - if ($transport) { - $options['transport'] = $transport; - } - - return $options; + protected function getTransport() + { + return $this->pubnub->getConfiguration()->getTransport() ?? $this->getDefaultTransport(); } /** @@ -345,7 +349,7 @@ protected function invokeRequest() $headers = array_merge($this->defaultHeaders(), $this->customHeaders()); $url = PubNubUtil::buildUrl( - $this->pubnub->getBasePath(), + $this->pubnub->getBasePath($this->customHost), $this->buildPath(), $this->buildParams() ); @@ -459,28 +463,40 @@ protected function invokeRequest() ['method' => $this->getName(), 'statusCode' => $request->status_code] ); - // NOTICE: 1 == JSON_OBJECT_AS_ARRAY (hhvm doesn't support this constant) - $parsedJSON = json_decode($request->body, true, 512, 1); - $errorMessage = json_last_error_msg(); - - if (json_last_error()) { - $this->pubnub->getLogger()->error( - "Unable to decode JSON body: " . $request->body, - ['method' => $this->getName()] + if (static::RESPONSE_IS_JSON) { + // NOTICE: 1 == JSON_OBJECT_AS_ARRAY (hhvm doesn't support this constant) + $parsedJSON = json_decode($request->body, true, 512, 1); + $errorMessage = json_last_error_msg(); + + if (json_last_error()) { + $this->pubnub->getLogger()->error( + "Unable to decode JSON body: " . $request->body, + ['method' => $this->getName()] + ); + + return new PNEnvelope(null, $this->createStatus( + $statusCategory, + $request->body, + $responseInfo, + (new PubNubResponseParsingException()) + ->setResponseString($request->body) + ->setDescription($errorMessage) + )); + } + + return new PNEnvelope( + $this->createResponse($parsedJSON), + $this->createStatus($statusCategory, $request->body, $responseInfo, null) + ); + } else { + return new PNEnvelope( + $this->createResponse($request->body), + $this->createStatus($statusCategory, $request->body, $responseInfo, null) ); - - return new PNEnvelope(null, $this->createStatus( - $statusCategory, - $request->body, - $responseInfo, - (new PubNubResponseParsingException()) - ->setResponseString($request->body) - ->setDescription($errorMessage) - )); } - + } elseif ($request->status_code === 307 && !$this->followRedirects) { return new PNEnvelope( - $this->createResponse($parsedJSON), + $this->createResponse($request), $this->createStatus($statusCategory, $request->body, $responseInfo, null) ); } else { @@ -572,7 +588,7 @@ protected function getAffectedUsers() */ private function getDefaultTransport() { - $need_ssl = (0 === stripos($this->pubnub->getBasePath(), 'https://')); + $need_ssl = (0 === stripos($this->pubnub->getBasePath($this->customHost), 'https://')); $capabilities = array('ssl' => $need_ssl); $cap_string = serialize($capabilities); diff --git a/src/PubNub/Endpoints/FileSharing/DeleteFile.php b/src/PubNub/Endpoints/FileSharing/DeleteFile.php new file mode 100644 index 00000000..be9897fa --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/DeleteFile.php @@ -0,0 +1,77 @@ +pubnub->getConfiguration()->getSubscribeKey(), + PubNubUtil::urlEncode($this->channel), + PubNubUtil::urlEncode($this->fileId), + PubNubUtil::urlEncode($this->fileName) + ); + } + + /** + * @return array + */ + protected function customParams() + { + return []; + } + + /** + * @return string PNHttpMethod + */ + protected function httpMethod() + { + return PNHttpMethod::DELETE; + } + + public function name() + { + return "Delete file"; + } +} diff --git a/src/PubNub/Endpoints/FileSharing/DownloadFile.php b/src/PubNub/Endpoints/FileSharing/DownloadFile.php new file mode 100644 index 00000000..9325376e --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/DownloadFile.php @@ -0,0 +1,64 @@ +pubnub->getConfiguration()->getSubscribeKey(), + $this->channel, + $this->fileId, + $this->fileName + ); + } + + protected function customParams() + { + return []; + } + + protected function httpMethod() + { + return PNHttpMethod::GET; + } + + protected function isAuthRequired() + { + return false; + } + + protected function createResponse($result) + { + if ($this->pubnub->isCryptoEnabled()) { + return new PNDownloadFileResult($this->pubnub->getCrypto()->decrypt((string)$result)); + } + return new PNDownloadFileResult($result); + } + + protected function getOperationType() + { + return PNOperationType::PNDownloadFileAction; + } + + protected function name() + { + return "Downloading file"; + } + + protected function buildData() + { + return null; + } +} diff --git a/src/PubNub/Endpoints/FileSharing/FetchFileUploadS3Data.php b/src/PubNub/Endpoints/FileSharing/FetchFileUploadS3Data.php new file mode 100644 index 00000000..1055bd1a --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/FetchFileUploadS3Data.php @@ -0,0 +1,107 @@ +channel = $channel; + return $this; + } + + public function fileName($fileName) + { + $this->fileName = $fileName; + return $this; + } + + protected function validateParams(): void + { + $this->validateSubscribeKey(); + $this->validateChannel(); + $this->validateFileName(); + } + + protected function getRequestTimeout(): int + { + return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + } + + protected function getConnectTimeout(): int + { + return $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + protected function validateChannel(): void + { + if (!$this->channel) { + throw new PubNubValidationException("Channel missing"); + } + } + + protected function validateFileName(): void + { + if ($this->fileName === null) { + throw new PubNubValidationException("File name missing"); + } + } + public function buildPath() + { + return sprintf( + static::ENDPOINT_URL, + $this->pubnub->getConfiguration()->getSubscribeKey(), + PubNubUtil::urlEncode($this->channel) + ); + } + + public function getOperationType() + { + return PNOperationType::PNFetchFileUploadS3DataAction; + } + + public function name() + { + return "Fetch file upload S3 data"; + } + + public function httpMethod() + { + return PNHttpMethod::POST; + } + + public function createResponse($result) + { + return new PNFetchFileUploadS3DataResult($result); + } + + public function isAuthRequired() + { + return true; + } + + public function buildData() + { + $params = [ + "name" => $this->fileName + ]; + return PubNubUtil::writeValueAsString($params); + } + + public function customParams() + { + return []; + } +} diff --git a/src/PubNub/Endpoints/FileSharing/FileSharingEndpoint.php b/src/PubNub/Endpoints/FileSharing/FileSharingEndpoint.php new file mode 100644 index 00000000..2b3cde0e --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/FileSharingEndpoint.php @@ -0,0 +1,70 @@ +channel = $channel; + return $this; + } + + public function fileId($fileId): self + { + $this->fileId = $fileId; + return $this; + } + + public function fileName($fileName) + { + $this->fileName = $fileName; + return $this; + } + + protected function validateParams(): void + { + $this->validateSubscribeKey(); + $this->validateChannel(); + $this->validateFileId(); + $this->validateFileName(); + } + + protected function getRequestTimeout(): int + { + return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + } + + protected function getConnectTimeout(): int + { + return $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + protected function validateChannel(): void + { + if (!$this->channel) { + throw new PubNubValidationException("Channel missing"); + } + } + + protected function validateFileId(): void + { + if ($this->fileId === null) { + throw new PubNubValidationException("File ID missing"); + } + } + + protected function validateFileName(): void + { + if ($this->fileName === null) { + throw new PubNubValidationException("File name missing"); + } + } +} diff --git a/src/PubNub/Endpoints/FileSharing/GetFileDownloadUrl.php b/src/PubNub/Endpoints/FileSharing/GetFileDownloadUrl.php new file mode 100644 index 00000000..0ff670f1 --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/GetFileDownloadUrl.php @@ -0,0 +1,69 @@ +pubnub->getConfiguration()->getSubscribeKey(), + $this->channel, + $this->fileId, + $this->fileName + ); + } + + + protected function customParams() + { + return []; + } + + protected function httpMethod() + { + return PNHttpMethod::GET; + } + + protected function isAuthRequired() + { + return false; + } + + protected function createResponse($result) + { + return new PNGetFileDownloadURLResult($result); + } + + protected function getOperationType() + { + return PNOperationType::PNGetFileDownloadURLAction; + } + + protected function name() + { + return "Downloading file"; + } + + protected function buildData() + { + return null; + } + + public function sync(): PNGetFileDownloadURLResult + { + return parent::sync(); + } +} diff --git a/src/PubNub/Endpoints/FileSharing/ListFiles.php b/src/PubNub/Endpoints/FileSharing/ListFiles.php new file mode 100644 index 00000000..753fd0e8 --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/ListFiles.php @@ -0,0 +1,91 @@ +channel = $channel; + return $this; + } + + protected function validateParams(): void + { + $this->validateSubscribeKey(); + $this->validateChannel(); + } + + protected function getRequestTimeout(): int + { + return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + } + + protected function getConnectTimeout(): int + { + return $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + protected function validateChannel(): void + { + if (!$this->channel) { + throw new PubNubValidationException("Channel missing"); + } + } + + protected function buildPath() + { + return sprintf( + static::ENDPOINT_URL, + $this->pubnub->getConfiguration()->getSubscribeKey(), + PubNubUtil::urlEncode($this->channel) + ); + } + + + protected function httpMethod(): string + { + return PNHttpMethod::GET; + } + + protected function customParams(): array + { + return []; + } + + protected function buildData() + { + return null; + } + + protected function isAuthRequired(): bool + { + return true; + } + + protected function createResponse($result): PNGetFilesResult + { + return new PNGetFilesResult($result); + } + + protected function getOperationType() + { + return PNOperationType::PNGetFilesAction; + } + + protected function name() + { + return "List files"; + } +} diff --git a/src/PubNub/Endpoints/FileSharing/PublishFileMessage.php b/src/PubNub/Endpoints/FileSharing/PublishFileMessage.php new file mode 100644 index 00000000..c9129809 --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/PublishFileMessage.php @@ -0,0 +1,123 @@ +message = $message; + return $this; + } + + public function meta($meta) + { + $this->meta = $meta; + return $this; + } + + public function shouldStore(bool $shouldStore) + { + $this->shouldStore = $shouldStore; + return $this; + } + + + public function ttl($ttl) + { + $this->ttl = $ttl; + return $this; + } + + protected function buildPath() + { + return sprintf( + self::ENDPOINT_URL, + $this->pubnub->getConfiguration()->getPublishKey(), + $this->pubnub->getConfiguration()->getSubscribeKey(), + urlencode($this->channel), + urlencode($this->buildMessage()) + ); + } + + public function encryptMessage($message) + { + $crypto = $this->pubnub->getCryptoSafe(); + $messageString = PubNubUtil::writeValueAsString($message); + if ($crypto) { + return $crypto->encrypt($messageString); + } + return $messageString; + } + + protected function buildMessage() + { + $messageData = [ + "message" => $this->message, + "file" => [ + "id" => $this->fileId, + "name" => $this->fileName + ] + ]; + + return $this->encryptMessage($messageData); + } + + protected function customParams() + { + $params['meta'] = json_encode($this->meta); + $params['ttl'] = $this->ttl; + $params['store'] = $this->shouldStore ? 1 : 0; + + return $params; + } + + protected function validateParams(): void + { + parent::validateParams(); + $this->validatePublishKey(); + } + + public function getOperationType(): string + { + return PNOperationType::PNSendFileAction; + } + + public function getName(): string + { + return "Sending file upload notification"; + } + + public function createResponse($result) + { + return new PNPublishFileMessageResult($result); + } + + protected function isAuthRequired() + { + return true; + } + + protected function buildData() + { + return null; + } + + protected function httpMethod() + { + return PNHttpMethod::GET; + } +} diff --git a/src/PubNub/Endpoints/FileSharing/SendFile.php b/src/PubNub/Endpoints/FileSharing/SendFile.php new file mode 100644 index 00000000..02ee6b43 --- /dev/null +++ b/src/PubNub/Endpoints/FileSharing/SendFile.php @@ -0,0 +1,283 @@ +channel = $channel; + return $this; + } + + public function fileName($fileName) + { + $this->fileName = $fileName; + return $this; + } + + public function message($message) + { + $this->message = $message; + return $this; + } + + public function meta($meta) + { + $this->meta = $meta; + return $this; + } + + public function shouldStore($shouldStore) + { + $this->shouldStore = $shouldStore; + return $this; + } + + public function ttl($ttl) + { + $this->ttl = $ttl; + return $this; + } + + public function fileHandle($fileHandle) + { + $this->fileHandle = $fileHandle; + return $this; + } + + public function fileContent($fileContent) + { + $this->fileContent = $fileContent; + return $this; + } + + public function requestTimeout() + { + return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + } + + protected function connectTimeout() + { + return $this->pubnub->getConfiguration()->getConnectTimeout(); + } + /** + * @throws PubNubValidationException + */ + protected function validateParams() + { + $this->validateSubscribeKey(); + $this->validateChannel(); + } + + protected function validateChannel(): void + { + if (!$this->channel) { + throw new PubNubValidationException("Channel missing"); + } + } + + /** + * @param array $result Decoded json + * @return PNPublishResult + */ + protected function createResponse($result) + { + throw new Exception('Not implemented'); + } + + /** + * @return bool + */ + protected function isAuthRequired() + { + return true; + } + + protected function buildData() + { + return null; + } + + /** + * @return int + */ + protected function getRequestTimeout() + { + return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + } + + /** + * @return int + */ + protected function getConnectTimeout() + { + return $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + /** + * @return string + */ + protected function httpMethod() + { + return PNHttpMethod::POST; + } + + /** + * @return int + */ + protected function getOperationType() + { + return PNOperationType::PNSendFileAction; + } + + /** + * @return string + */ + protected function getName() + { + return "Send File"; + } + + + /** + * @return array + */ + protected function customParams() + { + $params = []; + foreach ($this->customParamMapping as $customParam => $requestParam) { + if (isset($this->$customParam) && !empty($this->$customParam)) { + $params[$requestParam] = $this->$customParam; + } + } + + return $params; + } + + /** + * @return string + * @throws PubNubBuildRequestException + */ + protected function buildPath() + { + return parse_url($this->fileUploadEnvelope->getUrl(), PHP_URL_PATH); + } + + protected function encryptPayload() + { + $crypto = $this->pubnub->getCryptoSafe(); + if ($this->fileHandle) { + $fileContent = stream_get_contents($this->fileHandle); + } else { + $fileContent = $this->fileContent; + } + + if ($crypto) { + return $crypto->encrypt($fileContent); + } + return $fileContent; + } + + protected function getBoundary() + { + if (!isset($this->boundary)) { + $this->boundary = '---' . PubNubUtil::uuid() . '---'; + } + return $this->boundary; + } + + protected function buildPayload($data, $fileName, $fileContent) + { + $boundary = $this->getBoundary(); + + $payload = ''; + + foreach ($data as $element) { + $payload .= "--$boundary\r\n"; + $payload .= "Content-Disposition: form-data; name=\"{$element['key']}\"\r\n\r\n"; + $payload .= "{$element['value']}\r\n"; + } + + $payload .= "--$boundary\r\n"; + $payload .= "Content-Disposition: form-data; name=\"file\"; filename=\"{$fileName}\"\r\n\r\n"; + $payload .= "{$fileContent}\r\n"; + + $payload .= "--$boundary--\r\n"; + + return $payload; + } + + protected function uploadFile() + { + + $response = Requests::POST( + $this->fileUploadEnvelope->getUrl(), + ['Content-Type' => 'multipart/form-data; boundary=' . $this->getBoundary()], + $this->buildPayload( + $this->fileUploadEnvelope->getFormFields(), + $this->fileName, + $this->encryptPayload() + ) + ); + return $response; + } + + public function sync() + { + $this->fileUploadEnvelope = (new FetchFileUploadS3Data($this->pubnub)) + ->channel($this->channel) + ->fileName($this->fileName) + ->sync(); + + try { + $this->uploadFile(); + } catch (\Exception $e) { + throw $e; + } + + $publishRequest = new PublishFileMessage($this->pubnub); + $publishRequest->channel($this->channel) + ->fileId($this->fileUploadEnvelope->getFileId()) + ->fileName($this->fileName); + + if (isset($this->meta)) { + $publishRequest->meta($this->meta); + } + if (isset($this->message)) { + $publishRequest->message($this->message); + } + if (isset($this->shouldStore)) { + $publishRequest->shouldStore($this->shouldStore); + } + if (isset($this->ttl)) { + $publishRequest->ttl($this->ttl); + } + + $publishResponse = $publishRequest->sync(); + + return $publishResponse; + } +} diff --git a/src/PubNub/Endpoints/History.php b/src/PubNub/Endpoints/History.php index 8a932bc6..24b0ed2e 100644 --- a/src/PubNub/Endpoints/History.php +++ b/src/PubNub/Endpoints/History.php @@ -170,21 +170,21 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNHistoryResult */ - protected function createResponse($json) + protected function createResponse($result) { try { return PNHistoryResult::fromJson( - $json, + $result, $this->pubnub->getConfiguration()->getCryptoSafe(), $this->includeTimetoken, $this->pubnub->getConfiguration()->getCipherKey() ); } catch (PubNubValidationException $e) { return PNHistoryResult::fromJson( - $json, + $result, null, $this->includeTimetoken, null diff --git a/src/PubNub/Endpoints/HistoryDelete.php b/src/PubNub/Endpoints/HistoryDelete.php index 3e9997e7..66b9970c 100644 --- a/src/PubNub/Endpoints/HistoryDelete.php +++ b/src/PubNub/Endpoints/HistoryDelete.php @@ -67,10 +67,10 @@ protected function validateParams() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return mixed */ - protected function createResponse($json) + protected function createResponse($result) { return new PNHistoryDeleteResult(); } diff --git a/src/PubNub/Endpoints/MessageCount.php b/src/PubNub/Endpoints/MessageCount.php index 5cb1d70b..f6b7ca08 100644 --- a/src/PubNub/Endpoints/MessageCount.php +++ b/src/PubNub/Endpoints/MessageCount.php @@ -63,20 +63,20 @@ protected function validateParams() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNMessageCountResult * @throws PubNubServerException */ - protected function createResponse($json) + protected function createResponse($result) { - if (!isset($json['channels'])) { + if (!isset($result['channels'])) { $exception = (new PubNubServerException()) - ->setRawBody(json_encode($json)); + ->setRawBody(json_encode($result)); throw $exception; } - return new PNMessageCountResult($json['channels']); + return new PNMessageCountResult($result['channels']); } /** diff --git a/src/PubNub/Endpoints/Objects/Channel/GetAllChannelMetadata.php b/src/PubNub/Endpoints/Objects/Channel/GetAllChannelMetadata.php index deee0cfa..79be6e87 100644 --- a/src/PubNub/Endpoints/Objects/Channel/GetAllChannelMetadata.php +++ b/src/PubNub/Endpoints/Objects/Channel/GetAllChannelMetadata.php @@ -69,12 +69,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNGetAllChannelMetadataResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNGetAllChannelMetadataResult::fromPayload($json); + return PNGetAllChannelMetadataResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Channel/GetChannelMetadata.php b/src/PubNub/Endpoints/Objects/Channel/GetChannelMetadata.php index 5ba6796d..ad955c5a 100644 --- a/src/PubNub/Endpoints/Objects/Channel/GetChannelMetadata.php +++ b/src/PubNub/Endpoints/Objects/Channel/GetChannelMetadata.php @@ -61,12 +61,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNGetChannelMetadataResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNGetChannelMetadataResult::fromPayload($json); + return PNGetChannelMetadataResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Channel/RemoveChannelMetadata.php b/src/PubNub/Endpoints/Objects/Channel/RemoveChannelMetadata.php index bf8ea2f7..e7be49cd 100644 --- a/src/PubNub/Endpoints/Objects/Channel/RemoveChannelMetadata.php +++ b/src/PubNub/Endpoints/Objects/Channel/RemoveChannelMetadata.php @@ -60,12 +60,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return bool */ - protected function createResponse($json) + protected function createResponse($result) { - return array_key_exists("data", $json); + return array_key_exists("data", $result); } /** diff --git a/src/PubNub/Endpoints/Objects/Channel/SetChannelMetadata.php b/src/PubNub/Endpoints/Objects/Channel/SetChannelMetadata.php index 9c833d9d..2ed51a9d 100644 --- a/src/PubNub/Endpoints/Objects/Channel/SetChannelMetadata.php +++ b/src/PubNub/Endpoints/Objects/Channel/SetChannelMetadata.php @@ -80,12 +80,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNSetChannelMetadataResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNSetChannelMetadataResult::fromPayload($json); + return PNSetChannelMetadataResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Member/GetMembers.php b/src/PubNub/Endpoints/Objects/Member/GetMembers.php index 405f0dfc..0ef39e0e 100644 --- a/src/PubNub/Endpoints/Objects/Member/GetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/GetMembers.php @@ -75,12 +75,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNMembersResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNMembersResult::fromPayload($json); + return PNMembersResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php b/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php index a78ee8bd..b216847d 100644 --- a/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php @@ -109,12 +109,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNMembersResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNMembersResult::fromPayload($json); + return PNMembersResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Member/SetMembers.php b/src/PubNub/Endpoints/Objects/Member/SetMembers.php index b867c5c1..8446d800 100644 --- a/src/PubNub/Endpoints/Objects/Member/SetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/SetMembers.php @@ -128,12 +128,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNMembersResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNMembersResult::fromPayload($json); + return PNMembersResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php b/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php index aa18c140..c0e5af8d 100644 --- a/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php @@ -75,12 +75,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNMembershipsResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNMembershipsResult::fromPayload($json); + return PNMembershipsResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php index 2fd7a390..45950c89 100644 --- a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php @@ -109,12 +109,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNMembershipsResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNMembershipsResult::fromPayload($json); + return PNMembershipsResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php index 0305c645..6fd0cfdc 100644 --- a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php @@ -128,12 +128,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNMembershipsResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNMembershipsResult::fromPayload($json); + return PNMembershipsResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/UUID/GetAllUUIDMetadata.php b/src/PubNub/Endpoints/Objects/UUID/GetAllUUIDMetadata.php index a25f44f6..c09636c4 100644 --- a/src/PubNub/Endpoints/Objects/UUID/GetAllUUIDMetadata.php +++ b/src/PubNub/Endpoints/Objects/UUID/GetAllUUIDMetadata.php @@ -70,12 +70,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNGetAllUUIDMetadataResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNGetAllUUIDMetadataResult::fromPayload($json); + return PNGetAllUUIDMetadataResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/UUID/GetUUIDMetadata.php b/src/PubNub/Endpoints/Objects/UUID/GetUUIDMetadata.php index 6a890ac3..0c378bd3 100644 --- a/src/PubNub/Endpoints/Objects/UUID/GetUUIDMetadata.php +++ b/src/PubNub/Endpoints/Objects/UUID/GetUUIDMetadata.php @@ -61,12 +61,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNGetUUIDMetadataResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNGetUUIDMetadataResult::fromPayload($json); + return PNGetUUIDMetadataResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Objects/UUID/RemoveUUIDMetadata.php b/src/PubNub/Endpoints/Objects/UUID/RemoveUUIDMetadata.php index c830d4f8..54ba1bef 100644 --- a/src/PubNub/Endpoints/Objects/UUID/RemoveUUIDMetadata.php +++ b/src/PubNub/Endpoints/Objects/UUID/RemoveUUIDMetadata.php @@ -60,12 +60,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return bool */ - protected function createResponse($json) + protected function createResponse($result) { - return array_key_exists("data", $json); + return array_key_exists("data", $result); } /** diff --git a/src/PubNub/Endpoints/Objects/UUID/SetUUIDMetadata.php b/src/PubNub/Endpoints/Objects/UUID/SetUUIDMetadata.php index cbb022b9..6e138a9f 100644 --- a/src/PubNub/Endpoints/Objects/UUID/SetUUIDMetadata.php +++ b/src/PubNub/Endpoints/Objects/UUID/SetUUIDMetadata.php @@ -80,12 +80,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNSetUUIDMetadataResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNSetUUIDMetadataResult::fromPayload($json); + return PNSetUUIDMetadataResult::fromPayload($result); } /** diff --git a/src/PubNub/Endpoints/Presence/GetState.php b/src/PubNub/Endpoints/Presence/GetState.php index 14ea2182..15d69251 100644 --- a/src/PubNub/Endpoints/Presence/GetState.php +++ b/src/PubNub/Endpoints/Presence/GetState.php @@ -96,15 +96,15 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNGetStateResult */ - public function createResponse($json) + public function createResponse($result) { if (count($this->channels) === 1 && count($this->channelGroups) === 0) { - $channels = [$this->channels[0] => $json['payload']]; + $channels = [$this->channels[0] => $result['payload']]; } else { - $channels = $json['payload']['channels']; + $channels = $result['payload']['channels']; } return new PNGetStateResult($channels); diff --git a/src/PubNub/Endpoints/Presence/HereNow.php b/src/PubNub/Endpoints/Presence/HereNow.php index a5f9b699..d3d5f2fb 100644 --- a/src/PubNub/Endpoints/Presence/HereNow.php +++ b/src/PubNub/Endpoints/Presence/HereNow.php @@ -133,12 +133,12 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNHereNowResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNHereNowResult::fromJson($json, $this->channels); + return PNHereNowResult::fromJson($result, $this->channels); } /** diff --git a/src/PubNub/Endpoints/Presence/Leave.php b/src/PubNub/Endpoints/Presence/Leave.php index aa72fcf2..963e5462 100644 --- a/src/PubNub/Endpoints/Presence/Leave.php +++ b/src/PubNub/Endpoints/Presence/Leave.php @@ -87,12 +87,12 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return array */ - protected function createResponse($json) + protected function createResponse($result) { - return $json; + return $result; } /** diff --git a/src/PubNub/Endpoints/Presence/SetState.php b/src/PubNub/Endpoints/Presence/SetState.php index 0d590763..ede0e71a 100644 --- a/src/PubNub/Endpoints/Presence/SetState.php +++ b/src/PubNub/Endpoints/Presence/SetState.php @@ -127,15 +127,15 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNSetStateResult|array */ - public function createResponse($json) + public function createResponse($result) { - if (array_key_exists('status', $json) && $json['status'] === 200) { - return new PNSetStateResult($json['payload']); + if (array_key_exists('status', $result) && $result['status'] === 200) { + return new PNSetStateResult($result['payload']); } else { - return $json; + return $result; } } diff --git a/src/PubNub/Endpoints/Presence/WhereNow.php b/src/PubNub/Endpoints/Presence/WhereNow.php index e438607e..d1d2a51b 100644 --- a/src/PubNub/Endpoints/Presence/WhereNow.php +++ b/src/PubNub/Endpoints/Presence/WhereNow.php @@ -87,12 +87,12 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNWhereNowResult */ - protected function createResponse($json) + protected function createResponse($result) { - return PNWhereNowResult::fromPayload(static::fetchPayload($json)); + return PNWhereNowResult::fromPayload(static::fetchPayload($result)); } /** diff --git a/src/PubNub/Endpoints/PubSub/Publish.php b/src/PubNub/Endpoints/PubSub/Publish.php index fc205d8c..202f4d89 100755 --- a/src/PubNub/Endpoints/PubSub/Publish.php +++ b/src/PubNub/Endpoints/PubSub/Publish.php @@ -255,12 +255,12 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNPublishResult */ - protected function createResponse($json) + protected function createResponse($result) { - $timetoken = floatval($json[2]); + $timetoken = floatval($result[2]); return new PNPublishResult($timetoken); } diff --git a/src/PubNub/Endpoints/PubSub/Signal.php b/src/PubNub/Endpoints/PubSub/Signal.php index 3597e808..bc25e253 100644 --- a/src/PubNub/Endpoints/PubSub/Signal.php +++ b/src/PubNub/Endpoints/PubSub/Signal.php @@ -96,12 +96,12 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNPublishResult */ - protected function createResponse($json) + protected function createResponse($result) { - $timetoken = floatval($json[2]); + $timetoken = floatval($result[2]); return new PNSignalResult($timetoken); } diff --git a/src/PubNub/Endpoints/PubSub/Subscribe.php b/src/PubNub/Endpoints/PubSub/Subscribe.php index f9e17c4d..8c45176e 100644 --- a/src/PubNub/Endpoints/PubSub/Subscribe.php +++ b/src/PubNub/Endpoints/PubSub/Subscribe.php @@ -170,12 +170,12 @@ public function sync() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return SubscribeEnvelope */ - protected function createResponse($json) + protected function createResponse($result) { - return SubscribeEnvelope::fromJson($json); + return SubscribeEnvelope::fromJson($result); } /** diff --git a/src/PubNub/Endpoints/Push/AddChannelsToPush.php b/src/PubNub/Endpoints/Push/AddChannelsToPush.php index aac20698..aeb0f47b 100644 --- a/src/PubNub/Endpoints/Push/AddChannelsToPush.php +++ b/src/PubNub/Endpoints/Push/AddChannelsToPush.php @@ -86,10 +86,10 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNPushAddChannelResult */ - protected function createResponse($json) + protected function createResponse($result) { return new PNPushAddChannelResult(); } diff --git a/src/PubNub/Endpoints/Push/ListPushProvisions.php b/src/PubNub/Endpoints/Push/ListPushProvisions.php index 0c2cad8f..1a47fc2d 100644 --- a/src/PubNub/Endpoints/Push/ListPushProvisions.php +++ b/src/PubNub/Endpoints/Push/ListPushProvisions.php @@ -60,13 +60,13 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return mixed */ - protected function createResponse($json) + protected function createResponse($result) { - if ($json !== null || is_array($json)) { - return PNPushListProvisionsResult::fromJson($json); + if ($result !== null || is_array($result)) { + return PNPushListProvisionsResult::fromJson($result); } else { return new PNPushListProvisionsResult([]); } diff --git a/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php b/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php index 90646c44..30e93560 100644 --- a/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php +++ b/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php @@ -89,10 +89,10 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNPushRemoveChannelResult */ - protected function createResponse($json) + protected function createResponse($result) { return new PNPushRemoveChannelResult(); } diff --git a/src/PubNub/Endpoints/Push/RemoveDeviceFromPush.php b/src/PubNub/Endpoints/Push/RemoveDeviceFromPush.php index 90ff0978..2dd2632a 100644 --- a/src/PubNub/Endpoints/Push/RemoveDeviceFromPush.php +++ b/src/PubNub/Endpoints/Push/RemoveDeviceFromPush.php @@ -61,10 +61,10 @@ protected function buildPath() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return PNPushRemoveAllChannelsResult */ - protected function createResponse($json) + protected function createResponse($result) { return new PNPushRemoveAllChannelsResult(); } diff --git a/src/PubNub/Endpoints/Time.php b/src/PubNub/Endpoints/Time.php index cf777d00..f7d6b135 100755 --- a/src/PubNub/Endpoints/Time.php +++ b/src/PubNub/Endpoints/Time.php @@ -45,12 +45,12 @@ public function sync(): PNTimeResult } /** - * @param array $json + * @param array $result * @return PNTimeResult */ - protected function createResponse($json) + protected function createResponse($result) { - $timetoken = floatval($json[0]); + $timetoken = floatval($result[0]); $response = new PNTimeResult($timetoken); diff --git a/src/PubNub/Enums/PNOperationType.php b/src/PubNub/Enums/PNOperationType.php index 0365ea38..85428d5e 100755 --- a/src/PubNub/Enums/PNOperationType.php +++ b/src/PubNub/Enums/PNOperationType.php @@ -61,4 +61,12 @@ class PNOperationType // AccessManager v3 const PNAccessManagerGrantToken = 41; const PNAccessManagerRevokeToken = 42; + + const PNGetFilesAction = 46; + const PNDeleteFileOperation = 47; + const PNGetFileDownloadURLAction = 48; + const PNFetchFileUploadS3DataAction = 49; + const PNDownloadFileAction = 50; + const PNSendFileAction = 51; + const PNSendFileNotification = 52; } diff --git a/src/PubNub/Managers/BasePathManager.php b/src/PubNub/Managers/BasePathManager.php index f4641951..a3024282 100755 --- a/src/PubNub/Managers/BasePathManager.php +++ b/src/PubNub/Managers/BasePathManager.php @@ -2,16 +2,15 @@ namespace PubNub\Managers; - use PubNub\PNConfiguration; class BasePathManager { /** default subdomain used if cache busting is disabled. */ - const DEFAULT_SUBDOMAIN = "ps"; + protected const DEFAULT_SUBDOMAIN = "ps"; /** Default base path if a custom one is not provided.*/ - const DEFAULT_BASE_PATH = "pndsn.com"; + protected const DEFAULT_BASE_PATH = "pndsn.com"; /** @var PNConfiguration */ private $config; @@ -31,7 +30,7 @@ public function __construct($initialConfig) * * @return string */ - public function getBasePath() + public function getBasePath($customHost = null) { $constructedUrl = "http"; @@ -41,7 +40,9 @@ public function getBasePath() $constructedUrl .= "://"; - if ($this->config->getOrigin() != null) { + if ($customHost != null) { + $constructedUrl .= $customHost; + } elseif ($this->config->getOrigin() != null) { $constructedUrl .= $this->config->getOrigin(); } else { $constructedUrl .= static::DEFAULT_SUBDOMAIN . "." . static::DEFAULT_BASE_PATH; @@ -49,4 +50,4 @@ public function getBasePath() return $constructedUrl; } -} \ No newline at end of file +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNDeleteFileResult.php b/src/PubNub/Models/Consumer/FileSharing/PNDeleteFileResult.php new file mode 100644 index 00000000..0cfdb3d9 --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNDeleteFileResult.php @@ -0,0 +1,23 @@ +status = $json['status']; + } + + public function __toString() + { + return "File delete success with status: " . $this->status; + } + + public function getStatus() + { + return $this->status; + } +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNDownloadFileResult.php b/src/PubNub/Models/Consumer/FileSharing/PNDownloadFileResult.php new file mode 100644 index 00000000..728dcf22 --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNDownloadFileResult.php @@ -0,0 +1,23 @@ +fileContent = $result; + } + + public function __toString() + { + return "Download file success with file content: " . $this->fileContent; + } + + public function getFileContent() + { + return $this->fileContent; + } +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNFetchFileUploadS3DataResult.php b/src/PubNub/Models/Consumer/FileSharing/PNFetchFileUploadS3DataResult.php new file mode 100644 index 00000000..29481691 --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNFetchFileUploadS3DataResult.php @@ -0,0 +1,66 @@ +name = $json["data"]["name"]; + $this->fileId = $json["data"]["id"]; + $this->data = $json["file_upload_request"]; + $this->url = $json["file_upload_request"]["url"]; + $this->method = $json["file_upload_request"]["method"]; + $this->expirationDate = $json["file_upload_request"]["expiration_date"]; + $this->formFields = $json["file_upload_request"]["form_fields"]; + } + + public function __toString() + { + return sprintf("Fetch file upload S3 data success with id: %s", $this->fileId); + } + + public function getName() + { + return $this->name; + } + + public function getFileId() + { + return $this->fileId; + } + + public function getData() + { + return $this->data; + } + + public function getUrl() + { + return $this->url; + } + + public function getMethod() + { + return $this->method; + } + + public function getExpirationDate() + { + return $this->expirationDate; + } + + public function getFormFields() + { + return $this->formFields; + } +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNGetFileDownloadURLResult.php b/src/PubNub/Models/Consumer/FileSharing/PNGetFileDownloadURLResult.php new file mode 100644 index 00000000..e13fa413 --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNGetFileDownloadURLResult.php @@ -0,0 +1,23 @@ +fileUrl = $result->headers->getAll()['location'][0]; + } + + public function __toString() + { + return "Get file URL success with URL: %s" % $this->fileUrl; + } + + public function getFileUrl() + { + return $this->fileUrl; + } +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNGetFilesItem.php b/src/PubNub/Models/Consumer/FileSharing/PNGetFilesItem.php new file mode 100644 index 00000000..b91b3237 --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNGetFilesItem.php @@ -0,0 +1,50 @@ +id = $file['id']; + $this->name = $file['name']; + $this->size = $file['size']; + $this->creationTime = $file['created']; + } + + public function __toString() + { + return sprintf( + "File: %s with id: %s, size: %s, created at: %s", + $this->name, + $this->id, + $this->size, + $this->creationTime + ); + } + + public function getId() + { + return $this->id; + } + + public function getName() + { + return $this->name; + } + + public function getSize() + { + return $this->size; + } + + public function getCreationTime() + { + return $this->creationTime; + } +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNGetFilesResult.php b/src/PubNub/Models/Consumer/FileSharing/PNGetFilesResult.php new file mode 100644 index 00000000..50d7396a --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNGetFilesResult.php @@ -0,0 +1,57 @@ +data = $result['data']; + $this->count = (int)$result['count']; + $this->next = $result['next'] ?? null; + $this->prev = $result['prev'] ?? null; + if ($this->count === 0) { + $this->files = []; + } else { + foreach ($this->data as $file) { + $this->files[] = new PNGetFilesItem($file); + } + } + } + + public function __toString() + { + return "Get file success with data: " . $this->data; + } + + public function getData() + { + return $this->data; + } + + public function getCount() + { + return $this->count; + } + + public function getNext() + { + return $this->next; + } + + public function getPrev() + { + return $this->prev; + } + + public function getFiles() + { + return $this->files; + } +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNPublishFileMessageResult.php b/src/PubNub/Models/Consumer/FileSharing/PNPublishFileMessageResult.php new file mode 100644 index 00000000..6fbfc9ba --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNPublishFileMessageResult.php @@ -0,0 +1,23 @@ +timestamp = $json[2]; + } + + public function __toString() + { + return "Sending file notification success with timestamp: " . $this->timestamp; + } + + public function getTimestamp() + { + return $this->timestamp; + } +} diff --git a/src/PubNub/Models/Consumer/FileSharing/PNSendFileResult.php b/src/PubNub/Models/Consumer/FileSharing/PNSendFileResult.php new file mode 100644 index 00000000..adbcb2d6 --- /dev/null +++ b/src/PubNub/Models/Consumer/FileSharing/PNSendFileResult.php @@ -0,0 +1,30 @@ +name = $json["data"]["name"]; + $this->fileId = $json["data"]["file_id"]; + } + + public function __toString() + { + return sprintf("Send file success with id: %s", $this->fileId); + } + + public function getName() + { + return $this->name; + } + + public function getFileId() + { + return $this->fileId; + } +} diff --git a/src/PubNub/Models/Consumer/History/PNHistoryDeleteResult.php b/src/PubNub/Models/Consumer/History/PNHistoryDeleteResult.php index 64c24b17..a89d9cfb 100644 --- a/src/PubNub/Models/Consumer/History/PNHistoryDeleteResult.php +++ b/src/PubNub/Models/Consumer/History/PNHistoryDeleteResult.php @@ -5,4 +5,5 @@ class PNHistoryDeleteResult { + } \ No newline at end of file diff --git a/src/PubNub/PubNub.php b/src/PubNub/PubNub.php index 679cef65..2bdf12b2 100644 --- a/src/PubNub/PubNub.php +++ b/src/PubNub/PubNub.php @@ -52,10 +52,11 @@ use Psr\Log\LoggerInterface; use Psr\Log\LoggerAwareInterface; use Psr\Log\NullLogger; +use PubNub\Endpoints\FileSharing\{SendFile, DeleteFile, DownloadFile, GetFileDownloadUrl, ListFiles}; class PubNub implements LoggerAwareInterface { - protected const SDK_VERSION = "6.2.1"; + protected const SDK_VERSION = "6.3.0"; protected const SDK_NAME = "PubNub-PHP"; public static $MAX_SEQUENCE = 65535; @@ -471,9 +472,9 @@ public function getConfiguration() /** * @return string Base path */ - public function getBasePath() + public function getBasePath($customHost = null) { - return $this->basePathManager->getBasePath(); + return $this->basePathManager->getBasePath($customHost); } /** @@ -571,6 +572,20 @@ public function getCrypto(): CryptoModule | null } } + public function getCryptoSafe(): CryptoModule|null + { + if ($this->cryptoModule) { + return $this->cryptoModule; + } else { + return $this->configuration->getCryptoSafe(); + } + } + + public function isCryptoEnabled(): bool + { + return !empty($this->cryptoModule) || !empty($this->configuration->getCryptoSafe()); + } + public function setCrypto(CryptoModule $cryptoModule) { $this->cryptoModule = $cryptoModule; @@ -580,4 +595,29 @@ public function fetchMessages(): FetchMessages { return new FetchMessages($this); } + + public function sendFile() + { + return new SendFile($this); + } + + public function deleteFile() + { + return new DeleteFile($this); + } + + public function downloadFile() + { + return new DownloadFile($this); + } + + public function listFiles() + { + return new ListFiles($this); + } + + public function getFileDownloadUrl() + { + return new GetFileDownloadUrl($this); + } } diff --git a/src/PubNub/PubNubUtil.php b/src/PubNub/PubNubUtil.php index 493fb644..ce433b67 100755 --- a/src/PubNub/PubNubUtil.php +++ b/src/PubNub/PubNubUtil.php @@ -4,7 +4,6 @@ use PubNub\Exceptions\PubNubBuildRequestException; - class PubNubUtil { /** diff --git a/tests/functional/EndpointTest.php b/tests/functional/EndpointTest.php index d9e6f066..d99c07a6 100644 --- a/tests/functional/EndpointTest.php +++ b/tests/functional/EndpointTest.php @@ -86,10 +86,10 @@ public function validateParams() } /** - * @param array $json Decoded json + * @param array $result Decoded json * @return mixed */ - protected function createResponse($json) + protected function createResponse($result) { return null; } diff --git a/tests/functional/objects/channel/GetAllChannelMetadataTest.php b/tests/functional/objects/channel/GetAllChannelMetadataTest.php index 01be9474..0965ec8d 100644 --- a/tests/functional/objects/channel/GetAllChannelMetadataTest.php +++ b/tests/functional/objects/channel/GetAllChannelMetadataTest.php @@ -76,15 +76,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/functional/objects/channel/GetChannelMetadataTest.php b/tests/functional/objects/channel/GetChannelMetadataTest.php index e840e805..3e2ddac4 100644 --- a/tests/functional/objects/channel/GetChannelMetadataTest.php +++ b/tests/functional/objects/channel/GetChannelMetadataTest.php @@ -66,15 +66,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/functional/objects/channel/RemoveChannelMetadataTest.php b/tests/functional/objects/channel/RemoveChannelMetadataTest.php index 3dfbe9e5..cf3e7d3a 100644 --- a/tests/functional/objects/channel/RemoveChannelMetadataTest.php +++ b/tests/functional/objects/channel/RemoveChannelMetadataTest.php @@ -45,15 +45,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/functional/objects/member/GetMembersTest.php b/tests/functional/objects/member/GetMembersTest.php index e6c780e5..f296d15a 100644 --- a/tests/functional/objects/member/GetMembersTest.php +++ b/tests/functional/objects/member/GetMembersTest.php @@ -97,15 +97,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/functional/objects/membership/GetMembershipsTest.php b/tests/functional/objects/membership/GetMembershipsTest.php index ba1c8141..3d42a8b0 100644 --- a/tests/functional/objects/membership/GetMembershipsTest.php +++ b/tests/functional/objects/membership/GetMembershipsTest.php @@ -93,15 +93,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/functional/objects/uuid/GetAllUUIDMetadataTest.php b/tests/functional/objects/uuid/GetAllUUIDMetadataTest.php index 4a8b0e3d..a9571046 100644 --- a/tests/functional/objects/uuid/GetAllUUIDMetadataTest.php +++ b/tests/functional/objects/uuid/GetAllUUIDMetadataTest.php @@ -80,15 +80,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/functional/objects/uuid/GetUUIDMetadataTest.php b/tests/functional/objects/uuid/GetUUIDMetadataTest.php index 6442d848..6ae09fd1 100644 --- a/tests/functional/objects/uuid/GetUUIDMetadataTest.php +++ b/tests/functional/objects/uuid/GetUUIDMetadataTest.php @@ -70,15 +70,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/functional/objects/uuid/RemoveUUIDMetadataTest.php b/tests/functional/objects/uuid/RemoveUUIDMetadataTest.php index 7d806fdc..37755e56 100644 --- a/tests/functional/objects/uuid/RemoveUUIDMetadataTest.php +++ b/tests/functional/objects/uuid/RemoveUUIDMetadataTest.php @@ -45,15 +45,15 @@ public function buildParams() { return parent::buildParams(); } - + public function buildPath() { return parent::buildPath(); } - public function createResponse($json) + public function createResponse($result) { - return parent::createResponse($json); + return parent::createResponse($result); } } diff --git a/tests/helpers/Stub.php b/tests/helpers/Stub.php index 31dad6b2..79cd08ec 100644 --- a/tests/helpers/Stub.php +++ b/tests/helpers/Stub.php @@ -117,8 +117,8 @@ public function withQuery($query) private function stripKeys($path) { - $patterns = ['/sub-key\/(demo|sub-c-[a-z0-9-]{36})\//']; - $replaces = ["sub-key/{SUB_KEY}/"]; + $patterns = ['#\/(demo|sub-c-[a-z0-9-]{36})\/#']; + $replaces = ["/{SUB_KEY}/"]; return preg_replace($patterns, $replaces, $path); } diff --git a/tests/helpers/StubTransport.php b/tests/helpers/StubTransport.php index ae79d11f..3de975f0 100644 --- a/tests/helpers/StubTransport.php +++ b/tests/helpers/StubTransport.php @@ -63,6 +63,7 @@ public function stubFor($path) return $stub; } + // phpcs:ignore PSR1.Methods.CamelCapsMethodName public function request_multiple($requests, $options) { throw new \Exception("Not implemented"); diff --git a/tests/integrational/FilesTest.php b/tests/integrational/FilesTest.php new file mode 100644 index 00000000..90366d4d --- /dev/null +++ b/tests/integrational/FilesTest.php @@ -0,0 +1,105 @@ +pubnub->listFiles()->channel($this->channel)->sync(); + $this->assertNotEmpty($response); + $this->assertCount(0, $response->getData()); + } + + public function testSendTextFile() + { + $file = fopen($this->textFilePath, "r"); + + $response = $this->pubnub->sendFile() + ->channel($this->channel) + ->fileHandle($file) + ->fileName(basename($this->textFilePath)) + ->message("This is the requested text file") + ->sync(); + + $this->assertNotEmpty($response); + $this->assertNotEmpty($response->getTimestamp()); + } + + public function testSendBinaryFile() + { + $file = fopen($this->binaryFilePath, "r"); + + $response = $this->pubnub->sendFile() + ->channel($this->channel) + ->fileHandle($file) + ->fileName(basename($this->binaryFilePath)) + ->message("This is the requested binary file") + ->sync(); + + $this->assertNotEmpty($response); + $this->assertNotEmpty($response->getTimestamp()); + } + + public function testNonEmptyFileList() + { + $response = $this->pubnub->listFiles()->channel($this->channel)->sync(); + $this->assertNotEmpty($response); + $this->assertCount(2, $response->getData()); + $this->textFileId = $response->getData()[0]['id']; + } + + public function testGetDownloadUrls() + { + $listFilesResponse = $this->pubnub->listFiles()->channel($this->channel)->sync(); + foreach ($listFilesResponse->getData() as $file) { + $response = $this->pubnub->getFileDownloadUrl() + ->channel($this->channel) + ->fileId($file['id']) + ->fileName($file['name']) + ->sync(); + $this->assertNotEmpty($response); + $this->assertNotEmpty($response->getFileUrl()); + } + } + + public function testDownloadFiles() + { + $listFilesResponse = $this->pubnub->listFiles()->channel($this->channel)->sync(); + foreach ($listFilesResponse->getData() as $file) { + $response = $this->pubnub->downloadFile() + ->channel($this->channel) + ->fileId($file['id']) + ->fileName($file['name']) + ->sync(); + $this->assertNotEmpty($response); + if ($file['name'] == basename($this->binaryFilePath)) { + $this->assertEquals(file_get_contents($this->binaryFilePath), $response->getFileContent()); + } else { + $this->assertEquals(file_get_contents($this->textFilePath), $response->getFileContent()); + } + } + } + + public function testDeleteAllFiles() + { + $listFilesResponse = $this->pubnub->listFiles()->channel($this->channel)->sync(); + foreach ($listFilesResponse->getData() as $file) { + $response = $this->pubnub->deleteFile() + ->channel($this->channel) + ->fileId($file['id']) + ->fileName($file['name']) + ->sync(); + $this->assertNotEmpty($response); + $this->assertEquals(200, $response->getStatus(), "Unexpected status value"); + } + } +} diff --git a/tests/integrational/assets/pn.gif b/tests/integrational/assets/pn.gif new file mode 100644 index 00000000..e7e4e489 Binary files /dev/null and b/tests/integrational/assets/pn.gif differ diff --git a/tests/integrational/assets/spam.spam b/tests/integrational/assets/spam.spam new file mode 100644 index 00000000..fb7ea357 --- /dev/null +++ b/tests/integrational/assets/spam.spam @@ -0,0 +1,10 @@ +Egg and bacon +Egg, sausage and bacon +Egg and spam +Egg, bacon and spam +Egg, bacon, sausage and spam +Spam, bacon, sausage and spam +Spam, egg, spam, spam, bacon and spam +Spam, spam, spam, egg and spam +Spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam +Lobster thermidor aux crevettes with a mornay sauce garnished with truffle paté, brandy and a fried egg on top and spam \ No newline at end of file