This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosruConnector.php
428 lines (379 loc) · 15.1 KB
/
mosruConnector.php
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* Класс для взаимодействия с порталом https://my.mos.ru
* Поддерживаются получение, передача и удаление показаний счетчиков воды.
*
* @author Evgeny Volferts <eugene.wolferz@gmail.com>
*/
class mosruConnector
{
private $config = [];
private $response = '';
private $cookie = '';
private $coldCounter = [];
private $hotCounter = [];
private $curlDefaultOptions = [];
private $authorized = false;
private $error = false;
private $errorMessage = "";
const TYPE_COLD = 1;
const TYPE_HOT = 2;
/**
* mosruConnector constructor
*/
public function __construct()
{
if (!file_exists('config.php')) {
exit("Ошибка! Не найден файл конфигурации!\n Ознакомьтесь с примером в файле config.php.sample\n");
}
$this->config = require_once('config.php');
$this->cookie = tempnam(sys_get_temp_dir(), "mosru-cookie-");
$this->curlDefaultOptions = [
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
CURLOPT_REFERER => ';auto',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIEJAR => $this->cookie,
CURLOPT_COOKIEFILE => $this->cookie,
CURLOPT_RETURNTRANSFER => true,
];
}
/**
* @param array $cookies
* @return bool
*/
private static function tokenPresent($cookies = [])
{
foreach ($cookies as $cookie) {
if ($cookie['name'] == 'Ltpatoken2') {
return true;
}
}
return false;
}
/**
* mosruConnector destructor
*/
public function __destruct()
{
unlink($this->cookie);
}
/**
* @return boolean Auth result
*/
private function login()
{
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => 'https://my.mos.ru/login',
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
preg_match_all('/login_url = "(https:\/\/login.mos.ru\/sps\/oauth\/ae(.*))"/', $this->response, $matches);
if (!isset($matches[1])) {
return false;
}
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => $matches[1][0],
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => 'https://login.mos.ru/sps/login/methods/password',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(
[
'login' => urldecode($this->config['login']),
'password' => urldecode($this->config['password']),
'isDelayed' => 'false',
]
),
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => 'https://my.mos.ru/my/#/',
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
if (!self::tokenPresent(self::extractCookies(file_get_contents($this->cookie)))) {
return false;
} else {
$this->authorized = true;
return true;
}
}
/**
* Retrieves water counters info
*/
private function getWaterCountersInfo()
{
if (!$this->authorized && !$this->login()) {
exit("Ошибка авторизации!\n");
}
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => 'https://www.mos.ru/pgu/ru/application/guis/1111/',
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => 'https://www.mos.ru/pgu/common/ajax/index.php',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(
[
'ajaxModule' => 'Guis',
'ajaxAction' => 'getCountersInfo',
'items[paycode]' => $this->config['payerCode'],
'items[flat]' => $this->config['flatNumber'],
]
),
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
foreach (json_decode($this->response)->counter as $counter) {
switch ($counter->type) {
case self::TYPE_COLD:
$this->coldCounter[$counter->num] = [
'num' => $counter->num,
'counterId' => $counter->counterId,
];
foreach ($counter->indications as $line) {
$this->coldCounter[$counter->num]['indications'][str_replace('+03:00', '', $line->period)] = $line->indication;
}
break;
case self::TYPE_HOT:
$this->hotCounter[$counter->num] = [
'num' => $counter->num,
'counterId' => $counter->counterId,
];
foreach ($counter->indications as $line) {
$this->hotCounter[$counter->num]['indications'][str_replace('+03:00', '', $line->period)] = $line->indication;
}
break;
}
}
}
/**
* @return array
*/
private function parseWaterCountersInfo()
{
$dates = [];
foreach ($this->coldCounter as $counter) {
foreach ($counter['indications'] as $date => $value) {
$dates[$date]['cold'][$counter['num']] = $value;
}
}
foreach ($this->hotCounter as $counter) {
foreach ($counter['indications'] as $date => $value) {
$dates[$date]['hot'][$counter['num']] = $value;
}
}
return $dates;
}
/**
* @return array
*/
public function getWaterHistory()
{
$this->getWaterCountersInfo();
return $this->parseWaterCountersInfo();
}
/**
* @return string
*/
public function getWaterHistoryPrintable()
{
$this->getWaterCountersInfo();
$output = "История показаний счетчиков воды\n";
setlocale(LC_TIME, 'ru_RU.UTF-8');
foreach ($this->parseWaterCountersInfo() as $date => $values) {
$output .= strftime("\n%B %Y", strtotime($date)) . "\n";
foreach ($values['cold'] as $num => $value) {
$output .= "Холодная ({$num}): {$value}\t";
}
$output .= "\n";
foreach ($values['hot'] as $num => $value) {
$output .= "Горячая ({$num}): {$value}\t";
}
$output .= "\n";
}
return $output;
}
/**
* Sends current indications
* @param array $values
*/
public function updateWaterCountersInfo($values)
{
$this->error = false;
$this->errorMessage = '';
$this->getWaterCountersInfo();
$countersInfo = $this->parseWaterCountersInfo();
krsort($countersInfo);
foreach ($values as $num => $value) {
if (isset($this->coldCounter[$num]) && (array_values($countersInfo)[0]['cold'][$num] >= $value)) {
$this->error = true;
$this->errorMessage .= "Ошибка! Прежнее показание счетчика холодной воды ({$num}) больше или равно указанному!\n";
} elseif (isset($this->hotCounter[$num]) && (array_values($countersInfo)[0]['hot'][$num] >= $value)) {
$this->error = true;
$this->errorMessage .= "Ошибка! Прежнее показание счетчика горячей воды ({$num}) больше или равно указанному!\n";
}
}
if (!$this->error) {
$date = date("Y-m-d", strtotime('last day of this month', time()));
$query = [
'ajaxModule' => 'Guis',
'ajaxAction' => 'addCounterInfo',
'items[paycode]' => $this->config['payerCode'],
'items[flat]' => $this->config['flatNumber'],
];
$i = 0;
foreach ($values as $num => $value) {
if (isset($this->coldCounter[$num])) {
$id = $this->coldCounter[$num]['counterId'];
} elseif (isset($this->hotCounter[$num])) {
$id = $this->hotCounter[$num]['counterId'];
}
$query["items[indications][{$i}}][counterNum]"] = $id;
$query["items[indications][{$i}}][counterVal]"] = $value;
$query["items[indications][{$i}}][num]"] = $num;
$query["items[indications][{$i}}][period]"] = $date;
$i++;
}
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => 'https://www.mos.ru/pgu/common/ajax/index.php',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($query),
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
$result = json_decode($this->response);
if ($result->code != 0) {
$this->error = true;
$this->errorMessage .= "Ошибка! Невозможно передать показания счетчиков!\n";
$this->errorMessage .= $result->error . "\n";
}
}
if (!$this->error) {
echo "Показания счетчиков успешно переданы.\n";
} else {
echo $this->errorMessage;
}
}
/**
* Removes last sent indications (if possible)
*/
public function removeWaterCounterInfo()
{
$this->error = false;
$this->errorMessage = '';
$this->getWaterCountersInfo();
foreach ($this->coldCounter as $num => $counter) {
$result = $this->removeIndication($counter['counterId']);
if ($result->code != 0) {
$this->error = true;
$this->errorMessage .= "Ошибка! Невозможно удалить последнее показание для счетчика холодной воды ({$num})!\n";
$this->errorMessage .= $result->error . "\n";
}
}
foreach ($this->hotCounter as $num => $counter) {
$result = $this->removeIndication($counter['counterId']);
if ($result->code != 0) {
$this->error = true;
$this->errorMessage .= "Ошибка! Невозможно удалить последнее показание для счетчика горячей воды ({$num})!\n";
$this->errorMessage .= $result->error . "\n";
}
}
if (!$this->error) {
echo "Последние показания счетчиков успешно удалены.\n";
} else {
echo $this->errorMessage;
}
}
/**
* @param string $id
* @return mixed
*/
private function removeIndication($id)
{
$ch = curl_init();
$curlOptions = [
CURLOPT_URL => 'https://www.mos.ru/pgu/common/ajax/index.php',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(
[
'ajaxModule' => 'Guis',
'ajaxAction' => 'removeCounterIndication',
'items[paycode]' => $this->config['payerCode'],
'items[flat]' => $this->config['flatNumber'],
'items[counterId]' => $id,
]
),
];
curl_setopt_array($ch, $this->curlDefaultOptions + $curlOptions);
$this->response = curl_exec($ch);
curl_close($ch);
return json_decode($this->response);
}
/**
* Extract any cookies found from the cookie file. This function expects to get
* a string containing the contents of the cookie file which it will then
* attempt to extract and return any cookies found within.
*
* @param string $string The contents of the cookie file.
*
* @return array The array of cookies as extracted from the string.
*
* @link https://stackoverflow.com/questions/410109/php-reading-a-cookie-file
*
*/
private static function extractCookies($string)
{
$cookies = [];
$lines = explode(PHP_EOL, $string);
foreach ($lines as $line) {
$cookie = [];
// detect httponly cookies and remove #HttpOnly prefix
if (substr($line, 0, 10) == '#HttpOnly_') {
$line = substr($line, 10);
$cookie['httponly'] = true;
} else {
$cookie['httponly'] = false;
}
// we only care for valid cookie def lines
if (strlen($line) > 0 && $line[0] != '#' && substr_count($line, "\t") == 6) {
// get tokens in an array
$tokens = explode("\t", $line);
// trim the tokens
$tokens = array_map('trim', $tokens);
// Extract the data
$cookie['domain'] = $tokens[0]; // The domain that created AND can read the variable.
$cookie['flag'] = $tokens[1]; // A TRUE/FALSE value indicating if all machines within a given domain can access the variable.
$cookie['path'] = $tokens[2]; // The path within the domain that the variable is valid for.
$cookie['secure'] = $tokens[3]; // A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
$cookie['expiration-epoch'] = $tokens[4]; // The UNIX time that the variable will expire on.
$cookie['name'] = urldecode($tokens[5]); // The name of the variable.
$cookie['value'] = urldecode($tokens[6]); // The value of the variable.
// Convert date to a readable format
$cookie['expiration'] = date('Y-m-d h:i:s', $tokens[4]);
// Record the cookie.
$cookies[] = $cookie;
}
}
return $cookies;
}
}