Файл: adultscript-2.0.3-pro/files/modules/video/components/view.php
Строк: 208
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_video_view extends VModule_video
{
private $video_id;
public function __construct()
{
parent::__construct();
$this->video_id = (int) VUri::request(0);
}
public function render()
{
$access = VCfg::get('video.view_access');
if ($access != 'all' && !VBrowser::get('is_robot')) {
VAuth::check($access, null, __('view-access', array($access)));
}
$user_id = (VAuth::loggedin()) ? (int) $_SESSION['user_id'] : 0;
$cache_id = 'video_'.$this->video_id;
if (!$video = $this->cache->get($cache_id, 7200)) {
$this->db->query("SELECT v.video_id, v.user_id, v.title, v.slug, v.description, v.total_views, v.total_comments, v.premium,
v.allow_rating, v.allow_comment, v.allow_embed, v.likes, v.rated_by, v.embed_code, v.allow_download,
v.duration, v.add_time, v.add_date, v.thumb, v.thumbs, v.ext, v.type, v.adv, v.size, v.status, vm.meta_title,
vm.meta_desc, vm.meta_keys, u.username, v.mobile, v.hd, v.total_favorites, v.server, v.mobile,
v.channel_id, ch.name AS chname, ch.description as chdesc, ch.url as churl, ch.logo,
GROUP_CONCAT(DISTINCT c.cat_id) AS categories,
GROUP_CONCAT(DISTINCT c.slug) AS slugs,
GROUP_CONCAT(DISTINCT 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 #__channel AS ch ON (ch.channel_id = v.channel_id)
LEFT JOIN #__user AS u ON (u.user_id = v.user_id)
WHERE v.video_id = ".$this->video_id."
GROUP BY v.video_id
LIMIT 1");
if ($this->db->affected_rows()) {
$video = $this->db->fetch_assoc();
$this->cache->store($cache_id, $video, 7200);
} else {
VModule::load('404', TRUE);
}
}
// added in 1.0.3 for future development (might add deleted here...)
if ($video['status'] != '1' && $video['status'] != '8') {
VModule::load('404', true);
}
// we redirect here if anything is not set correctly
if (!VUri::match($this->video_id.'/'.$video['slug'].'/')) {
VF::redirect(BASE_URL.'/'.$this->video_id.'/'.$video['slug'].'/', '301');
}
if ($video['premium'] == '1' && VModule::enabled('premium')) {
VF::redirect(BASE_URL.'/premium/'.$this->video_id.'/'.$video['slug'].'/');
}
VLanguage::load('frontend.video');
$ip = VServer::ip(TRUE);
$owner_id = (int) $video['user_id'];
$friends = TRUE;
if ($user_id) {
if ($video['type'] == 'private' &&
$user_id !== (int) $owner_id) {
$this->db->query("SELECT user_id
FROM #__user_friends
WHERE user_id = ".$owner_id."
AND friend_id = ".$user_id."
AND status = 'approved'
LIMIT 1");
if (!$this->db->affected_rows()) {
$friends = FALSE;
}
}
} else {
if ($video['type'] == 'private') {
$friends = FALSE;
}
}
// robots are allowed to index private videos
if ($friends === FALSE && VBrowser::get('is_robot')) {
$friends = TRUE;
}
$date = date('Y-m-d h:i:s');
$time = time();
if (!VBrowser::get('is_robot') && $friends === TRUE) {
// view_date removed from the update...still in database
$this->db->query("UPDATE #__video
SET total_views = total_views+1,
view_time = ".time()."
WHERE video_id = ".$this->video_id." LIMIT 1");
$this->db->query("UPDATE #__user_activity SET total_video_views = total_video_views+1 WHERE user_id = ".$owner_id." LIMIT 1");
if (VCfg::get('video.track_views') != '0') {
$this->db->query("INSERT INTO #__video_views
SET video_id = ".$this->video_id.",
user_id = ".$user_id.",
ip = ".$ip.",
view_time = '".$time."'");
}
if ($user_id) {
$this->db->query("SELECT video_id
FROM #__video_history
WHERE video_id = ".$this->video_id."
AND user_id = ".$user_id."
AND view_time > 0
LIMIT 1");
if ($this->db->affected_rows()) {
$this->db->query("UPDATE #__video_history
SET view_time = ".time()."
WHERE video_id = ".$this->video_id."
AND user_id = ".$user_id."
LIMIT 1");
} else {
$this->db->query("INSERT #__video_history
SET video_id = ".$this->video_id.",
user_id = ".$user_id.",
view_time = ".time());
}
$this->db->query("UPDATE #__user_activity SET total_viewed_videos = total_viewed_videos+1 WHERE user_id = ".$user_id." LIMIT 1");
if (VModule::enabled('premium')) {
if ($_SESSION['group_id'] == VAuth::get_group_id('Registered')) {
if ($video['embed_code'] == '') {
$registered_method = VF::cfg_item('module.user.registered_method');
if ($registered_method != '0') {
VHelper::load('module.user.limit');
VHelper_user_limit::update($user_id, $video['duratin'], $registered_method);
}
}
}
}
} else {
if ($video['embed_code'] == '' && VF::cfg_item('module.user.guest_limit') && !VBrowser::get('is_robot')) {
VHelper::load('module.user.guest');
VHelper_user_guest::update($ip, $video['size'], $video['duration']);
}
}
}
if (VCfg::get('video.view_related')) {
$this->get_related_videos(
$video['video_id'],
$video['title'],
$video['tags'],
$video['categories']
);
}
if (VCfg::get('video.view_comments')) {
$this->get_comments();
}
if (VModule::enabled('pornstar')) {
$this->get_pornstars();
}
if (isset($_GET['playlist'])) {
$playlist_id = (int) trim($_GET['playlist']);
$this->get_playlist($playlist_id);
$this->get_playlist_videos($playlist_id);
}
$template = ($video['status'] == '8') ? 'video_view_deleted' : 'video_view';
$this->tpl->meta_title = ($video['meta_title'] != '') ? $video['meta_title'] : $video['title'].' - '.Vcfg::get('site_name');
$this->tpl->meta_desc = ($video['meta_desc'] != '') ? $video['meta_desc'] : __('watch').' '.$video['title'].'. '.VCfg::get('meta_desc');
$this->tpl->meta_keys = ($video['meta_keys'] != '') ? $video['meta_keys'] : $video['tags'];
$this->tpl->menu = 'video';
$this->tpl->cmenu = 'item';
$media_url = (defined('CDN_THUMB_URL')) ? CDN_THUMB_URL : MEDIA_URL.'/videos/tmb';
$metas = array(
'og:site_name' => VCfg::get('site_name'),
'og:title' => $video['title'],
'og:type' => 'video',
'og:url' => CURRENT_URL,
// need to handle CDN here in the future
'og:image' => $media_url.'/'.path($video['video_id']).'/'.$video['thumb'].'.jpg',
'og:video:duration' => (int) $video['duration'],
'og:video:release_date' => date('c', $video['add_time'])
);
if ($video['description']) {
$metas['og:description'] = e($video['description']);
}
$this->tpl->metas = $metas;
$this->tpl->canonical = BASE_URL.'/'.$video['video_id'].'/'.$video['slug'].'/';
if ($video['mobile'] == '1') {
$this->tpl->canonicalm = VF::cfg_item('mobile_url').'/'.$video['video_id'].'/'.$video['slug'].'/';
}
if (VCfg::get('video.player') == 'nuevoplayer') {
require 'nuevo.php';
}
$this->tpl->user_id = $user_id;
$this->tpl->video = $video;
$this->tpl->categories = $this->get_video_categories();
$this->tpl->download = $this->get_download_perms($user_id);
$this->tpl->user_id = $user_id;
$this->tpl->friends = $friends;
$this->tpl->load(array('header', $template, 'footer'));
$this->tpl->display();
}
private function get_download_perms($user_id)
{
$perms = VCfg::get('video.download_perms');
$download = (VCfg::get('video.download') == '1') ? true : false;
if ($download) {
if ($perms == 'all') {
$downoad = true;
} elseif ($perms == 'registered' && !$user_id) {
$download = false;
} elseif ($perms == 'premium') {
if (!VAuth::group('Premium')) {
$download = false;
}
}
}
return $download;
}
private function get_related_videos($video_id, $title, $tags, $categories)
{
$cache_id = 'video_related_'.$video_id;
if (!$related = $this->cache->get($cache_id, 3600) OR
!$related_total = $this->cache->get($cache_id.'_total', 3600)) {
$query = (VCfg::get('video.related_method') == 'simple')
? $this->get_related_simple($video_id, $title)
: $this->get_related_complex($video_id, $tags);
$related_total = $query['total'];
if ($query['sql'] !== false) {
$related = $this->db->get_rows($query['sql']);
if ($this->db->affected_rows()) {
$this->cache->store($cache_id, $related, 3600);
$this->cache->store($cache_id.'_total', $related_total, 3600);
}
} else {
$related = array();
}
}
$this->tpl->related = $related;
$this->tpl->related_total = (int) $related_total;
}
private function get_comments()
{
$sql_count = "SELECT COUNT(comment_id) AS total_comments
FROM #__video_comments
WHERE video_id = ".$this->video_id."
AND status = '1'";
$total_comments = $this->db->get_field($sql_count, 'total_comments');
$pagination = VPagination::get(1, $total_comments, VCfg::get('video.comments_per_page'));
$sql = "SELECT c.comment_id, c.parent_id, c.user_id,
c.comment, c.add_time, c.nickname,
c.likes, c.rated_by,
u.username, u.gender, u.avatar
FROM #__video_comments AS c
LEFT JOIN #__user AS u ON (u.user_id = c.user_id)
WHERE c.video_id = ".$this->video_id."
AND c.status = '1'
ORDER BY c.comment_id DESC
LIMIT ".$pagination['limit'];
$cache_id = $sql.$total_comments;
if (!$comments = $this->cache->get($cache_id, 3600)) {
$comments = $this->db->get_rows($sql);
if ($this->db->affected_rows()) {
$this->cache->store($cache_id, $comments, (3600));
}
}
$this->tpl->comments = $comments;
$this->tpl->total_comments = $total_comments;
$this->tpl->pagination = $pagination;
}
private function get_pornstars()
{
$sql = "SELECT m.slug, m.name
FROM #__model_videos AS mv
LEFT JOIN #__model AS m ON (mv.model_id = m.model_id)
WHERE mv.video_id = ".$this->video_id;
$cache_id = 'video-pornstars-'.$this->video_id;
if (!$pornstars = $this->cache->get($cache_id, 3600)) {
$pornstars = $this->db->get_rows($sql);
if ($this->db->affected_rows()) {
$this->cache->store($cache_id, $pornstars, 3600);
}
}
$this->tpl->pornstars = $pornstars;
}
private function get_related_simple($video_id, $title)
{
$title = $this->db->escape($title);
$sql_count = "SELECT COUNT(*) AS total_related
FROM #__video AS v
WHERE MATCH (v.title) AGAINST ('".$title."')
AND v.status = 1";
return array(
'total' => $this->db->get_field($sql_count, 'total_related'),
'sql' => "SELECT v.video_id, v.title, v.slug, v.likes, v.rating, v.rated_by, v.duration, v.thumb,
v.thumbs, v.total_views, v.add_time, v.ext, v.hd, v.premium, u.username,
MATCH (v.title) AGAINST ('".$title."') AS relevance
FROM #__video AS v
LEFT JOIN #__user AS u ON (u.user_id = v.user_id)
WHERE MATCH (v.title) AGAINST ('".$title."')
AND v.status = 1
AND v.video_id != ".$video_id."
ORDER BY relevance DESC LIMIT ".VCfg::get('video.related_per_page')
);
}
private function get_playlist($playlist_id)
{
$this->db->query("
SELECT p.playlist_id, p.name, p.slug, p.user_id, p.type,
p.total_videos, p.duration, p.add_time, u.username
FROM #__playlist AS p
LEFT JOIN #__user AS u ON (u.user_id = p.user_id)
WHERE playlist_id = ".$playlist_id."
LIMIT 1
");
if ($this->db->affected_rows()) {
$this->tpl->playlist = $this->db->fetch_assoc();
}
}
private function get_playlist_videos($playlist_id)
{
$this->db->query("
SELECT v.video_id, v.title, v.slug, v.duration, v.thumb
FROM #__playlist_videos AS pv
INNER JOIN #__video AS v ON (v.video_id = pv.video_id AND v.status = 1)
WHERE pv.playlist_id = ".$playlist_id."
ORDER BY pv.add_time ASC
");
if ($this->db->affected_rows()) {
$this->tpl->videos = $this->db->fetch_rows();
}
}
private function get_related_complex($video_id, $tags)
{
VF::load('sphinxapi.sphinxapi');
$sphinx = new SphinxClient();
$sphinx->SetServer(VF::cfg_item('sphinx_host'), VF::cfg_item('sphinx_port'));
$sphinx->SetConnectTimeout(1);
$sphinx->SetFieldWeights(array('title' => 100, 'tags' => 70, 'description' => 30));
$sphinx->SetFilter('video', array($video_id), true);
$sphinx->SetMatchMode(SPH_MATCH_EXT2);
$sphinx->SetLimits(0, VCfg::get('video.related_per_page'), 1000);
$tags = str_replace(',', ' ', VText::truncate_words($title, 40, ''));
$results = $sphinx->Query($tags, VF::cfg_item('sphinx_index'));
if (isset($results['total']) && isset($results['matches'])) {
$ids = implode(',', array_keys($results['matches']));
return array(
'total' => $results['total'],
'sql' => 'SELECT v.video_id, v.title, v.slug, v.likes, v.rating, v.rated_by,
v.duration, v.thumb, v.thumbs, v.total_views, v.add_time,
v.ext, v.hd, v.premium, u.username
FROM #__video AS v
LEFT JOIN #__user AS u on (u.user_id = v.user_id)
WHERE v.video_id IN ('.$ids.')
ORDER BY FIELD (v.video_id, '.$ids.')'
);
}
return array('total' => 0, 'sql' => FALSE);
}
}