<?
if (!(defined ('DATALIFEENGINE')))
{
exit ('Hacking attempt!');
}
class rss_parser
{
var $default_cp = 'cp1251';
var $CDATA = 'nochange';
var $cp = '';
var $items_limit = 0;
var $stripHTML = False;
var $date_format = '';
var $channeltags = array (0 =>'title',1 =>'link',2 =>'description',3 =>'language',4 =>'copyright',5 =>'managingEditor',6 =>'webMaster',7 =>'lastBuildDate',8 =>'rating',9 =>'docs');
var $itemtags = array (0 =>'title',1 =>'link',2 =>'description',3 =>'author',4 =>'category',5 =>'comments',6 =>'enclosure',7 =>'guid',8 =>'pubDate',9 =>'source');
var $imagetags = array (0 =>'title',1 =>'url',2 =>'link',3 =>'width',4 =>'height');
var $textinputtags = array (0 =>'title',1 =>'description',2 =>'name',3 =>'link');
function get ($rss_url ,$proxy)
{
$result = $this->Parse ($rss_url,$proxy);
return $result;
}
function my_preg_match ($pattern,$subject)
{
preg_match ($pattern,$subject,$out);
if (isset ($out[1]))
{
if ($this->CDATA == 'content')
{
$out[1] = strtr ($out[1],array ('<![CDATA['=>'',']]>'=>''));
}
else
{
if ($this->CDATA == 'strip')
{
$out[1] = strtr ($out[1],array ('<![CDATA['=>'',']]>'=>''));
}
}
return trim ($out[1]);
}
return '';
}
function unhtmlentities ($string)
{
$trans_tbl = get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
$trans_tbl = array_flip ($trans_tbl);
$trans_tbl += array ('''=>'\'');
return strtr ($string,$trans_tbl);
}
function parse ($rss_url ,$proxy)
{
global $row,$config_rss,$config;
$rss_content = '';
if(function_exists( 'curl_init') ) {
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL,$rss_url );
if ($proxy == 1){
if ($config_rss['proxy_file'] == "yes"or $config_rss['proxy'] == ''){
$proxy_url = @file(ENGINE_DIR."/inc/plugins/files/proxy.txt");
$proxy_url = $proxy_url[array_rand($proxy_url)];
}else{$proxy_url = $config_rss['proxy'];}
if (trim($proxy_url) != '')curl_setopt($ch,CURLOPT_PROXY,trim($proxy_url));
}
curl_setopt($ch,CURLOPT_REFERER,$rss_url);
curl_setopt( $ch,CURLOPT_USERAGENT,get_random_agent () );
@curl_setopt( $ch,CURLOPT_FOLLOWLOCATION,1 );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER,1 );
curl_setopt ($ch,CURLOPT_AUTOREFERER,1);
curl_setopt( $ch,CURLOPT_CONNECTTIMEOUT,10 );
$rss_content .= curl_exec( $ch );
curl_close( $ch );
}else{
$URL = get_urls($rss_url);
$host = $URL['host'];
$path = $URL['path'];
$query = $URL['query'];
$others = "";
$post="GET $path HTTP/1.1\r\nHost: $host\r\nContent-type: application/x-www-form-urlencoded\r\n{$others}User-Agent: ".get_random_agent ()."\r\nContent-length: ".strlen($query)."\r\nConnection: close\r\n\r\n$query";
$h=@fsockopen($host,80,$errno,$errstr,5);
if (!$h) {
return false;
}
else
{
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
}
$rss_content .= $r;
}
if ($rss_content != '')
{
preg_match ('#<.*?encoding="(.*?)".*?>#i',$rss_content,$charset);
if ($charset[1] =='')preg_match ('#<.*?encoding=\'(.*?)\'.*?>#i',$rss_content,$charset);
if (strtolower($charset[1]) != strtolower($config['charset'])) {$rss_content = @iconv(strtolower($charset[1]),strtolower($config['charset']).'//TRANSLIT',$rss_content);}
$result['html_title'] = $this->my_preg_match ("|<title>(.+)</title>|iU",$rss_content);
preg_match ('\'<channel.*?>(.*?)<item.*?>\'si',$rss_content,$out_channel);
foreach ($this->channeltags as $channeltag)
{
$temp = $this->my_preg_match ('\'<'.$channeltag .'.*?>(.*?)</'.$channeltag .'>\'si',$out_channel[1]);
if ($temp != '')
{
$result[$channeltag] = $temp;
continue;
}
}
if ($this->date_format != '')
{
if ($timestamp = strtotime ($result['lastBuildDate']) !== -1)
{
$result['lastBuildDate'] = date ($this->date_format,$timestamp);
}
}
preg_match ('\'<textinput(|[^>]*[^/])>(.*?)</textinput>\'si',$rss_content,$out_textinfo);
if (isset ($out_textinfo[2]))
{
foreach ($this->textinputtags as $textinputtag)
{
$temp = $this->my_preg_match ('\'<'.$textinputtag .'.*?>(.*?)</'.$textinputtag .'>\'si',$out_textinfo[2]);
if ($temp != '')
{
$result['textinput_'.$textinputtag] = $temp;
continue;
}
}
}
preg_match ('\'<image.*?>(.*?)</image>\'si',$rss_content,$out_imageinfo);
if (isset ($out_imageinfo[1]))
{
foreach ($this->imagetags as $imagetag)
{
$temp = $this->my_preg_match ('\'<'.$imagetag .'.*?>(.*?)</'.$imagetag .'>\'si',$out_imageinfo[1]);
if ($temp != '')
{
$result['image_'.$imagetag] = $temp;
continue;
}
}
}
preg_match_all ('\'<item(| .*?)>(.*?)</item>\'si',$rss_content,$items);
$rss_items = $items[2];
$i = 0;
$result['items'] = array ();
foreach ($rss_items as $rss_item)
{
if (!((!($i <$this->items_limit) AND !($this->items_limit == 0))))
{
foreach ($this->itemtags as $itemtag)
{
$temp = $this->my_preg_match ('\'<'.$itemtag .'.*?>(.*?)</'.$itemtag .'>\'si',$rss_item);
if ($temp != '')
{
$result['items'][$i][$itemtag] = $temp;
continue;
}
}
if ($this->stripHTML)
{
if ($result['items'][$i]['description'])
{
$result['items'][$i]['description'] = strip_tags ($this->unhtmlentities (strip_tags ($result['items'][$i]['description'])));
}
}
if ($this->stripHTML)
{
if ($result['items'][$i]['title'])
{
$result['items'][$i]['title'] = strip_tags ($this->unhtmlentities (strip_tags ($result['items'][$i]['title'])));
}
}
if ($this->date_format != '')
{
if ($timestamp = strtotime ($result['items'][$i]['pubDate']) !== -1)
{
$result['items'][$i]['pubDate'] = date ($this->date_format,$timestamp);
}
}
++$i;
continue;
}
}
$result['items_count'] = $i;
return $result;
}
return False;
}
}
class image_controller
{var $img;
var $short_story = '';
var $full_story = '';
var $allow_watermark = false;
var $images = array ();
var $thumbs = array ();
var $prefix = '';
var $upload_images = array ();
var $image = array ();
var $image_url = array ();
var $post = 'posts';
var $radikal;
function reset_url($url)
{
$value = str_replace("http://","",$url);
$value = str_replace("www.","",$value);
return reset(explode("/",$value));
}
function get_images ($content)
{
preg_match_all ('#\\[thumb.*?\\](.+?)\\[/thumb\\]#i',$content,$preg_array);
if (count ($preg_array[1]) != 0)
{
foreach ($preg_array[1] as $item)
{
if (!(in_array ($item,$this->images)))
{
$this->images[] = $item;
continue;
}
}
}
preg_match_all ('#\\[img.*?\\](.+?)\\[/img\\]#i',$content,$preg_array);
if (count ($preg_array[1]) != 0)
{
foreach ($preg_array[1] as $item)
{
if (!(in_array ($item,$this->images)))
{
$this->images[] = $item;
continue;
}
}
}
}
function download ($image_url,$i)
{
@set_time_limit (0);
global $config,$config_rss;
if (!is_dir(ROOT_DIR."/uploads/".$this->post)){
@mkdir(ROOT_DIR."/uploads/".$this->post,0777);
@chmod(ROOT_DIR."/uploads/".$this->post,0777);
}
if (!is_dir(ROOT_DIR."/uploads/".$this->post."/".date("Y-m"))) {
@mkdir(ROOT_DIR."/uploads/".$this->post."/".date("Y-m"),0777);
@chmod(ROOT_DIR."/uploads/".$this->post."/".date("Y-m"),0777);
@mkdir(ROOT_DIR."/uploads/".$this->post."/".date("Y-m")."/thumbs",0777);
@chmod(ROOT_DIR."/uploads/".$this->post."/".date("Y-m")."/thumbs",0777);
}
$image_news = basename ($image_url);
$image_arr = explode ('_',end (explode ('/',$image_news)));
if (count ($image_arr) != 0)
{
$image_new = end ($image_arr);
}
$image_new = $this->prefix +rand(1,100).'_'.$i.$image_new;
$image_new = str_replace ('%27','',$image_new);
if ($this->reset_url($_SERVER['HTTP_HOST'])!= $this->reset_url($image_url))
{
if(function_exists( 'curl_init') ) {
$ch=curl_init ($image_url);
if ($this->proxy == 1){
if ($config_rss['proxy_file'] == "yes"or $config_rss['proxy'] == ''){
$proxy_url = @file(ENGINE_DIR."/inc/plugins/files/proxy.txt");
$proxy_url = $proxy_url[array_rand($proxy_url)];
}else{$proxy_url = $config_rss['proxy'];}
if (trim($proxy_url) != '')curl_setopt($ch,CURLOPT_PROXY,trim($proxy_url));
}
$fp =fopen(UPLOAD_PATH .$image_new,'w+b');
curl_setopt ($ch,CURLOPT_FILE,$fp);
curl_setopt($ch,CURLOPT_USERAGENT,get_random_agent ());
curl_setopt ($ch,CURLOPT_REFERER,$image_url);
curl_setopt ($ch,CURLOPT_AUTOREFERER,1);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
}else{@copy($image_url ,UPLOAD_PATH .$image_new);}
if (@file_exists(UPLOAD_PATH .$image_new)){@chmod( UPLOAD_PATH .$image_new,0666 );}
}else{
@rename (ROOT_DIR."/uploads/posts/".date("Y-m")."/".$image_news,UPLOAD_PATH .$image_new);
if(@file_exists(ROOT_DIR."/uploads/posts/".date("Y-m")."/thumbs/".$image_news)){@rename (ROOT_DIR."/uploads/posts/".date("Y-m")."/thumbs/".$image_news,THUMB_PATH.$image_new);}
}
if (!(in_array ($image_url,$this->image)))
{
$this->image[$image_new] = $image_url;
}
}
function download_radikal ($image_url)
{
global $config,$config_rss;
if ($config_rss['water_radikal'] == "yes")$water_radikal = "&XE=yes&X=".$config['http_home_url'];
if ($config_rss['url_radikal'] == "1") $thumbs = "&O=yes&M=".$config['max_image'];
$fg = "upload=yes".$thumbs."&URLF=".$image_url."&JQ=100&IM=7&VM=".$config['max_image'].$water_radikal;
$rh = curl_init();
curl_setopt($rh,CURLOPT_URL,'http://www.radikal.ru/action.aspx');
curl_setopt($rh,CURLOPT_HEADER,0);
curl_setopt($rh,CURLOPT_TIMEOUT,50);
curl_setopt($rh,CURLOPT_USERAGENT,get_random_agent ());
curl_setopt($rh,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($rh,CURLOPT_RETURNTRANSFER,1);
curl_setopt($rh,CURLOPT_POST,1);
curl_setopt($rh,CURLOPT_POSTFIELDS,$fg);
$result = curl_exec($rh);
curl_close($rh);
if ($config_rss['url_radikal'] == "")$url_radikal = '1';else $url_radikal = $config_rss['url_radikal'];
preg_match('!<input id="input_link_'.$url_radikal.'" value="(.*?)".*?>!i',$result,$out);
$result = $out[1];
if ($result !=''){
if ($config_rss['url_radikal'] == "1"){
$this->short_story = preg_replace ('#\\[img(.*?)\\]'.$image_url.'\\[/img\\]#i','[img\\1]'.$result.'[/img]',$this->short_story);
$this->full_story = preg_replace ('#\\[img(.*?)\\]'.$image_url.'\\[/img\\]#i','[img\\1]'.$result.'[/img]',$this->full_story);
$this->short_story = preg_replace( "#\[thumb(.*?)\]".$image_url."\[/thumb\]#i",'[img\\1]'.$result.'[/img]',$this->short_story);
$this->full_story = preg_replace( "#\[thumb(.*?)\]".$image_url."\[/thumb\]#i",'[img\\1]'.$result.'[/img]',$this->full_story);
}else{
$this->short_story = preg_replace ('#\\[img(.*?)\\]'.$image_url.'\\[/img\\]#i',$result,$this->short_story);
$this->full_story = preg_replace ('#\\[img(.*?)\\]'.$image_url.'\\[/img\\]#i',$result,$this->full_story);
$this->short_story = preg_replace( "#\[thumb(.*?)\]".$image_url."\[/thumb\]#i",$result,$this->short_story);
$this->full_story = preg_replace( "#\[thumb(.*?)\]".$image_url."\[/thumb\]#i",$result,$this->full_story);
}
}else{return false;}
}
function process ()
{
global $config;
define ('UPLOAD_PATH',ROOT_DIR."/uploads/".$this->post."/".date("Y-m")."/");
define ('THUMB_PATH',UPLOAD_PATH ."thumbs/");
$rz = true;
$this->prefix = time ();
if (trim ($this->short_story) != '')
{
$this->get_images ($this->short_story);
}
if (trim ($this->full_story) != '')
{
$this->get_images ($this->full_story);
}
if ($this->radikal == 0)
{
$i= 0;
foreach ($this->images as $image_url)
{
++$i;
if ($this->download ($image_url,$i)===false) {
$rz = false;
}
}
foreach ($this->image as $image_new =>$image_url)
{
if ($this->reset_url($_SERVER['HTTP_HOST'])!= $this->reset_url($image_url))
{
if (@file_exists(UPLOAD_PATH .$image_new)){
$imageSizeInfo = @getimagesize( UPLOAD_PATH .$image_new);
if ($imageSizeInfo[2] == "1"){$imageType = "gif";}
if ($imageSizeInfo[2] == "2"){$imageType = "jpeg";}
if ($imageSizeInfo[2] == "3"){$imageType = "png";}
$image_form = explode ('.',$image_new);
if ($imageType != end($image_form) and $imageType)
{
$image_name = reset($image_form).'.'.$imageType;
@rename (UPLOAD_PATH .$image_new,UPLOAD_PATH .$image_name);
}else{$image_name = $image_new;}
if (@file_exists(UPLOAD_PATH .$image_name)){
@chmod( UPLOAD_PATH .$image_name,0666 );}
}
if (in_array($imageSizeInfo[2],array("1","2","3",)))
{
require_once ENGINE_DIR .'/inc/plugins/thumb.class.php';
$thumb = new rss_thumbnail (UPLOAD_PATH .$image_name);
if ($thumb->size_auto($config['max_image'],$_POST['t_seite']))
{
$thumb->jpeg_quality ($config['jpeg_quality']);
if($this->allow_watermark)
{
$thumb->watermark_image_light = $this->watermark_image_light;
$thumb->watermark_image_dark = $this->watermark_image_dark;
$thumb->x = $this->x;
$thumb->y = $this->y;
$thumb->margin = $this->margin;
$thumb->insert_watermark ($config['max_watermark']);
}
$thumb->save (THUMB_PATH .$image_name);
unset ($thumb);
}
if ($this->allow_watermark or $config['max_up_side'] ) {
$thumb = new rss_thumbnail (UPLOAD_PATH .$image_name);
$thumb->jpeg_quality ($config['jpeg_quality']);
if( $config['max_up_side'] ) $thumb->size_auto( $config['max_up_side'] );
if($this->allow_watermark)
{
$thumb->watermark_image_light = trim($this->watermark_image_light);
$thumb->watermark_image_dark = trim($this->watermark_image_dark);
$thumb->x = $this->x;
$thumb->y = $this->y;
$thumb->margin = $this->margin;
$thumb->insert_watermark ($config['max_watermark']);
}
$thumb->save (UPLOAD_PATH .$image_name);
if (@file_exists(UPLOAD_PATH .$image_name)){
@chmod( UPLOAD_PATH .$image_name,0666 );}
unset ($thumb);
}
}else{
@unlink(UPLOAD_PATH .$image_name);
return false;
}
}else{$image_name = $image_new;}
$serv_image_url = $config['http_home_url'] .'uploads/'.$this->post.'/'.date("Y-m").'/'.$image_name;
if (@file_exists(THUMB_PATH .$image_name)){
@chmod( THUMB_PATH .$image_name,0666 );
$this->short_story = preg_replace ('#\\[img(.*?)\\]'.$image_url.'\\[/img\\]#i','[thumb\\1]'.$serv_image_url.'[/thumb]',$this->short_story);
$this->full_story = preg_replace ('#\\[img(.*?)\\]'.$image_url.'\\[/img\\]#i','[thumb\\1]'.$serv_image_url.'[/thumb]',$this->full_story);
$this->short_story = preg_replace( "#\[thumb(.*?)\]".$image_url."\[/thumb\]#i","[thumb\\1]".$serv_image_url."[/thumb]",$this->short_story);
$this->full_story = preg_replace( "#\[thumb(.*?)\]".$image_url."\[/thumb\]#i","[thumb\\1]".$serv_image_url."[/thumb]",$this->full_story);
}else{
$this->short_story = preg_replace ('#\\[thumb(.*?)\\]('.$image_url.')\\[/thumb\\]#i','[img\\1]'.$serv_image_url.'[/img]',$this->short_story);
$this->full_story = preg_replace ('#\\[thumb(.*?)\\]('.$image_url.')\\[/thumb\\]#i','[img\\1]'.$serv_image_url.'[/img]',$this->full_story);
$this->short_story = str_replace ($image_url,$serv_image_url,$this->short_story);
$this->full_story = str_replace ($image_url,$serv_image_url,$this->full_story);
}
if (!(in_array ($image_name,$this->upload_images)))
{
$this->upload_images[] = $image_name;
}
}
return $rz;
}else{
foreach ($this->images as $image_url)
{
$this->download_radikal ($image_url);
}
}
}
}