view.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * @var $this \yii\web\View
  4. * @var $model \app\models\front\News
  5. */
  6. use yii\helpers\ArrayHelper;
  7. use app\modules\ldjson\jsonLD;
  8. use yii\web\UrlManager;
  9. use app\models\Authors;
  10. use app\models\base\Image;
  11. use \app\models\news;
  12. use yii\helpers\Html;
  13. use yii\helpers\Url;
  14. use magyarandras\AMPConverter\Converter;
  15. use yii\web\View;
  16. use app\components\amp\AMPDiv;
  17. use app\components\amp\AMPAhref;
  18. use app\components\amp\AMPSpan;
  19. use app\components\amp\AMPP;
  20. setlocale( LC_ALL, 'ru_RU.UTF-8' );
  21. $preview = $model->image;
  22. $retina = $preview->getUrl(Image::SIZE_HD,"jpg");
  23. $normal = $preview->getUrl(Image::SIZE_1040x586,"webp");
  24. $bad = $preview->getUrl(Image::SIZE_680x383,'jpg');
  25. //$bad = $bad?$bad:'/images/default.jpg';
  26. if( $normal === false ){
  27. $normal = $bad;
  28. }
  29. if( $retina === false ){
  30. $retina = $normal;
  31. }
  32. $normal .= "?v=".$model->rev;
  33. $retina .= "?v=".$model->rev;
  34. $bad .= "?v=".$model->rev;
  35. $picture = $normal;
  36. if( $model->show_author == 'Y' )
  37. {
  38. $author = Authors::getAuthor( $model->author );
  39. $authorName = $author->name;
  40. }else{
  41. $authorName = 'ИА Амител';
  42. }
  43. $title = Html::encode( ( trim($model->meta_title) == '' )?$model->title:$model->meta_title );
  44. $this->title = $title;
  45. $this->registerLinkTag([ 'rel' => 'canonical', 'href' => $model->getUrl(true)], 'canonical');
  46. $lid = Html::encode( ($model->lid && trim($model->lid) <> '')?$model->lid:mb_substr( trim($model->text) ,0,150)."..." );
  47. //$ampProducer = new AmpProducer(['model'=>$model]);
  48. $converter = new Converter([
  49. 'image_base_url' => 'https://www.amic.ru',
  50. 'image_timeout' => 5
  51. ]);
  52. //Чистка
  53. $s = preg_replace( '/<meta([^\>]+)>/i', '<p>', $model->text );
  54. $sa = explode( '+++', $s );
  55. $s = $sa[0];
  56. $pattern = array( '/<style[^>]*>[^>]*>/i', '/<\/?font[^>]*>/i', '/sp_eventWasSet="on"/i' );
  57. $s = preg_replace( $pattern, '', $s );
  58. $s = preg_replace( '/<lj([^\>]+)>/i', '<p>', $s );
  59. $s = preg_replace( '/<\/lj>/i', '</p>', $s );
  60. $s = preg_replace( '/<asid([^\>]*)>/i', '<p>', $s );
  61. $s = preg_replace( '/<\/asid>/i', '</p>', $s );
  62. $s = preg_replace( '/<br([^\>]*)>/i', '<br>', $s );
  63. $s = preg_replace( '/<em([^\>]*)>/i', '<i>', $s );
  64. $s = preg_replace( '/<\/em>/i', '</i>', $s );
  65. $s = preg_replace( '/<hc([^\>]+)>/i', '<p>', $s );
  66. $s = preg_replace( '/<\/hc>/i', '</p>', $s );
  67. $s = preg_replace( '/<col([^\>]+)>/i', '', $s );
  68. $s = preg_replace( '/<\/col>/i', '', $s );
  69. $s = preg_replace( '/<st1([^\>]+)>/i', '', $s );
  70. $s = preg_replace( '/<\/st1>/i', '', $s );
  71. $s = preg_replace( '/<form([^\>]+)>/i', '<p>', $s );
  72. $s = preg_replace( '/<\/form>/i', '</p>', $s );
  73. $s = preg_replace( '/<noindex>/i', '<p>', $s );
  74. $s = preg_replace( '/<\/noindex>/i', '</p>', $s );
  75. $s = preg_replace( '/<pl>/i', '', $s );
  76. $s = preg_replace( '/<\/pl>/i', '', $s );
  77. $s = preg_replace( '/<index>/i', '<p>', $s );
  78. $s = preg_replace( '/<\/index>/i', '</p>', $s );
  79. $s = preg_replace( '/<hypernews>/i', '<p>', $s );
  80. $s = preg_replace( '/<\/hypernews>/i', '</p>', $s );
  81. $s = preg_replace( '/<content>/i', '<p>', $s );
  82. $s = preg_replace( '/<\/content>/i', '</p>', $s );
  83. $s = preg_replace( '/<tspan([^\>]+)>/i', '<span>', $s );
  84. $s = preg_replace( '/<\/tspan>/i', '</span>', $s );
  85. $s = preg_replace( '/<text([^\>]+)>/i', '<p>', $s );
  86. $s = preg_replace( '/<\/text>/i', '</p>', $s );
  87. $s = preg_replace( '/\<metricconverter([^\>]*)\>/iux', 'span', $s );
  88. $s = preg_replace( '/metricconverter/iu', 'span', $s );
  89. $s = preg_replace( '/<pre([^\>]+)>/ix', '', $s );
  90. $s = preg_replace( '/<\/pre>/i', '', $s );
  91. $s = preg_replace( '/<tmp([^\>]*)>/ix', '', $s );
  92. $s = preg_replace( '/<\/tmp>/i', '', $s );
  93. $s = preg_replace( '/<nowiki([^\>]*)>/ix', '', $s );
  94. $s = preg_replace( '/<\/nowiki>/i', '', $s );
  95. $s = preg_replace( '/<foreach([^\>]+)>/ix', ' ', $s );
  96. $s = preg_replace( '/<\/foreach>/i', ' ', $s );
  97. $s = preg_replace( '/<link([^\>]+)>/ix', '', $s );
  98. $s = preg_replace( '/<\/link>/i', '', $s );
  99. $s = str_replace( '<h ', '<h3 ', $s );
  100. $s = str_replace( '/h>', '/h3> ', $s );
  101. $s = str_replace( 'xmlns:fn', 'uri ', $s );
  102. $s = str_replace( 'alt=""', ' ', $s );
  103. $s = str_replace( 'href="http://tel=', 'href="tel:', $s );
  104. $s = str_replace( 'publib/gimage.php?image=/', '', $s );
  105. //
  106. $converter->loadDefaultConverters();
  107. $converter->addConverter(new AMPDiv());
  108. $converter->addConverter(new AMPSpan());
  109. $converter->addConverter(new AMPAhref());
  110. $converter->addConverter(new AMPP());
  111. $content = $converter->convert($s);
  112. $content = str_replace( 'src="/', 'src="https://www.amic.ru/', $content ); //??
  113. $content = str_replace( 'http://', 'https://', $content );
  114. $content = str_replace('<div>&nbsp;</div>','', $content);
  115. $amp_scripts = $converter->getScripts();
  116. $yurl = $model->getYoutubeEmbedLink();
  117. /*
  118. ############
  119. // вставка youtube видео если Embed youtube
  120. ############
  121. */
  122. $shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_-]+)\??/i';
  123. $longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i';
  124. $youtube_key = false;
  125. if (preg_match($longUrlRegex, $yurl, $matches)) {
  126. $youtube_key = $matches[count($matches) - 1];
  127. }
  128. if (preg_match($shortUrlRegex, $yurl, $matches)) {
  129. $youtube_key = $matches[count($matches) - 1];
  130. }
  131. if( $youtube_key ){
  132. $amp_scripts[] ='<script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>';
  133. }
  134. /*
  135. ############
  136. // вставка VK видео если Embed VK
  137. ############
  138. */
  139. $shortUrlRegex = '/vk.com\/([a-zA-Z0-9_-]+)\??/i';
  140. $vk_key = false;
  141. if (preg_match($shortUrlRegex, $yurl, $matches)) {
  142. $vk_key = $yurl;
  143. }
  144. if( $vk_key ){
  145. // $amp_scripts[] = '<script nomodule src="https://cdn.ampproject.org/bento.js" crossorigin="anonymous"></script>';
  146. // $amp_scripts[] = '<script nomodule src="https://cdn.ampproject.org/v0/bento-iframe-1.0.js" crossorigin="anonymous"></script>';
  147. $amp_scripts[] = '<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>';
  148. // $amp_scripts[] = '<script async custom-element="amp-video-iframe" src="https://cdn.ampproject.org/v0/amp-video-iframe-0.1.js"></script>';
  149. }
  150. $re = '/##_gallery-(\d+)##/mU';
  151. if( preg_match($re, $content) ){
  152. $content = processGalleriesInjects($content, $model);
  153. $amp_scripts[] ='<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>';
  154. }
  155. $this->context->scripts = '';
  156. foreach( array_unique($amp_scripts) as $p_scripts ){
  157. $this->context->scripts .= $p_scripts."\n";
  158. }
  159. $text = trim($content);
  160. $text = str_replace(["\n","\r","&nbsp;"],'',$text);
  161. $text = str_replace(["&nbsp;"],' ',$text);
  162. $text = addslashes( strip_tags( $text ) );
  163. ?>
  164. <!-- Schema.org -->
  165. <script type="application/ld+json">
  166. {
  167. "@context": "http://schema.org",
  168. "@type": "NewsArticle",
  169. "mainEntityOfPage": {
  170. "@type": "WebPage",
  171. "@id": "<?= $model->getUrl(true) ?>"
  172. },
  173. "identifier":{"@type":"PropertyValue","propertyID":"WebPage","value":"<?= $model->uid?>"},
  174. "headline": "<?= $title ?>",
  175. "description": "<?= $lid ?>",
  176. <?php if ($picture) :?>
  177. "image": {
  178. "@type": "ImageObject",
  179. "representativeOfPage": "true",
  180. "url": "<?= $picture; ?>",
  181. "width": 1040,
  182. "height": 586
  183. },
  184. <?php endif ;?>
  185. "dateCreated": "<?= date(DATE_RSS, strtotime($model->dt_cr)); ?>",
  186. "dateModified": "<?= date(DATE_RSS, strtotime($model->dt_upd)); ?>",
  187. "datePublished": "<?= date(DATE_RSS, strtotime($model->dt_pub)); ?>",
  188. "author": {
  189. "@type": "Person",
  190. "name": "<?=$authorName ?>"
  191. },
  192. "publisher": {
  193. "@type": "Organization",
  194. "name": "<?= Yii::$app->params['orgname'] ?>",
  195. "logo": {
  196. "@type": "ImageObject",
  197. "url": "<?= Url::to('/img/amic-logo.svg', 'https') ?>",
  198. "width": 91,
  199. "height": 28
  200. }
  201. }
  202. }
  203. </script>
  204. <!-- !Schema.org -->
  205. <div class="block px3 pt2 mb2" data-newsId="<?= $model->id; ?>">
  206. <time class="pubdate block bold my1"><?= strftime('%e %B %Y, %H:%M', strtotime($model->dt_pub)); ?>, <?=$authorName?></time>
  207. <h1 class="mb1 px3"><?= $model->title ?></h1>
  208. <?php if ($youtube_key) : ?>
  209. <div class="amp-main-img">
  210. <amp-youtube data-videoid="<?= $youtube_key?>" width="358" height="204" layout="responsive" >
  211. <amp-img src="<?= $picture?>" placeholder layout="fill" alt="<?= \yii\helpers\Html::encode($model->photo_title); ?>" />
  212. </amp-youtube>
  213. </div>
  214. <?php endif; ?>
  215. <?php if ($vk_key) : ?>
  216. <amp-iframe id="myVideo" src="<?= $vk_key?>" width="160" height="90" layout="responsive" sandbox="allow-scripts allow-same-origin">
  217. <amp-img placeholder src="<?= $picture?>" layout="fill">
  218. </amp-img>
  219. </amp-iframe>
  220. <?php endif; ?>
  221. <div class="amp-preview"><?= $model->lid; ?></div>
  222. <?php if (!empty($picture) && $model->photo_include == 'Y' && !$youtube_key && !$vk_key) : ?>
  223. <div class="amp-main-img">
  224. <amp-img class="amp-main-img my3" alt="<?= \yii\helpers\Html::encode($model->title) ?>" src="<?= $picture; ?>" width="1040"
  225. height="586"
  226. layout="responsive"></amp-img>
  227. <div class="amp-img-title">
  228. <?= $model->photo_title; ?>
  229. </div>
  230. </div>
  231. <?php endif; ?>
  232. <div class="amp-news-content-text">
  233. <?= $content ?>
  234. </div>
  235. <a href="<?= $model->getUrl($schema=true) ?>" class="amp-button">Читайте полную версию на сайте</a>
  236. <section class="text">
  237. <?=($model->inscription == 2)?'<span class="advertisement">На правах рекламы</span>':''?>
  238. <?=($model->inscription == 1)?'<span class="advertisement">На правах рекламы</span><br><span class="med_advertisement">Имеются противопоказания, необходима консультация специалиста</span>':''?>
  239. <?=($model->inscription == 3)?'<span class="advertisement">Партнёр amic.ru</span>':''?>
  240. </section>
  241. <?=$this->render('@app/views/_etc/banners/amp1')?>
  242. </div>
  243. <?
  244. function processGalleriesInjects(string $body, News $post)
  245. {
  246. $re = '/##_gallery-(\d+)##/mU';
  247. $res = preg_replace_callback($re,function (array $matches): string
  248. {
  249. $gallery = \app\models\front\Gallery::findOne(['id'=>ArrayHelper::getValue($matches,1)]);
  250. if($gallery instanceof \app\models\front\Gallery) {
  251. return \Yii::$app->view->render("/news/view/amp-gallery",["gallery"=>$gallery]);
  252. } else {
  253. return "";
  254. }
  255. },$body);
  256. return $res;
  257. }