Файл: adultscript-2.0.3-pro/files/admin/modules/video/components/embed.php
Строк: 126
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_Admin_video_embed
{
private $db;
public function __construct()
{
$this->db = VF::factory('database');
}
public function render()
{
$vcfg = VF::cfg('module.video');
$errors = array();
$messages = array();
$embed = array(
'title' => '', 'description' => '', 'tags' => '', 'category' => array(),
'embed_code' => '', 'urls' => array(), 'thumb_method' => 'file', 'username' => '',
'duration' => '', 'status' => 1, 'broadcast' => 'public',
);
if (isset($_POST['submit_embed'])) {
$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');
$duration = $filter->get('duration');
$status = (int) trim($_POST['status']);
$broadcast = $filter->get('broadcast');
$embed_code = trim($_POST['embed_code']);
if ($username == '') {
$errors[] = 'Video username 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 = $this->db->fetch_field('user_id');
$embed['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 {
$embed['title'] = $title;
}
if ($desc != '') {
$embed['description'] = $desc;
}
if ($tags == '') {
$errors[] = 'Please enter the video tags!';
} else {
$embed['tags'] = prepare_tags($tags);
}
if (empty($category)) {
$errors[] = 'Please select at least one category for this video!';
} else {
$embed['category'] = $category;
}
if ($duration == '') {
$errors[] = 'Please enter the video duration!';
} else {
if (!preg_match('/[0-9]{2}:[0-9]{2}/', $duration)) {
$errors[] = 'Invalid duration format!';
} else {
$embed['duration'] = $duration;
}
}
if ($embed_code == '') {
$errors[] = 'Please enter video embed code!';
} else {
$embed['embed_code'] = $embed_code;
}
if (!$errors) {
$thumbs = array();
$thumb_urls = array();
$allowed_exts = $vcfg['thumb_allowed_ext'];
if (isset($_FILES['thumb_file']) && $_FILES['thumb_file']['tmp_name'] != '') {
$files = array();
$count = 0;
foreach ($_FILES as $key => $value) {
if (strpos($key, 'thumb_file') !== FALSE) {
$files[] = $key;
++$count;
}
}
foreach ($files as $key => $file) {
if ($_FILES[$file]['tmp_name'] != '') {
if (is_uploaded_file($_FILES[$file]['tmp_name'])) {
$filename = $_FILES[$file]['name'];
$ext = VFile::ext($filename);
$size = filesize($_FILES[$file]['tmp_name']);
if ($size !== 0) {
if (!in_array($ext, $allowed_exts)) {
$errors[] = 'Thumb file '.$_FILES[$file]['name'].' is not a valid image format! Supported formats: '.implode(', ', $allowed_exts).'!';
unset($files[$key]);
} else {
$thumbs[$file] = array('size' => $size, 'ext' => $ext);
}
} else {
$errors[] = 'Thumb file '.$_FILES[$file]['name'].' has size 0!';
}
} else {
$errors[] = 'Thumb file '.$_FILES[$file]['name'].' is not a valid uploaded file!';
}
} else {
$errors[] = 'Failed to upload file!';
}
}
if (!$thumbs) {
$errors[] = 'None of the uploaded files were compatible with our systems!';
}
}
if (isset($_POST['thumb_url']) && $_POST['thumb_url'] != '') {
$urls = array();
$count = 0;
foreach ($_POST as $key => $value) {
if (strpos($key, 'thumb_url') !== FALSE) {
$urls[] = $value;
++$count;
}
}
foreach ($urls as $key => $url) {
$filename = VFile::name($url);
$ext = VFile::ext($filename);
if (!in_array($ext, $allowed_exts)) {
$errors[] = 'Thumb image '.$url.' is not a valid image format! Supported formats: '.implode(', ', $allowed_exts).'!';
unset($urls[$key]);
} else {
$thumb_urls[$url] = array('url' => $url, 'ext' => $ext);
}
}
if (!$thumb_urls) {
$errors[] = 'None of the thumb images were compatible with our systems!';
}
}
if (!$thumbs AND !$thumb_urls) {
$errors[] = 'Please select at least one thumb file or enter one thumb url!';
}
}
if (!$errors) {
$this->db->query("INSERT INTO #__video
SET user_id = ".$user_id.",
title = '".$this->db->escape($title)."',
slug = '".$this->db->escape(prepare_string($title, TRUE))."',
description = '".$this->db->escape($desc)."',
duration = ".$this->duration_to_seconds($duration).",
embed_code = '".$this->db->escape($embed_code)."',
type = '".$this->db->escape($broadcast)."',
status = 3");
$video_id = $this->db->get_last_insert_id('#__video');
$image = VF::factory('image');
$count = 1;
$thumb_dir = MEDIA_DIR.'/videos/tmb/'.path($video_id);
VFolder::create($thumb_dir);
foreach ($thumbs as $key => $thumb) {
$tmp_thumb = TMP_DIR.'/thumbs/'.$video_id.'_'.$count.'.'.$thumb['ext'];
if (move_uploaded_file($_FILES[$key]['tmp_name'], $tmp_thumb)) {
if ($image->load($tmp_thumb) &&
$image->resize($vcfg['thumb_width'], $vcfg['thumb_height'], 'EXACT', $thumb_dir.'/'.$count.'.jpg')) {
++$count;
}
}
}
foreach ($thumb_urls as $key => $thumb) {
$tmp_thumb = TMP_DIR.'/thumbs/'.$video_id.'_'.$count.'.'.$thumb['ext'];
if (VCurl::file($thumb['url'], $tmp_thumb)) {
if ($image->load($tmp_thumb) &&
$image->resize($vcfg['thumb_width'], $vcfg['thumb_height'], 'EXACT', $thumb_dir.'/'.$count.'.jpg')) {
++$count;
}
}
}
if ($count >= 1) {
foreach ($category as $cat) {
$this->db->query("INSERT INTO #__video_category SET video_id = ".$video_id.", cat_id = ".(int) $cat);
}
$tags = explode(',', $embed['tags']);
foreach ($tags as $tag) {
$this->db->query("INSERT INTO #__video_tags SET video_id = ".$video_id.", name = '".$this->db->escape(trim($tag))."'");
}
$time = time();
$this->db->query("INSERT INTO #__video_orig
SET video_id = ".$video_id.",
user_id = ".$user_id.",
filename = '',
ext = '',
size = 0,
method = 'embed',
ip = ".VServer::ip(TRUE).",
add_time = ".$time);
$thumbs = ($count > 1) ? $count-1 : 1;
$this->db->query("UPDATE #__video
SET thumb = 1,
thumbs = ".$thumbs.",
add_date = '".date('Y-m-d h:i:s')."',
add_time = ".$time.",
status = ".$status."
WHERE video_id = ".$video_id."
LIMIT 1");
$messages[] = 'Video added!';
} else {
$errors[] = 'Failed to create thumbs!';
}
}
}
$tpl = VF::factory('template');
$tpl->meta_title = 'Admin::Video::Embed';
$tpl->menu = 'video';
$tpl->submenu = 'video_embed';
$tpl->submenu_top = 'video_upload';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->embed = $embed;
$tpl->categories = $this->get_video_categories();
$tpl->load(array('header', 'video_embed', 'footer'));
$tpl->display();
}
private function get_video_categories()
{
return $this->db->get_rows("SELECT cat_id, name FROM #__video_categories ORDER BY name ASC");
}
private function duration_to_seconds($duration)
{
$duration = explode(':', $duration);
$minutes = sprintf('%01d', $duration['0']);
$seconds = sprintf('%01d', $duration['1']);
return (float) (($minutes * 60) + $seconds);
}
}