2020-07-09 22:17:26 +07:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace VK\Robots;
|
|
|
|
|
|
2020-09-26 17:28:13 +07:00
|
|
|
|
use \Exception;
|
|
|
|
|
use \VK\Browsers\BrowserAbstract,
|
|
|
|
|
\VK\Proxies\ProxyAbstract,
|
|
|
|
|
\VK\Captcha\CaptchaAbstract,
|
|
|
|
|
\VK\Core;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Абстракция робота
|
|
|
|
|
*
|
|
|
|
|
* @property int $id Идентификатор
|
|
|
|
|
* @property int $session Сессия
|
|
|
|
|
* @property string $token Токен
|
|
|
|
|
* @property float $version Версия API
|
|
|
|
|
* @property BrowserAbstract $browser Браузер
|
|
|
|
|
* @property ProxyAbstract $proxy Прокси
|
|
|
|
|
* @property CaptchaAbstract $captcha Обработчик капчи
|
|
|
|
|
*
|
|
|
|
|
* @method protected static function put(string $url, ...$params) Создать
|
|
|
|
|
* @method protected static function post(string $url, ...$params) Изменить
|
|
|
|
|
* @method protected static function get(string $url, ...$params) Получить
|
|
|
|
|
* @method protected static function delete(string $url, ...$params) Удалить
|
|
|
|
|
* @method public function __set($name, $value) Магический метод-сеттер
|
|
|
|
|
* @method public function __get($name) Магический метод-геттер
|
|
|
|
|
*
|
|
|
|
|
* @package API/Methods
|
|
|
|
|
* @author Арсен Мирзаев <red@hood.su>
|
|
|
|
|
*/
|
2020-07-09 22:17:26 +07:00
|
|
|
|
abstract class RobotAbstract
|
|
|
|
|
{
|
|
|
|
|
/**
|
2020-09-17 19:27:12 +07:00
|
|
|
|
* Идентификатор
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
protected int $id;
|
|
|
|
|
|
2020-09-26 17:28:13 +07:00
|
|
|
|
/**
|
|
|
|
|
* Сессия
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
protected int $session;
|
|
|
|
|
|
2020-09-17 19:27:12 +07:00
|
|
|
|
/**
|
|
|
|
|
* Токен
|
2020-07-09 22:17:26 +07:00
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2020-09-17 19:27:12 +07:00
|
|
|
|
protected string $token;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Версия API
|
|
|
|
|
*
|
|
|
|
|
* @var float
|
|
|
|
|
*/
|
2020-09-26 17:28:13 +07:00
|
|
|
|
protected float $version = 5.124;
|
2020-07-09 22:17:26 +07:00
|
|
|
|
|
|
|
|
|
/**
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* Браузер
|
2020-07-09 22:17:26 +07:00
|
|
|
|
*
|
|
|
|
|
* @var BrowserAbstract
|
|
|
|
|
*/
|
|
|
|
|
protected BrowserAbstract $browser;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* Прокси
|
2020-07-09 22:17:26 +07:00
|
|
|
|
*
|
|
|
|
|
* @var ProxyAbstract
|
|
|
|
|
*/
|
|
|
|
|
protected ProxyAbstract $proxy;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Обработчик капчи
|
|
|
|
|
*
|
|
|
|
|
* @var CaptchaAbstract
|
|
|
|
|
*/
|
|
|
|
|
protected CaptchaAbstract $captcha;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-17 19:27:12 +07:00
|
|
|
|
* Магический сеттер
|
2020-07-09 22:17:26 +07:00
|
|
|
|
*
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* @param mixed $name Название
|
|
|
|
|
* @param mixed $value Значение
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2020-07-09 22:17:26 +07:00
|
|
|
|
*/
|
2020-09-26 17:28:13 +07:00
|
|
|
|
public function __set($name, $value): void
|
2020-07-09 22:17:26 +07:00
|
|
|
|
{
|
2020-09-17 19:27:12 +07:00
|
|
|
|
if ($name === 'id' && empty($this->id)) {
|
|
|
|
|
$this->id = $value;
|
2020-09-26 17:28:13 +07:00
|
|
|
|
} else if ($name === 'session' && empty($this->session)) {
|
|
|
|
|
$this->session = $value;
|
2020-09-17 19:27:12 +07:00
|
|
|
|
} else if ($name === 'token' && empty($this->token)) {
|
|
|
|
|
$this->token = $value;
|
|
|
|
|
} else if ($name === 'version' && empty($this->version)) {
|
|
|
|
|
$this->version = $value;
|
|
|
|
|
} else if ($name === 'browser') {
|
|
|
|
|
$this->browser = $value;
|
|
|
|
|
} else if ($name === 'proxy') {
|
|
|
|
|
$this->proxy = $value;
|
|
|
|
|
} else if ($name === 'captcha') {
|
|
|
|
|
$this->captcha = $value;
|
|
|
|
|
}
|
2020-07-09 22:17:26 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-17 19:27:12 +07:00
|
|
|
|
* Магический геттер
|
2020-07-09 22:17:26 +07:00
|
|
|
|
*
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* @param mixed $name Название
|
|
|
|
|
*
|
2020-09-17 19:27:12 +07:00
|
|
|
|
* @return mixed
|
2020-07-09 22:17:26 +07:00
|
|
|
|
*/
|
2020-09-17 19:27:12 +07:00
|
|
|
|
public function __get($name)
|
2020-07-09 22:17:26 +07:00
|
|
|
|
{
|
2020-09-17 19:27:12 +07:00
|
|
|
|
if ($name === 'id') {
|
|
|
|
|
return $this->id;
|
2020-09-26 17:28:13 +07:00
|
|
|
|
} else if ($name === 'session') {
|
|
|
|
|
return $this->session;
|
2020-09-17 19:27:12 +07:00
|
|
|
|
} else if ($name === 'token') {
|
|
|
|
|
return $this->token;
|
|
|
|
|
} else if ($name === 'version') {
|
|
|
|
|
return $this->version;
|
|
|
|
|
} else if ($name === 'browser') {
|
|
|
|
|
return $this->browser;
|
|
|
|
|
} else if ($name === 'proxy') {
|
|
|
|
|
return $this->proxy;
|
|
|
|
|
} else if ($name === 'captcha') {
|
|
|
|
|
return $this->captcha;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* Токен
|
|
|
|
|
*
|
|
|
|
|
* @param string $token Токен
|
2020-09-17 19:27:12 +07:00
|
|
|
|
*
|
|
|
|
|
* @return RobotAbstract
|
|
|
|
|
*/
|
|
|
|
|
public function token(string $token): RobotAbstract
|
|
|
|
|
{
|
|
|
|
|
$this->token = $token;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* Браузер
|
2020-09-17 19:27:12 +07:00
|
|
|
|
*
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* @param BrowserAbstract $browser Браузер
|
|
|
|
|
*
|
|
|
|
|
* @return BrowserAbstract
|
2020-09-17 19:27:12 +07:00
|
|
|
|
*/
|
2020-09-26 17:28:13 +07:00
|
|
|
|
public function browser(BrowserAbstract $browser = null): BrowserAbstract
|
2020-09-17 19:27:12 +07:00
|
|
|
|
{
|
2020-09-26 17:28:13 +07:00
|
|
|
|
if (isset($this->browser) && $browser === null) {
|
|
|
|
|
// Если не передан браузер и свойство иниициализировано
|
|
|
|
|
return $this->browser;
|
|
|
|
|
} else if ($browser === null) {
|
|
|
|
|
// Иначе если не передан браузер свойство не инициализировано
|
|
|
|
|
return $this->browser = new \VK\Browsers\Curl();
|
|
|
|
|
} else {
|
|
|
|
|
// Иначе полученный браузер записывается в свойство и возвращается
|
|
|
|
|
return $this->browser = $browser;
|
|
|
|
|
}
|
2020-09-17 19:27:12 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* Установка прокси
|
|
|
|
|
*
|
|
|
|
|
* @param ProxyAbstract $proxy Прокси
|
2020-09-17 19:27:12 +07:00
|
|
|
|
*
|
|
|
|
|
* @return RobotAbstract
|
|
|
|
|
*/
|
|
|
|
|
public function proxy(ProxyAbstract $proxy): RobotAbstract
|
|
|
|
|
{
|
|
|
|
|
$this->proxy = $proxy;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-26 17:28:13 +07:00
|
|
|
|
* Установка обработчика капчи
|
|
|
|
|
*
|
|
|
|
|
* @param CaptchaAbstract $captcha Обработчик капчи
|
2020-09-17 19:27:12 +07:00
|
|
|
|
*
|
|
|
|
|
* @return RobotAbstract
|
|
|
|
|
*/
|
|
|
|
|
public function captcha(CaptchaAbstract $captcha): RobotAbstract
|
|
|
|
|
{
|
|
|
|
|
$this->captcha = $captcha;
|
|
|
|
|
|
|
|
|
|
return $this;
|
2020-07-09 22:17:26 +07:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 17:28:13 +07:00
|
|
|
|
// /**
|
|
|
|
|
// * Метод получения токена аккаунта
|
|
|
|
|
// *
|
|
|
|
|
// * @return string
|
|
|
|
|
// */
|
|
|
|
|
// public function getToken($captcha_key = null, $captcha_id = null): string
|
|
|
|
|
// {
|
|
|
|
|
// if (!empty($_ENV['ACCOUNT_TOKEN'])) {
|
|
|
|
|
// return $_ENV['ACCOUNT_TOKEN'];
|
|
|
|
|
// } else if ($this->mobile) {
|
|
|
|
|
// $this->token_access = $this->genTokenMobile($captcha_key, $this->captcha_id ?? $captcha_id);
|
|
|
|
|
// } else {
|
|
|
|
|
// $this->token_access = $this->genToken();
|
|
|
|
|
// }
|
2020-07-09 22:17:26 +07:00
|
|
|
|
|
2020-09-26 17:28:13 +07:00
|
|
|
|
// return $this->token_access;
|
|
|
|
|
// }
|
2020-07-09 22:17:26 +07:00
|
|
|
|
|
|
|
|
|
|
2020-09-26 17:28:13 +07:00
|
|
|
|
// abstract protected function genToken(): string;
|
|
|
|
|
// abstract protected function genTokenMobile(string $captcha_key, int $captcha_id): string;
|
2020-07-09 22:17:26 +07:00
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * @param null $access_groups
|
|
|
|
|
// * @param bool $resend
|
|
|
|
|
// * @return mixed
|
|
|
|
|
// * @throws VkApiException
|
|
|
|
|
// */
|
|
|
|
|
// private function getTokenAccess($scope = null, $resend = false)
|
|
|
|
|
// {
|
|
|
|
|
// $get_url_token = Curl::getToken('https://oauth.vk.com/authorize?client_id=' . $_ENV['APP_ID'] .
|
|
|
|
|
// '&scope=' . $_ENV['ACCESS_GROUPS'] . ($resend ? '&revoke=1' : '') .
|
|
|
|
|
// '&response_type=token');
|
|
|
|
|
|
|
|
|
|
// echo 'https://oauth.vk.com/authorize?client_id=' . $_ENV['APP_ID'] .
|
|
|
|
|
// '&scope=' . $_ENV['ACCESS_GROUPS'] . ($resend ? '&revoke=1' : '') .
|
|
|
|
|
// '&response_type=token', PHP_EOL;
|
|
|
|
|
// if ($get_url_token['body'] !== false) {
|
|
|
|
|
// if (isset($get_url_token['header']['location'][0]))
|
|
|
|
|
// $url_token = $get_url_token['header']['location'][0];
|
|
|
|
|
// else {
|
|
|
|
|
// preg_match('!location.href = "(.*)"\+addr!s', $get_url_token['body'], $url_token);
|
|
|
|
|
|
|
|
|
|
// if (!isset($url_token[1])) {
|
|
|
|
|
// throw new VkApiException("Не получилось получить токен на этапе получения ссылки подтверждения");
|
|
|
|
|
// }
|
|
|
|
|
// $url_token = $url_token[1];
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// echo 'жопа';
|
|
|
|
|
// die;
|
|
|
|
|
// }
|
|
|
|
|
// echo $url_token, PHP_EOL;
|
|
|
|
|
// die;
|
|
|
|
|
// $access_token_location = Curl::getToken($url_token)['header']['location'][0];
|
|
|
|
|
|
|
|
|
|
// if (preg_match("!access_token=(.*?)&!s", $access_token_location, $access_token) != 1)
|
|
|
|
|
// throw new Exception("Не удалось найти access_token в строке ридеректа, ошибка:" . Curl::getToken($access_token_location, null, false)['body']);
|
|
|
|
|
|
|
|
|
|
// echo $url_token, PHP_EOL;
|
|
|
|
|
// die;
|
|
|
|
|
// return $access_token[1];
|
|
|
|
|
// }
|
|
|
|
|
}
|