Вариант автопостинга в ОД

#1 24 марта 2014 в 19:56
Наткнулся в инте на вот такой код автопостинга в ОД. Насколько его реально допилить до Icms? Может есть другие альтернативы у кого?
Ложу под спойлер ( режет код почему то)

new OK([
'username' => 'логин',
'password' => 'пароль',
'path' => 'ссылка на группу',
'post_link' => 'ссылка которую нужно постить'
]);

Сам Класс :

class OK
{
public $username;
public $password;
public $path;
public $post_link;
public $browser = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36';

public $auth_hash;

public $cookie;

public $response;

private $__group_id;
private $__tkn;
private $__gwt;
private $path_info;

public function __construct($params)
{
foreach($params as $key => $value)
{
$this->$key = $value;
}

if(filter_var($this->path, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))
{
$path = parse_url($this->path);

$this->path = $path['path'];
}

$this->path = '/'. trim($this->path, '/');

if($this->test_auth())
{
$this->cookie = $this->auth_hash;
}
else
{
$this->cookie = $this->login();
}

if(!$this->cookie)
{
return $this->say('php.loginError');
}

$this->path_info = $this->path_info($this->path, $this->cookie);

if(!is_array($this->path_info))
{
if($this->path_info)
{
return $this->say($this->path_info);
}
else
{
if(isset($this->__group_id) and isset($this->__tkn) and isset($this->__gwt))
{
$this->join_group();

$this->path_info = $this->path_info($this->path, $this->cookie);

if(!is_array($this->path_info))
{
return $this->say('php.groupErrorAfterJoin');
}
}
else
{
return $this->say('php.groupError');
}
}
}

$post = $this->post();

if($post)
{
if($post === true)
{
return $this->say('posted');
}
else
{
return $this->say($post);
}
}
else
{
return $this->say('php.undefinedPostError');
}
}

private function say($response)
{
$this->response = $response;
}

private function join_group()
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $this->browser);

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '1=1');

curl_setopt($ch, CURLOPT_COOKIE, 'JSESSIONID='. $this->cookie);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('TKN: '. $this->__tkn));

curl_setopt($ch, CURLOPT_URL, 'http://www.odnoklassniki.ru'. $this->path. '?cmd=LeftColumnTopCardAltGroup&st.cmd=altGroupMain&st.directLink=on&st.groupId='. $this->__group_id. '&st.altGroup.action=groupJoin&st._aid=LBAID_UCM_AltGroupTopCardButtonsJoin_altGroupMain&gwt.requested='. $this->__gwt. '&p_sId=');

$response = curl_exec($ch);
curl_close($ch);

return;
}

private function link_preview()
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $this->browser);

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'linkUrl='. $this->post_link. '&LinkPreviewId=5.linkPreview');

curl_setopt($ch, CURLOPT_COOKIE, 'JSESSIONID='. $this->cookie);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('TKN: '. $this->path_info['tkn']));

curl_setopt($ch, CURLOPT_URL, 'http://www.odnoklassniki.ru'. $this->path. '?cmd=LinkPreview&gwt.requested='. $this->path_info['gwt']. '&st.cmd=altGroupMain&st.directLink=on&st.groupId='. $this->path_info['group_id']);

$response = curl_exec($ch);

curl_close($ch);

preg_match('!style="display: none" class="data-with-image_i" src="([^"]*)"!', $response, $thumb);
preg_match('!,"7":"([^"]*)",!', $response, $title);
preg_match('!,"8":"([^"]*)",!', $response, $description);
preg_match('!,"11":"([^"]*)"!', $response, $url);

if(!$thumb[1] or !$title[1] or !$url[1])
{
return;
}

return '{"linkData":{"title":"'.$title[1].'", "description":"'.$description[1].'", "providerViewer":"1;1", "sourceUrl":"'.$url[1].'", "thumbnail":"'.$thumb[1].'", "movieId":0}}';
}

private function post()
{
$link_preview = $this->link_preview();

if(!$link_preview)
{
return 'php.noLinkPreview';
}

$form_data = '{"formType":"Group", "postDataList":['. $link_preview. '], "news":false, "toStatus":false}';

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $this->browser);

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'st.status.postpostForm='. $this->path_info['form_id'].
'&st.status.postgroupId='. $this->path_info['group_id'] .
'&postingFormData='. urlencode($form_data));

curl_setopt($ch, CURLOPT_COOKIE, 'JSESSIONID='. $this->cookie);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('TKN: '. $this->path_info['tkn']));

curl_setopt($ch, CURLOPT_URL, 'http://www.odnoklassniki.ru'. $this->path. '?cmd=MediaTopicPost&gwt.requested='. $this->path_info['gwt']. '&st.cmd=altGroupMain&st.directLink=on&st.groupId='. $this->path_info['group_id']. '&p_sId=');

$response = curl_exec($ch);

curl_close($ch);

if(empty($response))
{
return false;
}

if(strpos($response, '"status":"ok"') !== false)
{
return true;
}

preg_match('!"errorCodeValue":"([^"]*)"!', $response, $error);

if($error[1])
{
return $error[1];
}

return false;
}

private function path_info($path, $cookie)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $this->browser);

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'true=true');

curl_setopt($ch, CURLOPT_COOKIE, 'JSESSIONID='. $cookie);

curl_setopt($ch, CURLOPT_URL, 'http://www.odnoklassniki.ru'. $path);

$response = curl_exec($ch);

curl_close($ch);

if(strpos($response, 'HTTP/1.1 404 ') !== false)
{
return 'php.groupRemoved';
}

if(strpos($response, 'Location: www.odnoklassniki.ru/dk?st.cmd=altGroupRestricted') !== false)
{
return 'php.userBlocked';
}

preg_match('!state:"st\.cmd=altGroupMain&st\.groupId=([^"]*)"!', $response, $group_id);
preg_match('!,"s2":"([^"]*)",!', $response, $form_id);
preg_match('!TKN: (.*?)$!m', $response, $tkn);
preg_match('!,gwtHash:"([^"]*)",!', $response, $gwt);
preg_match('!<\!--{"gId":"([^"]*)",!', $response, $id);

if(!$group_id[1] or !$tkn[1] or !$gwt[1] or empty($group_id[1]) or empty($tkn[1]) or empty($gwt[1]))
{
return false;
}

$this->__gwt = $gwt[1];
$this->__tkn = $tkn[1];
$this->__group_id = $group_id[1];

if(!$form_id[1] or empty($form_id[1]) or !$id[1] or empty($id[1]))
{
return false;
}

return array('group_id' => $group_id[1], 'form_id' => $form_id[1], 'tkn' => $tkn[1], 'gwt' => $gwt[1], 'id' => $id[1]);
}

private function login()
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $this->browser);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'st.email='.urlencode($this->username).'&st.password='.urlencode($this->password).'&st.redirect=/ok&st.posted=set&st.screenSize=&st.screenSize=&st.browserSize=&st.flashVer');

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, 'http://www.odnoklassniki.ru/dk?cmd=AnonymLogin&st.cmd=anonymLogin&tkn='.mt_rand());

$response = curl_exec($ch);

curl_close($ch);

if(!preg_match('!Location: www.odnoklassniki.ru/ok!', $response))
{
return false;
}

preg_match('!JSESSIONID=([^;]*);!', $response, $cookie);

if(!$cookie[1] or empty($cookie[1]))
{
return false;
}

return $cookie[1];
}

private function test_auth()
{
if(!$this->auth_hash)
{
return false;
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36');

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_COOKIE, 'JSESSIONID='. $this->auth_hash);

curl_setopt($ch, CURLOPT_URL, 'http://ok.ru/settings');

$response = curl_exec($ch);

curl_close($ch);

if(strpos($response, 'HTTP/1.1 302') !== false)
{
return false;
}

return true;
}

static function auth($username, $password)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36');

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'st.email='.urlencode($username).'&st.password='.urlencode($password).'&st.redirect=/ok&st.posted=set&st.screenSize=&st.screenSize=&st.browserSize=&st.flashVer');

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, 'http://www.odnoklassniki.ru/dk?cmd=AnonymLogin&st.cmd=anonymLogin&tkn='.mt_rand());

$response = curl_exec($ch);

curl_close($ch);

if(!preg_match('!Location: www.odnoklassniki.ru/ok!', $response))
{
return false;
}

preg_match('!JSESSIONID=([^;]*);!', $response, $cookie);

if(!$cookie[1] or empty($cookie[1]))
{
return false;
}

return $cookie[1];
}
}
Вы не можете отвечать в этой теме.
Войдите или зарегистрируйтесь, чтобы писать на форуме.
Используя этот сайт, вы соглашаетесь с тем, что мы используем файлы cookie.