Вход Регистрация
Файл: adultscript-2.0.3-pro/files/admin/modules/rss/helpers/feed.php
Строк: 370
<?php
defined
('_VALID') or die('Restricted Access!');
set_time_limit(0);
class 
VHelper_rss_feed
{
    private 
$db;
    
    private 
$conv;
    
    private 
$image;

    private 
$tmb_width;
    private 
$tmb_height;
    
    private 
$options     = array();
    private 
$categories    = array();
    private 
$slug_limit = NULL;
    public function 
__construct()
    {
        
$this->db            = VF::factory('database');
        
$this->categories    = $this->get_video_categories();
        
$this->slug_limit    = VF::cfg_item('module.video.slug_max_length');
    }
    
    public function 
set_options($options) {
        
$this->options = $options;
        
        if (
$this->options['method'] == 'host' OR
            
$this->options['method_thumb'] == 'generate') {
            
$this->conv = VF::factory('video');
            
VHelper::load('module.video.conv');
        }
        
        if (
$this->options['method_thumb'] == 'download') {
            
$this->image         = VF::factory('image');
            
$this->tmb_width    = VF::cfg_item('module.video.thumb_width');
            
$this->tmb_height    = VF::cfg_item('module.video.thumb_height');
        }
        
        if (isset(
$this->options['categories']) &&
            
is_string($this->options['categories'])) {
            
$this->options['categories'] = unserialize($this->options['categories']);
        }
    }
    
    public function 
update()
    {
        
VF::factory_remove('database');
        
$this->db    = VF::factory('database');
        
$this->db->query("UPDATE #__video_feeds
                          SET last_updated = '"
.date('Y-m-d h:i:s')."'
                          WHERE feed_id = "
.$this->options['feed_id']."
                          LIMIT 1"
, TRUE);
    }
    
    public function 
parse()
    {
        
$count = 1;
        if (
$xml = $this->get_xml($this->options['url'])) {
            
// for some buggy formated XML Feeds
            
$xml = str_replace('&', '&amp;', $xml);
            
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOERROR, LIBXML_NOWARNING, LIBXML_NOCDATA);
            if (!
$xml) {
                
$errors = libxml_get_errors();
                foreach (
$errors as $error) {
                    echo 
VF::debug($error);
                }
                
                
// update for cron here
                
$this->update();
            }
            
            
$xml = $xml->xpath('//videos/video');
            
            
$amount        = $this->options['update_limit'];
            
$incr       = round((100/$amount), 2);
            
$progress   = 0;
            
$this->progress('0^Processing feed ...');
            
            foreach (
$xml as $item) {                  
                  
// unique_id defined by affiliate feed (WTF...why int, you need string here)
                  
$unique_id = (string) $item->id;
                  
                  
// check for duplicates
                  
if (!empty($this->options['block_duplicates']) &&
                      
$this->duplicate($this->options['feed_id'], $unique_id)) {
                        
$this->progress($progress.'^Duplicate found ... skipping!');
                      continue;
                  }
                  
                  
$item    = VArray::from_xml($item);
                  
$video    = $this->defaults();
                  
                  
//specially added for videosz and other sponsors with multiple clips in one entry
                  
if (isset($item['clips']['clip']['0'])) {
                      
$item['clips']['clip'] = $item['clips']['clip']['0'];
                  }
                  
                  
$video['unique_id']        = $unique_id;
                  
$video['title']            = $item['title'];
                  
$video['description']    = $item['description'];

                  
$this->progress($progress.'^Processing '.$video['title'].' ...');
                  
                  
// some feeds dont have tags
                  
$video['tags']            = (isset($item['tags'])) ? prepare_tags($item['tags']) : $this->get_tags($video['title']);
                  
$video['categories']    = $this->match_category($video['tags'], $video['title']);
                  
                  
// some feeds have pornstars
                  
if (isset($item['performers'])) {
                      
$video['pornstars'] = $item['performers'];
                  }
                  
                  if (isset(
$item['clips']['clip']['duration'])) {
                      
$video['duration'] = (float) $item['clips']['clip']['duration'];
                  }
                  
                  if (
$this->options['method'] == 'embed') {
                      
$video['embed']    = (isset($item['mp4_embed'])) ? $item['mp4_embed'] : $item['flv_embed'];
                  } else {
                      
$clip_root        = (isset($item['clipurl'])) ? $item['clipurl'] : $item['clip_url'];
                      
$filename        = NULL;
                      if (isset(
$item['clips']['clip']['mp4'])) {
                          
$ext            = VFile::ext($item['clips']['clip']['mp4']);
                          if (
in_array($ext, array('flv', 'mp4', 'f4v'))) {
                              
$filename        = $item['clips']['clip']['mp4'];
                          }
                      }
                      
                      if (!isset(
$filename)) {
                          
$filename        = $item['clips']['clip']['flv'];
                      }
                      
                      
$video['file']    = (VValid::url($filename)) ? $filename : $clip_root.$filename;
                      
                      
$screen_root        = (isset($item['screenurl'])) ? $item['screenurl'] : $item['screen_url'];
                      
$video['thumbs']    = array();
                      foreach (
$item['clips']['clip']['screens'] as $screen) {
                          
$screen = (is_array($screen)) ? $screen['0'] : $screen;
                          if (
VValid::url($screen)) {
                              
$video['thumbs'][]    = $screen;
                          } else {
                              
$video['thumbs'][]     = $screen_root.$screen;
                          }
                      }
                      
                      if (
$this->options['method'] == 'hotlink') {
                          
$video['url']    = $video['file'];
                          if (isset(
$video['file_mp4'])) {
                              
$video['mobile_url'] = $video['file_mp4'];
                          }
                      }
                  }
                  
                  if (
$this->process($video)) {
                      
$progress = sprintf("%01.2f", ($progress+$incr));
                      
$this->progress($progress.'^Done ... moving to next video!');
                    ++
$count;
                  }

                  
// we only add x number of videos options['update_limit']
                  
if ($count > $this->options['update_limit']) {
                      
$this->progress($progress.'^Import finished!');
                      break;
                  }
            }
        }
        
        
$this->update();
    }
    
    
// still need to figure out the clips problem, there can be more than 1 clips
    // we add a video entry for each clip
    
private function process($video)
    {
        
$status     = (isset($video['file'])) ? 5 : $video['status'];
        
$mobile     = (isset($video['mobile_url']) && $video['mobile_url'] != '') ? 1 : 0;
        
$thumbs        = 0;
        
$size        = 0;
        
$server_id    = 0;
        
$s3            = 0;
        
        
$vmodel        = VModel::load('video', 'video', true);
        
        
$time    = time();
        
$slug     = prepare_string($video['title'], TRUE, $this->slug_limit);
        if (
$video_id = $vmodel->reload(true)->add(array(
            
'user_id'            => $video['user_id'],
            
'title'                => $video['title'],
            
'description'        => $video['description'],
            
'slug'                => prepare_string($video['title'], true, $this->slug_limit),
            
'embed_code'        => $video['embed'],
            
'allow_embed'        => $video['allow_embed'],
            
'allow_rating'        => $video['allow_rating'],
            
'allow_download'    => $video['allow_download'],
            
'allow_comment'        => $video['allow_comment'],
            
'url'                => $video['url'],
            
'mobile'            => $mobile,
            
'mobile_url'        => $video['mobile_url'],
            
'channel_id'        => $video['channel_id'],
            
'premium'            => $video['premium'],
            
'type'                => $video['type'],
            
'status'            => $status
            
))) {

            if (
$video['channel_id'] !== 0) {
                
$vmodel->add_channel($video_id, $video['channel_id']);
            }
            
            
$tags    = explode(',', $video['tags']);
            foreach (
$tags as $tag) {
                
$vmodel->add_tag($video_id, $tag);
            }
            
            foreach (
$video['categories'] as $cat_id) {
                
$vmodel->add_category($video_id, $cat_id);
            }
            
            if (isset(
$video['pornstars'])) {
                
$pornstars = explode(',', $video['pornstars']);
                foreach (
$pornstars as $pornstar) {
                    
$vmodel->add_model($video_id, $pornstar);
                }
            }
            
            if (
$this->options['method'] == 'host' OR
                
$this->options['method_thumb'] == 'generate') {
                
$dst_tmp = TMP_DIR.'/downloads/'.VText::random();
                if (
VCurl::file($video['file'], $dst_tmp)) {
                    
$this->conv->log_setup($video_id, TRUE);
                    if (
$this->conv->load($dst_tmp)) {
                        
$size        = filesize($dst_tmp);
                        
$duration    = $this->conv->duration();
                        if (
$ext = $this->conv->identify()) {
                            
$dst        = MEDIA_DIR.'/videos/vid/'.$video_id.'.'.$ext;
                            if (!
rename($dst_tmp, $dst)) {
                                
$this->conv->log('Failed to rename '.$dst_tmp.' to '.$dst);
                            }
                            
                            
// delete temp file
                            
@VFile::delete($dst_tmp);
                        } else {
                            return 
false;
                        }
                    } else {
                        return 
false;
                    }
                } else {
                    return 
false;
                }
                
                if (
$this->options['queue'] == '0') {
                    
$proc = VHelper_video_conv::process($video_id, $dst, $this->conv, false);
                }
            }
            
            if (
$this->options['method_thumb'] == 'generate') {
                if (
$this->options['queue'] == '0') {
                    
$thumbs = $this->conv->extract_thumbs($dst, $video_id);
                }
            
//                if ($this->options['method'] == 'hotlink' OR
//                    $this->options['method'] == 'embed') {
//                      VFile::delete($dst);
//                      if (isset($dst_mp4)) {
//                          VFile::delete($dst_mp4);
//                      }
//                }
            
} else {
                
$index  = 1;
                
$path   = MEDIA_DIR.'/videos/tmb/'.path($video_id);

                
VFolder::create($path);

                foreach (
$video['thumbs'] as $thumb) {
                    
$dst_thumb_tmp = TMP_DIR.'/downloads/'.$video_id.'_'.$index.'.jpg';
                    if (
VCurl::file($thumb, $dst_thumb_tmp)) {
                        if (
$this->image->load($dst_thumb_tmp)) {
                            
$dst_thumb = $path.'/'.$index.'.jpg';
                            if (
$this->image->resize($this->tmb_width, $this->tmb_height, 'EXACT', $dst_thumb)) {
                                ++
$index;
                                ++
$thumbs;
                            }
                        }
                    }
                }                
            }
            
            
// some feeds dont have a video duration set (fails for hotlinking)
            
if (!isset($video['duration'])) {
                
$video['duration'] = 0;
            }
            
            
$o_filename    = ($this->options['method'] == 'host' OR $this->options['method'] == 'hotlink') ? VFile::name($video['file']) : '';
            
$o_ext        = ($this->options['method'] == 'host' OR $this->options['method'] == 'hotlink') ? VFile::ext($video['file']) : '';
            
$o_url        = ($this->options['method'] == 'host' OR $this->options['method'] == 'hotlink') ? $video['file'] : '';
            
$o_size        = (isset($size)) ? $size : 0;
            
            
$vmodel->reload(true)->add_orig(array(
                
'video_id'        => $video_id,
                
'user_id'        => $video['user_id'],
                
'filename'        => $o_filename,
                
'ext'            => $o_ext,
                
'size'            => $o_size,
                
'method'        => 'xml_'.$this->options['method'],
                
'url'            => $o_url
            
));
            
            
$ext         = (isset($ext)) ? $ext : 'flv';
            
$duration    = (isset($duration)) ? $duration : $video['duration'];
            
            
$vmodel->update($video_id, array(
                
'ext'        => (isset($ext)) ? $ext : 'flv',
                
'duration'    => (isset($duration)) ? $duration : $video['duration'],
                
'thumbs'    => $thumbs,
                
'server'    => (isset($proc)) ? $proc['server_id'] : 0,
                
's3'        => (isset($proc)) ? $proc['s3'] : 0,
                
'mobile'    => $mobile,
                
'status'    => ($this->options['queue']) ? 6 : $video['status']
            ));
            
            
$vmodel->add_activity($video['user_id']);
            
            if (
$this->options['queue']) {
                
$vmodel->add_queue($video_id, $video['status']);
            }
                        
            
VF::factory_remove('database');
            
$this->db = VF::factory('database');
            
            
$this->db->query("UPDATE #__video_feeds
                              SET total_videos = total_videos+1
                              WHERE feed_id = "
.$this->options['feed_id']."
                              LIMIT 1"
);
                              
            
$this->db->query("INSERT INTO #__video_feeds_imported
                              SET feed_id = "
.$this->options['feed_id'].",
                                  unique_id = '"
.$this->db->escape($video['unique_id'])."',
                                  video_id = "
.$video_id);
            
            return 
true;
        }
        
        return 
FALSE;
    }
    
    private function 
get_tags($title)
    {
        
$tags    = array();
        
$array    = explode(' ', prepare_tags($title));
        foreach (
$array as $tag) {
            if (
strlen($tag) >= 4) {
                
$tags[] = $tag;
            }
        }
        
        return 
implode(',', $tags);
    }
    
    private function 
match_category($tags, $title)
    {
        if (!empty(
$this->options['categories'])) {
            return 
$this->options['categories'];
        }
        
        
$categories    = array();
        foreach (
$this->categories as $category) {
            
$cat_id    = (int) $category['cat_id'];
            
$terms    = explode(',', $category['auto_term']);
            foreach (
$terms as $term) {
                
$term = trim($term);
                if (
stripos($title, $term) !== FALSE) {
                    if (!
in_array($cat_id, $categories)) {
                        
$categories[] = $cat_id;
                    }
                }
            
                if (
stripos($tags, $term) !== FALSE) {
                    if (!
in_array($cat_id, $categories)) {
                        
$categories[] = $cat_id;
                    }
                }
            }
            
            if (isset(
$category['subcategories'])) {
                foreach (
$category['subcategories'] as $subcategory) {
                    
$subcat_id     = (int) $subcategory['cat_id'];
                    
$terms        = explode(',', $subcategory['name']);
                    
                    foreach (
$terms as $term) {
                        
$term = trim($term);
                        
                        if (
stripos($title, $term) !== FALSE) {
                            if (!
in_array($subcat_id, $categories)) {
                                
$categories[] = $subcat_id;
                            }
                        }
                        
                        if (
stripos($tags, $term) !== FALSE) {
                            if (!
in_array($subcat_id, $categories)) {
                                
$categories[] = $subcat_id;
                            }
                        }
                        
                    }
                }
            }
        }
        
        
// no categories were matched...lets use the first available category
        
if (empty($categories)) {
            
$category        = array_slice($this->categories, 0, 1);
            
$categories[]     = $category['0']['cat_id'];
        }
        
        return 
array_unique($categories);
    }
    
    private function 
defaults()
    {
        return array(
            
'user_id'            => (int) $this->options['user_id'],
            
'channel_id'        => (int) $this->options['channel_id'],
            
'type'                => $this->options['type'],
            
'embed'                => '',
            
'allow_embed'        => (int) $this->options['allow_embed'],
            
'allow_download'    => (int) $this->options['allow_download'],
            
'allow_rating'        => (int) $this->options['allow_rating'],
            
'allow_comment'        => (int) $this->options['allow_comment'],
            
'mobile'            => (int) $this->options['mobile'],
            
'premium'            => (int) $this->options['premium'],
            
'url'                => '',
            
'mobile_url'        => '',
            
'status'            => (int) $this->options['status']
        );
    }
    
    private function 
duplicate($feed_id, $unique_id)
    {
        
VF::factory_remove('database');
        
$this->db    = VF::factory('database');
        
$this->db->query("SELECT feed_id
                          FROM #__video_feeds_imported
                          WHERE feed_id = "
.$feed_id."
                          AND unique_id = '"
.$this->db->escape($unique_id)."'
                          LIMIT 1"
);
        
        return 
$this->db->affected_rows();
    }
    
    private function 
get_xml($feed)
    {
        
$xml = FALSE;
        if (
VValid::url($feed)) {
            if (!
$xml = VCurl::string($feed)) {
                return 
FALSE;
            }
        } elseif (
file_exists($feed) && is_file($feed)) {
            
$xml    = file_get_contents($feed);
        } else {
            return 
FALSE;
        }
        
        if (
$xml !== FALSE) {
            if (
preg_match('~encoding="utf-8"~i', $xml)) {
                return 
utf8_encode($xml);
            } else {
                return 
$xml;
            }
        }
        
        return 
FALSE;
    }
    
    private function 
url_to_string($url)
    {
        if (
$string = VCurl::string($url)) {
            if (
preg_match('~encoding="utf-8"~i', $string)) {
                
$string = utf8_encode($string);
            }
            
            return 
$string;
        }
        
        return 
FALSE;
    }
    
    private function 
get_video_categories()
    {
        
$this->db->query("SELECT cat_id, parent_id, name, auto_term
                          FROM #__video_categories
                          ORDER BY slug ASC"
);
        
$categories     = $this->db->fetch_rows();
        if (
VF::cfg_item('module.video.subcategories') == '1') {
            
$categories = VArray::tree($categories);
        }
        
        return 
$categories;
    }
    
    private function 
progress($data)
    {
        if (isset(
$this->options['unique'])) {
            
VFile::write(VFile::safe(TMP_DIR.'/logs/feed_progress_'.$this->options['unique'].'.log'), $data);
        }
    }
    
    public function 
test($file=FALSE)
    {
        if (
$xml = $this->get_xml($this->options['url'])) {
            
// XML error reporting and & conversion
            
libxml_use_internal_errors(true);
            
$xml = str_replace('&', '&amp;', $xml);
            
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOERROR, LIBXML_NOWARNING, LIBXML_NOCDATA);
            if (!
$xml) {
                
$errors = libxml_get_errors();
                foreach (
$errors as $error) {
                    echo 
VF::debug($error);
                }
            }

            
$xml = $xml->xpath('//video');
            
            foreach (
$xml as $video) {
                
$video    = VArray::from_xml($video);
                
                if (!isset(
$video['id'])) {
                    return 
'XML Feed: missing video unique id! Aborting!';
                }
            
                if (!isset(
$video['title'])) {
                    return 
'XML Feed: missing video title! Aborting!';
                }
                
                if (!isset(
$video['description'])) {
                    return 
'XML Feed: missing video description! Aborting!';
                }
                
//                some feeds dont have tags :(
//                if (!isset($video['tags'])) {
//                    return 'XML Feed: missing video tags! Aborting!';
//                }
                
                
if ($this->options['method'] == 'host' OR
                    
$this->options['method'] == 'hotlink' OR
                    
$this->options['method_thumb'] == 'generate') {                    
                    if (!isset(
$video['clip_url']) && !isset($video['clipurl'])) {
                        return 
'XML Feed: missing video clip url! Aborting!';
                    } else {
                        
// specially crafted to support videosz and other sponsors that user multiple clips in one entry
                        
if (!isset($video['clips']['clip']['flv']) &&
                            !isset(
$video['clips']['clip']['0']['flv']) &&
                            !isset(
$video['clips']['clip']['mp4']) &&
                            !isset(
$video['clips']['clip']['0']['mp4'])) {
                            return 
'XML Feed: missing video clips flv and/or mp4! Aborting!';
                        }
                    }
                }
                
                if (
$this->options['method'] == 'embed') {
                    if (!isset(
$video['flv_embed'])) {
                        return 
'XML Feed: missing video embed code! Aborting!';
                    }
                }
                
                if (
$this->options['method_thumb'] == 'download') {
                    if (!isset(
$video['screen_url'])) {
                        return 
'XML Feed: missing video screen url! Aborting!';
                    } else {
                        if (!isset(
$video['clips']['clip']['screens']['screen'])) {
                            return 
'XML Feed: missing video screens screen! Aborting!';
                        }
                    }
                }
            
                return 
FALSE;
            }
        }
        
        return 
FALSE;
    }
}
Онлайн: 2
Реклама