Файл: adultscript-2.0.3-pro/files/admin/modules/grab/components/grab.php
Строк: 203
<?php
defined('_VALID') or die('Restricted Access!');
set_time_limit(0);
class VComponent_Admin_grab_grab
{
protected $db;
protected $tpl;
protected $gcfg;
protected $vcfg;
protected $url;
protected $user_id;
protected $category;
protected $status;
protected $categories = array();
protected $errors = array();
protected $video_added = 0;
protected $video_already = 0;
protected $overflow = 500;
protected $page;
protected $unique;
public $background = false;
public function __construct()
{
$this->db = VF::factory('database');
$this->tpl = VF::factory('template');
$this->vcfg = VF::cfg('module.video');
$this->gcfg = VF::cfg('module.grab');
$this->categories = $this->get_video_categories();
}
public function render()
{
$errors = array();
$messages = array();
$warnings = array();
$grab = array('url' => '', 'category' => '', 'username' => '', 'status' => 1, 'background' => 'off', 'broadcast' => 'public');
$unique = time().'_'.mt_rand();
if (isset($_POST['submitted'])) {
$filter = VF::factory('filter');
$url = trim($_POST['url']);
$username = $filter->get('username');
$category = $filter->get('category', 'INT');
$status = $filter->get('status', 'INT');
$background = (isset($_POST['background']) && $_POST['background'] = 'on') ? 1 : 0;
$unique = $filter->get('unique');
if ($url == '') {
$errors[] = 'Please enter a url to grab videos from!';
} elseif (!VValid::url($url)) {
$errors[] = 'URL is not a valid url address!';
} else {
$parts = explode('/', str_replace(array('http://www.', 'http://'), '', $url));
if (isset($parts['0'])) {
$site = $parts['0'];
if (!isset($this->gcfg['sites'][$site])) {
$errors[] = 'Invalid url! Supported sites: '.implode(', ', $this->gcfg['sites']).'!';
} else {
$site = VFile::strip_ext($site);
$grab['url'] = $url;
}
}
}
if ($username == '') {
$errors[] = 'Please enter a username for the grabbed videos!';
} 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');
$grab['username'] = $username;
} else {
$errors[] = 'Invalid username! Are you sure this user exists!?';
}
}
if ($category !== 0) {
$grab['category'] = $category;
}
$grab['status'] = $status;
$grab['background'] = ($background === 1) ? 'on' : 'off';
if (!$errors) {
session_write_close();
$this->unique = $unique;
if ($background) {
$php_cli_path = VF::cfg_item('php_cli_path');
$cmd = $php_cli_path.' '.ADMIN_DIR.'/modules/grab/scripts/grab.php '.$url.' '.$user_id.' '.$category.' '.$status.' '.$this->unique;
exec(escapeshellcmd($cmd).' >/dev/null &');
$messages[] = 'Videos will be embedded into background!';
} else {
$component_file = ADMIN_DIR.'/modules/grab/grabers/'.$site.'.php';
$component_name = 'VComponent_Admin_grab_'.$site;
if (file_exists($component_file) && is_file($component_file)) {
require $component_file;
$grabber = new $component_name($url, $user_id, $category, $status, $this->unique);
$this->update_progress('0^Processing...');
if ($grabber->get_videos()) {
$messages[] = $grabber->get('video_added').' videos added!';
$this->update_progress('100^Embed process finished!');
if ($grabber->get('video_already')) {
$warnings[] = $grabber->get('video_already').' are already added to your site!';
}
} else {
$errors = array_merge($errors, $grabber->get('errors'));
}
} else {
$errors[] = 'Failed to load grabber component!';
}
}
}
}
$this->tpl->menu = 'video';
$this->tpl->submenu = 'video_add';
$this->tpl->extramenu = 'video_grab';
$this->tpl->meta_title = 'Admin::Video::Mass Embed';
$this->tpl->errors = $errors;
$this->tpl->messages = $messages;
$this->tpl->warnings = $warnings;
$this->tpl->unique = $unique;
$this->tpl->grab = $grab;
$this->tpl->categories = $this->categories;
$this->tpl->load(array('header', 'video_grab', 'footer'));
$this->tpl->display();
}
protected function get_video_categories()
{
$this->db->query("SELECT cat_id, name, auto_term
FROM #__video_categories
ORDER BY slug ASC");
return $this->db->fetch_rows();
}
protected function add_video($video=array())
{
if (defined('_CONSOLE')) {
echo '* Adding video '.$video['url'].' ... ',"n";
}
if (defined('_RESIZE_THUMB')) {
$image = VF::factory('image');
}
$video['title'] = str_replace('&#039;', ''', $video['title']);
$video['title'] = str_replace('&amp;', '&', $video['title']);
$video['title'] = str_replace('&', '&', $video['title']);
$video['title'] = str_replace('#039;', ''', $video['title']);
$time = time();
$category = ($this->category)
? array((int) $this->category)
: $this->match_category($video['category'], $video['title'], $video['desc'], $video['tags']);
$sql = "INSERT INTO #__video
SET user_id = ".$this->user_id.",
title = '".$this->db->escape($video['title'])."',
slug = '".$this->db->escape(prepare_string($video['title'], TRUE))."',
description = '".$this->db->escape($video['desc'])."',
duration = ".$video['duration'].",
thumbs = ".(count($video['thumbs'])-1).",
embed_code = '".$this->db->escape($video['embed'])."',
type = 'public',
add_date = '".date('Y-m-d h:i:s')."',
add_time = ".$time.",
status = 5";
$this->db->query($sql);
if ($this->db->affected_rows()) {
$video_id = $this->db->get_last_insert_id('#__video');
$this->db->query("INSERT INTO #__video_graber
SET video_id = ".$video_id.",
site = '".$this->db->escape($video['site'])."',
url = '".$this->db->escape($video['url'])."'");
foreach ($category as $cat) {
$this->db->query("INSERT INTO #__video_category SET cat_id = ".$cat.", video_id = ".$video_id);
$this->db->query("UPDATE #__video_categories SET total_videos = total_videos+1 WHERE cat_id = ".$cat." LIMIT 1");
}
$this->db->query("UPDATE #__user_activity SET total_videos = total_videos+1 WHERE user_id = ".$this->user_id." LIMIT 1");
$tags = $this->get_tags($video['tags'], $video['title']);
$tags = (strpos($tags, ',') !== false) ? explode(',', $tags) : explode(' ', $tags);
foreach ($tags as $tag) {
$this->db->query("INSERT INTO #__video_tags SET video_id = ".$video_id.", name = '".$this->db->escape($tag)."'");
}
$this->db->query("INSERT INTO #__video_orig
SET video_id = ".$video_id.",
user_id = ".$this->user_id.",
filename = '',
ext = '',
size = 0,
method = 'mass_embed',
ip = ".VServer::ip(TRUE).",
add_time = ".$time);
$thumb_dir = MEDIA_DIR.'/videos/tmb/'.path($video_id);
$count = 1;
$valid = 0;
if (defined('_CONSOLE')) {
echo '-> creating thumb directory ... ';
}
if (VFolder::create($thumb_dir)) {
if (defined('_CONSOLE')) {
echo 'DONE',"n";
}
if (defined('_CROP_FROM_SPRITE')) {
$tmp_dir = TMP_DIR.'/downloads/'.$video_id;
$tmp_path = TMP_DIR.'/downloads/'.$video_id.'/sprite.jpg';
VFolder::create($tmp_dir);
if (defined('_CONSOLE')) {
echo ' -> saving sprite to '.$tmp_path.' ... ';
}
if (VCurl::file($video['thumbs']['0'], $tmp_path)) {
if (defined('_CONSOLE')) {
echo "DONEn";
}
if ($image->load($tmp_path)) {
$video['thumbs'] = array();
$s_hor = 1;
$s_ver = 1;
$w_hor = 155;
$w_ver = 115;
$i = 1;
while ($s_ver <= _CROP_LIMIT) {
if ($image->crop($s_hor, $s_ver, $w_hor, $w_ver, $tmp_dir.'/'.$i.'.jpg')) {
$video['thumbs'][] = BASE_URL.'/tmp/downloads/'.$video_id.'/'.$i.'.jpg';
}
++$i;
$s_ver = ($s_ver + 115);
}
} else {
$video['thumbs'] = array();
}
} else {
if (defined('_CONSOLE')) {
echo "FAILEDn";
}
$video['thumbs'] = array();
}
}
foreach ($video['thumbs'] as $thumb) {
if (defined('_CONSOLE')) {
echo ' -> saving thumb to '.$thumb_dir.'/'.$count.'.jpg ... ';
}
$tmb_path = $thumb_dir.'/'.$count.'.jpg';
if (defined('_RESIZE_THUMB')) {
VFolder::create(TMP_DIR.'/downloads/'.$video_id);
$tmb_path = TMP_DIR.'/downloads/'.$video_id.'/'.$count.'.jpg';
}
if (VCurl::file($thumb, $tmb_path)) {
if (defined('_RESIZE_THUMB')) {
if (!$image->load($tmb_path) OR
!$image->resize($this->vcfg['thumb_width'], $this->vcfg['thumb_height'], 'EXACT', $thumb_dir.'/'.$count.'.jpg')) {
$processed = FALSE;
} else {
$processed = TRUE;
}
$image->clear();
} else {
$processed = TRUE;
}
if ($processed === TRUE) {
if (defined('_CONSOLE')) {
echo 'DONE',"n";
}
++$valid;
++$count;
}
} else {
if (defined('_CONSOLE')) {
echo 'FAILED',"n";
}
}
}
if (defined('_RESIZE_THUMB')) {
VFolder::delete(TMP_DIR.'/downloads/'.$video_id);
}
if ($valid !== 0) {
$this->db->query("UPDATE #__video
SET status = ".$this->status.",
thumbs = ".$valid."
WHERE video_id = ".$video_id."
LIMIT 1");
if ($this->background) {
VFile::write(TMP_DIR.'/logs/grab_log', 'Added: '.$video['url']."n", TRUE);
}
return TRUE;
} else {
$this->errors[] = 'Failed to get at least one thumb for '.$video['url'].'! Dropping video!';
}
} else {
if (defined('_CONSOLE')) {
echo 'FAILED',"n";
}
$this->errors[] = 'Failed to create video thumb directory ('.$thumb_dir.')! Please make sure the parent directory is writable!';
}
} else {
$this->errors[] = 'Failed to add video to database!?';
}
if ($this->background) {
VFile::write(TMP_DIR.'/logs/grab_log', 'Errro: '.$video['url']."n", TRUE);
}
return FALSE;
}
protected function match_category($name, $title, $description, $tags)
{
$cats = array();
$first = NULL;
foreach ($this->categories as $category) {
$cat_id = (int) $category['cat_id'];
$terms = explode(',', $category['auto_term']);
if (!isset($first)) {
$first = $cat_id;
}
foreach ($terms as $term) {
$term = trim($term);
if (stripos($name, $term) !== FALSE) {
if (!in_array($cat_id, $cats)) {
$cats[] = $cat_id;
}
}
if (stripos($title, $term) !== FALSE) {
if (!in_array($cat_id, $cats)) {
$cats[] = $cat_id;
}
}
if (!empty($tags)) {
if (stripos($tags, $term) !== FALSE) {
if (!in_array($cat_id, $cats)) {
$cats[] = $cat_id;
}
}
}
if (!empty($description)) {
if (stripos($description, $term) !== FALSE) {
if (!in_array($cat_id, $cats)) {
$cats[] = $cat_id;
}
}
}
}
}
if (!$cats) {
$cats[] = $first;
}
return $cats;
}
protected function get_tags($tags, $title)
{
if ($tags != '') {
return $tags;
}
$title = prepare_string($title);
$title = explode(' ', $title);
$tags = array();
foreach ($title as $tag) {
if (strlen($tag) >= 4) {
$tags[] = $tag;
}
}
return implode(' ', $tags);
}
protected function duration_to_seconds($duration)
{
$duration = explode(':', $duration);
$minutes = sprintf('%01d', $duration['0']);
$seconds = sprintf('%01d', $duration['1']);
return (($minutes * 60) + $seconds);
}
protected function clean_html($html)
{
$html = str_replace(array("n", "r"), '', $html);
$html = preg_replace('/ss+/', ' ', $html);
$html = str_replace("t", ' ', $html);
return $html;
}
protected function already_added($site, $url)
{
$this->db->query("SELECT video_id
FROM #__video_graber
WHERE site = '".$this->db->escape($site)."'
AND url = '".$this->db->escape($url)."'
LIMIT 1");
return $this->db->affected_rows();
}
public function get_errors()
{
return $this->errors;
}
public function get($name)
{
if (isset($this->$name)) {
return $this->$name;
}
}
protected function update_progress($data)
{
VFile::write(VFile::safe(TMP_DIR.'/logs/'.$this->unique.'_mass_import_progress.log'), $data);
}
}