Вход Регистрация
Файл: adultscript-2.0.3-pro/files/admin/modules/video/components/upload_ftp.php
Строк: 147
<?php
defined
('_VALID') or die('Restricted Access!');
class 
VComponent_Admin_video_upload_ftp
{
    private 
$db;
    
    private 
$add_date;
    private 
$add_time;
    private 
$unique;
    private 
$error;
    
    private 
$vcfg;
    public function 
__construct()
    {
          
$this->db     = VF::factory('database');
          
$this->vcfg    = VF::cfg('module.video');
    }
    
    public function 
render()
    {
        
$tpl         = VF::factory('template');
        
$filter        = VF::factory('filter');
        
$exts        = $this->vcfg['video_allowed_ext'];
        
$unique     = time().'_'.mt_rand();
        
$statuses   = array(
            
'0' => 'Suspended',
            
'1' => 'Published',
            
'2' => 'Waiting Approval',
            
'3' => 'Uploading',
            
'4' => 'Converting',
            
'5' => 'Grabbing',
            
'6' => 'Queued',
            
'7' => 'Error'
        
);
                
        
$dir        = BASE_DIR.'/tmp/uploads';
        
$videos        = array();
        
$loaded        = FALSE;
        
$status        = 1;
        
$queue        = 1;
        
$premium    = 0;
        
$type        = 'public';
        
$username    = '';
        
$category    = array();
        
$categories    = VModel::load('category', 'video', true)->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['submit_load'])) {
              
$dir = VFolder::safe($filter->get('dir'));
              
              if (
$dir == '') {
                  
$errors[]     = 'Video FTP upload folder path cannot be left blank!';
              } elseif (!
file_exists($dir)) {
                  
$errors[]    = 'Video FTP upload folder does not exist! (is open_basedir enabled?)';
              } elseif (!
is_dir($dir)) {
                  
$errors[]    = 'Video FTP upload folder is not a folder!';
              } elseif (!
is_readable($dir)) {
                  
$errors[]    = 'Video FTP upload folder is not readable by the web server username!';
              } else {
                  
$files        = VFolder::files($dir);
                  foreach (
$files as $file) {
                      
$ext    = VFile::ext($file);
                      if (
in_array($ext, $exts)) {
                          
$videos[] = array(
                              
'path'    => $file,
                              
'ext'    => $ext
                          
);
                      }
                  }
              }
              
              if (!
$errors) {
                  if (!
$videos) {
                      
$errors[] = 'No videos found! Allowed extensions: '.implode(',', $exts).'!';
                  } else {
                      
$conv = VF::factory('video');
                      foreach (
$videos as $key => $video) {
                          
$file = $video['path'];
                          if (
$conv->load($file)) {
                              
$videos[$key]['title']            = VFile::strip_ext(VFile::name($file));
                              
$videos[$key]['description']    = '';
                              
$videos[$key]['tags']            = '';
                              
$videos[$key]['size']            = VText::bytes(filesize($file));
                              
$videos[$key]['duration']        = $conv->data['duration_timecode'];
                              
$videos[$key]['audio']            = $conv->data['audio'];
                              
$videos[$key]['format']            = $conv->data['format'];
                              
$videos[$key]['identify']        = $conv->identify();
                          } else {
                              unset(
$videos[$key]);
                          }
                      }
                  }
                  
                  if (!
$videos) {
                      
$errors[]    = 'Failed to load any videos! Please check video formats/path/restrictions!';
                  } else {
                      
$messages[]    = 'Videos loaded! Please enter required information below!';
                      
$loaded     = TRUE;
                  }
              }
        }
        
        if (isset(
$_POST['submit_process'])) {
            
$this->unique   = $filter->get('unique');
            
$status         = (int) trim($_POST['status']);
            
$premium        = (isset($_POST['premium'])) ? (int) trim($_POST['premium']) : 0;
            
$username       = $filter->get('username');
            
$type           = $filter->get('type');
            
$category        = (isset($_POST['category'])) ? (array) $_POST['category'] : array();
            
$queue            = (int) trim($_POST['queue']);
            
            if (
$username == '') {
                
$errors[] = 'Useranme field cannot be left blank!';
            } else {
                
$this->db->query("SELECT user_id
                                  FROM #__user
                                  WHERE username = '"
.$this->db->escape($username)."'
                                  LIMIT 1"
);
                if (
$this->db->affected_rows()) {
                    
$user_id     = (int) $this->db->fetch_field('user_id');
                } else {
                    
$errors[]   = 'Invalid username! Are you sure this username exists!?';
                }   
            }
            
            if (!
$category) {
                  
$errors[]    = 'Please check at least one category for your videos!';
            }
            
              foreach (
$_POST as $key => $values) {
                  if (
is_array($_POST[$key]) && is_numeric($key)) {
                      
$videos[$key] = $values;
                  }
              }
              
              if (!
$videos) {
                  
$errors[] = 'Failed to find any videos! Application error!?';
              }
              
              if (!
$errors) {
                  
$etitle    = FALSE;
                  
$etags    = FALSE;
                  
$etagsf = FALSE;
                  foreach (
$videos as $video) {
                      if (
$video['title'] == '') {
                          
$etitle = TRUE;
                      }
                      
                      if (
$video['tags'] == '') {
                          
$etags    = TRUE;
                      }
                      
                      
// i could add some tags check here....
                  
}
                  
                  if (
$etitle) {
                      
$errors[] = 'One or more videos do not have any title set!';
                  }
                  
                  if (
$etags) {
                      
$errors[] = 'One or more videos do not have any tags set!';
                  }
                  
                  if (!
$errors) {
                      
session_write_close();

                    
$count          = count($videos);
                    
$success        = 0;
                    
$incr           = round((100/$count), 2);
                    
$progress       = 0;

                    
$this->add_date = date('Y-m-d h:i:s');
                    
$this->add_time = time();
                    
                    
$this->update_progress($progress.'^Processing '.$count.' videos ...');

                    foreach (
$videos as $id => $video) {
                        
$this->update_progress($progress.'^Processing '.$video['title'].' ...');
                        if (!
$this->process_video($video, $user_id, $category, $type, $premium, $status, $queue)) {
                            
$errors[] = $this->get_error();
                        } else {
                            ++
$success;
                        }
                        
                        
$progress = sprintf("%01.2f", ($progress+$incr));
                    }
                    
                    if (!
$errors) {
                          
$message    = ($queue) ? 'queued ' : 'processed ';
                        
$messages[] = 'Successfully '.$message.$success.' videos!';
                    }                    
                  }
              }
              
              
$loaded = TRUE;
        }
          
        
$tpl->menu          = 'video';
        
$tpl->submenu       = 'video_add';
        
$tpl->extramenu        = 'video_upload_ftp';
        
$tpl->meta_title    = 'Admin::Video::Upload FTP';
        
$tpl->errors        = $errors;
        
$tpl->messages        = $messages;
        
$tpl->warnings        = $warnings;
        
$tpl->unique        = $unique;
        
$tpl->username        = $username;
        
$tpl->status        = $status;
        
$tpl->queue            = $queue;
        
$tpl->premium        = $premium;
        
$tpl->type            = $type;
        
$tpl->statuses        = $statuses;
        
$tpl->dir            = $dir;
        
$tpl->loaded        = $loaded;
        
$tpl->videos        = $videos;
        
$tpl->category        = $category;
        
$tpl->categories    = $categories;
        
$tpl->load(array('header', 'video_upload_ftp', 'footer'));
        
$tpl->display();
    }

    private function 
process_video($video, $user_id, $category, $type, $premium, $status, $queue)
    {
        
$vmodel    = VModel::load('video', 'video', true);
        
$vmodel->reload();
        
        if (
$video_id = $vmodel->add(array(
            
'user_id'        => $user_id,
            
'title'            => $video['title'],
            
'slug'            => prepare_string($video['title'], true, VF::cfg_item('module.video.slug_max_length')),
            
'description'    => $video['description'],
            
'type'            => $type,
            
'premium'        => $premium,
            
'status'        => 4))) {
            
            foreach (
$category as $cat_id) {
                
$vmodel->add_category($video_id, $cat_id);
            }
            
            
$tags = explode(',', $video['tags']);
            foreach (
$tags as $tag) {
                
$vmodel->add_tag($video_id, $tag);
            }
            
            
$thumbs    = 0;
            
$proc    = array(
                
'flv'        => false,
                
'mp4'        => false,
                
'mp4_hd'    => false,
                
'mobile'    => false,
                
'server_id'    => 0,
                
's3'        => 0,
                
'size'        => 0
            
);
            
            
$src            = $video['path'];
            if (
$queue === 0) {
                
$conv            = VF::factory('video');
                
$conv->log_setup($video_id);
                
$conv->log_clean    = FALSE;
            
                if (
$conv->load($src)) {
                    
// moved thumb extraction before conversion (watermarking hooks)
                      
$thumbs = $conv->extract_thumbs($src, $video_id);
                      if (
$thumbs <= 0) {
                          
$status = 7;
                      }
                      
                      
VHelper::load('module.video.conv');
                      
$proc = VHelper_video_conv::process($video_id, $src, $conv, true);
                } else {
                    
$this->error = 'Failed to load source file ('.$src.')!';
                
                    
$vmodel->reload(true)->update($video_id, array('status' => 7));
                    
                    return 
false;
                }
            } else {
                
$ext    = VFile::ext(basename($video['path']));
                if (!
copy($src, MEDIA_DIR.'/videos/vid/'.$video_id.'.'.$ext)) {
                    
$this->error = 'Failed to move source file!';
                
                    
$vmodel->reload(true)->update($video_id, array('status' => 7));
                    
                    return 
false;
                }
                
                @
VFile::delete($src);
            }
            
            
$vmodel->reload();
            
$vmodel->add_orig(array(
                
'video_id'    => $video_id,
                
'user_id'    => $user_id,
                
'filename'    => VFile::name($video['path']),
                
'ext'        => VFile::ext($video['path']),
                
'size'        => (VFile::exists($video['path'])) ? filesize($video['path']) : 0,
                
'method'    => 'upload_ftp',
                
'url'        => ''
            
));
            
            if (
$queue) {
                
$vmodel->add_queue($video_id, $status);
            }
            
            
$vmodel->update($video_id, array(
                
'ext'        => ($proc['mp4']) ? 'mp4' : 'flv',
                
'size'        => $proc['size'],
                
'duration'    => (isset($conv)) ? $conv->data['duration_seconds'] : 0,
                
'thumbs'    => $thumbs,
                
'mobile'    => $proc['mobile'],
                
'server'    => $proc['server_id'],
                
's3'        => $proc['s3'],
                
'status'    => ($queue) ? 6 : $status
            
));
            
            if (isset(
$conv)) {
                  
$conv->clean();
              }
                
            return 
true;
        }
    }
    
    private function 
get_error()
    {
        return 
$this->error;
    }
    
    private function 
update_progress($data)
    {
        
VFile::write(VFile::safe(TMP_DIR.'/logs/'.$this->unique.'_mass_ftp_progress.log'), $data);    
    }
}
Онлайн: 2
Реклама