Файл: adultscript-2.0.3-pro/files/modules/photo/components/photo.php
Строк: 91
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_photo_photo extends VModule_photo
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function render()
    {
        $photo_id     = VUri::request(1);
        $cache_id    = 'photo_'.$photo_id;
        if (!$photo = $this->cache->get($cache_id, 3600)) {
            $this->db->query("SELECT p.photo_id, p.album_id, p.server, p.caption, p.ext,
                                     p.total_views, p.allow_comment, p.total_comments, p.likes,
                                     p.rating, p.rated_by, p.total_favorites, p.description,
                                     a.title, a.slug, a.user_id, a.add_time, a.type, 
                                     a.password, u.username, s.url
                              FROM #__photo AS p
                              LEFT JOIN #__photo_albums AS a ON (a.album_id = p.album_id)
                              LEFT JOIN #__user AS u ON (u.user_id = a.user_id)
                              LEFT JOIN #__photo_servers AS s ON (s.server_id = p.server)
                              WHERE p.photo_id = ".$photo_id."
                              AND p.status = '1'
                              LIMIT 1");
            if ($this->db->affected_rows()) {
                $photo = $this->db->fetch_assoc();
                $this->cache->store($cache_id, $photo, 3600);
            } else {
                VModule::load('404', TRUE);
            }
        }
        
        if (!VUri::match('photo/'.$photo_id.'/')) {
            VF::redirect(BASE_URL.'/photo/'.$photo_id.'/');
        }
        
        VLanguage::load('frontend.photo');
        
        if (VCfg::get('photo.allow_password')) {
            $allowed    = (isset($_SESSION['albums']) && isset($_SESSION['albums'][$photo['album_id']])) ? true : false;
              if (isset($_POST['submit-password']) && !$allowed) {
                  $password   = trim($_POST['password']);
                  if (VHash::check($password, $photo['password'])) {
                      if (!isset($_SESSION['albums'])) {
                          $_SESSION['albums'] = array();
                      }
                      $_SESSION['albums'][$photo['album_id']] = 1;
                      $messages[] = __('password-accept');
                      $allowed    = true;
                  } else {
                      $errors[]   = __('password-invalid');
                  }
              }
            if ($photo['password'] != '' && !$allowed) {
                  $this->tpl->menu        = 'photo';
                  $this->tpl->css         = array(TPL_REL.'/css/style_photo.css');
                  $this->tpl->meta_title  = $photo['title'].' '.$photo['photo_id'].' - '.$this->tpl->cfg['site_name'];
                  $this->tpl->meta_desc   = $photo['title'].' '.$photo['photo_id'].' - '.$this->tpl->cfg['site_name'];
                  $this->tpl->errors        = $errors;
                  $this->tpl->load(array('header', 'photo_view_password', 'footer'));
                  $this->tpl->display();
                  exit;
            }
        }
        
        $album_id    = (int) $photo['album_id'];
        $ip            = VServer::ip(TRUE);
        $user_id    = (VAuth::loggedin()) ? (int) $_SESSION['user_id'] : 0;
        $friends    = TRUE;
        if ($photo['type'] == 'private') {
            if ($user_id) {
                $owner_id = (int) $photo['user_id'];
                if ($owner_id !== $user_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 {
                $friends = FALSE;
            }
        }        
        
        $prev_photo = NULL;
        $this->db->query("SELECT photo_id
                          FROM #__photo
                          WHERE album_id = ".$album_id."
                          AND photo_id != ".$photo_id."
                          AND photo_id < ".$photo_id."
                          ORDER BY photo_id DESC
                          LIMIT 1");
        if ($this->db->affected_rows()) {
            $arr        = $this->db->fetch_assoc();
            $prev_photo = $arr['photo_id'];
        }
        
        $next_photo = NULL;
        $this->db->query("SELECT photo_id
                          FROM #__photo
                          WHERE album_id = ".$album_id."
                          AND photo_id != ".$photo_id."
                          AND photo_id > ".$photo_id."
                          ORDER BY photo_id ASC
                          LIMIT 1");
        if ($this->db->affected_rows()) {
            $arr        = $this->db->fetch_assoc();
            $next_photo = $arr['photo_id'];
        }
        
        if (!VBrowser::get('is_robot')) {
            $this->db->query("UPDATE #__photo
                              SET total_views = total_views+1
                              WHERE photo_id = ".$photo_id."
                              LIMIT 1");
            
            if (VCfg::get('photo.track_views')) {
                $this->db->query("INSERT INTO #__photo_views
                                  SET photo_id = ".$photo_id.",
                                      user_id = ".$user_id.",
                                      ip = ".$ip.",
                                      view_time = ".time());
            }
        }
        
        if (VCfg::get('photo.view_comments')) {
            $this->get_photo_comments($photo_id);
        }
        
        $this->tpl->menu        = 'photo';
        $this->tpl->css            = array(TPL_REL.'/css/style_photo.css');
        $this->tpl->meta_title    = $photo['title'].' '.$photo['photo_id'].' - '.$this->tpl->cfg['site_name'];
        $this->tpl->meta_desc    = $photo['title'].' '.$photo['photo_id'].' - '.$this->tpl->cfg['site_name'];
        $this->tpl->canonical    = BASE_URL.'/photo/'.$photo_id.'/';
        $this->tpl->canonicalm    = MOBILE_URL.'/photo/'.$photo_id.'/';
        $this->tpl->user_id        = $user_id;
        $this->tpl->friends        = $friends;
        $this->tpl->photo        = $photo;
        $this->tpl->prev_photo    = $prev_photo;
        $this->tpl->next_photo    = $next_photo;
        $this->tpl->load(array('header', 'photo_view', 'footer'));
        $this->tpl->display();
    }
    
    private function get_photo_comments($photo_id)
    {
        $sql_count        = "SELECT COUNT(*) AS total_comments
                           FROM #__photo_comments
                           WHERE photo_id = ".$photo_id."
                           AND status = '1'";
        $total_comments    = $this->db->get_field($sql_count, 'total_comments');
        $pagination        = VPagination::get(1, $total_comments, VCfg::get('photo.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 #__photo_comments AS c
                           LEFT JOIN #__user AS u ON (u.user_id = c.user_id)
                           WHERE c.photo_id = ".$photo_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)) {
            $this->db->query($sql);
            if ($this->db->affected_rows()) {
                $comments = $this->db->fetch_rows();
                $this->cache->store($cache_id, $comments, 3600);
            }
        }
        
        $this->tpl->comments        = $comments;
        $this->tpl->pagination        = $pagination;
        $this->tpl->total_comments    = $total_comments;
    }
}