Файл: adultscript-2.0.3-pro/files/mobile/components/video_upload.php
Строк: 107
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_mobile_video_upload extends VComponent_mobile_video
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function render()
    {
        $errors        = array();
        $messages    = array();
        
        VLanguage::load('frontend.video');
    
        if (!VF::cfg_item('module.video.upload_enabled')) {
            $errors[]    = __('upload-disabled');
        }        
    
        $perm        = VF::cfg_item('module.video.upload_perm');
        $allow        = true;
        if ($perm != 'anonymous') {
            VAuth::check(ucfirst($perm), MOBILE_URL.'/user/login/', __('upload-perm', array($perm)));
            $user_id    = $_SESSION['user_id'];
        } else {
            $user_id     = (VAuth::loggedin()) ? (int) $_SESSION['user_id'] : $this->get_anonymous_id();
        }
        $limit        = VF::cfg_item('module.video.upload_limit');
        if (!$limit) {
            $add_time   = strtotime(date('Y-m-d').' 00:00:00');
            $this->db->query("SELECT COUNT(*) AS total_videos
                              FROM #__video
                              WHERE add_time > ".$add_time."
                              AND user_id = ".$user_id);
            if ($this->db->affected_rows()) {
                if ($this->db->fetch_field('total_videos') >= $limit) {
                    $errors[]   = __('upload-limit', array($limit));
                }
            }
        }
        $unique     = time().'0'.mt_rand();
        $video      = array(
            'title' => '', 'description' => '', 'tags' => '', 'category' => array(), 'type' => 'public'
        );
        if (isset($_POST['cancel-upload'])) {
            $allow = false;
        }
    
        $categories     = $this->get_video_categories();
        
        if (isset($_POST['upload-submitted']) && $allow) {
            $filter         = VF::factory('filter');
            $title          = $filter->get('title');
            $description    = $filter->get('description');
            $category       = (isset($_POST['category'])) ? (array) $_POST['category'] : array();
            $tags           = $filter->get('tags');
            $type           = $filter->get('type');
            $upload_id        = $filter->get('upload_id');
            $ext            = substr($filter->get('ext'), 1);
            $pornstars        = (isset($_POST['pornstars'])) ? (array) $_POST['pornstars'] : array();
                                
            if ($title == '') {
                $errors[] = __('title-empty');
            } elseif (!VValid::length($title, 1, 100)) {
                $errors[] = __('title-length');
            } else {
                  $video['title']    = $title;
            }
                    
            if (!$category) {
                $errors[]     = __('category-empty');
            } elseif (count($category) > VF::cfg_item('module.video.max_categories')) {
                  $errors[]    = __('category-max', array(VF::cfg_item('module.video.max_categories')));
            } else {
                  $video['category']    = $category;
            }
            
            if ($tags == '') {
                $errors[] = __('tags-empty');
            } else {
                $tags   = prepare_tags($tags);
                if ($tags == '') {
                    $errors[] = __('tags-invalid');
                } else {
                    $arr        = explode(',', $tags);
                    $max_length    = VF::cfg_item('module.video.tag_max_length');
                    foreach ($arr as $tag) {
                        if (strlen($tag) > $max_length) {
                            $errors[] = __('tag-length', array('"'.$tag.'"', $max_length));
                        }
                        
                        $max_words    = VF::cfg_item('module.video.tag_max_words');
                        if (str_word_count($tag) > $max_words) {
                            $errors[] = __('tag-words', array('"'.$tag.'"', $max_words));
                        }
                    }
                    
                    $video['tags'] = $tags;
                }
            }
            
            if (!ctype_digit($upload_id)) {
                  $errors[]    = 'Invalid upload identifier!';
            }
            
            if (!$errors) {
                  if (!$file = $this->process_file($upload_id, $ext, VF::cfg_item('module.video.video_max_size'), VF::cfg_item('module.video.video_allowed_ext'))) {
                      $errors = array_merge($errors, $this->errors);
                  }
              }
            
            $video['type']            = $type;
            $video['description']    = $description;
            if (!$errors) {
                  $vmodel    = VModel::load('video', 'video', true);
                  if ($video_id = $vmodel->add(array(
                    'user_id'       => $user_id,
                    'title'         => $title,
                    'slug'          => prepare_string($title, true, VF::cfg_item('module.video.slug_max_length')),
                    'description'   => $description,
                    'type'          => 'type',
                    'premium'       => '0',
                    'status'        => 3))) {
                    $dst        = MEDIA_DIR.'/videos/vid/'.$video_id.'.'.$file['ext'];
                    $status     = (VF::cfg_item('module.video.approve')) ? 2 : 1;                    
                    if (rename($file['path'], $dst)) {
                          @chmod($dst, 0777);
                        if (VF::cfg_item('module.video.queue')) {
                            $status = 6;
                        } else {
                            $cmd = VF::cfg_core_item('php_cli_path').' '.MODULES_DIR.'/video/scripts/convert.php '.$video_id.' '.$file['ext'].' '.$status;
                            exec(escapeshellcmd($cmd). ' >/dev/null &');
                        }
                        
                        if ($status !== 6) {
                            $status = 4;
                        }
                        
                        foreach ($category as $cat_id) {
                              $vmodel->add_category($video_id, (int) $cat_id);
                        }
                        
                        $tags = explode(',', $tags);
                        foreach ($tags as $tag) {
                              $vmodel->add_tag($video_id, trim($tag));
                        }
                        
                        $vmodel->add_orig(array(
                            'video_id'      => $video_id,
                            'user_id'       => $user_id,
                            'filename'      => $file['name'],
                            'ext'           => $file['ext'],
                            'size'          => $file['size'],
                            'method'        => 'upload_mobile'
                        ));
                        
                        foreach ($pornstars as $model_id) {
                              $vmodel->add_model($video_id, (int) $model_id);
                        }
                                                
                        if ($status === 6) {
                              $vmodel->add_queue($video_id);
                        }
                        
                        $vmodel->add_activity($user_id);
                        $vmodel->update($video_id, array(
                              'status'    => $status
                        ));
                                                
                        $video['title']         = '';
                        $video['description']   = '';
                        $video['tags']          = '';
                        $video['category']      = array();
                        if ($status === 1 OR $status === 4) {
                            $messages[] = __('upload-success');
                        } elseif ($status === 2 OR $status === 0) {
                            $messages[] = __('upload-approve');
                        } elseif ($status == 6) {
                            $messages[] = __('upload-queue');
                        }                          
                    }
                } else {
                      $errors[]    = 'Failed to create database entry!';
                }
            }
        }
        
        $this->tpl->menu        = 'video';
        $this->tpl->meta_title    = __('upload-video');
        
        $this->tpl->canonical    = BASE_URL.'/upload/';
        $this->tpl->errors        = $errors;
        $this->tpl->messages    = $messages;
        $this->tpl->unique        = $unique;
        $this->tpl->video        = $video;
        $this->tpl->pornstars    = $this->get_pornstars('a');
        $this->tpl->categories    = $categories;
        $this->tpl->load(array('header', 'video_upload', 'footer'));
        $this->tpl->display();
    }
    
    private function get_video_categories()
    {
        if (!$categories = $this->cache->get('categories', 86400)) {
            $this->db->query("SELECT cat_id, parent_id, name, slug, description, total_videos,
                                     title, meta_title, meta_desc, meta_keys
                              FROM #__video_categories
                              WHERE status = '1'
                              ORDER BY slug, parent_id ASC");
            if ($this->db->affected_rows()) {
                $categories = $this->db->fetch_rows();
                $this->cache->store('categories', $categories, 86400);
            }
        }
        return $categories;
    }
    private function get_pornstars($letter='a')
    {
        $this->db->query("SELECT model_id, name
                          FROM #__model
                          WHERE slug LIKE '".$letter."%'
                          AND status = '1'");
        return $this->db->fetch_rows();
    }
    
    private function process_file($upload_id, $ext, $max_size, $allowed_ext)
    {
        VLanguage::load('frontend.upload');
        $file    = TMP_DIR.'/uploads/'.$upload_id.'.'.$ext;
        if (file_exists($file) && is_file($file)) {-
            $size   = filesize($file);
            if ($max_size !== 0 && $size > ($max_size*1024*1024)) {
                $this->errors[] = __('file-limit', array($max_size));
            } else {
                if (in_array($ext, $allowed_ext)) {
                    return array(
                        'path'  => $file,
                        'name'  => $upload_id,
                        'size'  => $size,
                        'ext'   => $ext
                    );
                } else {
                    $this->errors[] = __('file-invalid', array(implode(', ', $allowed_ext)));
                }
            }
        } else {
            $this->errors[] = __('file-select');
        }
        
        return false;
    }
    private function get_anonymous_id()
    {
        $this->db->query("SELECT user_id FROM #__user WHERE username = 'anonymous' LIMIT 1");
        if ($this->db->affected_rows()) {
            return (int) $this->db->fetch_field('user_id');
        }
                
        throw new Exception('Failed to get anonymous id! Application error!?');
    }    
}