1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 09:58:59 +01:00

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.
If not, see <http://www.gnu.org/licenses/>.
*/
namespace danog\MadelineProto;
class API extends APIFactory
@ -18,15 +17,14 @@ class API extends APIFactory
use \danog\Serializable;
public $session;
public $serialized = 0;
public function __magic_construct($params = [])
{
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
set_exception_handler(['\\danog\\MadelineProto\\Serialization', 'serialize_all']);
if (is_string($params)) {
if (file_exists($params)) {
$this->session = $params;
$realpaths = Serialization::realpaths($params);
if (file_exists($realpaths['file'])) {
$this->session = $realpaths['file'];
if (!file_exists($realpaths['lockfile'])) {
touch($realpaths['lockfile']);
clearstatcache();
@ -34,7 +32,6 @@ class API extends APIFactory
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r');
\danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']);
flock($realpaths['lockfile'], LOCK_SH);
try {
$unserialized = file_get_contents($realpaths['file']);
} finally {
@ -47,7 +44,6 @@ class API extends APIFactory
}
class_exists('\\Volatile');
\danog\MadelineProto\Logger::class_exists();
try {
$unserialized = unserialize($tounserialize);
} catch (\danog\MadelineProto\Bug74586Exception $e) {
@ -72,7 +68,6 @@ class API extends APIFactory
$this->API = $unserialized->API;
$this->APIFactory();
}
return;
}
$this->API = new MTProto($params);
@ -85,13 +80,11 @@ class API extends APIFactory
//$this->future_salts = $this->get_future_salts(['num' => 3]);
\danog\MadelineProto\Logger::log([\danog\MadelineProto\Lang::$current_lang['madelineproto_ready']], Logger::NOTICE);
}
public function __wakeup()
{
//if (method_exists($this->API, 'wakeup')) $this->API = $this->API->wakeup();
$this->APIFactory();
}
public function __destruct()
{
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) {
@ -102,56 +95,45 @@ class API extends APIFactory
}
restore_error_handler();
}
public function __sleep()
{
return ['API'];
}
public function &__get($name)
{
if ($name === 'settings') {
$this->API->setdem = true;
return $this->API->settings;
}
return $this->API->storage[$name];
}
public function __set($name, $value)
{
if ($name === 'settings') {
return $this->API->__construct($value);
}
return $this->API->storage[$name] = $value;
}
public function __isset($name)
{
return isset($this->API->storage[$name]);
}
public function __unset($name)
{
unset($this->API->storage[$name]);
}
public function APIFactory()
{
foreach ($this->API->get_method_namespaces() as $namespace) {
$this->{$namespace} = new APIFactory($namespace, $this->API);
}
}
public function serialize($params = '')
{
if ($params === '') {
$params = $this->session;
}
Logger::log([\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']]);
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.
If not, see <http://www.gnu.org/licenses/>.
*/
namespace danog\MadelineProto;
/**
@ -21,7 +20,6 @@ class Serialization
public static function serialize_all($exception)
{
echo $exception . PHP_EOL;
return;
foreach (self::$instances as $instance) {
if (isset($instance->session)) {
@ -29,16 +27,14 @@ class Serialization
}
}
}
public static function realpaths($file)
{
if ($file[0] !== '/') {
$file = getcwd() . '/' . $file;
}
var_dump(getcwd());
return ['file' => $file, 'lockfile' => $file . '.lock', 'tempfile' => $file . '.temp.session'];
}
/**
* Serialize API class.
*
@ -63,7 +59,6 @@ class Serialization
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'w');
\danog\MadelineProto\Logger::log(['Waiting for exclusive lock of serialization lockfile...']);
flock($realpaths['lockfile'], LOCK_EX);
try {
$wrote = file_put_contents($realpaths['tempfile'], serialize($instance));
rename($realpaths['tempfile'], $realpaths['file']);
@ -71,10 +66,8 @@ class Serialization
flock($realpaths['lockfile'], LOCK_UN);
fclose($realpaths['lockfile']);
}
return $wrote;
}
/**
* Deserialize API class.
*
@ -86,8 +79,8 @@ class Serialization
*/
public static function deserialize($filename, $no_updates = false)
{
if (file_exists($filename)) {
$realpaths = self::realpaths($filename);
if (file_exists($realpaths['file'])) {
if (!file_exists($realpaths['lockfile'])) {
touch($realpaths['lockfile']);
clearstatcache();
@ -95,7 +88,6 @@ class Serialization
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r');
\danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']);
flock($realpaths['lockfile'], LOCK_SH);
try {
$unserialized = file_get_contents($realpaths['file']);
} finally {
@ -108,7 +100,6 @@ class Serialization
}
class_exists('\\Volatile');
\danog\MadelineProto\Logger::class_exists();
try {
$unserialized = unserialize($tounserialize);
} catch (\danog\MadelineProto\Bug74586Exception $e) {
@ -132,7 +123,6 @@ class Serialization
if ($unserialized instanceof \danog\MadelineProto\API) {
$unserialized->session = $filename;
}
return $unserialized;
}
}