diff --git a/api.include.php b/api.include.php index 1ff2d1df..b7f17d5d 100644 --- a/api.include.php +++ b/api.include.php @@ -1515,18 +1515,16 @@ public function createStream(string $content = ''): StreamInterface public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface { - try { - $resource = @\fopen($filename, $mode); - } catch (\Throwable $e) { - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename)); + if ('' === $filename) { + throw new \RuntimeException('Path cannot be empty'); } - if (false === $resource) { + if (false === $resource = @\fopen($filename, $mode)) { if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode)); } - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename)); + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? '')); } return Stream::create($resource); @@ -2143,6 +2141,9 @@ public function getAttributes(): array return $this->attributes; } + /** + * @return mixed + */ public function getAttribute($attribute, $default = null) { if (false === \array_key_exists($attribute, $this->attributes)) { @@ -2358,8 +2359,12 @@ public function getSize() /*:?int*/ public function tell(): int { - if (false === $result = \ftell($this->stream)) { - throw new \RuntimeException('Unable to determine stream position'); + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + + if (false === $result = @\ftell($this->stream)) { + throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -2367,7 +2372,7 @@ public function tell(): int public function eof(): bool { - return !$this->stream || \feof($this->stream); + return !isset($this->stream) || \feof($this->stream); } public function isSeekable(): bool @@ -2377,6 +2382,10 @@ public function isSeekable(): bool public function seek($offset, $whence = \SEEK_SET) /*:void*/ { + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + if (!$this->seekable) { throw new \RuntimeException('Stream is not seekable'); } @@ -2398,6 +2407,10 @@ public function isWritable(): bool public function write($string): int { + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + if (!$this->writable) { throw new \RuntimeException('Cannot write to a non-writable stream'); } @@ -2405,8 +2418,8 @@ public function write($string): int // We can't know the size after writing anything $this->size = null; - if (false === $result = \fwrite($this->stream, $string)) { - throw new \RuntimeException('Unable to write to stream'); + if (false === $result = @\fwrite($this->stream, $string)) { + throw new \RuntimeException('Unable to write to stream: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -2419,12 +2432,16 @@ public function isReadable(): bool public function read($length): string { + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + if (!$this->readable) { throw new \RuntimeException('Cannot read from non-readable stream'); } - if (false === $result = \fread($this->stream, $length)) { - throw new \RuntimeException('Unable to read from stream'); + if (false === $result = @\fread($this->stream, $length)) { + throw new \RuntimeException('Unable to read from stream: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -2433,16 +2450,19 @@ public function read($length): string public function getContents(): string { if (!isset($this->stream)) { - throw new \RuntimeException('Unable to read stream contents'); + throw new \RuntimeException('Stream is detached'); } - if (false === $contents = \stream_get_contents($this->stream)) { - throw new \RuntimeException('Unable to read stream contents'); + if (false === $contents = @\stream_get_contents($this->stream)) { + throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? '')); } return $contents; } + /** + * @return mixed + */ public function getMetadata($key = null) { if (!isset($this->stream)) { @@ -2539,7 +2559,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename if (\UPLOAD_ERR_OK === $this->error) { // Depending on the value set file or stream variable. - if (\is_string($streamOrFile)) { + if (\is_string($streamOrFile) && '' !== $streamOrFile) { $this->file = $streamOrFile; } elseif (\is_resource($streamOrFile)) { $this->stream = Stream::create($streamOrFile); @@ -2573,11 +2593,11 @@ public function getStream(): StreamInterface return $this->stream; } - try { - return Stream::create(\fopen($this->file, 'r')); - } catch (\Throwable $e) { - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file)); + if (false === $resource = @\fopen($this->file, 'r')) { + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? '')); } + + return Stream::create($resource); } public function moveTo($targetPath) /*:void*/ @@ -2589,20 +2609,23 @@ public function moveTo($targetPath) /*:void*/ } if (null !== $this->file) { - $this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath); + $this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath); + + if (false === $this->moved) { + throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? '')); + } } else { $stream = $this->getStream(); if ($stream->isSeekable()) { $stream->rewind(); } - try { - // Copy the contents of a stream into another stream until end-of-file. - $dest = Stream::create(\fopen($targetPath, 'w')); - } catch (\Throwable $e) { - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath)); + if (false === $resource = @\fopen($targetPath, 'w')) { + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? '')); } + $dest = Stream::create($resource); + while (!$stream->eof()) { if (!$dest->write($stream->read(1048576))) { break; @@ -2611,10 +2634,6 @@ public function moveTo($targetPath) /*:void*/ $this->moved = true; } - - if (false === $this->moved) { - throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath)); - } } public function getSize(): int @@ -7876,9 +7895,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $method = $request->getMethod(); if ($method == 'POST' && in_array($path, ['login', 'register', 'password'])) { $body = $request->getParsedBody(); - $username = isset($body->username) ? $body->username : ''; - $password = isset($body->password) ? $body->password : ''; - $newPassword = isset($body->newPassword) ? $body->newPassword : ''; + $usernameFormFieldName = $this->getProperty('usernameFormField', 'username'); + $passwordFormFieldName = $this->getProperty('passwordFormField', 'username'); + $newPasswordFormFieldName = $this->getProperty('newPasswordFormField', 'username'); + $username = isset($body->usernameFormFieldName) ? $body->usernameFormFieldName : ''; + $password = isset($body->passwordFormFieldName) ? $body->passwordFormFieldName : ''; + $newPassword = isset($body->newPasswordFormFieldName) ? $body->newPasswordFormFieldName : ''; $tableName = $this->getProperty('usersTable', 'users'); $table = $this->reflection->getTable($tableName); $usernameColumnName = $this->getProperty('usernameColumn', 'username'); diff --git a/api.php b/api.php index 441fc657..01188b4f 100644 --- a/api.php +++ b/api.php @@ -1515,18 +1515,16 @@ public function createStream(string $content = ''): StreamInterface public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface { - try { - $resource = @\fopen($filename, $mode); - } catch (\Throwable $e) { - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename)); + if ('' === $filename) { + throw new \RuntimeException('Path cannot be empty'); } - if (false === $resource) { + if (false === $resource = @\fopen($filename, $mode)) { if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode)); } - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename)); + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? '')); } return Stream::create($resource); @@ -2143,6 +2141,9 @@ public function getAttributes(): array return $this->attributes; } + /** + * @return mixed + */ public function getAttribute($attribute, $default = null) { if (false === \array_key_exists($attribute, $this->attributes)) { @@ -2358,8 +2359,12 @@ public function getSize() /*:?int*/ public function tell(): int { - if (false === $result = \ftell($this->stream)) { - throw new \RuntimeException('Unable to determine stream position'); + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + + if (false === $result = @\ftell($this->stream)) { + throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -2367,7 +2372,7 @@ public function tell(): int public function eof(): bool { - return !$this->stream || \feof($this->stream); + return !isset($this->stream) || \feof($this->stream); } public function isSeekable(): bool @@ -2377,6 +2382,10 @@ public function isSeekable(): bool public function seek($offset, $whence = \SEEK_SET) /*:void*/ { + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + if (!$this->seekable) { throw new \RuntimeException('Stream is not seekable'); } @@ -2398,6 +2407,10 @@ public function isWritable(): bool public function write($string): int { + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + if (!$this->writable) { throw new \RuntimeException('Cannot write to a non-writable stream'); } @@ -2405,8 +2418,8 @@ public function write($string): int // We can't know the size after writing anything $this->size = null; - if (false === $result = \fwrite($this->stream, $string)) { - throw new \RuntimeException('Unable to write to stream'); + if (false === $result = @\fwrite($this->stream, $string)) { + throw new \RuntimeException('Unable to write to stream: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -2419,12 +2432,16 @@ public function isReadable(): bool public function read($length): string { + if (!isset($this->stream)) { + throw new \RuntimeException('Stream is detached'); + } + if (!$this->readable) { throw new \RuntimeException('Cannot read from non-readable stream'); } - if (false === $result = \fread($this->stream, $length)) { - throw new \RuntimeException('Unable to read from stream'); + if (false === $result = @\fread($this->stream, $length)) { + throw new \RuntimeException('Unable to read from stream: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -2433,16 +2450,19 @@ public function read($length): string public function getContents(): string { if (!isset($this->stream)) { - throw new \RuntimeException('Unable to read stream contents'); + throw new \RuntimeException('Stream is detached'); } - if (false === $contents = \stream_get_contents($this->stream)) { - throw new \RuntimeException('Unable to read stream contents'); + if (false === $contents = @\stream_get_contents($this->stream)) { + throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? '')); } return $contents; } + /** + * @return mixed + */ public function getMetadata($key = null) { if (!isset($this->stream)) { @@ -2539,7 +2559,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename if (\UPLOAD_ERR_OK === $this->error) { // Depending on the value set file or stream variable. - if (\is_string($streamOrFile)) { + if (\is_string($streamOrFile) && '' !== $streamOrFile) { $this->file = $streamOrFile; } elseif (\is_resource($streamOrFile)) { $this->stream = Stream::create($streamOrFile); @@ -2573,11 +2593,11 @@ public function getStream(): StreamInterface return $this->stream; } - try { - return Stream::create(\fopen($this->file, 'r')); - } catch (\Throwable $e) { - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file)); + if (false === $resource = @\fopen($this->file, 'r')) { + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? '')); } + + return Stream::create($resource); } public function moveTo($targetPath) /*:void*/ @@ -2589,20 +2609,23 @@ public function moveTo($targetPath) /*:void*/ } if (null !== $this->file) { - $this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath); + $this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath); + + if (false === $this->moved) { + throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? '')); + } } else { $stream = $this->getStream(); if ($stream->isSeekable()) { $stream->rewind(); } - try { - // Copy the contents of a stream into another stream until end-of-file. - $dest = Stream::create(\fopen($targetPath, 'w')); - } catch (\Throwable $e) { - throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath)); + if (false === $resource = @\fopen($targetPath, 'w')) { + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? '')); } + $dest = Stream::create($resource); + while (!$stream->eof()) { if (!$dest->write($stream->read(1048576))) { break; @@ -2611,10 +2634,6 @@ public function moveTo($targetPath) /*:void*/ $this->moved = true; } - - if (false === $this->moved) { - throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath)); - } } public function getSize(): int @@ -7876,9 +7895,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $method = $request->getMethod(); if ($method == 'POST' && in_array($path, ['login', 'register', 'password'])) { $body = $request->getParsedBody(); - $username = isset($body->username) ? $body->username : ''; - $password = isset($body->password) ? $body->password : ''; - $newPassword = isset($body->newPassword) ? $body->newPassword : ''; + $usernameFormFieldName = $this->getProperty('usernameFormField', 'username'); + $passwordFormFieldName = $this->getProperty('passwordFormField', 'username'); + $newPasswordFormFieldName = $this->getProperty('newPasswordFormField', 'username'); + $username = isset($body->usernameFormFieldName) ? $body->usernameFormFieldName : ''; + $password = isset($body->passwordFormFieldName) ? $body->passwordFormFieldName : ''; + $newPassword = isset($body->newPasswordFormFieldName) ? $body->newPasswordFormFieldName : ''; $tableName = $this->getProperty('usersTable', 'users'); $table = $this->reflection->getTable($tableName); $usernameColumnName = $this->getProperty('usernameColumn', 'username'); diff --git a/composer.lock b/composer.lock index ec2bb730..a7d34e9b 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "nyholm/psr7", - "version": "1.4.1", + "version": "1.5.0", "source": { "type": "git", "url": "https://github.com/Nyholm/psr7.git", - "reference": "2212385b47153ea71b1c1b1374f8cb5e4f7892ec" + "reference": "1461e07a0f2a975a52082ca3b769ca912b816226" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Nyholm/psr7/zipball/2212385b47153ea71b1c1b1374f8cb5e4f7892ec", - "reference": "2212385b47153ea71b1c1b1374f8cb5e4f7892ec", + "url": "https://api.github.com/repos/Nyholm/psr7/zipball/1461e07a0f2a975a52082ca3b769ca912b816226", + "reference": "1461e07a0f2a975a52082ca3b769ca912b816226", "shasum": "" }, "require": { @@ -69,7 +69,7 @@ ], "support": { "issues": "https://github.com/Nyholm/psr7/issues", - "source": "https://github.com/Nyholm/psr7/tree/1.4.1" + "source": "https://github.com/Nyholm/psr7/tree/1.5.0" }, "funding": [ { @@ -81,7 +81,7 @@ "type": "github" } ], - "time": "2021-07-02T08:32:20+00:00" + "time": "2022-02-02T18:37:57+00:00" }, { "name": "nyholm/psr7-server", @@ -439,5 +439,5 @@ "ext-pdo": "*" }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.2.0" }