note 60896 deleted from function.unserialize by aharvey

From: Date: Tue, 06 Nov 2012 01:25:54 +0000
Subject: note 60896 deleted from function.unserialize by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-191697@lists.php.net to get a copy of this message
Note Submitter: mina86 at projektcode dot org ---- Accualy artistan's function does not work (there should be $serialized_data instead of $text in function body). Moreover, you must be *very* careful when using eval() as it can be easily used to do something nasty. Here comes what I've made basing on the two available versions: <?php function unserialize_data($str, $objs = true) { if ($objs && preg_match_all('/O:\d+:"([a-zA-Z0-9_]+)":/', $str, $arr)) { $callback = ini_get('unserialize_callback_func'); foreach ($arr[1] as $cls) { if (!class_exists($cls)) { call_user_func($callback, $cls); } if (!class_exists($cls)) { eval('class ' . $cls . ' { }'); } } } return unserialize($str); } ?> However, better (not tested though) sollution seems to be: <?php function declare_empty_class($classname) { static $callback = null; if ($callback===null) { $callback = $classname; return; } if ($callback) { call_user_func($callback, $classname); } if (!class_exists($classname)) { eval('class ' . $classname . ' { }'); } } declare_empty_class(ini_get('unserialize_callback_func')); ini_set('unserialize_callback_func', 'declare_empty_class'); ?>

« previous php.notes (#191697) next »