Файл: adultscript-2.0.3-pro/files/admin/modules/video/components/edit.php
Строк: 225
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_Admin_video_edit
{
private $db;
private $vcfg;
public function __construct()
{
$this->db = VF::factory('database');
$this->vcfg = VF::cfg('module.video');
}
public function render()
{
$video = array();
$errors = array();
$messages = array();
$warnings = array();
$categories = $this->get_video_categories();
$video_id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? (int) $_GET['id'] : 0;
$this->db->query("SELECT v.video_id, v.user_id, v.premium, v.server, v.channel_id,
GROUP_CONCAT(DISTINCT vc.cat_id) AS categories,
GROUP_CONCAT(DISTINCT t.name) AS tags
FROM #__video AS v
LEFT JOIN #__video_category AS vc ON (vc.video_id = v.video_id)
LEFT JOIN #__video_tags AS t ON (t.video_id = v.video_id)
WHERE v.video_id = ".$video_id."
GROUP BY v.video_id
LIMIT 1");
if ($this->db->affected_rows()) {
$o_data = $this->db->fetch_assoc();
$o_categories = explode(',', $o_data['categories']);
sort($o_categories);
$o_user_id = (int) $o_data['user_id'];
$o_tags = $o_data['tags'];
$o_premium = (int) $o_data['premium'];
$o_server = (int) $o_data['server'];
$o_channel_id = (int) $o_data['channel_id'];
if (isset($_POST['submit_edit_video'])) {
$filter = VF::factory('filter');
$username = $filter->get('username');
$title = $filter->get('title');
$desc = $filter->get('desc');
$tags = $filter->get('tags');
$type = $filter->get('type');
$n_categories = (array) $_POST['category'];
sort($n_categories);
$allow_rating = $filter->get('allow_rating', 'INT');
$allow_comment = $filter->get('allow_comment', 'INT');
$allow_download = $filter->get('allow_download', 'INT');
$rating = $filter->get('rating', 'FLOAT');
$rated_by = $filter->get('rated_by', 'INT');
$total_views = $filter->get('total_views', 'INT');
$total_comments = $filter->get('total_comments', 'INT');
$total_downloads = $filter->get('total_downloads', 'INT');
$total_favorites = $filter->get('total_favorites', 'INT');
$thumb = $filter->get('thumb', 'INT');
$mobile = $filter->get('mobile', 'INT');
$flagged = $filter->get('flagged', 'INT');
$locked = $filter->get('locked', 'INT');
$status = $filter->get('status', 'INT');
$embed_code = (isset($_POST['embed_code'])) ? htmlspecialchars_decode($_POST['embed_code'], ENT_QUOTES) : '';
$size = $filter->get('size', 'INT');
$format = $filter->get('ext');
$duration = $filter->get('duration', 'FLOAT');
$add_date = $filter->get('add_date');
$view_date = $filter->get('view_date');
$premium = (isset($_POST['premium'])) ? (int) trim($_POST['premium']) : $o_premium;
$channel_id = (int) trim($_POST['channel_id']);
if (isset($_POST['server'])) {
$n_server = (int) trim($_POST['server']);
}
$meta_title = $filter->get('meta_title');
$meta_desc = $filter->get('meta_desc');
$meta_keys = $filter->get('meta_keys');
if ($username == '') {
$errors[] = 'Please enter a video username!';
} 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 ($title == '') {
$errors[] = 'Please enter video title!';
} elseif (!VValid::length($title, $this->vcfg['title_min_length'], $this->vcfg['title_max_length'])) {
$errors[] = 'Video title must contain at least '.$this->vcfg['title_min_length'].' and no more than '.$this->vcfg['title_max_length'].' characters!';
}
if (!$n_categories) {
$errors[] = 'Please select at least one category for this video!';
}
if ($tags == '') {
$errors[] = 'Please enter video tags! Tags should be separated by spaces!';
} else {
$tags = prepare_tags($tags);
}
if (!strtotime($add_date) OR strlen($add_date) !== 19) {
$errors[] = 'Video add date is not a valid date (valid format: yyyy-mm-dd h:i:s)!';
}
if ($view_date != '0000-00-00 00:00:00') {
if (!strtotime($view_date) OR strlen($view_date) != 19) {
$errors[] = 'Video view date is not a valid date (valid format: yyyy-mm-dd h:i:s)!';
}
}
if (!$errors) {
$slug = prepare_string($title, TRUE, $this->vcfg['slug_max_length']);
$this->db->query("UPDATE #__video
SET user_id = ".$user_id.",
title = '".$this->db->escape($title)."',
slug = '".$this->db->escape($slug)."',
description = '".$this->db->escape($desc)."',
type = '".$this->db->escape($type)."',
rating = ".$rating.",
rated_by = ".$rated_by.",
ext = '".$this->db->escape($format)."',
duration = ".$duration.",
size = ".$size.",
thumb = ".$thumb.",
embed_code = '".$this->db->escape($embed_code)."',
allow_rating = '".$allow_rating."',
allow_comment = '".$allow_comment."',
total_views = ".$total_views.",
total_comments = ".$total_comments.",
add_date = '".$this->db->escape($add_date)."',
add_time = ".strtotime($add_date).",
view_date = '".$this->db->escape($view_date)."',
channel_id = ".$channel_id.",
premium = '".$premium."',
mobile = '".$mobile."',
flagged = '".$flagged."',
locked = '".$locked."',
status = ".$status."
WHERE video_id = ".$video_id."
LIMIT 1");
if ($meta_title != '' OR $meta_desc != '' OR $meta_keys != '') {
$this->db->query("SELECT video_id
FROM #__video_meta
WHERE video_id = ".$video_id."
LIMIT 1");
if ($this->db->affected_rows()) {
$this->db->query("UPDATE #__video_meta
SET meta_title = '".$this->db->escape($meta_title)."',
meta_desc = '".$this->db->escape($meta_desc)."',
meta_keys = '".$this->db->escape($meta_keys)."'
WHERE video_id = ".$video_id."
LIMIT 1");
} else {
$this->db->query("INSERT INTO #__video_meta
SET video_id = ".$video_id.",
meta_title = '".$this->db->escape($meta_title)."',
meta_desc = '".$this->db->escape($meta_desc)."',
meta_keys = '".$this->db->escape($meta_keys)."'");
}
}
if ($o_user_id !== $user_id) {
$this->db->query("UPDATE #__user_activity SET total_videos = total_videos-1 WHERE user_id = ".$o_user_id." LIMIT 1");
$this->db->query("UPDATE #__user_activity SET total_videos = total_videos+1 WHERE user_id = ".$user_id." LIMIT 1");
}
if ($o_categories != $n_categories) {
foreach ($o_categories as $category) {
if (!in_array($category, $n_categories)) {
$category = (int) $category;
$this->db->query("DELETE FROM #__video_category WHERE video_id = ".$video_id." AND cat_id = ".$category." LIMIT 1");
$this->db->query("UPDATE #__video_categories SET total_videos = total_videos-1 WHERE cat_id = ".$category." LIMIT 1");
}
}
foreach ($n_categories as $category) {
if (!in_array($category, $o_categories)) {
$category = (int) $category;
$this->db->query("INSERT INTO #__video_category SET video_id = ".$video_id.", cat_id = ".$category);
$this->db->query("UPDATE #__video_categories SET total_videos = total_videos+1 WHERE cat_id = ".$category." LIMIT 1");
}
}
}
$o_tags = explode(',', $o_tags);
sort($o_tags);
$tags = explode(',', $tags);
sort($tags);
if ($o_tags != $tags) {
$this->db->query("DELETE FROM #__video_tags WHERE video_id = ".$video_id);
foreach ($tags as $tag) {
$this->db->query("INSERT INTO #__video_tags SET video_id = ".$video_id.", name = '".$this->db->escape(trim($tag))."'");
}
}
if ($o_channel_id !== $channel_id) {
if ($o_channel_id === 0) {
$this->db->query("UPDATE #__channel
SET total_videos = total_videos+1,
last_add_time = ".time()."
WHERE channel_id = ".$channel_id."
LIMIT 1");
} elseif ($channel_id === 0) {
$this->db->query("UPDATE #__channel
SET total_videos = total_videos-1
WHERE channel_id = ".$o_channel_id."
LIMIT 1");
} else {
$this->db->query("UPDATE #__channel
SET total_videos = total_videos+1,
last_add_time = ".time()."
WHERE channel_id = ".$channel_id."
LIMIT 1");
$this->db->debug();
$this->db->query("UPDATE #__channel
SET total_videos = total_videos-1
WHERE channel_id = ".$o_channel_id."
LIMIT 1");
}
}
if (empty($embed_code) && isset($n_server) && $n_server !== $o_server) {
VHelper::load('module.video.server');
// video is no on the main server, move to main server
if ($o_server !== 0) {
if (!$this->move_to_main($o_server, $video_id, $format, $mobile)) {
$errors[] = 'Failed to move video to Main Server!';
}
}
// video was moved to main server
if (!$errors) {
// ok...so everything was moved on the main server
$c_server = 0;
// move to new server
if ($n_server !== 0) {
if ($this->move_to_server($n_server, $video_id, $format, $mobile)) {
$c_server = $n_server;
} else {
$errors[] = 'Failed to move video to new server (video is on the main server now)!';
}
}
VF::factory_remove('database');
$this->db = VF::factory('database');
$this->db->query("UPDATE #__video
SET server = ".$c_server."
WHERE video_id = ".$video_id."
LIMIT 1");
}
}
$messages[] = 'Video updated!';
VF::factory('cache')->remove('video_'.$video_id);
}
}
$this->db->query("SELECT v.*, vm.meta_title, vm.meta_desc, vm.meta_keys, u.username,
GROUP_CONCAT(DISTINCT vc.cat_id) AS category,
GROUP_CONCAT(c.name) AS name,
GROUP_CONCAT(DISTINCT t.name) AS tags
FROM #__video AS v
LEFT JOIN #__video_category AS vc ON (vc.video_id = v.video_id)
LEFT JOIN #__video_categories AS c ON (vc.cat_id = c.cat_id)
LEFT JOIN #__video_tags AS t ON (t.video_id = v.video_id)
LEFT JOIN #__video_meta AS vm ON (vm.video_id = v.video_id)
LEFT JOIN #__user AS u ON (u.user_id = v.user_id)
WHERE v.video_id = ".$video_id."
GROUP BY v.video_id
LIMIT 1");
$video = $this->db->fetch_assoc();
}
$tpl = VF::factory('template');
$tpl->menu = 'video';
$tpl->submenu = 'video_manage';
$tpl->meta_title = 'Admin::Video::Edit';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->warnings = $warnings;
$tpl->video = $video;
$tpl->video_id = $video_id;
$tpl->categories = $categories;
$tpl->channels = VModel::load('channel', 'channel', true)->channels(array('c.channel_id', 'c.name'));
$tpl->servers = $this->get_servers();
$tpl->load(array('header', 'video_edit', 'footer'));
$tpl->display();
}
private function get_video_categories()
{
$this->db->query("SELECT cat_id, parent_id, slug, name
FROM #__video_categories
ORDER BY slug ASC");
$categories = $this->db->fetch_rows();
return ($this->vcfg['subcategories'] == '1') ? VArray::tree($categories) : $categories;
}
private function get_sponsors()
{
$this->db->query("SELECT sponsor_id, sponsor_name
FROM #__video_sponsors
WHERE status = '1'");
return $this->db->fetch_rows();
}
private function get_servers()
{
$this->db->query("SELECT server_id, server_name
FROM #__server
ORDER BY server_id ASC");
return $this->db->fetch_rows();
}
private function move_to_main($server_id, $video_id, $format='flv', $mobile=0)
{
$server = VHelper_video_server::get($server_id, FALSE);
if (!$server) {
return FALSE;
}
$dst_flv = MEDIA_DIR.'/videos/flv/'.$video_id.'.flv';
if (VHelper_video_server::exists($server, 'flv/'.$video_id.'.flv')) {
if (VHelper_video_server::download($server, 'flv/'.$video_id.'.flv', $dst_flv)) {
$flv_downloaded = TRUE;
}
}
if ($format == 'mp4') {
$dst_mp4 = MEDIA_DIR.'/videos/mp4/'.$video_id.'.mp4';
if (VHelper_video_server::exists($server, 'mp4/'.$video_id.'.mp4')) {
if (VHelper_video_server::download($server, 'mp4/'.$video_id.'.mp4', $dst_mp4)) {
$mp4_downloaded = TRUE;
}
}
}
if ($mobile == '1') {
$dst_mobile = MEDIA_DIR.'/videos/mobile/'.$video_id.'.mp4';
if (VHelper_video_server::exists($server, 'mobile/'.$video_id.'.mp4')) {
if (VHelper_video_server::download($server, 'mobile/'.$video_id.'.mp4', $dst_mobile)) {
$mobile_downloaded = TRUE;
}
}
}
// only abort if no FLV was found if the format is FLV
if (!VFile::exists($dst_flv) && $format == 'flv') {
return FALSE;
}
if ($format == 'mp4') {
if (!VFile::exists($dst_mp4)) {
return FALSE;
}
}
if ($mobile == '1') {
if (!VFile::exists($dst_mobile)) {
return FALSE;
}
}
VHelper_video_server::delete($server, '/flv/'.$video_id.'.flv');
if (isset($dst_mp4)) {
VHelper_video_server::delete($server, '/mp4/'.$video_id.'.mp4');
}
if (isset($dst_mobile)) {
VHelper_video_server::delete($server, '/mobile/'.$video_id.'.mp4');
}
return TRUE;
}
private function move_to_server($server_id, $video_id, $format='flv', $mobile=0)
{
$server = VHelper_video_server::get($server_id, FALSE);
if (!$server) {
return FALSE;
}
$src_flv = MEDIA_DIR.'/videos/flv/'.$video_id.'.flv';
if (VFile::exists($src_flv)) {
if (!VHelper_video_server::upload($server, $src_flv, '/flv/'.$video_id.'.flv')) {
return FALSE;
}
} else {
if ($format == 'flv') {
return FALSE;
}
}
if ($format == 'mp4') {
$src_mp4 = MEDIA_DIR.'/videos/mp4/'.$video_id.'.mp4';
if (!VFile::exists($src_mp4)) {
return FALSE;
}
if (!VHelper_video_server::upload($server, $src_mp4, '/mp4/'.$video_id.'.mp4')) {
return FALSE;
}
}
if ($mobile == '1') {
$src_mobile = MEDIA_DIR.'/videos/mobile/'.$video_id.'.mp4';
if (!VFile::exists($src_mobile)) {
return FALSE;
}
if (!VHelper_video_server::upload($server, $src_mobile, '/mobile/'.$video_id.'.mp4')) {
return FALSE;
}
}
VFile::delete($src_flv);
if (isset($src_mp4)) {
VFile::delete($src_mp4);
}
if (isset($src_mobile)) {
VFile::delete($src_mobile);
}
return TRUE;
}
}
?>