Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Sep 27, 2016
1 parent e11bee3 commit e7b6630
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
23 changes: 5 additions & 18 deletions lib/Zyberspace/Telegram/Cli/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function msg($peer, $msg)
public function replymsg($id, $msg)
{
$msg = $this->escapeStringArgument($msg);

return $this->exec('reply ' . $id . ' ' . $msg);
}
/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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
*
Expand Down
10 changes: 7 additions & 3 deletions lib/Zyberspace/Telegram/Cli/RawClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '"');
Expand Down

0 comments on commit e7b6630

Please sign in to comment.