From e7b663032bb84bbe9f535b97930ef9f2bf5b3449 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 27 Sep 2016 16:51:40 +0200 Subject: [PATCH] Update --- lib/Zyberspace/Telegram/Cli/Client.php | 23 +++++------------------ lib/Zyberspace/Telegram/Cli/RawClient.php | 10 +++++++--- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/lib/Zyberspace/Telegram/Cli/Client.php b/lib/Zyberspace/Telegram/Cli/Client.php index 5d34ebd..b921a77 100644 --- a/lib/Zyberspace/Telegram/Cli/Client.php +++ b/lib/Zyberspace/Telegram/Cli/Client.php @@ -84,7 +84,6 @@ public function msg($peer, $msg) public function replymsg($id, $msg) { $msg = $this->escapeStringArgument($msg); - return $this->exec('reply ' . $id . ' ' . $msg); } /** @@ -454,14 +453,14 @@ public function pwrsendFile($peer, $type, $path, $hash) */ $formattedPath = $this->formatFileName($path); $cmd = "send_" . $type . " " . $peer . " " . $formattedPath; - $res = shell_exec("export TELEGRAM_HOME=".$this->tgpath."; ".$GLOBALS["homedir"] . "/tg/bin/telegram-cli --json --permanent-msg-ids -U pwrtelegram -WNRe " . escapeshellarg($cmd) . " 2>&1"); + $res = shell_exec($this->tgcmd." --json --permanent-msg-ids -U pwrtelegram -WNRe " . escapeshellarg($cmd) . " 2>&1"); $newres = null; $finalres = null; foreach (explode("\n", $res) as $line) { if(preg_match('|^{|', $line) && !preg_match('|{"result": "SUCCESS"}|', $line)) $newres = json_decode(preg_replace(array('|^[^{]*{|', "|}[^}]*$|"), array("{", "}"), $line), true); else continue; - if(isset($newres["out"]) && $newres["out"] && isset($newres["media"]["type"]) && $newres["media"]["type"] == $type && isset($newres["from"]["peer_id"]) && $newres["from"]["peer_id"] == $GLOBALS["botusername"]) $finalres = $newres; + if(isset($newres["out"]) && $newres["out"] && isset($newres["media"]["type"]) && $newres["media"]["type"] == $type && isset($newres["from"]["peer_id"]) && $newres["from"]["peer_id"] == $this->botusername) $finalres = $newres; } - return $newres; + return $finalres; } /** @@ -474,28 +473,16 @@ public function pwrsendFile($peer, $type, $path, $hash) * @uses exec() * @uses escapePeer() */ - public function getdFile($id, $type) - { - $res = shell_exec("export TELEGRAM_HOME=".$this->tgpath."; ".$GLOBALS["homedir"] . "/tg/bin/telegram-cli --json --permanent-msg-ids -WNRe 'load_file $id' 2>&1 | sed 's/[>]//g;/{/!d;/{\"event\": \"download\"/!d;/^\s*$/d;s/^[^{]*{/{/;s/}[^}]*$/}/'"); - error_log($res); - return json_decode($res); - } public function getFile($user, $file_id, $type) { - $script = escapeshellarg($GLOBALS["pwrhomedir"] . "/lua/download.lua"); - $res = shell_exec("export TELEGRAM_HOME=".$this->tgpath."; ".$GLOBALS["homedir"] . "/tg/bin/telegram-cli --json -WNRs " . $script . " --lua-param ".escapeshellarg($user." ".$file_id." ".$type)." 2>&1"); + $script = escapeshellarg($this->pwrhomedir . "/lua/download.lua"); + $res = shell_exec($this->tgcmd." --json -WNRs " . $script . " --lua-param ".escapeshellarg($user." ".$file_id." ".$type)." 2>&1"); foreach(explode("\n", $res) as $line) { if(preg_match('|.*{"event":"download", "result"|', $line)) $res = preg_replace(array('|.*{"event":"download", "result"|', "|}.*|"), array('{"event":"download", "result"', "}"), $line); } return json_decode($res); } - public function oldgetFile($user, $id, $type) - { - - return $this->exec('load_' . $type . ' ' . $id); - } - /** * Get info about current user * diff --git a/lib/Zyberspace/Telegram/Cli/RawClient.php b/lib/Zyberspace/Telegram/Cli/RawClient.php index e98d0b4..258573f 100755 --- a/lib/Zyberspace/Telegram/Cli/RawClient.php +++ b/lib/Zyberspace/Telegram/Cli/RawClient.php @@ -42,14 +42,18 @@ class RawClient * * @throws ClientException Throws an exception if no connection can be established. */ - public function __construct($deep = false) + public function __construct($params, $deep = false) { + foreach ($params as $key => $value) { + $this->{$key} = $value; + } $path = $deep ? "/tmp/deeptg.sck" : "/tmp/tg.sck"; $remoteSocket = "unix://" . $path; - $this->tgpath = $GLOBALS["homedir"] . ($deep ? "/deeptgstorage" : "/tgstorage"); + $this->tgpath = $this->homedir . ($deep ? "/deeptgstorage" : "/tgstorage"); + $this->tgcmd = "export TELEGRAM_HOME=".$this->tgpath.";".$this->homedir."/tg/bin/telegram-cli "; $this->_fp = stream_socket_client($remoteSocket); if ($this->_fp === false) { - shell_exec("pkill telegram-cli; rm ".$path."; export TELEGRAM_HOME=".$this->tgpath."; ". $GLOBALS["homedir"] . "/tg/bin/telegram-cli --json --permanent-msg-ids -dWS ".$path."&"); + shell_exec("pkill telegram-cli; rm ".$path.";".$this->tgcmd." --json --permanent-msg-ids -dWS ".$path."&"); $this->_fp = stream_socket_client($remoteSocket); if ($this->_fp === false) { throw new ClientException('Could not connect to socket "' . $remoteSocket . '"');