<?php
/*
=====================================================
DataLife Engine - by SoftNews Media Group
-----------------------------------------------------
https://dle-news.ru/
-----------------------------------------------------
Copyright (c) 2004-2024 SoftNews Media Group
=====================================================
This code is protected by copyright
=====================================================
File: rssinform.php
-----------------------------------------------------
USe: RSS informers
=====================================================
*/

if( !defined('DATALIFEENGINE') ) {
	header( "HTTP/1.1 403 Forbidden" );
	header ( 'Location: ../../' );
	die( "Hacking attempt!" );
}

$informers = get_vars( "informers" );

if( ! is_array( $informers ) ) {
	$informers = array ();
	
	$db->query( "SELECT * FROM " . PREFIX . "_rssinform ORDER BY id ASC" );
	
	while ( $row_b = $db->get_row() ) {
		
		$informers[$row_b['id']] = array ();
		
		foreach ( $row_b as $key => $value ) {
			$informers[$row_b['id']][$key] = stripslashes( $value );
		}
	
	}
	set_vars( "informers", $informers );
	$db->free();
}

$allow_cache = $config['allow_cache'];
$config['allow_cache'] = 1;
$temp = array ();
$i = 0;

if( count( $informers ) ) {
	foreach ( $informers as $name => $value ) {
		if( $value['approve'] ) {
			

			if( $value['category'] ) {
				$value['category'] = explode( ',', $value['category'] );
				
				if( ! in_array( $category_id, $value['category'] ) ) $value['url'] = "";
			}
			
			$temp[$value['tag']][$i]['id'] = $value['id'];
			$temp[$value['tag']][$i]['tag'] = $value['tag'];
			$temp[$value['tag']][$i]['url'] = $value['url'];
			$temp[$value['tag']][$i]['template'] = $value['template'];
			$temp[$value['tag']][$i]['news_max'] = $value['news_max'];
			$temp[$value['tag']][$i]['tmax'] = $value['tmax'];
			$temp[$value['tag']][$i]['dmax'] = $value['dmax'];
			$temp[$value['tag']][$i]['rss_date_format'] = $value['rss_date_format'];
		
		}
		$i ++;
	}
	
	foreach ( $temp as $key => $value ) {
		
		$r_key = array_rand( $temp[$key] );
		$temp[$key] = $temp[$key][$r_key];
	
	}
	
	$informers = array ();
	
	foreach ( $temp as $key => $value ) {
		
		if( $value['url'] == "" ) {
			
			$informers[$value['tag']] = "";
			continue;
		
		}
		
		$buffer = dle_cache( "informer_" . $value['id'], $config['skin'] );

		if ( $buffer !== false) {

			$file_date = @filemtime( ENGINE_DIR.'/cache/informer_'.$value['id'].'_'.md5($config['skin']).'.tmp' );

			if ( $file_date ) {

				if (date ( "d-H", $file_date ) != date ( "d-H" )) {

					$buffer = false;
					@unlink( ENGINE_DIR.'/cache/informer_'.$value['id'].'_'.md5($config['skin']).'.tmp' );

				}

			}

		}
		
		if( $buffer === false ) {

			include_once (DLEPlugins::Check(ENGINE_DIR . '/classes/rss.class.php'));
			
			$xml = new DLExmlParser( stripslashes( $value['url'] ), $value['news_max'] );
						
			$xml->pre_parse( 0 );

			$tpl->load_template( $value['template'] . '.tpl' );
			
			foreach ( $xml->content as $content ) {
				
				if ( isset($content['title']) ) {
					$content['title'] = trim(strip_tags( $content['title'] ));
				} else $content['title'] = "";
				
				if ( isset($content['category']) ) {
					$content['category'] = trim( strip_tags( $content['category'] ) );
				} else $content['category'] = "";
				
				if ( isset($content['author']) ) {
					$content['author'] = trim( strip_tags( $content['author'] ) );
				} else $content['author'] = "";
				
				if( $value['tmax'] and dle_strlen( $content['title'] ) > $value['tmax'] ) $content['title'] = dle_substr( $content['title'], 0, $value['tmax'] ) . " ...";
				
				if (stripos ( $tpl->copy_template, "{image-" ) !== false) {
		
					$images = array();
					preg_match_all('/(img|src)=("|\')[^"\'>]+/i', $content['description'], $media);
					$data=preg_replace('/(img|src)("|\'|="|=\')(.*)/i',"$3",$media[0]);
		
					foreach($data as $url) {
						$info = pathinfo($url);
						if (isset($info['extension'])) {
							if ($info['filename'] == "spoiler-plus" OR $info['filename'] == "spoiler-minus" OR strpos($info['dirname'], 'engine/data/emoticons') !== false) continue;
							$info['extension'] = strtolower($info['extension']);
							if (($info['extension'] == 'jpg') || ($info['extension'] == 'jpeg') || ($info['extension'] == 'gif') || ($info['extension'] == 'png') || ($info['extension'] == 'webp') || ($info['extension'] == 'bmp') || ($info['extension'] == 'avif')) array_push($images, $url);
						}
					}
		
					if ( count($images) ) {
						
						$i=0;
						
						foreach($images as $url) {
							$i++;
							$tpl->copy_template = str_replace( '{image-'.$i.'}', $url, $tpl->copy_template );
							$tpl->copy_template = str_replace( '[image-'.$i.']', "", $tpl->copy_template );
							$tpl->copy_template = str_replace( '[/image-'.$i.']', "", $tpl->copy_template );
						}
		
					} elseif( $content['image'] ) {
						$tpl->copy_template = str_replace( '{image-1}', $content['image'], $tpl->copy_template );
						$tpl->copy_template = str_replace( '[image-1]', "", $tpl->copy_template );
						$tpl->copy_template = str_replace( '[/image-1]', "", $tpl->copy_template );
					}
		
					$tpl->copy_template = preg_replace( "#\[image-(.+?)\](.+?)\[/image-(.+?)\]#is", "", $tpl->copy_template );
					$tpl->copy_template = preg_replace( "#\\{image-(.+?)\\}#i", "{THEME}/dleimages/no_image.jpg", $tpl->copy_template );
		
				}
				
				$content['description'] = str_replace( "<br>", " ", str_replace( "<br />", " ", $content['description'] ) );
				$content['description'] = strip_tags( $content['description'] );
				$content['description'] = trim( $content['description'] );
				
				if( $value['dmax'] and dle_strlen( $content['description'] ) > $value['dmax'] ) {
					
					$content['description'] = dle_substr( $content['description'], 0, $value['dmax'] );
					
					if( ($temp_dmax = dle_strrpos( $content['description'], ' ' )) ) $content['description'] = dle_substr( $content['description'], 0, $temp_dmax );
					
					$content['description'] .= " ...";
				
				}

				$content['link'] = htmlspecialchars ($content['link'], ENT_QUOTES, 'UTF-8');

				$tpl->set( '{title}', $content['title'] );
				$tpl->set( '{news}', $content['description'] );
				$tpl->set( '[link]', "<a href=\"{$content['link']}\" target=\"_blank\">" );
				$tpl->set( '[/link]', "</a>" );
				$tpl->set( '{link}', $content['link'] );
				$tpl->set( '{category}', $content['category'] );
				$tpl->set( '{author}', $content['author'] );
				$tpl->set( '{date}', langdate( $value['rss_date_format'], $content['date'] ) );

				$news_date = $content['date'];
				$tpl->copy_template = preg_replace_callback ( "#\{date=(.+?)\}#i", "formdate", $tpl->copy_template );
				
				$tpl->compile( 'rss_info' );
				
			}
			
			if( isset($tpl->result['rss_info']) AND $tpl->result['rss_info'] ) {
				$buffer = $tpl->result['rss_info'];
			} else $buffer = '';


			$tpl->result['rss_info'] = '';
			$tpl->clear();

			create_cache( "informer_" . $value['id'], $buffer, $config['skin'] );
		
		}
		
		$informers[$value['tag']] = $buffer;
	
	}

}

$temp = array ();
unset( $temp );
$config['allow_cache'] = $allow_cache;

?>