123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821 |
- <?php
- namespace app\models;
- use app\helpers\Transliterator;
- use app\models\base\Comments;
- use app\models\base\Gallery;
- use app\models\base\Image;
- use app\models\base\OldNews;
- use DOMDocument;
- use yii\db\ActiveQuery;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Inflector;
- use yii;
- use app\components\behaviors\CachedBehavior;
- use yii\helpers\Url;
- /**
- * @property string $url
- * @property $mainPic
- * @property Image $preview
- * @property Image $image
- * @property OldNews $old
- * @property string $publishedAt
- * @property Comments[] $commentsAll
- * @property Gallery[] $galleries
- */
- class News extends \app\models\base\News
- {
- /*
- * Ключи кэширования для авто сброса
- */
- public static $keysCache=[
- 'MainPageBlock' => 120,
- 'main_page_lenta' =>120,
- 'main-page-main-view-glob' => 60, //600
- 'main-page-main-view' => 60,
- 'topic-news-##' => 3600
- ];
- private static function processPersons(string $body)
- {
- $re = '/<a.*class=[\'"](person|person-inject)[\'"].*href=[\'"]\/person\/(.+)[\'"].*>(\W+)<\/a>/m';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $person = Person::find()->andWhere(['alias'=>ArrayHelper::getValue($matches,'2')])->one();
- if($person instanceof Person){
- switch (ArrayHelper::getValue($matches,'1')) {
- case "person-inject":
- return \Yii::$app->view->render("/news/view/inject-person-as-widget", ["person" => $person]);
- break;
- default:
- return \Yii::$app->view->render("/news/view/inject-person-as-link", ["person" => $person]);
- }
- }
- return ArrayHelper::getValue($matches,0);
- }, $body);
- return $res;
- }
- private static function processBody($body,$model)
- {
- $counter = 0;
- $re = '/<(?:p|h2).*>(.*)<\/(?:p|h2)>/msU';
- $res = preg_replace_callback($re,function (array $matches) use(&$counter,$model): string
- {
- $counter++;
- if($counter==2 && Yii::$app->deviceDetect->isMobile() && !$model->inTopic(158)) return $matches[0].Yii::$app->controller->renderPartial("@app/views/_etc/banners/adfinityInContent");
- if($counter==3 && !Yii::$app->deviceDetect->isMobile() && !$model->inTopic(158)) return $matches[0].Yii::$app->controller->renderPartial("@app/views/_etc/banners/desktopInContent");
- return $matches[0];
- },$body);
- return $res;
- }
- private static function processGalleriesInjects(string $body, News $post)
- {
- $re = '/<p>(##_gallery-(\d+)##)<\/p>/mU';
- $body = preg_replace( $re, '\1', $body );
- $re = '/##_gallery-(\d+)##/mU';
- $res = preg_replace_callback($re,function (array $matches) use($post): string
- {
- $gallery = \app\models\front\Gallery::findOne(['id'=>ArrayHelper::getValue($matches,1)]);
- if($gallery instanceof \app\models\front\Gallery) {
- return \Yii::$app->view->render("/news/view/inject-gallery-x",["gallery"=>$gallery, "model"=>$post]);
- } else {
- return "";
- }
- },$body);
- return $res;
- }
- public function getUrl($full = false):string
- {
- if(count($this->topics)>0){
- $topic = Transliterator::toUrl($this->topics[0]->title)."/";
- } else{
- $topic = "";
- }
- switch ($this->type){
- // case 6:
- // return "https://old.amic.ru/long/{$this->id}/";
- // break;
- default:
- $base = Url::base('https');
- $alias = trim( $this->alias );
- if( $alias == '' ){
- $alias = Transliterator::toUrl($this->title);
- }
- if( isset( $this->dt_pub ) && strtotime( $this->dt_pub ) > strtotime('2023-01-27 15:00:00') ){
- return ($full ? $base : "")."/news/".$alias."-".$this->id;
- }
- return ($full ? $base : "")."/news/".$topic.$alias."-".$this->id;
- }
- }
- public function getMainPic(){
- }
- /**
- * @return Image
- */
- public function getPreview($type="webp"):Image{
- return Image::findOne($this->id,$type,$this->photo_name);
- }
- public function getOld():ActiveQuery
- {
- return $this->hasOne(OldNews::class,['alias'=>'alias']);
- }
- /**
- * @return ActiveQuery
- */
- public function getCommentsAll(): ActiveQuery
- {
- if($this->comments=="Y"){
- return $this->hasMany(Comments::class,['news_id'=>"id"])->andWhere(['visible'=>'Y']);
- }
- return $this->hasMany(Comments::class,['news_id'=>"id"])->andWhere(0);
- }
- public static function getMainView(){
- return self::find()->joinWith("topics t")->andWhere(["t.id"=>[33,]]);
- }
- public static function getNH(){
- return self::find()->andWhere(['active'=>'Y', 'NH'=>['Y','F']])->andWhere(['between', 'dt_pub', date("Y-m-d H:i:00",time()-2678400*3 ) , date("Y-m-d H:i:00")])->orderBy(["NH"=>SORT_DESC,"dt_pub"=>SORT_DESC])->limit(1);
- }
- public static function getPartnersNews(){
- return self::find()->joinWith("topics t")->andWhere(["t.id"=>[34,]]);
- }
- public static function getMainOfWeek(){
- return self::find()->joinWith("topics t")->andWhere(["t.id"=>[35,]]);
- }
- public function getDatePub(){
- return $this->dt_pub;
- }
- public function getPublishedAt(){
- if($this->dt_pub<date("Y-m-d H:i:s",strtotime("-1 day"))){
- return
- date("d",strtotime($this->dt_pub))." ".mb_strtolower(Transliterator::month(date("n",strtotime($this->dt_pub))))
- .date(" Y, H:i", strtotime($this->dt_pub))
- ;
- } else {
- $diff = ceil((time() - strtotime($this->dt_pub))/60); //В минутах
- if($diff<=60){
- return Transliterator::plural($diff,['минуту','минуты', 'минут'],true,'только что','минуту')." назад";
- } else {
- $diff = (int)floor($diff/60);
- return Transliterator::plural($diff,['час','часа', 'часов'],true,'только что','час')." назад";
- }
- }
- }
- public function getPublishedNorm(){
- return
- date("d",strtotime($this->dt_pub))." ".mb_strtolower(Transliterator::month(date("n",strtotime($this->dt_pub))))
- .date(" Y, H:i", strtotime($this->dt_pub))
- ;
- }
- public function isPhotosOnNews( $dt_pub = null )
- {
- if( $this->inscription) return true;
- if( isset( $this->photo_title ) && stripos($this->photo_title, 'amic.ru') !== false ) return true;
- if( $this->isAmicPhoto() ) return true;
- if( $dt_pub ){
- $dt_pub = strtotime($dt_pub);
- }else{
- $dt_pub = strtotime($this->dt_pub);
- }
- if( $dt_pub >= strtotime(Yii::$app->params['delPhotoB']) && $dt_pub < strtotime(Yii::$app->params['delPhotoE']) ) return false;
- return true;
- }
- public function renderBody()
- {
- $post = $this;
- $device = Yii::$app->deviceDetect->isMobile()?"mobile":"desktop";
- return \Yii::$app->cache->getOrSet("post-body8-".$this->id."-".$device,function () use ($post){
- $body = $post->text;
- //Нужно воткнуть рекламу после второго абзаца
- $body = self::processBody($body,$this);
- // $body = self::processSingleImg($body);
- $body = $this->DateDelImg($body);
- $body = self::processTextImg($body);
- $body = self::processInjects($body);
- $body = self::processAudio($body);
- if( $this->isPhotosOnNews($post->getDatePub() ) ){
- $body = self::processSlider($body, $post); //old slider
- $body = self::processGalleriesInjects($body, $post);
- }
- $body = self::processYoutube($body);
- $body = self::processIframe($body);
- $body = self::processSpecialFormats($body);
- $body = self::processPersons($body);
- $body = self::processTest($body);
- $body = self::processApiImg($body);
- $body = self::ShowPollsWidget($body);
- $body = self::ShowPreportWidget($body);
- if( $post->inscription == 0 ) $body = self::processAhref($body); // кроме комерческих
- return $body;
- },YII_ENV_DEV?1:60);
- }
- public static function processApiImg($text)
- {
- return str_replace( 'https://api.amic.ru', '', $text);
- }
- public static function processTest($text)
- {
- $text = str_replace("\xc2\xa0", ' ', $text);
- $text = str_replace(['<p> </p>','<p> </p>'], "\c", $text);
- $text = trim( $text );
- $text = str_replace( "\c", "<p></p>", $text);
- $text = str_replace( 'href="http://tel=', 'href="tel:', $text );
- return $text;
- }
- public static function processSpecialFormats($text)
- {
- if( stripos($text, 'class="juxtapose"') !== false ){
- $str = '
- <script src="/js/juxtapose/juxtapose.min.js"></script>
- <link rel="stylesheet" href="/js/juxtapose/juxtapose.css">
- ';
- return $text.$str;
- }
- return $text;
- }
- /*
- * Парсинг для старых слайдеров (очень старых)
- */
- public static function processSlider($text, $obj)
- {
- if( $obj->uid == '' ) return $text;
- $dir = '/images/items/'.$obj->uid;
- $files1 = @scandir(Yii::getAlias('@webroot').$dir.'/thumbnails/',0);
- $old_title = ( trim($obj->old_gallery_title) != '' )?''.trim($obj->old_gallery_title).'':'';
- if( $files1 == false || count( $files1 ) < 4){
- $files1 = @scandir(Yii::getAlias('@webroot').$dir.'/images/thumbnails/', 0);
- if( $files1 === false || count( $files1 ) == 0){
- return $text;
- }else{
- $i=1;
- ob_start();
- foreach( $files1 as $file ){
- if( preg_match('/^.*\.(gif|jpe?g|png)/i', $file ) ){
- $str = @file_get_contents( Yii::getAlias('@webroot').$dir.'/info/'.$file.'.json' );
- if( $str == false ){
- $str='{"title":"","copyrate":""}';
- }
- $js_title = json_decode( $str );
- $str_title = ( $js_title->title == '' )?$old_title:$js_title->title;//iconv('UTF-8','cp1251',$js_title->title);
- $str_title .= ( $js_title->copyrate == '' )?'':'<br><span class="style8">'.$js_title->copyrate.'</span>';
- $add_cap = ($str_title != '')?", caption: '".$str_title."'":'';
- ?>
- {img: '<?=$dir?>/images/<?=$file?>', thumb: '<?=$dir?>/images/thumbnails/<?=$file?>' <?=$add_cap?>},
- <?
- }
- }
- $ltext = ob_get_contents();
- ob_end_clean();
- }
- }else{
- $i=1;
- ob_start();
- foreach( $files1 as $file ){
- if( preg_match('/^.*\.(gif|jpe?g|png)/i', $file ) ){
- ?>
- {img: '<?=$dir?>/big/<?=$file?>', thumb: '<?=$dir?>/thumbnails/<?=$file?>'},
- <?
- }
- }
- $ltext = ob_get_contents();
- ob_end_clean();
- }
- ob_start();
- if( isset( $ltext ) ){
- if( strstr( $text, 'id="mycarousel"') === false ){
- echo '<div id="mycarousel"></div>';
- }
- ?>
- <!-- Fotorama -->
- <?
- $c = Yii::$app->acache;
- $c->set('fotorama', true);
- ?>
- <!-- <div class="fotorama" data-width="700" data-ratio="700/467" data-max-width="100%"></div>-->
- <script type="text/javascript">
- window.addEventListener("DOMContentLoaded",function () {
- $('#mycarousel').fotorama({ width:'100%',maxwidth:780,thumbheight:70,thumbwidth:94, captions: true,nav:'thumbs', caption: 'overlay', allowfullscreen:'native', data: [ <?=$ltext?> ] });
- });
- </script>
- <?
- }
- $ltext = ob_get_clean();
- return $ltext.$text;
- }
- public static function processAudio($text)
- {
- $re = '/<a([^>]+)href=\"([^>]*\/files\/)(.+)\.mp3\"([^>]*)>([^<]*)<\/a>/i';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $title = isset($matches[5])?$matches[5]:'';
- if( !strstr( $matches[1], "download") ) {
- $url = $matches[2];
- $audio = '<source src="'.$url.$matches[3].'.mp3" type="audio/mpeg">'."\n";
- if( file_exists( $_SERVER['DOCUMENT_ROOT'].''.$matches[2].$matches[3].'.mp3.ogg' ) ){
- $audio .= '<source src="'.$url.$matches[3].'.mp3.ogg" type="audio/ogg; codecs=vorbis">'."\n";
- }
- return \Yii::$app->view->render("/news/view/audio",["audio"=>$audio, "title"=>$title]);
- } else {
- return ArrayHelper::getValue($matches,0);
- }
- },$text);
- return $res;
- }
- /*
- * Спрятать внешнии ссылки от поисковиков
- * Псевдо SEO
- *
- */
- public static function processAhref($text)
- {
- $re = '/<a[^>]+href=\"(https?:\/\/[^"]*)\".*>/Ui';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $mydomain = \Yii::$app->params['mydomain'];
- foreach( $mydomain as $find ){
- if( stripos( $matches[1], $find) !== false ) return $matches[0];
- }
- $ret = str_replace( $matches[1], "/go/?u=".urlencode($matches[1]), $matches[0] );
- return $ret;
- },$text);
- return $res;
- }
- public static function processInjects($text)
- {
- $re = '/##news_(.*)##/mU';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $post = News::findOne(['uid'=>ArrayHelper::getValue($matches,1)]);
- if($post instanceof News) {
- return \Yii::$app->view->render("/news/view/inject",["post"=>$post]);
- } else {
- return "";
- }
- },$text);
- if( strlen($res) != strlen($text) ) return $res;
- $re = "|<div[^>]+>.[^<]*</div>|U";
- $res = preg_replace_callback($re,function (array $matches): string
- {
- @$xml = simplexml_load_string( str_replace( ' ', '', ArrayHelper::getValue($matches,0) ), 'SimpleXMLElement', LIBXML_NOCDATA );
- if( $xml ){
- $attributes =$xml->attributes();
- if( $attributes['class'] == 'insinject' || $attributes['id'] == "inject" ){
- $r = preg_match( "/^https?:\/\/(.*)\/(.*)\/(\d+)\/$/i", $attributes['url'], $aa );
- $id = 0;
- if( $r && ($aa[1] == 'www.amic.ru' || $aa[1] == Yii::$app->request->serverName) && $aa[3]*1 > 0 ){
- // old style inject
- $id = $aa[3]*1;
- }
- $r = preg_match( "/^https?:\/\/(.*)\/.*(\d+)\/?$/Ui", $attributes['url'], $aa );
- if( $r && ($aa[1] == Yii::$app->request->serverName || $aa[1] == 'www.amic.ru')&& $aa[2]*1 > 0 ){
- // new style inject
- $id = $aa[2]*1;
- }
- $r = preg_match( '/^https?:\/\/(.*)\/person\/(.*)\/?$/Ui', $attributes['url'], $aa );
- if( $r && ($aa[1] == Yii::$app->request->serverName || $aa[1] == 'www.amic.ru')&& $aa[2] != '' ){
- $person = \app\models\Person::findOne(['alias'=>$aa[2]]);
- if( ( !$person instanceof Person ) || $person->show == 'N' ){
- return "";
- }
- return \Yii::$app->view->render("/news/view/injectp",["post"=>$person]);
- }
- if( $id ){
- //$attributes['type'] to do
- $post = News::findOne(['id'=>$id]);
- if($post instanceof News) {
- if( $attributes['type'] == 1 ){
- return \Yii::$app->view->render("/news/view/inject1",["post"=>$post]);
- }else if( $attributes['type'] == 2 ){
- return \Yii::$app->view->render("/news/view/inject2",["post"=>$post]);
- }else{
- return \Yii::$app->view->render("/news/view/inject",["post"=>$post]);
- }
- } else {
- return "Битый инжект";
- }
- }
- }
- }
- return ArrayHelper::getValue($matches,0);
- },$text);
- return $res;
- }
- public static function processIframe($text) //https://youtu.be/sfzX5fMaSj4 -> https://www.youtube.com/embed/sfzX5fMaSj4
- {
- $re = '/(<iframe[^>]*>).*(<\/iframe>)/Ui';
- $text = preg_replace_callback($re,function (array $mt): string
- {
- //print_r($mt);
- // libxml_use_internal_errors(true);
- $content = $mt[0];
- $content = str_replace( '&', '#-#', $content);
- $HXML='<?xml version="1.0" encoding="UTF-8"?>';
- $doc = new DOMDocument('1.0','UTF-8'); // нормализация xml
- @$doc->loadHTML($HXML.$content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOCDATA);
- // $doc->encoding = 'UTF-8';
- $doc->normalizeDocument();
- $content = $doc->saveXML($doc->documentElement);
- $content = html_entity_decode($content);
- $content = str_replace( '<?xml version="1.0" standalone="yes"?>', '', $content);
- $content = str_replace( '<?xml version="1.0" encoding="UTF-8"??>', '', $content);
- $content = str_replace( '<?xml version="1.0" encoding="UTF-8"?>', '', $content);
- $xml = simplexml_load_string( $content, 'SimpleXMLElement',LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL |LIBXML_HTML_NOIMPLIED );
- if( $xml ){
- $attrs = $attributes =$xml->attributes();
- //print_r( $attrs );
- $xmlo = simplexml_load_string( $HXML.'<iframe loading="lazy"> </iframe>', 'SimpleXMLElement', LIBXML_NOCDATA );
- // var_dump($attrs);
- if( !Yii::$app->deviceDetect->isMobile() ){
- if( $attrs['src'] && strpos( $attrs['src'], 'vk.com' ) !== false ) $fixedRatio = '16/9';
- }
- foreach( $attrs as $key=>$attr ){
- if( $key == 'height'){
- $height = preg_replace('/[\D]/', '', $attr);
- if( $height == 0 ) $height = 0.1;
- $attr = 'auto';
- }
- if( $key == 'width'){
- $width = preg_replace('/[\D]/', '', $attr);
- if( strpos($attr, '%') !== false ){
- $width = $width*720/100;
- }
- $attr = '100%';
- }
- if( isset($height) && isset($width) ){
- if( isset( $fixedRatio ) ){
- $ratio = $fixedRatio;
- }else{
- $ratio = round( $width/$height, 2 );
- }
- if( !isset($xmlo->attributes()->style) ){
- $xmlo->addAttribute('style', 'width:100%; aspect-ratio:'.$ratio);
- }else{
- $xmlo->attributes()->style = $xmlo->attributes()->style.'width:100%; aspect-ratio:'.$ratio;
- }
- unset($height);
- unset($width);
- }
- @$xmlo->addAttribute($key, $attr);
- }
- return str_replace( '#-#', '&', preg_replace('|^\<\?xml version="1.0" encoding="UTF-8"\?\>\n|i', '',$xmlo->asXML()) );
- }
- return $mt[0];
- }, $text);
- return $text;
- }
- /*
- * Парсинг короткой ссылки ютуба
- *
- */
- public static function processYoutube($text) //https://youtu.be/sfzX5fMaSj4 -> https://www.youtube.com/embed/sfzX5fMaSj4
- {
- return str_replace( 'https://youtu.be/', 'https://www.youtube.com/embed/', $text );
- }
- public static function processSingleImg($text)
- {
- $re = '/(<[p|a][^>]*>)(<img.*>)(<\/[p|a]>)/i';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $img = ArrayHelper::getValue($matches,2);
- $a = array();
- $title = '';
- if( preg_match( '/title="(.*)"/mU', $img, $a) ){
- $title = '<div class="image-title" style="margin-top: 20px;">'.$a[1].'</div>';
- }
- if( $title && preg_match( '/rel-credit="(.*)"/mU', $img, $a) ){
- if( trim( $a[1] ) != '' ){
- $title = str_replace('</div>', ' фото: '.$a[1].'</div>', $title);
- }
- }
- if( $title && preg_match( '/longdesc="(.*)"/mU', $img, $a) ){
- $title = str_replace('</div>', ' <a href="'.$a[1].'">'.$a[1].'</a></div>', $title);
- }
- $style = '';
- $a = array();
- if( preg_match( '/style="(.*)"/mU', $img, $a) ){
- $style = $a[1];
- $style = str_replace('height', 'data-height', $style);
- $img = str_replace('margin', 'margin-block', $img);
- }
- //class='picture-cont-16x9'
- $pic= "<div style='{$style}' class=\"pic\">
- <picture class='w-100'>
- {$img}
- </picture>
- {$title}
- </div>
- ";
- return str_replace('<p','<div',ArrayHelper::getValue($matches,1)).$pic.str_replace('p>','div>',ArrayHelper::getValue($matches,3));
- },$text);
- return $res;
- }
- public function DateDelImg($text)
- {
- $obj = $this;
- $re = '/(<img[^>]*>)/i';
- $text = preg_replace_callback($re,function (array $mt) use($obj): string
- {
- /*
- После этой даты не выводить фото до подтверждения юриста
- */
- if( !$this->isPhotosOnNews( $obj->getDatePub() ) ){
- $bad = '/images/default.jpg';
- return '<span><picture class="w-100"><img src="'.$bad.'" width="100%"/></picture></span>';
- };
- return $mt[0];
- }, $text);
- return $text;
- }
- public static function processTextImg($text)
- {
- $text = str_replace('src="http://', 'src="https://', $text); //в качестве бреда
- $text = str_replace( 'publib/gimage.php?image=/', '', $text ); // очень очень старый движ
- $re = '/(<img[^>]*>)/i';
- $text = preg_replace_callback($re,function (array $mt): string
- {
- //print_r($mt);
- libxml_use_internal_errors(true);
- $staic = false;
- $content = $mt[0];
- $content = str_replace('&','$%$',$content);
- $HXML='<?xml version="1.0" encoding="UTF-8"?>';
- $doc = new DOMDocument('1.0','UTF-8'); // нормализация xml
- $doc->loadHTML($HXML.$content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOCDATA);
- // $doc->encoding = 'UTF-8';
- $doc->normalizeDocument();
- $content = $doc->saveXML($doc->documentElement);
- $content = html_entity_decode($content);
- $content = str_replace( '<?xml version="1.0" standalone="yes"?>', '', $content);
- $content = str_replace( '<?xml version="1.0" encoding="UTF-8"??>', '', $content);
- $content = str_replace( '<?xml version="1.0" encoding="UTF-8"?>', '', $content);
- $xml = simplexml_load_string( $content, 'SimpleXMLElement',LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL |LIBXML_HTML_NOIMPLIED );
- if( $xml ){
- $attrs = $attributes =$xml->attributes();
- //print_r( $attrs );
- $xmlo = simplexml_load_string( $HXML.'<span><picture class="w-100"><img /></picture></span>', 'SimpleXMLElement', LIBXML_NOCDATA );
- // var_dump($attrs);
- $url = isset($attrs->longdesc)?$attrs->longdesc:'';
- foreach( $attrs as $key=>$attr ){
- if( $key == 'height'){
- $xmlo->addAttribute('data-height', $attr);
- continue;
- }
- if( $key == 'style'){
- $attr = trim( $attr, " ;\r\n" );
- //$attr = str_replace(' ', '', $attr);
- $css = explode( ";", $attr );
- $stylesd = array();
- $stylesi = array();
- foreach( $css as $attrx ){
- list($key1, $value) = explode( ":", $attrx );
- $key1 = trim($key1);
- if( $key1 == 'height' || $key1 == '' ){
- continue;
- }elseif( stristr( 'margin', $key1 ) !== false ){
- $stylesd[] = "{$key1}:{$value}";
- }elseif( $key1 == 'float' ){
- $stylesd[] = "{$key1}:{$value}";
- }elseif( $key1 == 'width' ){
- //$value = preg_replace("/[^0-9\-.]/", '', $value);
- if( preg_replace("/[^0-9\-.]/", '', $value) > 720-150 ){
- $value = '100%';
- }
- $stylesd[] = "{$key1}:{$value}";
- $stylesi[] = "{$key1}:100%";
- $stylesd[] = "display:block";
- }elseif( $key1 && $value){
- $stylesd[] = "{$key1}:{$value}";
- if( $key1 == 'margin-right' || $key1 == 'margin-left' ) $value = 0;
- if( $key1 != 'margin' ) {
- $stylesi[] = "{$key1}:{$value}";
- }
- }
- }
- $attr = implode('; ', $stylesd);
- $xmlo->addAttribute($key, $attr);
- $attr = implode('; ', $stylesi);
- $xmlo->picture->img->addAttribute($key, $attr);
- }
- if( $key == 'class'){
- if( $attr == 'staic' ){
- $staic = true;
- }
- }
- $title = '';
- if( $key == 'title'){
- $title = str_replace( '"', "'", trim($attr));
- $xmlo->picture->img->addAttribute($key, $title);
- /*
- if( isset( $attrs['rel-credit'] ) && trim($attrs->rel-credit) != ''){
- $attr = trim($attrs->{rel-credit})?$attrs->rel-credit:'';
- $title .= ' фото: '.$attr;
- }
- */
- }else{
- if( $xmlo->picture->img->$key && trim($key) != ''){
- $xmlo->picture->img->$key = $attr;
- }else{
- if( trim($key) != '' ) @$xmlo->picture->img->addAttribute($key, $attr);
- }
- }
- if( $title ){
- $e = $xmlo->addChild('span', $title); // class="image-title" style="margin-top: 20px;"
- $e->addAttribute('class',"image-title image-title-fix");
- //$e->addAttribute('style',"margin-top: 20px;");
- if( $url ){
- $e1 = $e->addChild('a', $url);
- $e1->addAttribute('href', $url);
- }
- }
- }
- if( isset( $xmlo->attributes()->style ) ){
- $s = $xmlo->attributes()->style .= ';overflow: hidden;';
- unset($xmlo->attributes()->style);
- $xmlo->addAttribute('style',$s );
- }else{
- $xmlo->addAttribute('style', 'overflow: hidden;');
- }
- $str = str_replace('$%$','&',preg_replace('|^\<\?xml version="1.0" encoding="UTF-8"\?\>\n|i', '',$xmlo->asXML()));
- if( $staic ){
- $str = str_replace( '100%', 'auto', $str);
- }
- return $str;
- }
- return $mt[0];
- }, $text);
- return $text;
- }
- public function behaviors()
- {
- $keys = array_keys(self::$keysCache);
- return [
- 'CachedBehavior' => [
- 'class' => CachedBehavior::class,
- 'cache_key' => $keys,
- ]
- ];
- }
- public static function find0()
- {
- return parent::find();
- }
- public static function find()
- {
- return parent::find()->orderBy(["dt_pub"=>SORT_DESC]);
- }
- /**
- * @return ActiveQuery
- */
- public function getGalleries()
- {
- return $this->hasMany(Gallery::class,['post_id'=>'id']);
- }
- public function getYoutubeEmbedLink(){
- $data = parse_url($this->embed_url);
- if(is_array($data)){
- switch (ArrayHelper::getValue($data,'host')){
- case "vk.com":
- $path = preg_split( '/[-_]/', ArrayHelper::getValue($data,'path') );
- if( isset($path[1]) && isset($path[2]) && is_numeric($path[1]) && is_numeric($path[2]) ){
- return "https://vk.com/video_ext.php?oid=-{$path[1]}&id={$path[2]}";
- }
- return '';
- break;
- default:
- switch (ArrayHelper::getValue($data,'path')){
- case "/watch":
- $re = '/v=(.*)/m';
- preg_match_all($re, ArrayHelper::getValue($data,'query'), $matches, PREG_SET_ORDER, 0);
- $video_id = '/'.ArrayHelper::getValue($matches,'0.1');
- break;
- default:
- $video_id = ArrayHelper::getValue($data,'path','');
- }
- }
- return "https://www.youtube.com/embed".$video_id;
- }
- return "";
- }
- public function inTopic(int $topic_id): bool
- {
- return (bool)$this->getTopicRelations()->andWhere(['topic_id'=>$topic_id])->count();
- }
- public static function ShowPollsWidget( $str ){
- preg_match_all( "|<div[^>]+>.[^<]*</div>|U", $str, $a, PREG_SET_ORDER );
- foreach ( $a as $item ){
- $xml = @simplexml_load_string( str_replace( ' ', '', $item[0] ), 'SimpleXMLElement', LIBXML_NOCDATA );
- if( $xml ){
- $attributes =$xml->attributes();
- if( $attributes['id'] == 'widgetpolls' ){
- // старый стиль url
- $r = preg_match( "/^https?:\/\/(.*)\/(.*)\/widget\/(\d+)\/$/i", trim($attributes['url']), $aa );
- if( $r && $aa[3]*1 > 0 ){
- if( $aa[2] == 'polls' ){
- $str = str_replace( $item[0], self::ShowPollsHtml( str_replace( ['http://www.amic.ru'],[''],$attributes['url']), $attributes['type'] ), $str );
- }
- }else{
- $r = preg_match( "/^https?:\/\/(.*)\/inquirer\/(\d+)$/i", trim($attributes['url']), $aa );
- if( $r && $aa[2]*1 > 0 ){
- $str = str_replace( $item[0], self::ShowPollsHtml( "/polls/widget/".$aa[2]*1, $attributes['type'] ), $str );
- }
- }
- }
- }
- }
- return $str;
- }
- public static function ShowPollsHtml( $url, $type ){
- static $i = 0;
- $url = trim($url);
- ob_start();
- ?>
- <script type="text/javascript">
- function AdjustIframeHeightOnLoad() { document.getElementById("form-iframe").style.height = document.getElementById("form-iframe").contentWindow.document.body.scrollHeight + "px"; }
- function AdjustIframeHeight(i) { document.getElementById("form-iframe").style.height = parseInt(i) + "px"; }
- </script>
- <div class="shadow p-2 mb-4 rounded" style="width:100%">
- <div class="header"></div>
- <iframe id="form-iframe" onload="AdjustIframeHeightOnLoad()" scrolling="no" src="<?=$url.'?widget='.$type?>" style="margin:0; width:100%; height:400px; border:none; overflow:hidden;"></iframe></div>
- <?
- $i++;
- return ob_get_clean();
- }
- public static function ShowPreportWidget( $str ){
- preg_match_all( "|<div[^>]+>.[^<]*</div>|U", $str, $a, PREG_SET_ORDER );
- foreach ( $a as $item ){
- $xml = @simplexml_load_string( str_replace( ' ', '', $item[0] ), 'SimpleXMLElement', LIBXML_NOCDATA );
- if( $xml ){
- $attributes =$xml->attributes();
- if( $attributes['id'] == 'widgetgallery' ){
- // старый стиль url
- $r = preg_match( "/^https?:\/\/(.*)\/(.*)\/(\d+)\/$/i", trim($attributes['url']), $aa );
- if( $r && $aa[3]*1 > 0 ){
- if( $aa[2] == 'photo' ){
- $str = str_replace( $item[0], self::ShowPreportHtml( "/photo/widget/".$aa[3]*1, $attributes['type'] ), $str );
- $c = Yii::$app->acache;
- $c->set('fotorama', true);
- }
- }
- }
- }
- }
- return $str;
- }
- public static function ShowPreportHtml( $url, $type ){
- static $i = 0;
- ob_start();
- ?>
- <div id='galleryID<?=$i?>'></div>
- <script>window.addEventListener("DOMContentLoaded",function () {$( '#galleryID<?=$i?>' ).load( '<?=$url?>'+'?widget=<?=$type?>');});</script>
- <?
- $i++;
- return ob_get_clean();
- }
- }
|