Skip to content

Commit 055564b

Browse files
committed
Add safe wrapper fo unserialize
1 parent 44aa2a6 commit 055564b

File tree

11 files changed

+87
-0
lines changed

11 files changed

+87
-0
lines changed

generated/8.1/functionsList.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.1/rector-migrate.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.2/functionsList.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.2/rector-migrate.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.3/rector-migrate.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.4/functionsList.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.4/rector-migrate.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.5/functionsList.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.5/rector-migrate.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/special_cases.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,64 @@ function ftp_raw(\FTP\Connection $ftp, string $command): array
514514
}
515515
return $safeResult;
516516
}
517+
518+
/**
519+
* Creates a PHP value from a stored representation
520+
*
521+
* @param string $data <p>
522+
* The serialized string.
523+
*
524+
* If the variable being unserialized is an object, after successfully
525+
* reconstructing the object PHP will automatically attempt to call the
526+
* __wakeup member function (if it exists).
527+
*
528+
* unserialize_callback_func directive
529+
*
530+
* It's possible to set a callback-function which will be called,
531+
* if an undefined class should be instantiated during unserializing.
532+
* (to prevent getting an incomplete object "__PHP_Incomplete_Class".)
533+
* Use your "php.ini", ini_set or ".htaccess"
534+
* to define 'unserialize_callback_func'. Everytime an undefined class
535+
* should be instantiated, it'll be called. To disable this feature just
536+
* empty this setting.
537+
*
538+
* @param mixed[] $options [optional]
539+
* Any options to be provided to unserialize(), as an associative array.
540+
*
541+
* The 'allowed_classes' option key may be set to a value that is
542+
* either an array of class names which should be accepted, FALSE to
543+
* accept no classes, or TRUE to accept all classes. If this option is defined
544+
* and unserialize() encounters an object of a class that isn't to be accepted,
545+
* then the object will be instantiated as __PHP_Incomplete_Class instead.
546+
* Omitting this option is the same as defining it as TRUE: PHP will attempt
547+
* to instantiate objects of any class.
548+
*
549+
* @return mixed The converted value is returned, and can be a boolean,
550+
* integer, float, string, array or object.
551+
*
552+
* In case the passed value is not unserializeable, an \ErrorException will
553+
* be thrown.
554+
*/
555+
function unserialize(string $data, array $options = []): mixed
556+
{
557+
error_clear_last();
558+
559+
$previous = set_error_handler(function ($severity, $message, $file, $line) use (&$previous) {
560+
$unserialize_error_msg_prefix = 'unserialize():';
561+
if (str_starts_with($message, $unserialize_error_msg_prefix)) {
562+
throw new \ErrorException($message, 0, $severity, $file, $line);
563+
}
564+
565+
if (!$previous) {
566+
return false;
567+
}
568+
569+
return $previous($severity, $message, $file, $line);
570+
});
571+
572+
try {
573+
return \unserialize($data, $options);
574+
} finally {
575+
restore_error_handler();
576+
}
577+
}

0 commit comments

Comments
 (0)