News.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. <?php
  2. namespace app\models;
  3. use app\helpers\Transliterator;
  4. use app\models\base\Comments;
  5. use app\models\base\Gallery;
  6. use app\models\base\Image;
  7. use app\models\base\OldNews;
  8. use DOMDocument;
  9. use yii\db\ActiveQuery;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Inflector;
  12. use yii;
  13. use app\components\behaviors\CachedBehavior;
  14. use yii\helpers\Url;
  15. /**
  16. * @property string $url
  17. * @property $mainPic
  18. * @property Image $preview
  19. * @property Image $image
  20. * @property OldNews $old
  21. * @property string $publishedAt
  22. * @property Comments[] $commentsAll
  23. * @property Gallery[] $galleries
  24. */
  25. class News extends \app\models\base\News
  26. {
  27. /*
  28. * Ключи кэширования для авто сброса
  29. */
  30. public static $keysCache=[
  31. 'MainPageBlock' => 120,
  32. 'main_page_lenta' =>120,
  33. 'main-page-main-view-glob' => 60, //600
  34. 'main-page-main-view' => 60,
  35. 'topic-news-##' => 3600
  36. ];
  37. private static function processPersons(string $body)
  38. {
  39. $re = '/<a.*class=[\'"](person|person-inject)[\'"].*href=[\'"]\/person\/(.+)[\'"].*>(\W+)<\/a>/m';
  40. $res = preg_replace_callback($re,function (array $matches): string
  41. {
  42. $person = Person::find()->andWhere(['alias'=>ArrayHelper::getValue($matches,'2')])->one();
  43. if($person instanceof Person){
  44. switch (ArrayHelper::getValue($matches,'1')) {
  45. case "person-inject":
  46. return \Yii::$app->view->render("/news/view/inject-person-as-widget", ["person" => $person]);
  47. break;
  48. default:
  49. return \Yii::$app->view->render("/news/view/inject-person-as-link", ["person" => $person]);
  50. }
  51. }
  52. return ArrayHelper::getValue($matches,0);
  53. }, $body);
  54. return $res;
  55. }
  56. private static function processBody($body,$model)
  57. {
  58. $counter = 0;
  59. $re = '/<(?:p|h2).*>(.*)<\/(?:p|h2)>/msU';
  60. $res = preg_replace_callback($re,function (array $matches) use(&$counter,$model): string
  61. {
  62. $counter++;
  63. if($counter==2 && Yii::$app->deviceDetect->isMobile() && !$model->inTopic(158)) return $matches[0].Yii::$app->controller->renderPartial("@app/views/_etc/banners/adfinityInContent");
  64. if($counter==3 && !Yii::$app->deviceDetect->isMobile() && !$model->inTopic(158)) return $matches[0].Yii::$app->controller->renderPartial("@app/views/_etc/banners/desktopInContent");
  65. return $matches[0];
  66. },$body);
  67. return $res;
  68. }
  69. private static function processGalleriesInjects(string $body, News $post)
  70. {
  71. $re = '/<p>(##_gallery-(\d+)##)<\/p>/mU';
  72. $body = preg_replace( $re, '\1', $body );
  73. $re = '/##_gallery-(\d+)##/mU';
  74. $res = preg_replace_callback($re,function (array $matches) use($post): string
  75. {
  76. $gallery = \app\models\front\Gallery::findOne(['id'=>ArrayHelper::getValue($matches,1)]);
  77. if($gallery instanceof \app\models\front\Gallery) {
  78. return \Yii::$app->view->render("/news/view/inject-gallery-x",["gallery"=>$gallery, "model"=>$post]);
  79. } else {
  80. return "";
  81. }
  82. },$body);
  83. return $res;
  84. }
  85. public function getUrl($full = false):string
  86. {
  87. if(count($this->topics)>0){
  88. $topic = Transliterator::toUrl($this->topics[0]->title)."/";
  89. } else{
  90. $topic = "";
  91. }
  92. switch ($this->type){
  93. // case 6:
  94. // return "https://old.amic.ru/long/{$this->id}/";
  95. // break;
  96. default:
  97. $base = Url::base('https');
  98. $alias = trim( $this->alias );
  99. if( $alias == '' ){
  100. $alias = Transliterator::toUrl($this->title);
  101. }
  102. if( isset( $this->dt_pub ) && strtotime( $this->dt_pub ) > strtotime('2023-01-27 15:00:00') ){
  103. return ($full ? $base : "")."/news/".$alias."-".$this->id;
  104. }
  105. return ($full ? $base : "")."/news/".$topic.$alias."-".$this->id;
  106. }
  107. }
  108. public function getMainPic(){
  109. }
  110. /**
  111. * @return Image
  112. */
  113. public function getPreview($type="webp"):Image{
  114. return Image::findOne($this->id,$type,$this->photo_name);
  115. }
  116. public function getOld():ActiveQuery
  117. {
  118. return $this->hasOne(OldNews::class,['alias'=>'alias']);
  119. }
  120. /**
  121. * @return ActiveQuery
  122. */
  123. public function getCommentsAll(): ActiveQuery
  124. {
  125. if($this->comments=="Y"){
  126. return $this->hasMany(Comments::class,['news_id'=>"id"])->andWhere(['visible'=>'Y']);
  127. }
  128. return $this->hasMany(Comments::class,['news_id'=>"id"])->andWhere(0);
  129. }
  130. public static function getMainView(){
  131. return self::find()->joinWith("topics t")->andWhere(["t.id"=>[33,]]);
  132. }
  133. public static function getNH(){
  134. 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);
  135. }
  136. public static function getPartnersNews(){
  137. return self::find()->joinWith("topics t")->andWhere(["t.id"=>[34,]]);
  138. }
  139. public static function getMainOfWeek(){
  140. return self::find()->joinWith("topics t")->andWhere(["t.id"=>[35,]]);
  141. }
  142. public function getDatePub(){
  143. return $this->dt_pub;
  144. }
  145. public function getPublishedAt(){
  146. if($this->dt_pub<date("Y-m-d H:i:s",strtotime("-1 day"))){
  147. return
  148. date("d",strtotime($this->dt_pub))." ".mb_strtolower(Transliterator::month(date("n",strtotime($this->dt_pub))))
  149. .date(" Y, H:i", strtotime($this->dt_pub))
  150. ;
  151. } else {
  152. $diff = ceil((time() - strtotime($this->dt_pub))/60); //В минутах
  153. if($diff<=60){
  154. return Transliterator::plural($diff,['минуту','минуты', 'минут'],true,'только что','минуту')." назад";
  155. } else {
  156. $diff = (int)floor($diff/60);
  157. return Transliterator::plural($diff,['час','часа', 'часов'],true,'только что','час')." назад";
  158. }
  159. }
  160. }
  161. public function getPublishedNorm(){
  162. return
  163. date("d",strtotime($this->dt_pub))." ".mb_strtolower(Transliterator::month(date("n",strtotime($this->dt_pub))))
  164. .date(" Y, H:i", strtotime($this->dt_pub))
  165. ;
  166. }
  167. public function isPhotosOnNews( $dt_pub = null )
  168. {
  169. if( $this->inscription) return true;
  170. if( isset( $this->photo_title ) )
  171. {
  172. if( stripos($this->photo_title, 'amic.ru') !== false ) return true;
  173. if( stripos($this->photo_title, 'Смолихина') !== false ) return true;
  174. }
  175. if( $this->isAmicPhoto() ) return true;
  176. if( $dt_pub ){
  177. $dt_pub = strtotime($dt_pub);
  178. }else{
  179. $dt_pub = strtotime($this->dt_pub);
  180. }
  181. if( $dt_pub >= strtotime(Yii::$app->params['delPhotoB']) && $dt_pub < strtotime(Yii::$app->params['delPhotoE']) ) return false;
  182. return true;
  183. }
  184. public function renderBody()
  185. {
  186. $post = $this;
  187. $device = Yii::$app->deviceDetect->isMobile()?"mobile":"desktop";
  188. return \Yii::$app->cache->getOrSet("post-body8-".$this->id."-".$device,function () use ($post){
  189. $body = $post->text;
  190. //Нужно воткнуть рекламу после второго абзаца
  191. $body = self::processBody($body,$this);
  192. // $body = self::processSingleImg($body);
  193. $body = $this->DateDelImg($body);
  194. $body = self::processTextImg($body);
  195. $body = self::processInjects($body);
  196. $body = self::processAudio($body);
  197. if( $this->isPhotosOnNews($post->getDatePub() ) ){
  198. $body = self::processSlider($body, $post); //old slider
  199. $body = self::processGalleriesInjects($body, $post);
  200. }
  201. $body = self::processYoutube($body);
  202. $body = self::processIframe($body);
  203. $body = self::processSpecialFormats($body);
  204. $body = self::processPersons($body);
  205. $body = self::processTest($body);
  206. $body = self::processApiImg($body);
  207. $body = self::ShowPollsWidget($body);
  208. $body = self::ShowPreportWidget($body);
  209. if( $post->inscription == 0 ) $body = self::processAhref($body); // кроме комерческих
  210. return $body;
  211. },YII_ENV_DEV?1:60);
  212. }
  213. public static function processApiImg($text)
  214. {
  215. return str_replace( 'https://api.amic.ru', '', $text);
  216. }
  217. public static function processTest($text)
  218. {
  219. $text = str_replace("\xc2\xa0", ' ', $text);
  220. $text = str_replace(['<p>&nbsp;</p>','<p> </p>'], "\c", $text);
  221. $text = trim( $text );
  222. $text = str_replace( "\c", "<p></p>", $text);
  223. $text = str_replace( 'href="http://tel=', 'href="tel:', $text );
  224. return $text;
  225. }
  226. public static function processSpecialFormats($text)
  227. {
  228. if( stripos($text, 'class="juxtapose"') !== false ){
  229. $str = '
  230. <script src="/js/juxtapose/juxtapose.min.js"></script>
  231. <link rel="stylesheet" href="/js/juxtapose/juxtapose.css">
  232. ';
  233. return $text.$str;
  234. }
  235. return $text;
  236. }
  237. /*
  238. * Парсинг для старых слайдеров (очень старых)
  239. */
  240. public static function processSlider($text, $obj)
  241. {
  242. if( $obj->uid == '' ) return $text;
  243. $dir = '/images/items/'.$obj->uid;
  244. $files1 = @scandir(Yii::getAlias('@webroot').$dir.'/thumbnails/',0);
  245. $old_title = ( trim($obj->old_gallery_title) != '' )?''.trim($obj->old_gallery_title).'':'';
  246. if( $files1 == false || count( $files1 ) < 4){
  247. $files1 = @scandir(Yii::getAlias('@webroot').$dir.'/images/thumbnails/', 0);
  248. if( $files1 === false || count( $files1 ) == 0){
  249. return $text;
  250. }else{
  251. $i=1;
  252. ob_start();
  253. foreach( $files1 as $file ){
  254. if( preg_match('/^.*\.(gif|jpe?g|png)/i', $file ) ){
  255. $str = @file_get_contents( Yii::getAlias('@webroot').$dir.'/info/'.$file.'.json' );
  256. if( $str == false ){
  257. $str='{"title":"","copyrate":""}';
  258. }
  259. $js_title = json_decode( $str );
  260. $str_title = ( $js_title->title == '' )?$old_title:$js_title->title;//iconv('UTF-8','cp1251',$js_title->title);
  261. $str_title .= ( $js_title->copyrate == '' )?'':'<br><span class="style8">'.$js_title->copyrate.'</span>';
  262. $add_cap = ($str_title != '')?", caption: '".$str_title."'":'';
  263. ?>
  264. {img: '<?=$dir?>/images/<?=$file?>', thumb: '<?=$dir?>/images/thumbnails/<?=$file?>' <?=$add_cap?>},
  265. <?
  266. }
  267. }
  268. $ltext = ob_get_contents();
  269. ob_end_clean();
  270. }
  271. }else{
  272. $i=1;
  273. ob_start();
  274. foreach( $files1 as $file ){
  275. if( preg_match('/^.*\.(gif|jpe?g|png)/i', $file ) ){
  276. ?>
  277. {img: '<?=$dir?>/big/<?=$file?>', thumb: '<?=$dir?>/thumbnails/<?=$file?>'},
  278. <?
  279. }
  280. }
  281. $ltext = ob_get_contents();
  282. ob_end_clean();
  283. }
  284. ob_start();
  285. if( isset( $ltext ) ){
  286. if( strstr( $text, 'id="mycarousel"') === false ){
  287. echo '<div id="mycarousel"></div>';
  288. }
  289. ?>
  290. <!-- Fotorama -->
  291. <?
  292. $c = Yii::$app->acache;
  293. $c->set('fotorama', true);
  294. ?>
  295. <!-- <div class="fotorama" data-width="700" data-ratio="700/467" data-max-width="100%"></div>-->
  296. <script type="text/javascript">
  297. window.addEventListener("DOMContentLoaded",function () {
  298. $('#mycarousel').fotorama({ width:'100%',maxwidth:780,thumbheight:70,thumbwidth:94, captions: true,nav:'thumbs', caption: 'overlay', allowfullscreen:'native', data: [ <?=$ltext?> ] });
  299. });
  300. </script>
  301. <?
  302. }
  303. $ltext = ob_get_clean();
  304. return $ltext.$text;
  305. }
  306. public static function processAudio($text)
  307. {
  308. $re = '/<a([^>]+)href=\"([^>]*\/files\/)(.+)\.mp3\"([^>]*)>([^<]*)<\/a>/i';
  309. $res = preg_replace_callback($re,function (array $matches): string
  310. {
  311. $title = isset($matches[5])?$matches[5]:'';
  312. if( !strstr( $matches[1], "download") ) {
  313. $url = $matches[2];
  314. $audio = '<source src="'.$url.$matches[3].'.mp3" type="audio/mpeg">'."\n";
  315. if( file_exists( $_SERVER['DOCUMENT_ROOT'].''.$matches[2].$matches[3].'.mp3.ogg' ) ){
  316. $audio .= '<source src="'.$url.$matches[3].'.mp3.ogg" type="audio/ogg; codecs=vorbis">'."\n";
  317. }
  318. return \Yii::$app->view->render("/news/view/audio",["audio"=>$audio, "title"=>$title]);
  319. } else {
  320. return ArrayHelper::getValue($matches,0);
  321. }
  322. },$text);
  323. return $res;
  324. }
  325. /*
  326. * Спрятать внешнии ссылки от поисковиков
  327. * Псевдо SEO
  328. *
  329. */
  330. public static function processAhref($text)
  331. {
  332. $re = '/<a[^>]+href=\"(https?:\/\/[^"]*)\".*>/Ui';
  333. $res = preg_replace_callback($re,function (array $matches): string
  334. {
  335. $mydomain = \Yii::$app->params['mydomain'];
  336. foreach( $mydomain as $find ){
  337. if( stripos( $matches[1], $find) !== false ) return $matches[0];
  338. }
  339. $ret = str_replace( $matches[1], "/go/?u=".urlencode($matches[1]), $matches[0] );
  340. return $ret;
  341. },$text);
  342. return $res;
  343. }
  344. public static function processInjects($text)
  345. {
  346. $re = '/##news_(.*)##/mU';
  347. $res = preg_replace_callback($re,function (array $matches): string
  348. {
  349. $post = News::findOne(['uid'=>ArrayHelper::getValue($matches,1)]);
  350. if($post instanceof News) {
  351. return \Yii::$app->view->render("/news/view/inject",["post"=>$post]);
  352. } else {
  353. return "";
  354. }
  355. },$text);
  356. if( strlen($res) != strlen($text) ) return $res;
  357. $re = "|<div[^>]+>.[^<]*</div>|U";
  358. $res = preg_replace_callback($re,function (array $matches): string
  359. {
  360. @$xml = simplexml_load_string( str_replace( '&nbsp;', '', ArrayHelper::getValue($matches,0) ), 'SimpleXMLElement', LIBXML_NOCDATA );
  361. if( $xml ){
  362. $attributes =$xml->attributes();
  363. if( $attributes['class'] == 'insinject' || $attributes['id'] == "inject" ){
  364. $r = preg_match( "/^https?:\/\/(.*)\/(.*)\/(\d+)\/$/i", $attributes['url'], $aa );
  365. $id = 0;
  366. if( $r && ($aa[1] == 'www.amic.ru' || $aa[1] == Yii::$app->request->serverName) && $aa[3]*1 > 0 ){
  367. // old style inject
  368. $id = $aa[3]*1;
  369. }
  370. $r = preg_match( "/^https?:\/\/(.*)\/.*(\d+)\/?$/Ui", $attributes['url'], $aa );
  371. if( $r && ($aa[1] == Yii::$app->request->serverName || $aa[1] == 'www.amic.ru')&& $aa[2]*1 > 0 ){
  372. // new style inject
  373. $id = $aa[2]*1;
  374. }
  375. $r = preg_match( '/^https?:\/\/(.*)\/person\/(.*)\/?$/Ui', $attributes['url'], $aa );
  376. if( $r && ($aa[1] == Yii::$app->request->serverName || $aa[1] == 'www.amic.ru')&& $aa[2] != '' ){
  377. $person = \app\models\Person::findOne(['alias'=>$aa[2]]);
  378. if( ( !$person instanceof Person ) || $person->show == 'N' ){
  379. return "";
  380. }
  381. return \Yii::$app->view->render("/news/view/injectp",["post"=>$person]);
  382. }
  383. if( $id ){
  384. //$attributes['type'] to do
  385. $post = News::findOne(['id'=>$id]);
  386. if($post instanceof News) {
  387. if( $attributes['type'] == 1 ){
  388. return \Yii::$app->view->render("/news/view/inject1",["post"=>$post]);
  389. }else if( $attributes['type'] == 2 ){
  390. return \Yii::$app->view->render("/news/view/inject2",["post"=>$post]);
  391. }else{
  392. return \Yii::$app->view->render("/news/view/inject",["post"=>$post]);
  393. }
  394. } else {
  395. return "Битый инжект";
  396. }
  397. }
  398. }
  399. }
  400. return ArrayHelper::getValue($matches,0);
  401. },$text);
  402. return $res;
  403. }
  404. public static function processIframe($text) //https://youtu.be/sfzX5fMaSj4 -> https://www.youtube.com/embed/sfzX5fMaSj4
  405. {
  406. $re = '/(<iframe[^>]*>).*(<\/iframe>)/Ui';
  407. $text = preg_replace_callback($re,function (array $mt): string
  408. {
  409. //print_r($mt);
  410. // libxml_use_internal_errors(true);
  411. $content = $mt[0];
  412. $content = str_replace( '&', '#-#', $content);
  413. $HXML='<?xml version="1.0" encoding="UTF-8"?>';
  414. $doc = new DOMDocument('1.0','UTF-8'); // нормализация xml
  415. @$doc->loadHTML($HXML.$content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOCDATA);
  416. // $doc->encoding = 'UTF-8';
  417. $doc->normalizeDocument();
  418. $content = $doc->saveXML($doc->documentElement);
  419. $content = html_entity_decode($content);
  420. $content = str_replace( '<?xml version="1.0" standalone="yes"?>', '', $content);
  421. $content = str_replace( '<?xml version="1.0" encoding="UTF-8"??>', '', $content);
  422. $content = str_replace( '<?xml version="1.0" encoding="UTF-8"?>', '', $content);
  423. $xml = simplexml_load_string( $content, 'SimpleXMLElement',LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL |LIBXML_HTML_NOIMPLIED );
  424. if( $xml ){
  425. $attrs = $attributes =$xml->attributes();
  426. //print_r( $attrs );
  427. $xmlo = simplexml_load_string( $HXML.'<iframe loading="lazy"> </iframe>', 'SimpleXMLElement', LIBXML_NOCDATA );
  428. // var_dump($attrs);
  429. if( !Yii::$app->deviceDetect->isMobile() ){
  430. if( $attrs['src'] && strpos( $attrs['src'], 'vk.com' ) !== false ) $fixedRatio = '16/9';
  431. }
  432. foreach( $attrs as $key=>$attr ){
  433. if( $key == 'height'){
  434. $height = preg_replace('/[\D]/', '', $attr);
  435. if( $height == 0 ) $height = 0.1;
  436. $attr = 'auto';
  437. }
  438. if( $key == 'width'){
  439. $width = preg_replace('/[\D]/', '', $attr);
  440. if( strpos($attr, '%') !== false ){
  441. $width = $width*720/100;
  442. }
  443. $attr = '100%';
  444. }
  445. if( isset($height) && isset($width) ){
  446. if( isset( $fixedRatio ) ){
  447. $ratio = $fixedRatio;
  448. }else{
  449. $ratio = round( $width/$height, 2 );
  450. }
  451. if( !isset($xmlo->attributes()->style) ){
  452. $xmlo->addAttribute('style', 'width:100%; aspect-ratio:'.$ratio);
  453. }else{
  454. $xmlo->attributes()->style = $xmlo->attributes()->style.'width:100%; aspect-ratio:'.$ratio;
  455. }
  456. unset($height);
  457. unset($width);
  458. }
  459. @$xmlo->addAttribute($key, $attr);
  460. }
  461. return str_replace( '#-#', '&', preg_replace('|^\<\?xml version="1.0" encoding="UTF-8"\?\>\n|i', '',$xmlo->asXML()) );
  462. }
  463. return $mt[0];
  464. }, $text);
  465. return $text;
  466. }
  467. /*
  468. * Парсинг короткой ссылки ютуба
  469. *
  470. */
  471. public static function processYoutube($text) //https://youtu.be/sfzX5fMaSj4 -> https://www.youtube.com/embed/sfzX5fMaSj4
  472. {
  473. return str_replace( 'https://youtu.be/', 'https://www.youtube.com/embed/', $text );
  474. }
  475. public static function processSingleImg($text)
  476. {
  477. $re = '/(<[p|a][^>]*>)(<img.*>)(<\/[p|a]>)/i';
  478. $res = preg_replace_callback($re,function (array $matches): string
  479. {
  480. $img = ArrayHelper::getValue($matches,2);
  481. $a = array();
  482. $title = '';
  483. if( preg_match( '/title="(.*)"/mU', $img, $a) ){
  484. $title = '<div class="image-title" style="margin-top: 20px;">'.$a[1].'</div>';
  485. }
  486. if( $title && preg_match( '/rel-credit="(.*)"/mU', $img, $a) ){
  487. if( trim( $a[1] ) != '' ){
  488. $title = str_replace('</div>', ' фото: '.$a[1].'</div>', $title);
  489. }
  490. }
  491. if( $title && preg_match( '/longdesc="(.*)"/mU', $img, $a) ){
  492. $title = str_replace('</div>', ' <a href="'.$a[1].'">'.$a[1].'</a></div>', $title);
  493. }
  494. $style = '';
  495. $a = array();
  496. if( preg_match( '/style="(.*)"/mU', $img, $a) ){
  497. $style = $a[1];
  498. $style = str_replace('height', 'data-height', $style);
  499. $img = str_replace('margin', 'margin-block', $img);
  500. }
  501. //class='picture-cont-16x9'
  502. $pic= "<div style='{$style}' class=\"pic\">
  503. <picture class='w-100'>
  504. {$img}
  505. </picture>
  506. {$title}
  507. </div>
  508. ";
  509. return str_replace('<p','<div',ArrayHelper::getValue($matches,1)).$pic.str_replace('p>','div>',ArrayHelper::getValue($matches,3));
  510. },$text);
  511. return $res;
  512. }
  513. public function DateDelImg($text)
  514. {
  515. $obj = $this;
  516. $re = '/(<img[^>]*>)/i';
  517. $text = preg_replace_callback($re,function (array $mt) use($obj): string
  518. {
  519. /*
  520. После этой даты не выводить фото до подтверждения юриста
  521. */
  522. if( !$this->isPhotosOnNews( $obj->getDatePub() ) ){
  523. $bad = '/images/default.jpg';
  524. return '<span><picture class="w-100"><img src="'.$bad.'" width="100%"/></picture></span>';
  525. };
  526. return $mt[0];
  527. }, $text);
  528. return $text;
  529. }
  530. public static function processTextImg($text)
  531. {
  532. $text = str_replace('src="http://', 'src="https://', $text); //в качестве бреда
  533. $text = str_replace( 'publib/gimage.php?image=/', '', $text ); // очень очень старый движ
  534. $re = '/(<img[^>]*>)/i';
  535. $text = preg_replace_callback($re,function (array $mt): string
  536. {
  537. //print_r($mt);
  538. libxml_use_internal_errors(true);
  539. $staic = false;
  540. $content = $mt[0];
  541. $content = str_replace('&','$%$',$content);
  542. $HXML='<?xml version="1.0" encoding="UTF-8"?>';
  543. $doc = new DOMDocument('1.0','UTF-8'); // нормализация xml
  544. $doc->loadHTML($HXML.$content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOCDATA);
  545. // $doc->encoding = 'UTF-8';
  546. $doc->normalizeDocument();
  547. $content = $doc->saveXML($doc->documentElement);
  548. $content = html_entity_decode($content);
  549. $content = str_replace( '<?xml version="1.0" standalone="yes"?>', '', $content);
  550. $content = str_replace( '<?xml version="1.0" encoding="UTF-8"??>', '', $content);
  551. $content = str_replace( '<?xml version="1.0" encoding="UTF-8"?>', '', $content);
  552. $xml = simplexml_load_string( $content, 'SimpleXMLElement',LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL |LIBXML_HTML_NOIMPLIED );
  553. if( $xml ){
  554. $attrs = $attributes =$xml->attributes();
  555. //print_r( $attrs );
  556. $xmlo = simplexml_load_string( $HXML.'<span><picture class="w-100"><img /></picture></span>', 'SimpleXMLElement', LIBXML_NOCDATA );
  557. // var_dump($attrs);
  558. $url = isset($attrs->longdesc)?$attrs->longdesc:'';
  559. foreach( $attrs as $key=>$attr ){
  560. if( $key == 'height'){
  561. $xmlo->addAttribute('data-height', $attr);
  562. continue;
  563. }
  564. if( $key == 'style'){
  565. $attr = trim( $attr, " ;\r\n" );
  566. //$attr = str_replace(' ', '', $attr);
  567. $css = explode( ";", $attr );
  568. $stylesd = array();
  569. $stylesi = array();
  570. foreach( $css as $attrx ){
  571. list($key1, $value) = explode( ":", $attrx );
  572. $key1 = trim($key1);
  573. if( $key1 == 'height' || $key1 == '' ){
  574. continue;
  575. }elseif( stristr( 'margin', $key1 ) !== false ){
  576. $stylesd[] = "{$key1}:{$value}";
  577. }elseif( $key1 == 'float' ){
  578. $stylesd[] = "{$key1}:{$value}";
  579. }elseif( $key1 == 'width' ){
  580. //$value = preg_replace("/[^0-9\-.]/", '', $value);
  581. if( preg_replace("/[^0-9\-.]/", '', $value) > 720-150 ){
  582. $value = '100%';
  583. }
  584. $stylesd[] = "{$key1}:{$value}";
  585. $stylesi[] = "{$key1}:100%";
  586. $stylesd[] = "display:block";
  587. }elseif( $key1 && $value){
  588. $stylesd[] = "{$key1}:{$value}";
  589. if( $key1 == 'margin-right' || $key1 == 'margin-left' ) $value = 0;
  590. if( $key1 != 'margin' ) {
  591. $stylesi[] = "{$key1}:{$value}";
  592. }
  593. }
  594. }
  595. $attr = implode('; ', $stylesd);
  596. $xmlo->addAttribute($key, $attr);
  597. $attr = implode('; ', $stylesi);
  598. $xmlo->picture->img->addAttribute($key, $attr);
  599. }
  600. if( $key == 'class'){
  601. if( $attr == 'staic' ){
  602. $staic = true;
  603. }
  604. }
  605. $title = '';
  606. if( $key == 'title'){
  607. $title = str_replace( '"', "'", trim($attr));
  608. $xmlo->picture->img->addAttribute($key, $title);
  609. /*
  610. if( isset( $attrs['rel-credit'] ) && trim($attrs->rel-credit) != ''){
  611. $attr = trim($attrs->{rel-credit})?$attrs->rel-credit:'';
  612. $title .= ' фото: '.$attr;
  613. }
  614. */
  615. }else{
  616. if( $xmlo->picture->img->$key && trim($key) != ''){
  617. $xmlo->picture->img->$key = $attr;
  618. }else{
  619. if( trim($key) != '' ) @$xmlo->picture->img->addAttribute($key, $attr);
  620. }
  621. }
  622. if( $title ){
  623. $e = $xmlo->addChild('span', $title); // class="image-title" style="margin-top: 20px;"
  624. $e->addAttribute('class',"image-title image-title-fix");
  625. //$e->addAttribute('style',"margin-top: 20px;");
  626. if( $url ){
  627. $e1 = $e->addChild('a', $url);
  628. $e1->addAttribute('href', $url);
  629. }
  630. }
  631. }
  632. if( isset( $xmlo->attributes()->style ) ){
  633. $s = $xmlo->attributes()->style .= ';overflow: hidden;';
  634. unset($xmlo->attributes()->style);
  635. $xmlo->addAttribute('style',$s );
  636. }else{
  637. $xmlo->addAttribute('style', 'overflow: hidden;');
  638. }
  639. $str = str_replace('$%$','&',preg_replace('|^\<\?xml version="1.0" encoding="UTF-8"\?\>\n|i', '',$xmlo->asXML()));
  640. if( $staic ){
  641. $str = str_replace( '100%', 'auto', $str);
  642. }
  643. return $str;
  644. }
  645. return $mt[0];
  646. }, $text);
  647. return $text;
  648. }
  649. public function behaviors()
  650. {
  651. $keys = array_keys(self::$keysCache);
  652. return [
  653. 'CachedBehavior' => [
  654. 'class' => CachedBehavior::class,
  655. 'cache_key' => $keys,
  656. ]
  657. ];
  658. }
  659. public static function find0()
  660. {
  661. return parent::find();
  662. }
  663. public static function find()
  664. {
  665. return parent::find()->orderBy(["dt_pub"=>SORT_DESC]);
  666. }
  667. /**
  668. * @return ActiveQuery
  669. */
  670. public function getGalleries()
  671. {
  672. return $this->hasMany(Gallery::class,['post_id'=>'id']);
  673. }
  674. public function getYoutubeEmbedLink(){
  675. $data = parse_url($this->embed_url);
  676. if(is_array($data)){
  677. switch (ArrayHelper::getValue($data,'host')){
  678. case "vk.com":
  679. $path = preg_split( '/[-_]/', ArrayHelper::getValue($data,'path') );
  680. if( isset($path[1]) && isset($path[2]) && is_numeric($path[1]) && is_numeric($path[2]) ){
  681. return "https://vk.com/video_ext.php?oid=-{$path[1]}&id={$path[2]}";
  682. }
  683. return '';
  684. break;
  685. default:
  686. switch (ArrayHelper::getValue($data,'path')){
  687. case "/watch":
  688. $re = '/v=(.*)/m';
  689. preg_match_all($re, ArrayHelper::getValue($data,'query'), $matches, PREG_SET_ORDER, 0);
  690. $video_id = '/'.ArrayHelper::getValue($matches,'0.1');
  691. break;
  692. default:
  693. $video_id = ArrayHelper::getValue($data,'path','');
  694. }
  695. }
  696. return "https://www.youtube.com/embed".$video_id;
  697. }
  698. return "";
  699. }
  700. public function inTopic(int $topic_id): bool
  701. {
  702. return (bool)$this->getTopicRelations()->andWhere(['topic_id'=>$topic_id])->count();
  703. }
  704. public static function ShowPollsWidget( $str ){
  705. preg_match_all( "|<div[^>]+>.[^<]*</div>|U", $str, $a, PREG_SET_ORDER );
  706. foreach ( $a as $item ){
  707. $xml = @simplexml_load_string( str_replace( '&nbsp;', '', $item[0] ), 'SimpleXMLElement', LIBXML_NOCDATA );
  708. if( $xml ){
  709. $attributes =$xml->attributes();
  710. if( $attributes['id'] == 'widgetpolls' ){
  711. // старый стиль url
  712. $r = preg_match( "/^https?:\/\/(.*)\/(.*)\/widget\/(\d+)\/$/i", trim($attributes['url']), $aa );
  713. if( $r && $aa[3]*1 > 0 ){
  714. if( $aa[2] == 'polls' ){
  715. $str = str_replace( $item[0], self::ShowPollsHtml( str_replace( ['http://www.amic.ru'],[''],$attributes['url']), $attributes['type'] ), $str );
  716. }
  717. }else{
  718. $r = preg_match( "/^https?:\/\/(.*)\/inquirer\/(\d+)$/i", trim($attributes['url']), $aa );
  719. if( $r && $aa[2]*1 > 0 ){
  720. $str = str_replace( $item[0], self::ShowPollsHtml( "/polls/widget/".$aa[2]*1, $attributes['type'] ), $str );
  721. }
  722. }
  723. }
  724. }
  725. }
  726. return $str;
  727. }
  728. public static function ShowPollsHtml( $url, $type ){
  729. static $i = 0;
  730. $url = trim($url);
  731. ob_start();
  732. ?>
  733. <script type="text/javascript">
  734. function AdjustIframeHeightOnLoad() { document.getElementById("form-iframe").style.height = document.getElementById("form-iframe").contentWindow.document.body.scrollHeight + "px"; }
  735. function AdjustIframeHeight(i) { document.getElementById("form-iframe").style.height = parseInt(i) + "px"; }
  736. </script>
  737. <div class="shadow p-2 mb-4 rounded" style="width:100%">
  738. <div class="header"></div>
  739. <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>
  740. <?
  741. $i++;
  742. return ob_get_clean();
  743. }
  744. public static function ShowPreportWidget( $str ){
  745. preg_match_all( "|<div[^>]+>.[^<]*</div>|U", $str, $a, PREG_SET_ORDER );
  746. foreach ( $a as $item ){
  747. $xml = @simplexml_load_string( str_replace( '&nbsp;', '', $item[0] ), 'SimpleXMLElement', LIBXML_NOCDATA );
  748. if( $xml ){
  749. $attributes =$xml->attributes();
  750. if( $attributes['id'] == 'widgetgallery' ){
  751. // старый стиль url
  752. $r = preg_match( "/^https?:\/\/(.*)\/(.*)\/(\d+)\/$/i", trim($attributes['url']), $aa );
  753. if( $r && $aa[3]*1 > 0 ){
  754. if( $aa[2] == 'photo' ){
  755. $str = str_replace( $item[0], self::ShowPreportHtml( "/photo/widget/".$aa[3]*1, $attributes['type'] ), $str );
  756. $c = Yii::$app->acache;
  757. $c->set('fotorama', true);
  758. }
  759. }
  760. }
  761. }
  762. }
  763. return $str;
  764. }
  765. public static function ShowPreportHtml( $url, $type ){
  766. static $i = 0;
  767. ob_start();
  768. ?>
  769. <div id='galleryID<?=$i?>'></div>
  770. <script>window.addEventListener("DOMContentLoaded",function () {$( '#galleryID<?=$i?>' ).load( '<?=$url?>'+'?widget=<?=$type?>');});</script>
  771. <?
  772. $i++;
  773. return ob_get_clean();
  774. }
  775. }