123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?php
- namespace rss;
- use Yii;
- use rss\BaseModule;
- use yii\base\InvalidConfigException;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Url;
- use \app\models\News;
- use yii\base\Module;
- use app\models\Authors;
- use yii\db\Expression;
- /**
- * Yandex.Turbo module definition class
- */
- class Rss extends BaseModule
- {
- /**
- * {@inheritdoc}
- */
- public $controllerNamespace = 'rss\controllers';
- /**
- * {@inheritdoc}
- */
- public $defaultRoute = "default";
- /**
- * @var string, the name of module
- */
- public $name = "Yandex.Turbo";
- /**
- * @var string, the description of module
- */
- public $description = "Новости Алтайского края";
- /**
- * @var array list of supported models for displaying a Turbo-pages feed
- */
- public $supportModels = [
- 'News' => '\models\News',
- ];
- /**
- * @var int cache lifetime, `0` - for not use cache
- */
- public $cacheExpire = 180;
- /**
- * @var array default channel options
- */
- public $channelOptions = [];
- /**
- * @var string default route to render Turbo-pages feed (use "/" - for root)
- */
- public $turboRoute = "/rss/turbo";
- /**
- * @var string the module version
- */
- private $version = "1.0.4";
- /**
- * @var integer, priority of initialization
- */
- private $priority = 5;
- /**
- * {@inheritdoc}
- */
- public function init()
- {
- parent::init();
- // Set version of current module
- $this->setVersion($this->version);
- // Set priority of current module
- $this->setPriority($this->priority);
- // Process and normalize route for frontend
- $this->turboRoute = self::normalizeRoute($this->turboRoute);
- // $this->bootstrap(Yii::$app);
- }
- /**
- * {@inheritdoc}
- */
- public function dashboardNavItems($createLink = false)
- {
- $items = [
- 'label' => $this->name,
- 'icon' => 'fa fa-fw fa-rocket',
- 'url' => [$this->routePrefix . '/'. $this->id],
- 'active' => (in_array(\Yii::$app->controller->module->id, [$this->id]) && Yii::$app->controller->id == 'list'),
- ];
- return $items;
- }
- /**
- * {@inheritdoc}
- */
- public function bootstrap($app)
- {
- parent::bootstrap($app);
- if (isset(Yii::$app->params["turbo.supportModels"]))
- $this->supportModels = Yii::$app->params["turbo.supportModels"];
- if (isset(Yii::$app->params["turbo.cacheExpire"]))
- $this->cacheExpire = Yii::$app->params["turbo.cacheExpire"];
- if (isset(Yii::$app->params["turbo.channelOptions"]))
- $this->channelOptions = Yii::$app->params["turbo.channelOptions"];
- if (isset(Yii::$app->params["turbo.turboRoute"]))
- $this->turboRoute = Yii::$app->params["turbo.turboRoute"];
- if (!isset($this->supportModels))
- throw new InvalidConfigException("Required module property `supportModels` isn't set.");
- if (!isset($this->cacheExpire))
- throw new InvalidConfigException("Required module property `cacheExpire` isn't set.");
- if (!isset($this->channelOptions))
- throw new InvalidConfigException("Required module property `channelOptions` isn't set.");
- if (!isset($this->turboRoute))
- throw new InvalidConfigException("Required module property `turboRoute` isn't set.");
- if (!is_array($this->supportModels))
- throw new InvalidConfigException("Module property `supportModels` must be array.");
- if (!is_array($this->channelOptions))
- throw new InvalidConfigException("Module property `channelOptions` must be array.");
- if (!is_integer($this->cacheExpire))
- throw new InvalidConfigException("Module property `cacheExpire` must be integer.");
- if (!is_string($this->turboRoute))
- throw new InvalidConfigException("Module property `turboRoute` must be a string.");
- // Add route to pass turbo-pages in frontend
- $turboRoute = $this->turboRoute;
- if (empty($turboRoute) || $turboRoute == "/") {
- $app->getUrlManager()->addRules([
- [
- 'pattern' => '/rss',
- 'route' => '/rss/default',
- 'suffix' => '.xml'
- ],
- '/turbo.xml' => '/rss/default'
- ], true);
- } else if (is_string($turboRoute)) {
- $app->getUrlManager()->addRules([
- [
- 'pattern' => $turboRoute . '/feed',
- 'route' => 'rss/default',
- 'suffix' => '.xml'
- ],
- $turboRoute . '/feed.xml' => 'rss/default'
- ], true);
- $app->getUrlManager()->addRules([
- [
- 'pattern' => $turboRoute . '/turbo',
- 'route' => 'rss/default/turbo',
- 'suffix' => '.xml'
- ],
- $turboRoute . '/feed.xml' => 'rss/default'
- ], true);
- $app->getUrlManager()->addRules([
- [
- 'pattern' => $turboRoute . '/dzen',
- 'route' => 'rss/default/dzen',
- 'suffix' => '.xml'
- ],
- $turboRoute . '/dzen.xml' => 'rss/default'
- ], true);
- $app->getUrlManager()->addRules([
- [
- 'pattern' => $turboRoute . '/ria',
- 'route' => 'rss/default/ria',
- 'suffix' => '.xml'
- ],
- $turboRoute . '/ria.xml' => 'rss/default'
- ], true);
- }
- // echo $turboRoute;
- // Attach to events of create/change/remove of models for the subsequent clearing cache of feeds
- if (!($app instanceof \yii\console\Application)) {
- if ($cache = $app->getCache()) {
- if (is_array($models = $this->supportModels)) {
- foreach ($models as $name => $class) {
- if (class_exists($class)) {
- $model = new $class();
- \yii\base\Event::on($class, $model::EVENT_AFTER_INSERT, function ($event) use ($cache) {
- $cache->delete(md5('yandex-turbo'));
- });
- \yii\base\Event::on($class, $model::EVENT_AFTER_UPDATE, function ($event) use ($cache) {
- $cache->delete(md5('yandex-dzen'));
- });
- \yii\base\Event::on($class, $model::EVENT_AFTER_DELETE, function ($event) use ($cache) {
- $cache->delete(md5('yandex-ria'));
- });
- }
- }
- }
- }
- }
- }
- /**
- * Generate current RSS-feed URL
- *
- * @return null|string
- */
- public function getFeedURL() {
- $url = null;
- $turboRoute = $this->turboRoute;
- if (empty($turboRoute) || $turboRoute == "/") {
- $url = Url::to('/turbo.xml', true);
- } else {
- $url = Url::to($turboRoute . '/feed.xml', true);
- }
- return $url;
- }
- /**
- * Get items for building a Yandex.Turbo pages
- *
- * @return array
- */
- public function getTurboItems() {
- $items = [];
- if (is_array($models = $this->supportModels)) {
- foreach ($models as $name => $class) {
- // If class of model exist
- if (class_exists($class)) {
- $model = new $class();
- $newsDataProvider = new \yii\data\ActiveDataProvider(
- [
- "query"=>\app\models\News::find()->andwhere(['active'=>'Y'])->andWhere(['between', 'news.dt_pub', date("Y-m-d H:i:00",time()-2678400*1 ) , date("Y-m-d H:i:00")])->orderBy(["dt_pub"=>SORT_DESC])->limit(200),
- "pagination" =>[
- "pageSize"=>200
- ],
- ]
- );
- // If module is loaded
- if (1) {
- $append = [];
- foreach ($newsDataProvider->getModels() as $item) {
- $turbo = true;
- if( stripos( $item->text, '<script') !== false ) $turbo = false;
- if( stripos( $item->text, '##_gallery-') !== false ) $turbo = false;
- //Сюжеты
- $story = new \app\models\base\Story();
- $stn = $story->getForNews($item->id);
- $tstory = null;
- if( count($stn) > 0 ){
- $tstory = $stn[0]->url?:null;
- }
- // Рубрики
- $topic = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->one();
- $tcategory = null;
- $stopnews = false;
- $riatop = false;
- if($topic instanceof \app\models\base\NewsTopic){
- $tcategory = $topic->title;
- }
- $atopics = [];
- $topics = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->all();
- foreach( $topics as $aa ){
- $atopics[] = ['title'=>$aa->title,'url'=>$aa->getUrl(true)];
- if( $aa->id == 109 ) $riatop = true;
- if( $aa->id == 137 ) $riatop = true;
- }
- if( $item->show_author == 'Y' )
- {
- $author = Authors::getAuthor( $item->author );
- $authorName = $author->name;
- }else{
- $authorName = 'ИА Амител';
- }
- // $stopnews = false;
- $append[] = [
- 'url' => $item->getUrl(),
- 'name' => (isset($item->name)) ? $item->name : null,
- 'title' => (isset($item->title)) ? $item->title : null,
- 'image' => ($item->getPreview()) ? $item->getPreview()->getUrl(2,'jpg',true) : null,
- 'description' => (isset($item->lid)) ? trim($item->lid) : ((isset($item->description)) ? trim($item->description) : null),
- 'content' => (isset($item->text)) ? self::prepare($item->text, $item) : null,
- 'newscontent' => (isset($item->text)) ? self::prepare($item->text, $item) : null,
- 'stext' => (isset($item->text)) ? self::prepare2($item->text, $item) : null,
- 'updated_at' => (isset($item->dt_pub)) ? $item->dt_pub : null,
- 'status' => $turbo, // отключение турбо
- 'export_rss' => $item->export_rss,
- 'type' => $item->type,
- 'story' => null, //$tstory,
- 'category'=> $tcategory,
- 'topics'=>$atopics,
- 'uid' => $item->uid,
- 'author'=>$authorName,
- 'top'=>($item->top == 'Y' || $item->NH == 'Y' || $item->NH == 'F'),
- 'riatop'=>$riatop,
- 'stopnews' =>$stopnews
- ];
- };
- //print_r($append);
- $items = ArrayHelper::merge($items, $append);
- }
- }
- }
- }
- return $items;
- }
- /**
- * Get items for building a Yandex.Turbo pages
- *
- * @return array
- */
- public function getTopicsItems($gtopic) {
- $items = [];
- if (is_array($models = $this->supportModels)) {
- foreach ($models as $name => $class) {
- // If class of model exist
- if (class_exists($class)) {
- $model = new $class();
- $newsDataProvider = new \yii\data\ActiveDataProvider(
- [
- "query"=>\app\models\News::find()->from(new Expression("news FORCE INDEX (top,calendar) , news_topic_relation t USE index(news_id)"))
- ->andwhere(['active'=>'Y'])->andWhere(['between', 'news.dt_pub', "2023-06-27 12:00:00" , date("Y-m-d H:i:00")])->orderBy(["dt_pub"=>SORT_DESC])
- ->andWhere(new Expression('t.news_id=id'))
- ->andWhere(['t.topic_id'=>$gtopic])
- ->limit(200),
- "pagination" =>[
- "pageSize"=>200
- ],
- ]
- );
- // If module is loaded
- if (1) {
- $append = [];
- foreach ($newsDataProvider->getModels() as $item) {
- $turbo = true;
- if( stripos( $item->text, '<script') !== false ) $turbo = false;
- if( stripos( $item->text, '##_gallery-') !== false ) $turbo = false;
- //Сюжеты
- $story = new \app\models\base\Story();
- $stn = $story->getForNews($item->id);
- $tstory = null;
- if( count($stn) > 0 ){
- $tstory = $stn[0]->url?:null;
- }
- // Рубрики
- $topic = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->one();
- $tcategory = null;
- if($topic instanceof \app\models\base\NewsTopic){
- $tcategory = $topic->title;
- }
- $atopics = [];
- $topics = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->all();
- $stopnews = false;
- foreach( $topics as $aa ){
- $atopics[] = ['title'=>$aa->title,'url'=>$aa->getUrl(true)];
- }
- if( $item->show_author == 'Y' )
- {
- $author = Authors::getAuthor( $item->author );
- $authorName = $author->name;
- }else{
- $authorName = 'ИА Амител';
- }
- $re = '|<div[^>]+>(\d+)<\/div>|is';
- $text = preg_replace($re, '', $item->text );
- $append[] = [
- 'url' => $item->getUrl(),
- 'name' => (isset($item->name)) ? $item->name : null,
- 'title' => (isset($item->title)) ? $item->title : null,
- 'image' => ($item->getPreview()) ? $item->getPreview()->getUrl(2,'jpg',true) : null,
- 'description' => (isset($item->lid)) ? trim($item->lid) : ((isset($item->description)) ? trim($item->description) : null),
- 'content' => (isset($item->text)) ? self::prepare($text, $item) : null,
- 'newscontent' => (isset($item->text)) ? self::prepare($text, $item) : null,
- 'stext' => (isset($item->text)) ? self::prepare2($text, $item) : null,
- 'updated_at' => (isset($item->dt_pub)) ? $item->dt_pub : null,
- 'status' => $turbo, // отключение турбо
- 'export_rss' => $item->export_rss,
- 'type' => $item->type,
- 'story' => null, //$tstory,
- 'category'=> $tcategory,
- 'topics'=>$atopics,
- 'uid' => $item->uid,
- 'author'=>$authorName,
- 'top'=>($item->top == 'Y' || $item->NH == 'Y' || $item->NH == 'F'),
- 'stopnews' =>$stopnews
- ];
- };
- //print_r($append);
- $items = ArrayHelper::merge($items, $append);
- }
- }
- }
- }
- return $items;
- }
- public function prepare( $text, $item )
- {
- // $f = News::find()->andWhere(['id'=>$item->id])->one();
- $text = $this->processInjects($text);
- $text = $this->processGalleriesInjects($text);
- $text = preg_replace('|src="/([^/])|im','src="https://www.amic.ru/${1}',$text);
- $text = preg_replace('|href="/([^/])|im','href="https://www.amic.ru/${1}',$text);
- $text = preg_replace('/[\x00-\x09\x0B-\x0C\x0E-\x1F]/', '', $text);
- $text = preg_replace("/<span class=\"style3\">.+<\/span>/i",'',$text);
- $text = str_replace('­','',$text);
- $text = str_replace(' ',' ',$text);
- $text = str_replace('<p> </p>',' ',$text);
- $text = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $text);
- //$text = News::processInjects($text);
- // var_dump($f);
- //$text = $f->renderBody();
- // $f->renderBody();
- // $text = CutLastInject( $text, $a['id'] );
- // $text = ShowInject( $text );
- // $text = str_replace( 'id="inject"', '', $text );
- // $text = ShowGalleryWidgetm( $text );
- $text = strip_tags( $text,'<p></p><br></br><span></span><div></div><img><a></a><i></i><b></b><sup></sup><ul></ul><li></li><table></table><td></td><tr></tr><h2></h2><h3></h3><blockquote></blockquote>' );
- $text = str_replace('<p></p>',' ',$text);
- $text = preg_replace('#\s(id)="[^"]+"#', '', $text);
- return $text;
- }
- public function prepare2( $text, $item )
- {
- // $f = News::find()->andWhere(['id'=>$item->id])->one();
- //$text = $this->processInjects($text);
- if( $item->embed_url != '' ){
- $text = '<iframe src="'.$item->embed_url.'" style="position: absolute; z-index: 2;" title="video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="100%" height="100%" frameborder="0"></iframe>'.$text;
- }
- $text = $this->processGalleriesInjects($text);
- $text = preg_replace('|src="/([^/])|im','src="https://www.amic.ru/${1}',$text);
- $text = preg_replace('|href="/([^/])|im','href="https://www.amic.ru/${1}',$text);
- $text = preg_replace('/[\x00-\x09\x0B-\x0C\x0E-\x1F]/', '', $text);
- $text = preg_replace("/<span class=\"style3\">.+<\/span>/i",'',$text);
- $text = str_replace('­','',$text);
- $text = str_replace(' ',' ',$text);
- $text = str_replace('<p> </p>',' ',$text);
- $text = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $text);
- //$text = News::processInjects($text);
- // var_dump($f);
- //$text = $f->renderBody();
- // $f->renderBody();
- // $text = CutLastInject( $text, $a['id'] );
- // $text = ShowInject( $text );
- // $text = str_replace( 'id="inject"', '', $text );
- // $text = ShowGalleryWidgetm( $text );
- $re = '|<div[^>]+>(\s+)<\/div>|is';
- $text = preg_replace($re, '', $text );
- $text = strip_tags( $text,'<p></p><br></br><span></span><div></div><img><a></a><i></i><b></b><sup></sup><ul></ul><li></li><table></table><td></td><tr></tr><h2></h2><h3></h3><blockquote></blockquote><iframe></iframe>' );
- $text = str_replace('<p></p>',' ',$text);
- $text = preg_replace('#\s(id)="[^"]+"#', '', $text);
- return $text;
- }
- public 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("@app/views/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+)\/?$/iU", $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;
- }
- if( $id ){
- //$attributes['type'] to do
- $post = News::findOne(['id'=>$id]);
- if($post instanceof News) {
- if( $attributes['type'] == 1 ){
- return \Yii::$app->view->render("@app/views/news/view/inject1",["post"=>$post]);
- }else if( $attributes['type'] == 2 ){
- return \Yii::$app->view->render("@app/views/news/view/inject2",["post"=>$post]);
- }else{
- return \Yii::$app->view->render("@app/views/news/view/inject",["post"=>$post]);
- }
- } else {
- return "Битый инжект";
- }
- }
- }
- }
- return ArrayHelper::getValue($matches,0);
- },$text);
- return $res;
- }
- public function processGalleriesInjects(string $body)
- {
- $re = '/##_gallery-(\d+)##/mU';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $gallery = \app\models\front\Gallery::findOne(['id'=>ArrayHelper::getValue($matches,1)]);
- if($gallery instanceof \app\models\front\Gallery) {
- return \Yii::$app->view->render("@app/views/news/view/inject-gallery",["gallery"=>$gallery]);
- } else {
- return "";
- }
- },$body);
- return $res;
- }
- }
|