Created
May 6, 2016 10:51
-
-
Save ilyautkin/815eb13a26b3ccad6cbcfee25d01cf7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$notifications_email = '[email protected]'; | |
$appid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
$oauth_link = 'https://oauth.yandex.ru/verification_code#access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&token_type=bearer&expires_in=15552000'; | |
switch($modx->event->name) { | |
case 'OnDocFormSave': | |
case 'OnDocPublished': | |
case 'OnDocUnPublished': | |
$site_url = $modx->getOption('site_url'); | |
if ($curl = curl_init()) { | |
curl_setopt($curl, CURLOPT_URL, 'https://webmaster.yandex.ru/api/v2/hosts'); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Authorization: OAuth '.$modx->getOption('yandex_oauth_token') | |
)); | |
$out = curl_exec($curl); | |
curl_close($curl); | |
$hosts = new SimpleXMLElement($out); | |
foreach($hosts as $host) { | |
if (strpos($host->name, str_replace(array('http://','https://','/'), '', $site_url)) !== false) { | |
$site_id = str_replace('https://webmaster.yandex.ru/api/v2/hosts/', '', $host['href']); | |
break; | |
} | |
} | |
} | |
if ($site_id && $curl = curl_init()) { | |
curl_setopt($curl, CURLOPT_URL, 'https://webmaster.yandex.ru/api/v2/hosts/'.$site_id); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Authorization: OAuth '.$modx->getOption('yandex_oauth_token') | |
)); | |
$out = curl_exec($curl); | |
curl_close($curl); | |
$links = new SimpleXMLElement($out); | |
foreach($links as $link) { | |
if (strpos($link['rel'], 'original-texts') !== false) { | |
$origLink = $link['href']; | |
break; | |
} | |
} | |
} | |
if ($resource->get('published')) { | |
$content = strip_tags($resource->get('content')); | |
$content = str_replace(array('&','"', '\''), array('&', '"','''), $content); | |
$content = '<original-text><content>'.$content.'</content></original-text>'; | |
$content = urlencode($content); | |
$contentLength = strlen($content); | |
if ($mode == "upd") { | |
$textID = $resource->getProperty('textID', 'yandex-original-texts'); | |
if ($textID && $origLink && $site_id && $curl = curl_init()) { | |
curl_setopt($curl, CURLOPT_URL, $origLink.$textID); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Host: webmaster.yandex.ru', | |
'Authorization: OAuth '.$modx->getOption('yandex_oauth_token') | |
)); | |
$out = curl_exec($curl); | |
curl_close($curl); | |
} | |
} | |
if ($origLink && $site_id && $curl = curl_init()) { | |
curl_setopt($curl, CURLOPT_URL, $origLink); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $content); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Host: webmaster.yandex.ru', | |
'Authorization: OAuth '.$modx->getOption('yandex_oauth_token'), | |
'Content-Length: '.$contentLength | |
)); | |
$out = curl_exec($curl); | |
$modx->log(1, $out); | |
curl_close($curl); | |
$text = new SimpleXMLElement($out); | |
$textID = (string) $text->id[0]; | |
$resource->setProperty('textID', $textID, 'yandex-original-texts'); | |
$resource->save(); | |
} | |
} else { | |
$textID = $resource->getProperty('textID', 'yandex-original-texts'); | |
if ($textID && $origLink && $site_id && $curl = curl_init()) { | |
curl_setopt($curl, CURLOPT_URL, $origLink.$textID); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Host: webmaster.yandex.ru', | |
'Authorization: OAuth '.$modx->getOption('yandex_oauth_token') | |
)); | |
$out = curl_exec($curl); | |
curl_close($curl); | |
$resource->setProperty('textID', '', 'yandex-original-texts'); | |
$resource->save(); | |
} | |
} | |
break; | |
default: | |
break; | |
} | |
return ''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment