Файл: adultscript-2.0.3-pro/files/admin/modules/csv/csv.php
Строк: 380
<?php
defined('_VALID') or die('Restricted Access!');
define('RESIZE_THUMB', true);
class VModule_Admin_csv
{
    private $db;
    private $vcfg;
    private $unique;
    public function __construct()
    {
        $this->db    = VF::factory('database');
        $this->vcfg    = VF::cfg('module.video');
    }
    public function render()
    {
        $cmodel        = VModel::load('category', 'video', true);
        $chmodel    = VModel::load('channel', 'channel', true);
        $umodel     = VModel::load('user', 'user', true);
        $vmodel        = VModel::load('video', 'video', true);
        
        $option        = array(
            'username' => 'anonymous', 'category' => array(), 'status' => 1, 'type' => 'public',
            'method' => 'flv', 'delimiter' => '|', 'thumb_method' => 'generate', 'duplicates' => 1,
            'channel_id' => 0, 'premium' => 0, 'categ_method' => 'auto', 'limit' => 10,
            'ignore' => 1, 'csv_method' => 'file', 'search' => '', 'replace' => '', 'text' => '',
            'limit_proc' => 100, 'mobile' => 1, 'queue' => 1, 'watermark' => 0
        );
        
        $unique = time().'_'.mt_rand();
        $orders = array(
              'flv'           => '1',
              'title'         => '2',
              'description'   => '3',
              'thumb'         => '4',
              'tags'          => '5',
              'embed'         => '6',
              'duration'        => '7',
              'pornstar'        => '8'
        );
        
        $channels    = $chmodel->channels(array('c.channel_id', 'c.name'), array('sort' => 'c.name', 'order' => 'ASC'));
        $categories    = $cmodel->categories(array('c.cat_id', 'c.name', 'c.auto_term'), array('sort' => 'c.name', 'order' => 'ASC'));
        
        $errors        = array();
        $messages    = array();
        $warnings    = array();
        if (isset($_POST['submitted'])) {
            $filter         = VF::factory('filter');
            $username        = $filter->get('username');
            $categ_method    = $filter->get('category_method');
            $category        = (isset($_POST['category'])) ? (array) $_POST['category'] : array();
            $method            = $filter->get('method');
            $thumb_method    = $filter->get('thumb_method');
            $duplicates        = (int) trim($_POST['duplicates']);
            $delimiter        = $filter->get('delimiter');
            $unique            = $filter->get('unique');
            $status            = (int) trim($_POST['status']);
            $type            = $filter->get('type');
            $channel_id        = (int) trim($_POST['channel_id']);
            $premium        = (VModule::enabled('premium')) ? (int) trim($_POST['premium']) : 0;
            $limit            = (int) trim($_POST['limit']);
            $limit_proc        = (int) trim($_POST['limit_proc']);
            $ignore            = (int) trim($_POST['ignore']);
            $csv_method        = $filter->get('csv_method');
            $text            = trim($_POST['csv']);
            $search            = trim($_POST['search']);
            $replace        = trim($_POST['replace']);
            $mobile            = (int) trim($_POST['mobile']);
            $queue            = (int) trim($_POST['queue']);
            
            if ($method == 'hotlink' or $method == 'embed') {
                $queue        = 0;
            }
            
            $order_1        = $filter->get('order_1');
            $order_2        = $filter->get('order_2');
            $order_3        = $filter->get('order_3');
            $order_4        = $filter->get('order_4');
            $order_5        = $filter->get('order_5');
            $order_6        = $filter->get('order_6');
            $order_7        = $filter->get('order_7');
            $order_8        = $filter->get('order_8');
            
            $ordering        = array(
                $order_1, $order_2, $order_3, $order_4, $order_5, $order_6, $order_7
            );
            
            if ($username == '') {
                $errors[]    = 'Username field cannot be left blank!';
            } else {
                if ($user_id = $umodel->exists('username', $username)) {
                    $option['username'] = $username;
                } else {
                    $errors[]    = 'Invalid username! Are you sure this user exists!?';
                }
            }
            
            if (!in_array($method, array('flv', 'embed', 'hotlink'))) {
                $errors[]     = 'Invalid import method! What exactly did you select!?';
            } else {
                $option['method']    = $method;
            }
            
            if (in_array($thumb_method, array('generate', 'download'))) {
                if ($thumb_method == 'generate' && ($method == 'embed')) {
                    $errors[] = 'Cannot generate thumbs when using embed import method!';
                } else {
                    $option['thumb_method'] = $thumb_method;
                }
            } else {
                $errors[]     = 'Invalid thumb method! What exactly did you select!?';
            }
            
            if (!in_array($type, array('public', 'private'))) {
                $errors[]    = 'Invalid type! What exactly did you select!?';
            } else {
                $option['type'] = $type;
            }
            
            if ($csv_method == 'file') {
                if ($_FILES['csv']['tmp_name'] != '') {
                    if (!is_uploaded_file($_FILES['csv']['tmp_name'])) {
                        $errors[] = 'Uploaded CSV file is not a valid uploaded file!';
                    }
                } else {
                    $errors[] = 'Please upload a CSV file to import!';
                }
            } elseif ($csv_metod == 'text') {
                if ($text == '') {
                    $errors[]    = 'Please enter the CSV text!';
                }
            }
            
            if ($categ_method == 'select' && empty($category)) {
                $errors[]    = 'Please check at least one category for the imported videos!';
            }
            
            $option['status']        = $status;
            $option['category']        = $category;
            $option['delimiter']    = $delimiter;
            $option['duplicates']    = $duplicates;
            $option['categ_method']    = $categ_method;
            $option['limit']        = $limit;
            $option['limit_proc']    = $limit_proc;
            $option['ignore']        = $ignore;
            $option['csv_method']    = $csv_method;
            $option['search']        = $search;
            $option['replace']        = $replace;
            $option['queue']        = $queue;
            
            if (!$errors) {
                // remove garbage
                if ($csv_method == 'file') {
                    $csv     = VFile::read($_FILES['csv']['tmp_name']);
                } else {
                    $csv    = $text;
                }
                $csv        = VText::dos2unix($csv);
                $csv        = explode("n", trim($csv));
                $list        = array();
                $count        = 0;
                $error        = false;
                $success    = 0;
                
                VHelper::load('module.video.conv');
                
                // servers with php < 3 dont have str_getcsv, lets add it :-)
                VF::load('framework.compat.functions.csv');
                
                foreach ($csv as $row) {
                    // empty rows?
                    if (empty($row)) {
                        continue;
                    }
                
                      if ($delimiter == 'tab') {
                          $row        = str_replace("t", '|', $row);
                          $delimiter  = '|';
                      }
                    
//                    $row                    = explode($delimiter, trim($row));
                    $row                    = str_getcsv(trim($row), $delimiter, """, "\");
                      $list[$count]           = VArray::combine($ordering, $row);
                      
                      // currently only continue if ignore is 1 and only for flv url and title
                      if ($ignore === 1) {
                          if ($method == 'flv' OR $method == 'hotlink') {
                              if (!isset($list[$count]['flv']) OR empty($list[$count]['flv'])) {
                                  continue;
                              }
                          }
                          if ($method == 'embed') {
                             if (!isset($list[$count]['embed']) OR empty($list[$count]['embed'])) {
                                  continue;
                             }
                          }
                      
                            if (!isset($list[$count]['title']) OR empty($list[$count]['title'])) {
                                continue;
                          }                          
                      }
                    
                    if (($method == 'flv' OR $method == 'hotlink') AND !isset($list[$count]['flv'])) {
                        $error = 'Import method set to FLV Source (or FLV Hotlink) and flv source url not present in the CSV file!';
                    }
                    
                    if ($thumb_method == 'generate' && !isset($list[$count]['flv'])) {
                        $error = 'Thumb method set to Generate and FLV Source URL not present in the CSV file!';
                    }
                    
                    if ($method == 'embed' && !isset($list[$count]['embed'])) {
                        $error = 'Import method set to Embed and embed code not found in the CSV file!';
                    }
                    
                    ++$count;
                }
                
                if ($error) {
                      $errors[] = $error;
                  }
                
                if (!$errors) {
                    session_write_close();
                    
                    $this->unique     = $unique;
                    $count            = count($list);
                    $incr            = round((100/$count), 2);
                    $progress        = 0;
                    $processed        = 0;
                    
                    $this->update_progress($progress.'^Processing CSV file ('.$count.' videos)!');
                    if (RESIZE_THUMB === true) {
                        $image = VF::factory('image');
                    }
                    
                    $conv =  VF::factory('video');
                    foreach ($list as $index => $values) {
                        $this->update_progress($progress.'^Processing: '.$values['title'].' ...');
                        
                        if ($processed >= $limit_proc) {
                            $warnings[] = 'Processed '.$limit_proc.' and no videos were added! Aborting!';
                            break;
                        }
                        
                        if ($success >= $limit) {
                            $warnings[] = 'Limit of '.$limit.' videos reached! Aborting!';
                            break;
                        }
                        
                        //we will regenerate the database link here to avoid timeouts
                        VF::factory_remove('database');
                        $this->db = VF::factory('database');
                        
                        // make sure we have all the fields...
                        foreach ($orders as $k => $v) {
                            if (!isset($values[$k])) {
                                $values[$k] = '';
                            }
                        }
                        
                        // make sure we dont have any white spaces..
                        foreach ($values as $k => $v) {
                            $values[$k] = trim($v);
                        }
                        
                        $unique_id = ($method == 'embed') ? md5($values['embed']) : md5($values['flv']);
                        if ($duplicates === 1) {
                            $this->db->query("SELECT unique_id
                                              FROM #__video_csv_imported
                                              WHERE unique_id = '".$this->db->escape($unique_id)."'
                                              LIMIT 1");
                            if ($this->db->affected_rows()) {
                                $progress = sprintf("%01.2f", ($progress+$incr));
                                $this->update_progress($progress.'^Duplicate found...skipping!');
                                continue;
                            }
                        }
                        
                        ++$processed;
                                                
                        $this->update_progress($progress.'^Processing: '.$values['title'].' (database entry) ...');
                        
                        $embed         = ($method == 'embed') ? urldecode($values['embed']) : '';
                        if ($method == 'embed' && ($search != '' && $replace != '')) {
                            $embed    = str_replace($search, $replace, $embed);
                        }
                        
                        // lets add a fix for unclosed iframes (some sponsors dont have embed code formatted correctly)
                        if (strpos($embed, 'iframe') !== FALSE &&
                            strpos($embed, '</iframe') === FALSE) {
                            $embed .= '</iframe>';
                        }
                                                
                        $url        = ($method == 'hotlink') ? $values['flv'] : '';
                        $categ        = $this->get_category($category, $values['title'], $categories, $values['tags']);
                        $tags        = $this->get_tags($values['title'], $values['tags']);
                        $slug        = prepare_string($values['title'], TRUE, $this->vcfg['slug_max_length']);
                        $pornstar    = $values['pornstar'];
                        $time        = time();
                        
                        if (!$video_id = $vmodel->reload(true)->add(array(
                            'user_id'        => $user_id,
                            'title'            => $values['title'],
                            'slug'            => $slug,
                            'description'    => $values['description'],
                            'type'            => $type,
                            'embed_code'    => $embed,
                            'url'            => $url,
                            'channel_id'    => $channel_id,
                            'premium'        => $premium,
                            'status'        => 5))) {
                            $progress = sprintf("%01.2f", ($progress+$incr));
                            $this->update_progress($progress.'^Failed to create database entry...skiping!');
                            continue;
                        }
                        
                        $this->db->query("UPDATE #__user_activity SET total_videos = total_videos+1 WHERE user_id = ".$user_id." LIMIT 1");
                        
                        foreach ($categ as $cat_id) {
                            $cmodel->add_video($cat_id, $video_id);
                        }
                        
                        $tags = explode(',', $tags);
                        foreach ($tags as $tag) {
                            $vmodel->add_tag($video_id, $tag);
                        }
                        
                        $pornstars    = explode(',', $pornstar);
                        foreach ($pornstars as $star) {
                            $vmodel->add_model($video_id, $star);
                        }
                        $conv->log_setup($video_id);
                        if ($method == 'flv' or $thumb_method == 'generate' or $mobile) {
                            $this->update_progress($progress.'^Processing: '.$values['title'].' (downloading) ...');
                        
                            $src         = trim($values['flv']);
                            $ext        = VFile::ext($src);
                            $dst_vid    = MEDIA_DIR.'/videos/vid/'.$video_id.'.'.$ext;
                            if (!VCurl::file($src, $dst_vid)) {
                                $progress = sprintf("%01.2f", ($progress+$incr));
                                  $this->update_progress($progress.'^Failed to download video source file...skiping!');
                                  continue;
                            }
                            
                            if (!$conv->load($dst_vid)) {
                                $progress = sprintf("%01.2f", ($progress+$incr));
                                $this->update_progress($progress.'^Failed to load video source file...skiping!');
                                continue;
                            }
                            
                            @chmod($dst_vid, 0777);
                        }
                        
                        $proc            = array(
                            'flv'        => false,
                            'mp4'        => false,
                            'mp4_hd'    => false,
                            'mobile'    => false,
                            'server_id'    => 0,
                            's3'        => 0
                        );
                        
                        if ($method == 'flv' && $queue === 0) {    
                            $this->update_progress($progress.'^Processing: '.$values['title'].' (converting) ...');
                            
                            $proc = VHelper_video_conv::process($video_id, $dst_vid, $conv);
                            if (!$proc['mp4'] && !$proc['flv']) {
                                $progress = sprintf("%01.2f", ($progress+$incr));
                                $this->update_progress($progress.'^Failed to convert video...skiping!');
                                continue;
                            } else {
                                $ext    = ($proc['mp4']) ? 'mp4' : 'flv';
                            }
                                
                            $size        = filesize($dst_vid);
                            $duration    = $conv->data['duration_seconds'];
                        } else {
                            $size         = 0;
                            $duration    = (!empty($values['duration'])) ? (float) $values['duration'] : 0;
                        }
                        
                        if ($method == 'hotlink' && $mobile) {
                            if ($this->vcfg['mobile_convert'] && $mobile) {
                                $this->update_progress($progress.'^Processing: '.$values['title'].' (converting to Mobile) ...');
                                  $dst_mobile = MEDIA_DIR.'/videos/mobile/'.$video_id.'.mp4';
                                  if ($conv->convert_to_mobile($dst_vid, $dst_mobile)) {
                                      $conv->log('Video converted to Mobile.');
                                      $proc['mobile']    = true;
                                      @chmod($dst_mobile, 0777);
                                  } else {
                                      $conv->log('Failed to convert video to Mobile!');
                                  }
                            }
                              if ($this->vcfg['multi_server'] == '1') {
                                  $conv->log('Initializing multi-server ...');
                                  VHelper::load('module.video.server');
                                  $server = VHelper_video_server::get(FALSE, 1);
                                  if ($server && 
                                      VHelper_video_server::process_upload($server, $video_id, false, false, false, true, $conv)) {
                                      $proc['server_id'] = (int) $server['server_id'];
                                  }
                              }
                              if ($this->vcfg['amazon_s3'] == '1') {
                                  $conv->log('Initializaing Amazon S3 storage ...');
                                  VHelper::load('module.video.s3');
                                  if (VHelper_video_s3::process_upload($video_id, false, false, false, false, $conv)) {
                                      $proc['s3'] = 1;
                                  }
                              }
                        }
                        
                        $thumbs = 0;
                        if ($thumb_method == 'generate') {
                            if ($queue === 0) {
                                $this->update_progress($progress.'^Processing: '.$values['title'].' (extracting thumbs) ...');
                            
                                if (!$thumbs = $conv->extract_thumbs($dst_vid, $video_id)) {
                                    $thumbs        = 0;
                                    $progress     = sprintf("%01.2f", ($progress+$incr));
                                    $this->update_progress($progress.'^Failed to extract video thumbs...skiping!');
                                    continue;
                                }
                            }
                        } else {
                            $this->update_progress($progress.'^Processing: '.$values['title'].' (downloading thumb) ...');
                            
                            // lets get more than one URL here
                            $urls        = array();
                            if (strpos($values['thumb'], ';') !== FALSE) {
                                $urls    = explode(';', $values['thumb']);
                            } else {
                                $urls[]    = $values['thumb'];
                            }
                            
                            $thumbs    = 1;
                            foreach ($urls as $thumb) {
                                $thumb_dir    = MEDIA_DIR.'/videos/tmb/'.path($video_id);
                                VFolder::create($thumb_dir, 0777);
                                $thumb_dst    = $thumb_dir.'/'.$thumbs.'.jpg';
                                if (!VCurl::file(trim($thumb), $thumb_dst)) {
                                    $progress = sprintf("%01.2f", ($progress+$incr));
                                    $this->update_progress($progress.'^Failed to download video thumb...skiping!');
                                    continue;
                                }
                            
                                if (RESIZE_THUMB === true) {
                                    $thumb_tmp = TMP_DIR.'/thumbs/'.$video_id.'.jpg';
                                
                                    $image->clear();
                                    if (!$image->load($thumb_dst)) {
                                        $progress = sprintf("%01.2f", ($progress+$incr));
                                        $this->update_progress($progress.'^Failed to load video thumb...skiping!');
                                        continue;
                                    }
                                    
                                      if (!$image->resize($this->vcfg['thumb_width'], $this->vcfg['thumb_height'], 'EXACT', $thumb_tmp)) {
                                        $progress = sprintf("%01.2f", ($progress+$incr));
                                        $this->update_progress($progress.'^Failed to resize video thumb...skiping!');
                                        continue;
                                    }
                                    
                                    rename($thumb_tmp, $thumb_dst);
                                }
                                
                                
                                ++$thumbs;
                            }
                            
                            
                            $thumbs = ($thumbs === 1) ? 1 : ($thumbs-1);
                            
                            if ($thumbs > 0) {
                                VHelper::load('module.video.thumb');
                                  VHelper_video_thumb::upload($video_id, $thumbs);
                              }
                        }
                        
                        $conv->clean();
                        
                        $this->update_progress($progress.'^Processing: '.$values['title'].' (finalizing) ...');
                        
                        VF::factory_remove('database');
                        $this->db = VF::factory('database');
                        
                        $ext         = (isset($ext)) ? $ext : 'flv';
                        $s3            = (isset($proc) && isset($proc['s3'])) ? $proc['s3'] : 0;
                        $server_id    = (isset($proc) && isset($proc['server_id'])) ? $proc['server_id'] : 0;
                        $mob        = (isset($proc) && isset($proc['mobile'])) ? $proc['mobile'] : 0;
                        
                          $o_filename    = ($method == 'flv' OR $method == 'hotlink') ? VFile::name(trim($values['flv'])) : '';
                          $o_url        = ($method == 'flv' OR $method == 'hotlink') ? trim($values['flv']) : '';
                          $o_ext        = ($method == 'flv' OR $method == 'hotlink') ? VFile::ext(trim($values['flv'])) : '';
                          $o_size        = (isset($dst_vid)) ? filesize($dst_vid) : 0;
                          
                          $vmodel->reload();
                          $vmodel->add_orig(array(
                              'video_id'    => $video_id,
                              'user_id'    => $user_id,
                              'filename'    => $o_filename,
                              'ext'        => $o_ext,
                              'size'        => $o_size,
                              'method'    => 'csv_'.$method,
                              'url'        => $o_url
                          ));
                          
                          $vmodel->update($video_id, array(
                              'ext'        => $ext,
                              'size'        => $size,    
                              'duration'    => $duration,
                              'mobile'    => $mob,
                              'thumb'        => (string) 1,
                              'thumbs'    => $thumbs,
                              'server'    => $server_id,
                              's3'        => $s3,
                              'status'    => ($queue) ? 6 : $status
                          ));
                          
                        if ($queue) {
                              $vmodel->add_queue($video_id, $status);
                        }                          
                          
                        if ($channel_id !== 0) {
                            $cmodel->reload(true)->add_video($channel_id, $video_id);
                        }
                        
                        $vmodel->reload('true')->add_csv($video_id, $unique_id);
                        
                        // thumbs are generated, lets delete the files from the local server
                        // if multi-server was enabled
                        if ($server_id !== 0 OR $s3 === 1) {
                            if (isset($dst_flv)) {
                                VFile::delete($dst_flv);
                            }
                            
                            if (isset($dst_mp4)) {
                                VFile::delete($dst_mp4);
                            }
                            
                            if (isset($dst)) {
                                VFile::delete($dst);
                            }
                        }
                        // delete the original video from the videos/vid folder
                        if (isset($dst_vid) && !$queue) {
                            VFile::delete($dst_vid);
                        }
                        
                        $progress = sprintf("%01.2f", ($progress+$incr));
                        ++$success;                        
                    }
                    
                    if (!$errors) {
                        $messages[] = 'Successfuly imported '.$success.' videos!';
                        VFile::delete(VFile::safe(TMP_DIR.'/logs/'.$this->unique.'_mass_csv_progress.log'));
                    }
                }
            }
        }
        
        $tpl    = VF::factory('template');
        $tpl->menu            = 'video';
        $tpl->submenu        = 'video_add';
        $tpl->extramenu        = 'csv';
        $tpl->meta_title    = 'Admin::Video::CSV';
        $tpl->errors        = $errors;
        $tpl->messages        = $messages;
        $tpl->warnings        = $warnings;
        $tpl->option        = $option;
        $tpl->orders        = $orders;
        $tpl->unique        = $unique;
        $tpl->categories    = $categories;
        $tpl->channels        = $channels;
        $tpl->load(array('header', 'csv', 'footer'));
        $tpl->display();
    }
    
    private function update_progress($data)
    {
        VFile::write(VFile::safe(TMP_DIR.'/logs/'.$this->unique.'_mass_csv_progress.log'), $data);
    }
    
    private function get_category($category, $title, $categories, $tags='')
    {
        if (!empty($category)) {
            if (is_array($category)) {
                return $category;
            }
            
            return array((int) $category);
        }
        
        $cats = array();
        foreach ($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, $cats)) {
                        $cats[] = $cat_id;
                    }
                }
                
                if (!empty($tags)) {
                    if (stripos($tags, $term) !== FALSE) {
                        if (!in_array($cat_id, $cats)) {
                            $cats[] = $cat_id;
                        }
                    }
                }
            }
        }
        
        if (!$cats) {
            // no category found? return last category...should be Uncategorized!
            $cats[] = $cat_id;
        }
        
        return $cats;
    }
    
    private function get_tags($title, $tags='')
    {
        if ($tags != '') {
            return prepare_tags($tags);
        }
        
        $title     = preg_replace('/[^A-Za-z0-9 ]+/', ' ', trim($title));
        $title     = preg_replace('/ss+/', ' ', $title);
        $tags    = array();
        $array    = explode(' ', $title);
        foreach ($array as $string) {
            if (strlen($string) >= 4) {
                $tags[] = $string;
            }
        }
        
        return implode(',', $tags);
    }
}