Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generated/8.1/functionsList.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.1/rector-migrate.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.2/functionsList.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.2/rector-migrate.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.3/rector-migrate.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.4/functionsList.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.4/rector-migrate.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.5/functionsList.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.5/rector-migrate.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.6/functionsList.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generated/8.6/rector-migrate.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions lib/special_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,64 @@ function ftp_raw(\FTP\Connection $ftp, string $command): array
}
return $safeResult;
}

/**
* Creates a PHP value from a stored representation
*
* @param string $data <p>
* The serialized string.
*
* If the variable being unserialized is an object, after successfully
* reconstructing the object PHP will automatically attempt to call the
* __wakeup member function (if it exists).
*
* unserialize_callback_func directive
*
* It's possible to set a callback-function which will be called,
* if an undefined class should be instantiated during unserializing.
* (to prevent getting an incomplete object "__PHP_Incomplete_Class".)
* Use your "php.ini", ini_set or ".htaccess"
* to define 'unserialize_callback_func'. Everytime an undefined class
* should be instantiated, it'll be called. To disable this feature just
* empty this setting.
*
* @param mixed[] $options [optional]
* Any options to be provided to unserialize(), as an associative array.
*
* The 'allowed_classes' option key may be set to a value that is
* either an array of class names which should be accepted, FALSE to
* accept no classes, or TRUE to accept all classes. If this option is defined
* and unserialize() encounters an object of a class that isn't to be accepted,
* then the object will be instantiated as __PHP_Incomplete_Class instead.
* Omitting this option is the same as defining it as TRUE: PHP will attempt
* to instantiate objects of any class.
*
* @return mixed The converted value is returned, and can be a boolean,
* integer, float, string, array or object.
*
* In case the passed value is not unserializeable, an \ErrorException will
* be thrown.
*/
function unserialize(string $data, array $options = []): mixed
{
error_clear_last();

$previous = set_error_handler(function ($severity, $message, $file, $line) use (&$previous) {
$unserialize_error_msg_prefix = 'unserialize():';
if (str_starts_with($message, $unserialize_error_msg_prefix)) {
throw new \ErrorException($message, 0, $severity, $file, $line);
}

if (!$previous) {
return false;
}

return $previous($severity, $message, $file, $line);
});

try {
return \unserialize($data, $options);
} finally {
restore_error_handler();
}
}
17 changes: 17 additions & 0 deletions tests/SpecialCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ public function testFgetcsvReturnFalseonEndOfFile(): void
\fclose($handle);
}

public function testUnserialize(): void
{
$data = new stdClass();
$serialized_data = \serialize($data);
$this->assertEquals($data, \Safe\unserialize($serialized_data));

$serialized_data = \serialize(false);
$this->assertFalse(\Safe\unserialize($serialized_data));
}

public function testUnserializeOnInvalidData(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('unserialize(): Error at offset 0 of 8 bytes');
\Safe\unserialize('invalid{');
}

/*public function testFgetcsvThrowsOnError()
{
if (($handle = \fopen(__DIR__."/csv/test3.csv", "r")) === false) {
Expand Down