Allow deserialization from relative paths in phar

This commit is contained in:
Daniil Gentili 2018-02-25 16:49:40 +00:00
parent 79618223b7
commit 633aee1019
2 changed files with 14 additions and 42 deletions

View File

@ -10,7 +10,6 @@ See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto. You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace danog\MadelineProto; namespace danog\MadelineProto;
class API extends APIFactory class API extends APIFactory
@ -18,15 +17,14 @@ class API extends APIFactory
use \danog\Serializable; use \danog\Serializable;
public $session; public $session;
public $serialized = 0; public $serialized = 0;
public function __magic_construct($params = []) public function __magic_construct($params = [])
{ {
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
set_exception_handler(['\\danog\\MadelineProto\\Serialization', 'serialize_all']); set_exception_handler(['\\danog\\MadelineProto\\Serialization', 'serialize_all']);
if (is_string($params)) { if (is_string($params)) {
if (file_exists($params)) {
$this->session = $params;
$realpaths = Serialization::realpaths($params); $realpaths = Serialization::realpaths($params);
if (file_exists($realpaths['file'])) {
$this->session = $realpaths['file'];
if (!file_exists($realpaths['lockfile'])) { if (!file_exists($realpaths['lockfile'])) {
touch($realpaths['lockfile']); touch($realpaths['lockfile']);
clearstatcache(); clearstatcache();
@ -34,7 +32,6 @@ class API extends APIFactory
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r'); $realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r');
\danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']); \danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']);
flock($realpaths['lockfile'], LOCK_SH); flock($realpaths['lockfile'], LOCK_SH);
try { try {
$unserialized = file_get_contents($realpaths['file']); $unserialized = file_get_contents($realpaths['file']);
} finally { } finally {
@ -43,11 +40,10 @@ class API extends APIFactory
} }
$tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized); $tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized);
foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) { foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) {
class_exists('\\danog\\MadelineProto\\'.$class); class_exists('\\danog\\MadelineProto\\' . $class);
} }
class_exists('\\Volatile'); class_exists('\\Volatile');
\danog\MadelineProto\Logger::class_exists(); \danog\MadelineProto\Logger::class_exists();
try { try {
$unserialized = unserialize($tounserialize); $unserialized = unserialize($tounserialize);
} catch (\danog\MadelineProto\Bug74586Exception $e) { } catch (\danog\MadelineProto\Bug74586Exception $e) {
@ -72,7 +68,6 @@ class API extends APIFactory
$this->API = $unserialized->API; $this->API = $unserialized->API;
$this->APIFactory(); $this->APIFactory();
} }
return; return;
} }
$this->API = new MTProto($params); $this->API = new MTProto($params);
@ -80,18 +75,16 @@ class API extends APIFactory
$this->APIFactory(); $this->APIFactory();
\danog\MadelineProto\Logger::log(['Ping...'], Logger::ULTRA_VERBOSE); \danog\MadelineProto\Logger::log(['Ping...'], Logger::ULTRA_VERBOSE);
$pong = $this->ping(['ping_id' => 3]); $pong = $this->ping(['ping_id' => 3]);
\danog\MadelineProto\Logger::log(['Pong: '.$pong['ping_id']], Logger::ULTRA_VERBOSE); \danog\MadelineProto\Logger::log(['Pong: ' . $pong['ping_id']], Logger::ULTRA_VERBOSE);
//\danog\MadelineProto\Logger::log(['Getting future salts...'], Logger::ULTRA_VERBOSE); //\danog\MadelineProto\Logger::log(['Getting future salts...'], Logger::ULTRA_VERBOSE);
//$this->future_salts = $this->get_future_salts(['num' => 3]); //$this->future_salts = $this->get_future_salts(['num' => 3]);
\danog\MadelineProto\Logger::log([\danog\MadelineProto\Lang::$current_lang['madelineproto_ready']], Logger::NOTICE); \danog\MadelineProto\Logger::log([\danog\MadelineProto\Lang::$current_lang['madelineproto_ready']], Logger::NOTICE);
} }
public function __wakeup() public function __wakeup()
{ {
//if (method_exists($this->API, 'wakeup')) $this->API = $this->API->wakeup(); //if (method_exists($this->API, 'wakeup')) $this->API = $this->API->wakeup();
$this->APIFactory(); $this->APIFactory();
} }
public function __destruct() public function __destruct()
{ {
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) { if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) {
@ -102,56 +95,45 @@ class API extends APIFactory
} }
restore_error_handler(); restore_error_handler();
} }
public function __sleep() public function __sleep()
{ {
return ['API']; return ['API'];
} }
public function &__get($name) public function &__get($name)
{ {
if ($name === 'settings') { if ($name === 'settings') {
$this->API->setdem = true; $this->API->setdem = true;
return $this->API->settings; return $this->API->settings;
} }
return $this->API->storage[$name]; return $this->API->storage[$name];
} }
public function __set($name, $value) public function __set($name, $value)
{ {
if ($name === 'settings') { if ($name === 'settings') {
return $this->API->__construct($value); return $this->API->__construct($value);
} }
return $this->API->storage[$name] = $value; return $this->API->storage[$name] = $value;
} }
public function __isset($name) public function __isset($name)
{ {
return isset($this->API->storage[$name]); return isset($this->API->storage[$name]);
} }
public function __unset($name) public function __unset($name)
{ {
unset($this->API->storage[$name]); unset($this->API->storage[$name]);
} }
public function APIFactory() public function APIFactory()
{ {
foreach ($this->API->get_method_namespaces() as $namespace) { foreach ($this->API->get_method_namespaces() as $namespace) {
$this->{$namespace} = new APIFactory($namespace, $this->API); $this->{$namespace} = new APIFactory($namespace, $this->API);
} }
} }
public function serialize($params = '') public function serialize($params = '')
{ {
if ($params === '') { if ($params === '') {
$params = $this->session; $params = $this->session;
} }
Logger::log([\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']]); Logger::log([\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']]);
return Serialization::serialize($params, $this); return Serialization::serialize($params, $this);
} }
} }

View File

@ -10,7 +10,6 @@ See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto. You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace danog\MadelineProto; namespace danog\MadelineProto;
/** /**
@ -20,8 +19,7 @@ class Serialization
{ {
public static function serialize_all($exception) public static function serialize_all($exception)
{ {
echo $exception.PHP_EOL; echo $exception . PHP_EOL;
return; return;
foreach (self::$instances as $instance) { foreach (self::$instances as $instance) {
if (isset($instance->session)) { if (isset($instance->session)) {
@ -29,16 +27,14 @@ class Serialization
} }
} }
} }
public static function realpaths($file) public static function realpaths($file)
{ {
if ($file[0] !== '/') { if ($file[0] !== '/') {
$file = getcwd().'/'.$file; $file = getcwd() . '/' . $file;
} }
var_dump(getcwd());
return ['file' => $file, 'lockfile' => $file.'.lock', 'tempfile' => $file.'.temp.session']; return ['file' => $file, 'lockfile' => $file . '.lock', 'tempfile' => $file . '.temp.session'];
} }
/** /**
* Serialize API class. * Serialize API class.
* *
@ -63,7 +59,6 @@ class Serialization
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'w'); $realpaths['lockfile'] = fopen($realpaths['lockfile'], 'w');
\danog\MadelineProto\Logger::log(['Waiting for exclusive lock of serialization lockfile...']); \danog\MadelineProto\Logger::log(['Waiting for exclusive lock of serialization lockfile...']);
flock($realpaths['lockfile'], LOCK_EX); flock($realpaths['lockfile'], LOCK_EX);
try { try {
$wrote = file_put_contents($realpaths['tempfile'], serialize($instance)); $wrote = file_put_contents($realpaths['tempfile'], serialize($instance));
rename($realpaths['tempfile'], $realpaths['file']); rename($realpaths['tempfile'], $realpaths['file']);
@ -71,10 +66,8 @@ class Serialization
flock($realpaths['lockfile'], LOCK_UN); flock($realpaths['lockfile'], LOCK_UN);
fclose($realpaths['lockfile']); fclose($realpaths['lockfile']);
} }
return $wrote; return $wrote;
} }
/** /**
* Deserialize API class. * Deserialize API class.
* *
@ -86,8 +79,8 @@ class Serialization
*/ */
public static function deserialize($filename, $no_updates = false) public static function deserialize($filename, $no_updates = false)
{ {
if (file_exists($filename)) {
$realpaths = self::realpaths($filename); $realpaths = self::realpaths($filename);
if (file_exists($realpaths['file'])) {
if (!file_exists($realpaths['lockfile'])) { if (!file_exists($realpaths['lockfile'])) {
touch($realpaths['lockfile']); touch($realpaths['lockfile']);
clearstatcache(); clearstatcache();
@ -95,7 +88,6 @@ class Serialization
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r'); $realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r');
\danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']); \danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']);
flock($realpaths['lockfile'], LOCK_SH); flock($realpaths['lockfile'], LOCK_SH);
try { try {
$unserialized = file_get_contents($realpaths['file']); $unserialized = file_get_contents($realpaths['file']);
} finally { } finally {
@ -104,11 +96,10 @@ class Serialization
} }
$tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized); $tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized);
foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) { foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) {
class_exists('\\danog\\MadelineProto\\'.$class); class_exists('\\danog\\MadelineProto\\' . $class);
} }
class_exists('\\Volatile'); class_exists('\\Volatile');
\danog\MadelineProto\Logger::class_exists(); \danog\MadelineProto\Logger::class_exists();
try { try {
$unserialized = unserialize($tounserialize); $unserialized = unserialize($tounserialize);
} catch (\danog\MadelineProto\Bug74586Exception $e) { } catch (\danog\MadelineProto\Bug74586Exception $e) {
@ -132,7 +123,6 @@ class Serialization
if ($unserialized instanceof \danog\MadelineProto\API) { if ($unserialized instanceof \danog\MadelineProto\API) {
$unserialized->session = $filename; $unserialized->session = $filename;
} }
return $unserialized; return $unserialized;
} }
} }