Файл: adultscript-2.0.3-pro/files/admin/modules/video/components/fhg.php
Строк: 279
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_Admin_video_fhg extends VModule_Admin_video
{
private $db;
private $add_date;
private $add_time;
private $unique;
private $error;
private $vcfg;
public function __construct()
{
parent::__construct();
$this->db = VF::factory('database');
$this->vcfg = VF::cfg('module.video');
}
public function render()
{
$errors = array();
$messages = array();
$comments = array();
$videos = array();
$processed = FALSE;
$url = NULL;
$username = '';
$categories = $this->get_video_categories();
$type = 'public';
$premium = 0;
$status = 1;
$unique = time().'_'.mt_rand();
$statuses = array(
'0' => 'Suspended',
'1' => 'Published',
'2' => 'Waiting Approval',
'3' => 'Uploading',
'4' => 'Converting',
'5' => 'Grabbing',
'6' => 'Queued',
'7' => 'Error'
);
if (isset($_POST['submit_url'])) {
$url = trim($_POST['url']);
if ($url == '') {
$errors[] = 'Free Hosted Gallery URL field cannot be left blank!';
} else {
VF::load('simple_html_dom.simple_html_dom');
$html = VCurl::string($url);
$html = str_get_html($html);
$exts = array('avi' => 1, 'wmv' => 1, 'mp4' => 1, 'flv' => 1, 'mpg' => 1);
$vids = array();
foreach ($html->find('a') as $element) {
$href = $element->href;
$title = $element->title;
$ext = strtolower(substr($href, strrpos($href, '.')+1));
if (isset($exts[$ext])) {
if (!preg_match('/^http:/', $href) && !preg_match('/^www./', $href)) {
$href = $this->get_url($href, $url);
}
$vids[] = array('url' => $href, 'ext' => $ext, 'title' => $title);
}
}
if ($vids) {
$conv = VF::factory('video');
foreach ($vids as $vid) {
$video = array();
$random = VText::random();
$tmp = TMP_DIR.'/downloads/'.$random.'.'.$vid['ext'];
if (VCurl::file($vid['url'], $tmp)) {
$conv->clean();
if ($conv->load($tmp)) {
$video['random'] = $random;
$video['ext'] = $vid['ext'];
$video['path'] = $tmp;
$video['process'] = 1;
$video['title'] = $vid['title'];
$video['description'] = '';
$video['tags'] = '';
$video['format'] = $conv->data['format'];
$video['audio'] = $conv->data['audio'];
$video['duration'] = $conv->data['duration_timecode'];
$video['size'] = VText::bytes(filesize($tmp));
$video['thumb'] = $conv->extract_thumb($tmp, $random, (int) $conv->data['duration_seconds']);
$video['identify'] = $conv->identify();
$videos[] = $video;
} else {
$errors[] = 'Failed to load video from: '.$tmp;
}
}
}
if ($videos) {
$processed = TRUE;
} else {
$errors[] = 'Failed to process any scrapped videos!';
}
} else {
$errors[] = 'No videos found while parsing!';
}
}
}
if (isset($_POST['submit_process'])) {
$filter = VF::factory('filter');
$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 = (array) $_POST['category'];
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[] = 'You need to select at least one video to process!';
}
if (!$errors) {
$error_title = FALSE;
$error_tags = FALSE;
foreach ($videos as $video) {
if (isset($video['process']) && $video['process'] == '1') {
if ($video['title'] == '') {
$error_title = TRUE;
}
if ($video['tags'] == '') {
$error_tags = TRUE;
}
}
}
if ($error_title) {
$errors[] = 'One or more videos do not have any title set!';
}
if ($error_tags) {
$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 (isset($video['process']) && $video['process'] == '1') {
if (!$this->add_video($video, $user_id, $category, $type, $premium, $status)) {
$errors[] = $this->get_error();
} else {
++$success;
}
}
$progress = sprintf("%01.2f", ($progress+$incr));
}
if (!$errors) {
$messages[] = 'Successfully processed '.$success.' videos!';
}
}
}
}
$tpl = VF::factory('template');
$tpl->menu = 'video';
$tpl->submenu = 'video_add';
$tpl->extramenu = 'video_fhg';
$tpl->meta_title = 'Admin::Video::FHG Scrapper';
$tpl->vcfg = VF::cfg('module.video');
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->unique = $unique;
$tpl->url = $url;
$tpl->username = $username;
$tpl->categories = $categories;
$tpl->type = $type;
$tpl->premium = $premium;
$tpl->status = $status;
$tpl->statuses = $statuses;
$tpl->videos = $videos;
$tpl->processed = $processed;
$tpl->load(array('header', 'video_fhg', 'footer'));
$tpl->display();
}
private function get_video_categories()
{
return $this->db->get_rows("SELECT cat_id, name, auto_term
FROM #__video_categories
ORDER BY name ASC");
}
private function get_url($base, $url)
{
return substr($url, 0, strrpos($url, '/')).'/'.$base;
}
private function add_video($video, $user_id, $category, $type, $premium, $status)
{
VF::factory_remove('database');
$this->db = VF::factory('database');
$slug = prepare_string($video['title'], TRUE, $this->vcfg['slug_max_length']);
$this->db->query("INSERT INTO #__video
SET user_id = ".$user_id.",
title = '".$this->db->escape($video['title'])."',
slug = '".$this->db->escape($slug)."',
description = '".$this->db->escape($video['description'])."',
type = '".$this->db->escape($type)."',
add_date = '".$this->add_date."',
add_time = ".$this->add_time.",
premium = '".$premium."',
status = 4");
if ($this->db->affected_rows()) {
$video_id = $this->db->get_last_insert_id('#__video');
foreach ($category as $cat_id) {
$this->db->query("INSERT INTO #__video_category
SET video_id = ".$video_id.",
cat_id = ".$cat_id);
$this->db->query("UPDATE #__video_categories
SET total_videos = total_videos+1
WHERE cat_id = ".$cat_id."
LIMIT 1");
}
$tags = explode(',', $video['tags']);
foreach ($tags as $tag) {
$this->db->query("INSERT INTO #__video_tags
SET video_id = ".$video_id.",
name = '".$this->db->escape($tag)."'");
}
$src = $video['path'];
$conv = VF::factory('video');
$conv->log_setup($video_id);
$conv->log_clean = FALSE;
if ($conv->load($src)) {
$flv_processed = FALSE;
$hd_processed = FALSE;
$mobile_processed = FALSE;
if ($video['convert'] == '1') {
$conv->log('FLV: starting conversion...');
$dst_flv_nometa = MEDIA_DIR.'/videos/flv/'.$video_id.'.nometa.flv';
if ($conv->convert_to_flv($src, $dst_flv_nometa)) {
$flv_processed = TRUE;
} else {
$conv->log('Failed to convert to FLV!');
}
if ($flv_processed === TRUE) {
$conv->log('FLV: processed ... updating meta tags!');
$dst_flv = MEDIA_DIR.'/videos/flv/'.$video_id.'.flv';
if ($conv->update_meta($dst_flv_nometa, $dst_flv)) {
$conv->log('FLV: meta tags updated!');
VFile::delete($dst_flv_nometa);
} else {
$conv->log('FLV: failed to update meta tags...using original file!');
rename($dst_flv_nometa, $dst_flv);
}
}
if ($this->vcfg['hd_convert'] == '1') {
$conv->log('HD: starting conversion...');
$dst_mp4_nomov = MEDIA_DIR.'/videos/mp4/'.$video_id.'.nomov.mp4';
if ($conv->convert_to_mp4($src, $dst_mp4_nomov)) {
$hd_processed = TRUE;
} else {
$conv->log('HD: conversion failed!');
}
if ($hd_processed === TRUE) {
$conv->log('HD: processed ... relocating moov atom ...');
$dst_mp4 = MEDIA_DIR.'/videos/mp4/'.$video_id.'.mp4';
if ($conv->fix_moov_atom($dst_mp4_nomov, $dst_mp4)) {
$conv->log('HD: moov atom relocated!');
VFile::delete($dst_mp4_nomov);
} else {
$conv->log('HD: failed to relocate moov atom...using original file!');
rename($dst_mp4_nomov, $dst_mp4);
}
}
} else {
$conv->log('HD conversion not enabled in admin!');
}
if (VModule::enabled('mobile') && $this->vcfg['mobile_convert'] == '1') {
$conv->log('MOBILE: converting...');
$dst_mobile_nomov = MEDIA_DIR.'/videos/mobile/'.$video_id.'.nomov.mp4';
if ($conv->convert_to_mobile($src, $dst_mobile_nomov)) {
$conv->log('MOBILE: conversion success!');
$mobile_processed = TRUE;
}
} else {
$conv->log('MOBILE: no conversion :(');
}
if ($mobile_processed === TRUE) {
$conv->log('MOBILE: processed...relocating moov atom!');
$dst_mobile = MEDIA_DIR.'/videos/mobile/'.$video_id.'.mp4';
if ($conv->fix_moov_atom($dst_mobile_nomov, $dst_mobile)) {
$conv->log('MOBILE: moov atom relocated!');
VFile::delete($dst_mobile_nomov);
} else {
$conv->log('MOBILE: moov atom relocation failed ... using original file!');
rename($dst_mobile_nomov, $dst_mobile);
}
}
} else {
$ext = $video['identify'];
$dst_tmp = MEDIA_DIR.'/videos/'.$ext.'/'.$video_id.'.nometa.'.$ext;
if (!copy($src, $dst_tmp)) {
$conv->log('Failed to copy '.$src.' to '.$dst_tmp.'! Aborting...');
return FALSE;
}
$dst = MEDIA_DIR.'/videos/'.$ext.'/'.$video_id.'.'.$ext;
$function = ($video['identify'] == 'flv') ? 'update_meta' : 'fix_moov_atom';
if (!$conv->$function($dst_tmp, $dst)) {
$conv->log('Failed to prepare video file ('.$video_id.'.'.$ext.')! Using original file...');
rename($dst_tmp, $dst);
}
if ($ext == 'mp4') {
$hd_processed = TRUE;
} else {
$flv_processed = TRUE;
}
}
$thumbs = $conv->extract_thumbs($src, $video_id);
if ($thumbs <= 0) {
$status = 7;
}
$format = ($hd_processed === TRUE) ? 'mp4' : 'flv';
$mobile = ($mobile_processed === TRUE) ? 1 : 0;
$server_id = 0;
if ($this->vcfg['multi_server'] == '1') {
$conv->log('Initializing multi-server...');
VHelper::load('module.video.server');
$server = VHelper_video_server::get(FALSE, 1);
if ($server) {
$conv->log('Server fetched!');
if (VHelper_video_server::upload($server, $dst_flv, '/flv/'.$video_id.'.flv')) {
$conv->log('Uploaded FLV to server!');
$server_id = (int) $server['server_id'];
VFile::delete($dst_flv);
} else {
$conv->log('Failed to upload FLV to server!');
}
if ($hd_processed === TRUE) {
if (VHelper_video_server::upload($server, $dst_mp4, '/mp4/'.$video_id.'.mp4')) {
$conv->log('Uploaded MP4 to server!');
$server_id = (int) $server['server_id'];
VFile::delete($dst_mp4);
} else {
$conv->log('Failed to upload MP4 to server!');
}
}
// only upload mobile to server if flv or mp4 were uploaded successfuly!
if ($server_id !== 0) {
if ($mobile_processed === TRUE) {
if (VHelper_video_server::upload($server, $dst_mobile, '/mobile/'.$video_id.'.mp4')) {
$conv->log('Uploaded Mobile to server!');
$server_id = (int) $server['server_id'];
VFile::delete($dst_mobile);
} else {
$conv->log('Failed to upload Mobile to server!');
}
}
}
} else {
$conv->log('Failed to get server configuration!');
}
}
$s3 = 0;
if ($this->vcfg['amazon_s3'] == '1') {
$conv->log('Initializing Amazon S3 storage...');
VHelper::load('module.video.s3');
if (VHelper_video_s3::upload($dst_flv, 'flv/'.$video_id.'.flv')) {
$conv->log('Uploaded FLV to Amazon S3!');
$s3 = 1;
VFile::delete($dst_flv);
} else {
$conv->log('Failed to upload FLV to Amazon S3!');
}
if ($hd_processed === TRUE) {
if (VHelper_video_s3::upload($dst_mp4, 'mp4/'.$video_id.'.mp4')) {
$conv->log('Uploaded MP4 to Amazon S3!');
$s3 = 1;
VFile::delete($dst_mp4);
} else {
$conv->log('Failed to upload MP4 to Amazon S3!');
}
}
// only upload mobile to server if flv or mp4 were uploaded successfuly!
if ($s3 !== 0) {
if ($mobile_processed === TRUE) {
if (VHelper_video_s3::upload($dst_mobile, 'mobile/'.$video_id.'.mp4')) {
$conv->log('Uploaded Mobile to Amazon S3!');
$s3 = 1;
VFile::delete($dst_mobile);
} else {
$conv->log('Failed to upload Mobile to Amazon S3!');
}
}
}
}
VF::factory_remove('database');
$this->db = VF::factory('database');
if ($server_id !== 0) {
$this->db->query("UPDATE #__server
SET last_used = '".$db->escape(date('Y-m-d h:i:s'))."',
total_videos = total_videos+1
WHERE server_id = ".$server_id."
LIMIT 1");
}
$ext = ($hd_processed === TRUE) ? 'mp4' : 'flv';
$size = filesize($src);
$sql = "UPDATE #__video
SET ext = '".$this->db->escape($ext)."',
size = ".$size.",
duration = ".(float) $conv->data['duration_seconds'].",
thumbs = ".$thumbs.",
mobile = '".$mobile."',
server = ".$server_id.",
s3 = ".$s3.",
status = ".$status."
WHERE video_id = ".$video_id."
LIMIT 1";
$this->db->query($sql);
if ($this->db->affected_rows()) {
$conv->log('Executed update query:'."n".$sql);
}
VFile::delete($src);
$conv->clean();
return TRUE;
} else {
$this->error = 'Failed to load source file ('.$src.')!';
VF::factory_remove('database');
$this->db = VF::factory('database');
$this->db->query("UPDATE #__video
SET status = 7
WHERE video_id = ".$video_id."
LIMIT 1");
return FALSE;
}
}
}
private function update_progress($data)
{
VFile::write(VFile::safe(TMP_DIR.'/logs/'.$this->unique.'_fhg_scrapper_progress.log'), $data);
}
private function get_error()
{
return $this->error;
}
}
?>