Created
January 25, 2018 15:48
-
-
Save Sentinel-7/b56ae02f2cb863c19438fe787d2ecff3 to your computer and use it in GitHub Desktop.
Grabber
This file contains 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 | |
define('ROOT', MODX_BASE_PATH); | |
define('EX', 'jpg,png,gif,jpeg,doc,xls,zip,pdf'); // Через запятую имена расширений (нижний регистр) | |
$folder=isset($folder) ? $folder : "assets/images/"; // папка назначения стыренного контента | |
if(!is_dir(MODX_BASE_PATH.$folder)) mkdir(MODX_BASE_PATH.$folder); | |
// Функция траслита | |
function rus2translit($string) { | |
$converter = array( | |
'а' => 'a', 'б' => 'b', 'в' => 'v', | |
'г' => 'g', 'д' => 'd', 'е' => 'e', | |
'ё' => 'e', 'ж' => 'zh', 'з' => 'z', | |
'и' => 'i', 'й' => 'y', 'к' => 'k', | |
'л' => 'l', 'м' => 'm', 'н' => 'n', | |
'о' => 'o', 'п' => 'p', 'р' => 'r', | |
'с' => 's', 'т' => 't', 'у' => 'u', | |
'ф' => 'f', 'х' => 'h', 'ц' => 'c', | |
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', | |
'ь' => '\'', 'ы' => 'y', 'ъ' => '\'', | |
'э' => 'e', 'ю' => 'yu', 'я' => 'ya', | |
'А' => 'A', 'Б' => 'B', 'В' => 'V', | |
'Г' => 'G', 'Д' => 'D', 'Е' => 'E', | |
'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', | |
'И' => 'I', 'Й' => 'Y', 'К' => 'K', | |
'Л' => 'L', 'М' => 'M', 'Н' => 'N', | |
'О' => 'O', 'П' => 'P', 'Р' => 'R', | |
'С' => 'S', 'Т' => 'T', 'У' => 'U', | |
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', | |
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch', | |
'Ь' => '\'', 'Ы' => 'Y', 'Ъ' => '\'', | |
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', | |
); | |
return strtr($string, $converter); | |
} | |
function str2url($str) { | |
// переводим в транслит | |
$str = rus2translit($str); | |
// в нижний регистр | |
$str = strtolower($str); | |
// заменям все ненужное нам на "-" | |
$str = preg_replace('~[^-a-z0-9_]+~u', '-', $str); | |
// удаляем начальные и конечные '-' | |
$str = trim($str, "-"); | |
return $str; | |
} | |
function clearUrl ($url){ | |
$pieces = explode("#", $url); | |
$url=$pieces[0]; | |
$pieces = explode("?", $url); | |
$url=$pieces[0]; | |
return end(explode(".", $url)); | |
} | |
function captor($value,$folder) | |
{ | |
//Заветная регулярка | |
preg_match_all('#(?<!\])\bhttp://[^\s\[<]+\b#i', $value, $matches); | |
foreach ($matches[0] as $srcOrig) | |
{ | |
$src = urldecode($srcOrig); | |
$info = pathinfo($src); | |
if (in_array(mb_strtolower($info['extension']),explode(',',EX))) | |
{ | |
// Операции по копированию | |
$filename = @basename($src,'.'.$info['extension']); | |
$name = ROOT.''.$folder.''.str2url(trim($filename)).''.mt_rand().'.'.$info['extension']; | |
@copy($src,$name); | |
$nameArr = explode('/',$name); | |
$name = './'.$folder.''.array_pop($nameArr); | |
$value = str_replace($srcOrig,$name,$value); | |
} | |
} | |
return $value; | |
} | |
if(!empty($resource->get("uri"))&&(empty($resource->get("content")))){ | |
require_once(MODX_BASE_PATH."phpQuery.php"); | |
$doc = phpQuery::newDocument(file_get_contents("http://alekspodshipnik.ru/".$resource->get("uri")));//Смените адрес | |
$content=pq(".p-con")->html();//Контейнер с контентом | |
$title=pq("title")->text(); | |
$kwrds=pq("meta[name=keywords]")->attr("content"); | |
$description=pq("meta[name=description]")->attr("content"); | |
$resource->set("content",$content); | |
$resource->setTVValue("seo-title",$title); | |
$resource->setTVValue("seo-keywords",$kwrds); | |
$resource->setTVValue("seo-description",$description); | |
/*Вставляем фото с галереи в ms2Gallery | |
$modx->ms2Gallery = $modx->getService('ms2gallery', 'ms2Gallery', MODX_CORE_PATH . 'components/ms2gallery/model/ms2gallery/'); | |
foreach(pq(".photo-block a.fancy") as $link){//Смените идентификатор | |
$src=pq($link)->attr("href"); | |
$img_title=pq($link)->attr("title"); | |
$info = pathinfo($src); | |
print_r($info); | |
$filename = @basename($src,'.'.$info['extension']); | |
$name = ROOT.''.$folder.''.str2url(trim($filename)).''.mt_rand().'.'.$info['extension']; | |
@copy($src,$name); | |
$response = $modx->runProcessor('gallery/upload', array( | |
'name'=>$img_title, | |
'file' => $name, // Картинка | |
'id' => $resource->get("id") // id ресурса с галереей | |
), | |
array('processors_path' => MODX_CORE_PATH.'components/ms2gallery/processors/mgr/') | |
); | |
// | |
}*/ | |
} | |
// Забираем фотки из контентной части | |
$content = captor($resource->get("content"),$folder); | |
$resource->set("content",$content); | |
$resource->save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment