Файл: adultscript-2.0.3-pro/files/admin/modules/video/components/upload.php
Строк: 121
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_Admin_video_upload
{
private $db;
private $vcfg;
public function __construct()
{
$this->db = VF::factory('database');
$this->vcfg = VF::cfg('module.video');
}
public function render()
{
$vcfg = VF::cfg('module.video');
$errors = array();
$messages = array();
$cmodel = VModel::load('category', 'video', true);
$chmodel = VModel::load('channel', 'channel', true);
$vmodel = VModel::load('video', 'video', true);
$umodel = VModel::load('user', 'user', true);
$upload = array(
'title' => '', 'description' => '', 'tags' => '', 'category' => array(),
'broadcast' => 'public', 'status' => 1, 'username' => '', 'premium' => 0,
'method' => 'file', 'url' => '', 'channel_id' => 0, 'queue' => 1
);
if (isset($_POST['submit_upload'])) {
$filter = VF::factory('filter');
$username = $filter->get('username');
$title = $filter->get('title');
$desc = $filter->get('desc');
$category = (isset($_POST['categories'])) ? (array) $_POST['categories'] : array();
$tags = $filter->get('tags');
$status = (int) trim($_POST['status']);
$broadcast = $filter->get('broadcast');
$method = $filter->get('method');
$url = $filter->get('url');
$premium = (VModule::enabled('premium')) ? (int) trim($_POST['premium']) : 0;
$channel_id = (int) $filter->get('channel_id');
$queue = (int) trim($_POST['queue']);
if ($username == '') {
$errors[] = 'Video username field cannot be left blank!';
} else {
if ($user_id = $umodel->exists('username', $username)) {
$upload['username'] = $username;
} else {
$errors[] = 'Username cannot be found! Are you sure this user exists!?';
}
}
if ($title == '') {
$errors[] = 'Video title field cannot be left blank!';
} elseif (!VValid::length($title, 1, 100)) {
$errors[] = 'Video title can contain maximum 100 characters!';
} else {
$upload['title'] = $title;
}
if ($desc != '') {
$upload['description'] = $desc;
}
if (!$category) {
$errors[] = 'Please select at least one category for this video!';
} else {
$upload['category'] = $category;
}
if ($tags == '') {
$errors[] = 'Please enter the video tags!';
} else {
$upload['tags'] = prepare_tags($tags);
}
if ($method == 'url') {
if ($url == '') {
$errors[] = 'Please enter a url for the file video!';
} elseif (!VValid::url($url)) {
$errors[] = 'Video file url is not a valid url address!';
} else {
$filename = VFile::name($url);
$ext = VFile::ext($filename);
if (in_array($ext, $vcfg['video_allowed_ext'])) {
$file = tempnam(TMP_DIR.'/downloads', 'video_');
chmod($file, 0644);
session_write_close();
if (VCUrl::file($url, $file)) {
$size = filesize($file);
if ($size > ($vcfg['video_max_size']*1024*1024)) {
$errors[] = 'Uploaded file exceeds the maximum allowed filesize of '.$vcfg['video_max_size'].' MB!';
}
} else {
$errors[] = 'Failed to download video file!';
}
} else {
$errors[] = 'Uploaded file is not supported! Valid formats: '.implode(', ', $vcfg['video_allowed_ext']).'!';
}
}
} elseif ($method == 'file') {
if ($_FILES['file']['tmp_name'] != '') {
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$file = $_FILES['file']['name'];
$filename = VFile::name($file);
$ext = VFile::ext($filename);
if (in_array($ext, $vcfg['video_allowed_ext'])) {
$size = filesize($_FILES['file']['tmp_name']);
if ($size > ($vcfg['video_max_size']*1024*1024)) {
$errors[] = 'Uploaded file exceeds the maximum allowed filesize of '.$vcfg['video_max_size'].' MB!';
}
} else {
$errors[] = 'Uploaded file is not supported! Valid formats: '.implode(', ', $vcfg['video_allowed_ext']).'!';
}
} else {
$errors[] = 'Uploaded video is not a valid uploaded file!';
}
} else {
$errors[] = 'Please upload a video file!';
}
} else {
$errors[] = 'Invalid upload method selected! What exactly did you select!?';
}
$this->upload['status'] = $status;
$this->upload['channel_id'] = $channel_id;
$this->upload['broadcast'] = $broadcast;
$this->upload['premium'] = $premium;
if (!$errors) {
if ($method != 'file') {
VF::factory_remove('database');
$this->db = VF::factory('database');
}
$slug = prepare_string($title, TRUE, $this->vcfg['slug_max_length']);
if ($video_id = $vmodel->add(array(
'user_id' => $user_id,
'title' => $title,
'slug' => $slug,
'description' => $desc,
'type' => $broadcast,
'premium' => $premium,
'status' => 3))) {
$src = ($method == 'file') ? $_FILES['file']['tmp_name'] : $file;
$dst = MEDIA_DIR.'/videos/vid/'.$video_id.'.'.$ext;
$move = ($method == 'file') ? 'move_uploaded_file' : 'rename';
if ($move($src, $dst)) {
chmod($dst, 0777);
if (!$queue) {
$conv = VF::factory('video');
if ($conv->load($dst)) {
$cmd = VF::cfg_item('php_cli_path').' '.MODULES_DIR.'/video/scripts/convert.php '.$video_id.' '.$ext.' '.$status;
exec(escapeshellcmd($cmd). ' >/dev/null &');
$status = 4;
} else {
$errors[] = 'Failed to prepare video file for conversion!';
}
}
} else {
$errors[] = 'Failed to move uploaded file!';
}
if (!$errors) {
foreach ($category as $cat_id) {
$cmodel->add_video($cat_id, $video_id);
}
$tags = explode(',', $upload['tags']);
foreach ($tags as $tag) {
$vmodel->add_tag($video_id, $tag);
}
$this->db->query("UPDATE #__user_activity SET total_videos = total_videos+1 WHERE user_id = ".$user_id." LIMIT 1");
$url = (isset($url)) ? $url : '';
$vmodel->add_orig(array(
'video_id' => $video_id,
'user_id' => $user_id,
'filename' => $filename,
'ext' => $ext,
'size' => $size,
'method' => 'upload_'.$method,
'url' => $url
));
$vmodel->update($video_id, array(
'size' => $size,
'status' => ($queue) ? 6 : $status
));
if ($queue) {
$vmodel->add_queue($video_id, $status);
}
if ($channel_id !== 0) {
$vmodel->add_channel($video_id, $channel_id);
}
if ($queue) {
$messages[] = 'Video uploaded and queued!';
} else {
$messages[] = 'Video uploaded! Conversion is running in background!';
}
}
} else {
$errors[] = 'Failed to create video entry! Application error!?';
}
}
}
$tpl = VF::factory('template');
$tpl->meta_title = 'Admin::Video::Upload';
$tpl->menu = 'video';
$tpl->submenu = 'video_add';
$tpl->extramenu = 'video_upload';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->upload = $upload;
$tpl->categories = $cmodel->categories(
array('c.cat_id', 'c.name'),
array('sort' => 'c.name', 'order' => 'ASC')
);
$tpl->channels = $chmodel->channels(array('c.channel_id', 'c.name'));
$tpl->load(array('header', 'video_upload', 'footer'));
$tpl->display();
}
}