1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Copyright 2015 Eric Enold <zyberspace@zyberware.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace Zyberspace\Telegram\Cli;
/**
* Raw part of the php-client for telegram-cli.
* Takes care of the socket-connection and some helper-methods.
*/
class RawClient
{
/**
* The file handler for the socket-connection
*
* @var ressource
*/
protected $_fp;
/**
* If telegram-cli returns an error, the error-message gets stored here.
*
* @var string
*/
protected $_errorMessage = null;
/**
* If telegram-cli returns an error, the error-code gets stored here.
*
* @var int
*/
protected $_errorCode = null;
/**
* Connects to the telegram-cli.
*
* @param string $remoteSocket Address of the socket to connect to. See stream_socket_client() for more info.
* Can be 'unix://' or 'tcp://'.
*
* @throws ClientException Throws an exception if no connection can be established.
*/
public function __construct($remoteSocket)
{
$this->_fp = stream_socket_client($remoteSocket, $errno, $errstr, 2);
if (!$this->_fp) {
$this->_errorMessage = $errstr;
$this->_errorCode = $errno;
return false;
}
/*if ($this->_fp === false) {
return false;
//throw new ClientException('Could not connect to socket "' . $remoteSocket . '"');
}*/
}
/**
* Closes the connection to the telegram-cli.
*/
public function __destruct()
{
fclose($this->_fp);
}
/**
* Executes a command on the telegram-cli. Line-breaks will be escaped, as telgram-cli does not support them.
*
* @param string $command The command. Command-arguments can be passed as additional method-arguments.
*
* @return object|boolean Returns the answer as a json-object or true on success, false if there was an error.
*/
public function exec($command)
{
$command = implode(' ', func_get_args());
fwrite($this->_fp, str_replace("\n", '\n', $command) . PHP_EOL);
$answer = fgets($this->_fp); //"ANSWER $bytes" or false if an error occurred
// $this->tLog("INCA: ".$answer);
if (is_string($answer)) {
// $this->tLog("INC: ".$answer);
if (substr($answer, 0, 7) === 'ANSWER ') {
$bytes = ((int) substr($answer, 7)) + 1; //+1 because the json-return seems to miss one byte
if ($bytes > 0) {
$bytesRead = 0;
$jsonString = '';
//Run fread() till we have all the bytes we want
//(as fread() can only read a maximum of 8192 bytes from a read-buffered stream at once)
do {
$jsonString .= fread($this->_fp, $bytes - $bytesRead);
$bytesRead = strlen($jsonString);
} while ($bytesRead < $bytes);
// $this->tLog("JSON_ANS: ".$jsonString);
$json = json_decode($jsonString);
if (!isset($json->error)) {
//Reset error-message and error-code
$this->_errorMessage = null;
$this->_errorCode = null;
//For "status_online" and "status_offline"
if (isset($json->result) && $json->result === 'SUCCESS') {
return $jsonString; //$json; //true;
}
//Return json-object
return $json;
} else {
$this->_errorMessage = $json->error;
$this->_errorCode = $json->error_code;
}
}
}
}
return false;
}
public function tLog( $text ) {
//$logfile = "/var/www/html/tg/full.log";
$logfile = "/var/www/html/bx24/vendor/tg/src/tgc.log";
$nowdate = date("Y-m-d_H:i:s");
file_put_contents($logfile, "TGC: ".$nowdate." ".$text."\n", FILE_APPEND | LOCK_EX);
}
/**
* Returns the error-message retrieved vom telegram-cli, if there is one.
*
* @return string|null The error-message retrieved from telegram-cli or null if there was no error.
*/
public function getErrorMessage()
{
return $this->_errorMessage;
}
/**
* Returns the error-code retrieved vom telegram-cli, if there is one.
*
* @return string|null The error-message retrieved from telegram-cli or null if there was no error.
*/
public function getErrorCode()
{
return $this->_errorCode;
}
/**
* Escapes strings for usage as command-argument.
* T"es't -> "T\"es\'t"
*
* @param string $argument The argument to escape
*
* @return string The escaped command enclosed by double-quotes
*/
public function escapeStringArgument($argument)
{
return '"' . addslashes($argument) . '"';
}
/**
* Replaces all spaces with underscores.
*
* @param string $peer The peer to escape
*
* @return string The escaped peer
*/
public function escapePeer($peer)
{
return str_replace(' ', '_', $peer);
}
/**
* Takes a list of peers and turns it into a format needed by the most commands that handle multiple peers.
* Every single peer gets escaped by escapePeer().
*
* @param array $peerList The list of peers that shall get formated
*
* @return string The formated list of peers
*
* @uses escapePeer()
*/
public function formatPeerList(array $peerList)
{
return implode(' ', array_map(array($this, 'escapePeer'), $peerList));
}
/**
* Turns the given $fileName into an absolute file path and escapes him
*
* @param string $fileName The path to the file (can be relative or absolute)
*
* @return string The absolute path to the file
*/
public function formatFileName($fileName)
{
return $this->escapeStringArgument(realpath($fileName));
}
}