223 lines
5.8 KiB
PHP
223 lines
5.8 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace VK\Robots;
|
||
|
||
use \VK\Browsers\BrowserAbstract;
|
||
use \VK\Proxies\ProxyAbstract;
|
||
use \VK\Proxies\CaptchaAbstract;
|
||
|
||
abstract class RobotAbstract
|
||
{
|
||
/**
|
||
* Идентификатор
|
||
*
|
||
* @var int
|
||
*/
|
||
protected int $id;
|
||
|
||
/**
|
||
* Токен
|
||
*
|
||
* @var string
|
||
*/
|
||
protected string $token;
|
||
|
||
/**
|
||
* Версия API
|
||
*
|
||
* @var float
|
||
*/
|
||
public float $version = 5.103;
|
||
|
||
/**
|
||
* Используемый браузер
|
||
*
|
||
* @var BrowserAbstract
|
||
*/
|
||
protected BrowserAbstract $browser;
|
||
|
||
/**
|
||
* Прокси-сервер
|
||
*
|
||
* @var ProxyAbstract
|
||
*/
|
||
protected ProxyAbstract $proxy;
|
||
|
||
/**
|
||
* Обработчик капчи
|
||
*
|
||
* @var CaptchaAbstract
|
||
*/
|
||
protected CaptchaAbstract $captcha;
|
||
|
||
/**
|
||
* Магический сеттер
|
||
*
|
||
* @return mixed
|
||
*/
|
||
public function __set($name, $value)
|
||
{
|
||
if ($name === 'id' && empty($this->id)) {
|
||
$this->id = $value;
|
||
} 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;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Магический геттер
|
||
*
|
||
* @return mixed
|
||
*/
|
||
public function __get($name)
|
||
{
|
||
if ($name === 'id') {
|
||
return $this->id;
|
||
} 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;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Установка токена
|
||
*
|
||
* @return RobotAbstract
|
||
*/
|
||
public function token(string $token): RobotAbstract
|
||
{
|
||
$this->token = $token;
|
||
|
||
return $this;
|
||
}
|
||
|
||
|
||
/**
|
||
* Установка браузера
|
||
*
|
||
* @return RobotAbstract
|
||
*/
|
||
public function browser(BrowserAbstract $browser): RobotAbstract
|
||
{
|
||
$this->browser = $browser;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* Установка прокси-сервера
|
||
*
|
||
* @return RobotAbstract
|
||
*/
|
||
public function proxy(ProxyAbstract $proxy): RobotAbstract
|
||
{
|
||
$this->proxy = $proxy;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* Установка сервиса антикапчи
|
||
*
|
||
* @return RobotAbstract
|
||
*/
|
||
public function captcha(CaptchaAbstract $captcha): RobotAbstract
|
||
{
|
||
$this->captcha = $captcha;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* Получение браузера
|
||
*
|
||
* @return int
|
||
*/
|
||
public function getBrowser(): BrowserAbstract
|
||
{
|
||
// return $this->browser;
|
||
return new \VK\Browsers\Curl;
|
||
}
|
||
|
||
/**
|
||
* Метод получения токена аккаунта
|
||
*
|
||
* @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();
|
||
}
|
||
|
||
return $this->token_access;
|
||
}
|
||
|
||
|
||
abstract protected function genToken(): string;
|
||
abstract protected function genTokenMobile(string $captcha_key, int $captcha_id): string;
|
||
|
||
// /**
|
||
// * @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];
|
||
// }
|
||
}
|